2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
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
38 var kubectlConfigmapOutput = `
47 "protPort": "tcp:4560",
50 "txMessages": ["RIC_X2_LOAD_INFORMATION"],
51 "rxMessages": ["RIC_X2_LOAD_INFORMATION"]
54 "namespace": "ricxapp",
61 type ConfigSample struct {
66 type MockedConfigMapper struct {
69 func (cm *MockedConfigMapper) ReadSchema(name string, c *XAppConfig) (err error) {
73 func (cm *MockedConfigMapper) UploadConfig() (cfg []XAppConfig) {
77 func (cm *MockedConfigMapper) CreateConfigMap(r XAppConfig) (errList []CMError, err error) {
81 func (cm *MockedConfigMapper) GetConfigMap(m XappDeploy, c *interface{}) (err error) {
85 func (cm *MockedConfigMapper) UpdateConfigMap(r XAppConfig) (errList []CMError, err error) {
89 func (cm *MockedConfigMapper) DeleteConfigMap(r XAppConfig) (c interface{}, err error) {
93 func (cm *MockedConfigMapper) PurgeConfigMap(m XappDeploy) (c interface{}, err error) {
97 func (cm *MockedConfigMapper) RestoreConfigMap(m XappDeploy, c interface{}) (err error) {
101 func (cm *MockedConfigMapper) ReadConfigMap(name string, ns string, c *interface{}) (err error) {
105 func (cm *MockedConfigMapper) ApplyConfigMap(r XAppConfig, action string) (err error) {
109 func (cm *MockedConfigMapper) FetchChart(name string) (err error) {
113 func (cm *MockedConfigMapper) GetMessages(name string) (msgs MessageTypes) {
117 func (cm *MockedConfigMapper) GetNamespace(ns string) (n string) {
121 func (cm *MockedConfigMapper) GetNamesFromHelmRepo() (names []string) {
126 func TestGetMessages(t *testing.T) {
128 expectedMsgs := MessageTypes{
129 TxMessages: []string{"RIC_X2_LOAD_INFORMATION"},
130 RxMessages: []string{"RIC_X2_LOAD_INFORMATION"},
133 KubectlExec = func(args string) (out []byte, err error) {
134 return []byte(kubectlConfigmapOutput), nil
137 result := cm.GetMessages("dummy-xapp")
138 if !reflect.DeepEqual(result, expectedMsgs) {
139 t.Errorf("TestGetMessages failed: expected: %v, got: %v", expectedMsgs, result)
143 func TestHelmNamespace(t *testing.T) {
146 if cm.GetNamespace("pltxapp") != "pltxapp" {
147 t.Errorf("getNamespace failed!")
150 if cm.GetNamespace("") != "default" {
151 t.Errorf("getNamespace failed!")
155 func TestFetchChartFails(t *testing.T) {
158 if cm.FetchChart("dummy-xapp") == nil {
159 t.Errorf("TestFetchChart failed!")
163 func TestFetchChartSuccess(t *testing.T) {
166 HelmExec = func(args string) (out []byte, err error) {
170 if cm.FetchChart("dummy-xapp") != nil {
171 t.Errorf("TestFetchChart failed!")
175 func TestGetNamesFromHelmRepoSuccess(t *testing.T) {
177 expectedResult := []string{"anr", "appmgr", "dualco", "reporter", "uemgr"}
178 HelmExec = func(args string) (out []byte, err error) {
179 return []byte(helmSearchOutput), nil
182 names := cm.GetNamesFromHelmRepo()
183 if !reflect.DeepEqual(names, expectedResult) {
184 t.Errorf("GetNamesFromHelmRepo failed: expected %v, got %v", expectedResult, names)
188 func TestGetNamesFromHelmRepoFailure(t *testing.T) {
190 expectedResult := []string{}
191 HelmExec = func(args string) (out []byte, err error) {
192 return []byte(helmSearchOutput), errors.New("Command failed!")
195 names := cm.GetNamesFromHelmRepo()
197 t.Errorf("GetNamesFromHelmRepo failed: expected %v, got %v", expectedResult, names)
201 func TestApplyConfigMapSuccess(t *testing.T) {
203 m := ConfigMetadata{Name: "dummy-xapp", Namespace: "ricxapp"}
204 s := ConfigSample{5, "localhost"}
206 KubectlExec = func(args string) (out []byte, err error) {
207 log.Println("TestApplyConfigMapSuccess: ", args)
208 return []byte(`{"logger": {"level": 2}}`), nil
211 err := cm.ApplyConfigMap(XAppConfig{Metadata: m, Configuration: s}, "create")
213 t.Errorf("ApplyConfigMap failed: %v", err)
217 func TestRestoreConfigMapSuccess(t *testing.T) {
219 m := XappDeploy{Name: "dummy-xapp", Namespace: "ricxapp"}
220 s := ConfigSample{5, "localhost"}
222 KubectlExec = func(args string) (out []byte, err error) {
223 log.Println("TestRestoreConfigMapSuccess: ", args)
224 return []byte(`{"logger": {"level": 2}}`), nil
227 err := cm.RestoreConfigMap(m, s)
229 t.Errorf("RestoreConfigMap failed: %v", err)
233 func TestDeleteConfigMapSuccess(t *testing.T) {
236 HelmExec = func(args string) (out []byte, err error) {
237 return []byte("ok"), nil
240 KubectlExec = func(args string) (out []byte, err error) {
241 log.Println("TestDeleteConfigMapSuccess: ", args)
242 return []byte(`{"logger": {"level": 2}}`), nil
245 c, err := cm.DeleteConfigMap(XAppConfig{})
247 t.Errorf("DeleteConfigMap failed: %v -> %v", err, c)
251 func TestPurgeConfigMapSuccess(t *testing.T) {
254 HelmExec = func(args string) (out []byte, err error) {
255 return []byte("ok"), nil
258 KubectlExec = func(args string) (out []byte, err error) {
259 return []byte(`{"logger": {"level": 2}}`), nil
262 c, err := cm.PurgeConfigMap(XappDeploy{})
264 t.Errorf("PurgeConfigMap failed: %v -> %v", err, c)
268 func TestCreateConfigMapFails(t *testing.T) {
271 c, err := cm.CreateConfigMap(XAppConfig{})
273 t.Errorf("CreateConfigMap failed: %v -> %v", err, c)
277 func TestUpdateConfigMapFails(t *testing.T) {
280 c, err := cm.UpdateConfigMap(XAppConfig{})
282 t.Errorf("CreateConfigMap failed: %v -> %v", err, c)
286 func TestValidationSuccess(t *testing.T) {
289 var cfg map[string]interface{}
291 err := json.Unmarshal([]byte(`{"local": {"host": ":8080"}, "logger": {"level": 3}}`), &cfg)
293 err = cm.ReadFile("./test/schema.json", &d)
295 t.Errorf("ReadFile failed: %v -> %v", err, d)
298 feedback, err := cm.doValidate(d, cfg)
300 t.Errorf("doValidate failed: %v -> %v", err, feedback)
304 func TestValidationFails(t *testing.T) {
307 var cfg map[string]interface{}
309 err := json.Unmarshal([]byte(`{"local": {"host": ":8080"}, "logger": {"level": "INVALID"}}`), &cfg)
311 err = cm.ReadFile("./test/schema.json", &d)
313 t.Errorf("ConfigMetadata failed: %v -> %v", err, d)
316 feedback, err := cm.doValidate(d, cfg)
318 t.Errorf("doValidate should faile but didn't: %v -> %v", err, feedback)
321 log.Println("Feedbacks: ", feedback)