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) {
122 func TestGetMessages(t *testing.T) {
124 expectedMsgs := MessageTypes{
125 TxMessages: []string{"RIC_X2_LOAD_INFORMATION"},
126 RxMessages: []string{"RIC_X2_LOAD_INFORMATION"},
129 KubectlExec = func(args string) (out []byte, err error) {
130 return []byte(kubectlConfigmapOutput), nil
133 result := cm.GetMessages("dummy-xapp")
134 if !reflect.DeepEqual(result, expectedMsgs) {
135 t.Errorf("TestGetMessages failed: expected: %v, got: %v", expectedMsgs, result)
139 func TestHelmNamespace(t *testing.T) {
142 if cm.GetNamespace("pltxapp") != "pltxapp" {
143 t.Errorf("getNamespace failed!")
146 if cm.GetNamespace("") != "ricxapp" {
147 t.Errorf("getNamespace failed!")
151 func TestFetchChartFails(t *testing.T) {
154 if cm.FetchChart("dummy-xapp") == nil {
155 t.Errorf("TestFetchChart failed!")
159 func TestFetchChartSuccess(t *testing.T) {
162 HelmExec = func(args string) (out []byte, err error) {
166 if cm.FetchChart("dummy-xapp") != nil {
167 t.Errorf("TestFetchChart failed!")
171 func TestGetNamesFromHelmRepoSuccess(t *testing.T) {
173 expectedResult := []string{"anr", "appmgr", "dualco", "reporter", "uemgr"}
174 HelmExec = func(args string) (out []byte, err error) {
175 return []byte(helmSearchOutput), nil
178 names := cm.GetNamesFromHelmRepo()
179 if !reflect.DeepEqual(names, expectedResult) {
180 t.Errorf("GetNamesFromHelmRepo failed: expected %v, got %v", expectedResult, names)
184 func TestGetNamesFromHelmRepoFailure(t *testing.T) {
186 expectedResult := []string{}
187 HelmExec = func(args string) (out []byte, err error) {
188 return []byte(helmSearchOutput), errors.New("Command failed!")
191 names := cm.GetNamesFromHelmRepo()
193 t.Errorf("GetNamesFromHelmRepo failed: expected %v, got %v", expectedResult, names)
197 func TestApplyConfigMapSuccess(t *testing.T) {
199 m := ConfigMetadata{Name: "dummy-xapp", Namespace: "ricxapp"}
200 s := ConfigSample{5, "localhost"}
202 KubectlExec = func(args string) (out []byte, err error) {
203 log.Println("TestApplyConfigMapSuccess: ", args)
204 return []byte(`{"logger": {"level": 2}}`), nil
207 err := cm.ApplyConfigMap(XAppConfig{Metadata: m, Configuration: s}, "create")
209 t.Errorf("ApplyConfigMap failed: %v", err)
213 func TestRestoreConfigMapSuccess(t *testing.T) {
215 m := XappDeploy{Name: "dummy-xapp", Namespace: "ricxapp"}
216 s := ConfigSample{5, "localhost"}
218 KubectlExec = func(args string) (out []byte, err error) {
219 log.Println("TestRestoreConfigMapSuccess: ", args)
220 return []byte(`{"logger": {"level": 2}}`), nil
223 err := cm.RestoreConfigMap(m, s)
225 t.Errorf("RestoreConfigMap failed: %v", err)
229 func TestDeleteConfigMapSuccess(t *testing.T) {
232 HelmExec = func(args string) (out []byte, err error) {
233 return []byte("ok"), nil
236 KubectlExec = func(args string) (out []byte, err error) {
237 log.Println("TestDeleteConfigMapSuccess: ", args)
238 return []byte(`{"logger": {"level": 2}}`), nil
241 c, err := cm.DeleteConfigMap(XAppConfig{})
243 t.Errorf("DeleteConfigMap failed: %v -> %v", err, c)
247 func TestPurgeConfigMapSuccess(t *testing.T) {
250 HelmExec = func(args string) (out []byte, err error) {
251 return []byte("ok"), nil
254 KubectlExec = func(args string) (out []byte, err error) {
255 return []byte(`{"logger": {"level": 2}}`), nil
258 c, err := cm.PurgeConfigMap(XappDeploy{})
260 t.Errorf("PurgeConfigMap failed: %v -> %v", err, c)
264 func TestCreateConfigMapFails(t *testing.T) {
267 c, err := cm.CreateConfigMap(XAppConfig{})
269 t.Errorf("CreateConfigMap failed: %v -> %v", err, c)
273 func TestUpdateConfigMapFails(t *testing.T) {
276 c, err := cm.UpdateConfigMap(XAppConfig{})
278 t.Errorf("CreateConfigMap failed: %v -> %v", err, c)
282 func TestValidationSuccess(t *testing.T) {
285 var cfg map[string]interface{}
287 err := json.Unmarshal([]byte(`{"local": {"host": ":8080"}, "logger": {"level": 3}}`), &cfg)
289 err = cm.ReadFile("./test/schema.json", &d)
291 t.Errorf("ReadFile failed: %v -> %v", err, d)
294 feedback, err := cm.doValidate(d, cfg)
296 t.Errorf("doValidate failed: %v -> %v", err, feedback)
300 func TestValidationFails(t *testing.T) {
303 var cfg map[string]interface{}
305 err := json.Unmarshal([]byte(`{"local": {"host": ":8080"}, "logger": {"level": "INVALID"}}`), &cfg)
307 err = cm.ReadFile("./test/schema.json", &d)
309 t.Errorf("ConfigMetadata failed: %v -> %v", err, d)
312 feedback, err := cm.doValidate(d, cfg)
314 t.Errorf("doValidate should faile but didn't: %v -> %v", err, feedback)
317 log.Println("Feedbacks: ", feedback)