Add a new API to return the list of undeployed xApps
[ric-plt/appmgr.git] / cmd / appmgr / desc_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package main
21
22 import (
23         "testing"
24         "reflect"
25         "errors"
26         "encoding/json"
27         "log"
28 )
29
30 var helmSearchOutput = `
31 helm-repo/anr           0.0.1           1.0             Helm Chart for Nokia ANR (Automatic Neighbour Relation) xAPP
32 helm-repo/appmgr        0.0.2           1.0             Helm Chart for xAppManager
33 helm-repo/dualco        0.0.1           1.0             Helm Chart for Nokia dualco xAPP
34 helm-repo/reporter      0.0.1           1.0             Helm Chart for Reporting xAPP
35 helm-repo/uemgr         0.0.1           1.0             Helm Chart for Nokia uemgr xAPP
36 `
37
38 var kubectlConfigmapOutput = `
39 {
40     "local": {
41         "host": ":8080"
42     },
43     "logger": {
44         "level": 3
45     },
46     "rmr": {
47        "protPort": "tcp:4560",
48        "maxSize": 2072,
49        "numWorkers": 1,
50        "txMessages": ["RIC_X2_LOAD_INFORMATION"],
51        "rxMessages": ["RIC_X2_LOAD_INFORMATION"]
52     },
53     "db": {
54         "namespace": "ricxapp",
55         "host": "dbaas",
56         "port": 6379
57     }
58 }
59 `
60
61 type ConfigSample struct {
62         Level   int
63         Host    string
64 }
65
66 type MockedConfigMapper struct {
67 }
68
69 func (cm *MockedConfigMapper) ReadSchema(name string, c *XAppConfig) (err error) {
70         return
71 }
72
73 func (cm *MockedConfigMapper) UploadConfig() (cfg []XAppConfig) {
74         return
75 }
76
77 func (cm *MockedConfigMapper) CreateConfigMap(r XAppConfig) (errList []CMError, err error){
78         return
79 }
80
81 func (cm *MockedConfigMapper) GetConfigMap(m XappDeploy, c *interface{}) (err error) {
82         return
83 }
84
85 func (cm *MockedConfigMapper) UpdateConfigMap(r XAppConfig) (errList []CMError, err error) {
86         return
87 }
88
89 func (cm *MockedConfigMapper) DeleteConfigMap(r XAppConfig) (c interface{}, err error) {
90         return
91 }
92
93 func (cm *MockedConfigMapper) PurgeConfigMap(m XappDeploy) (c interface{}, err error) {
94         return
95 }
96
97 func (cm *MockedConfigMapper) RestoreConfigMap(m XappDeploy, c interface{}) (err error) {
98         return
99 }
100
101 func (cm *MockedConfigMapper) ReadConfigMap(name string, ns string, c *interface{}) (err error) {
102         return
103 }
104
105 func (cm *MockedConfigMapper) ApplyConfigMap(r XAppConfig, action string) (err error) {
106         return
107 }
108
109 func (cm *MockedConfigMapper) FetchChart(name string) (err error) {
110         return
111 }
112
113 func (cm *MockedConfigMapper) GetMessages(name string) (msgs MessageTypes) {
114         return
115 }
116
117 func (cm *MockedConfigMapper) GetNamespace(ns string) (n string) {
118         return
119 }
120
121 func (cm *MockedConfigMapper) GetNamesFromHelmRepo() (names []string) {
122         return
123 }
124
125 // Test cases
126 func TestGetMessages(t *testing.T) {
127         cm := ConfigMap{}
128         expectedMsgs := MessageTypes{
129                 TxMessages: []string{"RIC_X2_LOAD_INFORMATION"},
130                 RxMessages: []string{"RIC_X2_LOAD_INFORMATION"},
131         }
132
133         KubectlExec = func(args string) (out []byte, err error) {
134                 return []byte(kubectlConfigmapOutput), nil
135         }
136
137     result := cm.GetMessages("dummy-xapp")
138         if !reflect.DeepEqual(result, expectedMsgs) {
139                 t.Errorf("TestGetMessages failed: expected: %v, got: %v", expectedMsgs, result)
140         }
141 }
142
143 func TestHelmNamespace(t *testing.T) {
144         cm := ConfigMap{}
145
146         if cm.GetNamespace("pltxapp") != "pltxapp" {
147         t.Errorf("getNamespace failed!")
148         }
149
150     if cm.GetNamespace("") != "ricxapp" {
151         t.Errorf("getNamespace failed!")
152         }
153 }
154
155 func TestFetchChartFails(t *testing.T) {
156         cm := ConfigMap{}
157
158         if cm.FetchChart("dummy-xapp") == nil {
159                 t.Errorf("TestFetchChart failed!")
160         }
161 }
162
163 func TestFetchChartSuccess(t *testing.T) {
164         cm := ConfigMap{}
165
166         HelmExec = func(args string) (out []byte, err error) {
167                 return
168         }
169
170         if cm.FetchChart("dummy-xapp") != nil {
171                 t.Errorf("TestFetchChart failed!")
172         }
173 }
174
175 func TestGetNamesFromHelmRepoSuccess(t *testing.T) {
176         cm := ConfigMap{}
177         expectedResult := []string{"anr", "appmgr", "dualco", "reporter", "uemgr"}
178         HelmExec = func(args string) (out []byte, err error) {
179                 return []byte(helmSearchOutput), nil
180         }
181
182         names := cm.GetNamesFromHelmRepo()
183         if !reflect.DeepEqual(names, expectedResult) {
184                 t.Errorf("GetNamesFromHelmRepo failed: expected %v, got %v", expectedResult, names)
185         }
186 }
187
188 func TestGetNamesFromHelmRepoFailure(t *testing.T) {
189         cm := ConfigMap{}
190         expectedResult := []string{}
191         HelmExec = func(args string) (out []byte, err error) {
192                 return []byte(helmSearchOutput), errors.New("Command failed!")
193         }
194
195         names := cm.GetNamesFromHelmRepo()
196         if names != nil {
197                 t.Errorf("GetNamesFromHelmRepo failed: expected %v, got %v", expectedResult, names)
198         }
199 }
200
201 func TestApplyConfigMapSuccess(t *testing.T) {
202         cm := ConfigMap{}
203         m := ConfigMetadata{Name: "dummy-xapp", Namespace: "ricxapp"}
204         s := ConfigSample{5, "localhost"}
205
206         KubectlExec = func(args string) (out []byte, err error) {
207                 log.Println("TestApplyConfigMapSuccess: ", args)
208                 return []byte(`{"logger": {"level": 2}}`), nil
209         }
210
211         err := cm.ApplyConfigMap(XAppConfig{Metadata: m, Configuration: s}, "create")
212         if err != nil {
213                 t.Errorf("ApplyConfigMap failed: %v", err)
214         }
215 }
216
217 func TestRestoreConfigMapSuccess(t *testing.T) {
218         cm := ConfigMap{}
219         m := XappDeploy{Name: "dummy-xapp", Namespace: "ricxapp"}
220         s := ConfigSample{5, "localhost"}
221
222         KubectlExec = func(args string) (out []byte, err error) {
223                 log.Println("TestRestoreConfigMapSuccess: ", args)
224                 return []byte(`{"logger": {"level": 2}}`), nil
225         }
226
227         err := cm.RestoreConfigMap(m, s)
228         if err != nil {
229                 t.Errorf("RestoreConfigMap failed: %v", err)
230         }
231 }
232
233 func TestDeleteConfigMapSuccess(t *testing.T) {
234         cm := ConfigMap{}
235
236         HelmExec = func(args string) (out []byte, err error) {
237                 return []byte("ok"), nil
238         }
239
240         KubectlExec = func(args string) (out []byte, err error) {
241                 log.Println("TestDeleteConfigMapSuccess: ", args)
242                 return []byte(`{"logger": {"level": 2}}`), nil
243         }
244
245         c, err := cm.DeleteConfigMap(XAppConfig{})
246         if err != nil {
247                 t.Errorf("DeleteConfigMap failed: %v -> %v", err, c)
248         }
249 }
250
251 func TestPurgeConfigMapSuccess(t *testing.T) {
252         cm := ConfigMap{}
253
254         HelmExec = func(args string) (out []byte, err error) {
255                 return []byte("ok"), nil
256         }
257
258         KubectlExec = func(args string) (out []byte, err error) {
259                 return []byte(`{"logger": {"level": 2}}`), nil
260         }
261
262         c, err := cm.PurgeConfigMap(XappDeploy{})
263         if err != nil {
264                 t.Errorf("PurgeConfigMap failed: %v -> %v", err, c)
265         }
266 }
267
268 func TestCreateConfigMapFails(t *testing.T) {
269         cm := ConfigMap{}
270
271         c, err := cm.CreateConfigMap(XAppConfig{})
272         if err == nil {
273                 t.Errorf("CreateConfigMap failed: %v -> %v", err, c)
274         }
275 }
276
277 func TestUpdateConfigMapFails(t *testing.T) {
278         cm := ConfigMap{}
279
280         c, err := cm.UpdateConfigMap(XAppConfig{})
281         if err == nil {
282                 t.Errorf("CreateConfigMap failed: %v -> %v", err, c)
283         }
284 }
285
286 func TestValidationSuccess(t *testing.T) {
287         cm := ConfigMap{}
288         var d interface{}
289         var cfg map[string]interface{}
290
291         err := json.Unmarshal([]byte(`{"local": {"host": ":8080"}, "logger": {"level": 3}}`), &cfg)
292
293         err = cm.ReadFile("./test/schema.json", &d)
294         if err != nil {
295                 t.Errorf("ReadFile failed: %v -> %v", err, d)
296         }
297
298         feedback, err := cm.doValidate(d, cfg)
299         if err != nil {
300                 t.Errorf("doValidate failed: %v -> %v", err, feedback)
301         }
302 }
303
304 func TestValidationFails(t *testing.T) {
305         cm := ConfigMap{}
306         var d interface{}
307         var cfg map[string]interface{}
308
309         err := json.Unmarshal([]byte(`{"local": {"host": ":8080"}, "logger": {"level": "INVALID"}}`), &cfg)
310
311         err = cm.ReadFile("./test/schema.json", &d)
312         if err != nil {
313                 t.Errorf("ConfigMetadata failed: %v -> %v", err, d)
314         }
315
316         feedback, err := cm.doValidate(d, cfg)
317         if err == nil {
318                 t.Errorf("doValidate should faile but didn't: %v -> %v", err, feedback)
319         }
320
321         log.Println("Feedbacks: ", feedback)
322 }