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 ==================================================================================
29 "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/appmgr"
30 "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/models"
31 "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/util"
34 var helmSearchOutput = `
35 helm-repo/anr 0.0.1 1.0 Helm Chart for Nokia ANR (Automatic Neighbour Relation) xAPP
36 helm-repo/appmgr 0.0.2 1.0 Helm Chart for xAppManager
37 helm-repo/dualco 0.0.1 1.0 Helm Chart for Nokia dualco xAPP
38 helm-repo/reporter 0.0.1 1.0 Helm Chart for Reporting xAPP
39 helm-repo/uemgr 0.0.1 1.0 Helm Chart for Nokia uemgr xAPP
42 var kubectlConfigmapOutput = `
51 "protPort": "tcp:4560",
54 "txMessages": ["RIC_X2_LOAD_INFORMATION"],
55 "rxMessages": ["RIC_X2_LOAD_INFORMATION"],
56 "policies": [11, 22, 33]
59 "namespace": "ricxapp",
66 type ConfigSample struct {
71 type MockedConfigMapper struct {
74 func (cm *MockedConfigMapper) ReadSchema(name string, c *models.XAppConfig) (err error) {
78 func (cm *MockedConfigMapper) UploadConfig() (cfg []models.XAppConfig) {
82 func (cm *MockedConfigMapper) UpdateConfigMap(r models.XAppConfig) (errList models.ConfigValidationErrors, err error) {
86 func (cm *MockedConfigMapper) ReadConfigMap(name string, ns string, c *interface{}) (err error) {
90 func (cm *MockedConfigMapper) FetchChart(name string) (err error) {
94 func (cm *MockedConfigMapper) GetRtmData(name string) (msgs appmgr.RtmData) {
98 func (cm *MockedConfigMapper) GetNamespace(ns string) (n string) {
102 func (cm *MockedConfigMapper) GetNamesFromHelmRepo() (names []string) {
107 func TestMain(m *testing.M) {
109 appmgr.Logger.SetLevel(0)
115 func TestGetRtmData(t *testing.T) {
116 expectedMsgs := appmgr.RtmData{
117 TxMessages: []string{"RIC_X2_LOAD_INFORMATION"},
118 RxMessages: []string{"RIC_X2_LOAD_INFORMATION"},
119 Policies: []int64{11, 22, 33},
122 util.KubectlExec = func(args string) (out []byte, err error) {
123 return []byte(kubectlConfigmapOutput), nil
126 result := NewCM().GetRtmData("dummy-xapp")
127 if !reflect.DeepEqual(result, expectedMsgs) {
128 t.Errorf("TestGetRtmData failed: expected: %v, got: %v", expectedMsgs, result)
132 func TestHelmNamespace(t *testing.T) {
133 if NewCM().GetNamespace("pltxapp") != "pltxapp" {
134 t.Errorf("getNamespace failed!")
137 if NewCM().GetNamespace("") != "ricxapp" {
138 t.Errorf("getNamespace failed!")
142 func TestFetchChartFails(t *testing.T) {
143 if NewCM().FetchChart("dummy-xapp") == nil {
144 t.Errorf("TestFetchChart failed!")
148 func TestFetchChartSuccess(t *testing.T) {
149 util.HelmExec = func(args string) (out []byte, err error) {
153 if NewCM().FetchChart("dummy-xapp") != nil {
154 t.Errorf("TestFetchChart failed!")
158 func TestGetNamesFromHelmRepoSuccess(t *testing.T) {
159 expectedResult := []string{"anr", "appmgr", "dualco", "reporter", "uemgr"}
160 util.HelmExec = func(args string) (out []byte, err error) {
161 return []byte(helmSearchOutput), nil
164 names := NewCM().GetNamesFromHelmRepo()
165 if !reflect.DeepEqual(names, expectedResult) {
166 t.Errorf("GetNamesFromHelmRepo failed: expected %v, got %v", expectedResult, names)
170 func TestGetNamesFromHelmRepoFailure(t *testing.T) {
171 expectedResult := []string{}
172 util.HelmExec = func(args string) (out []byte, err error) {
173 return []byte(helmSearchOutput), errors.New("Command failed!")
176 names := NewCM().GetNamesFromHelmRepo()
178 t.Errorf("GetNamesFromHelmRepo failed: expected %v, got %v", expectedResult, names)
182 func TestBuildConfigMapSuccess(t *testing.T) {
184 namespace := "ricxapp"
185 m := models.ConfigMetadata{XappName: &name, Namespace: &namespace}
186 s := `{"Metadata": {"XappName": "ueec", "Namespace": "ricxapp"}, "Config": {"active": true, "interfaceId":{"globalENBId": {"eNBId": 77, "plmnId": "6666"}}}}`
188 util.KubectlExec = func(args string) (out []byte, err error) {
189 return []byte(`{"logger": {"level": 2}}`), nil
192 cmString, err := NewCM().BuildConfigMap(models.XAppConfig{Metadata: &m, Config: s})
194 t.Errorf("BuildConfigMap failed: %v -> %v", err, cmString)
198 func TestUpdateConfigMapFails(t *testing.T) {
200 namespace := "ricxapp"
201 config := models.XAppConfig{Metadata: &models.ConfigMetadata{XappName: &name, Namespace: &namespace}}
203 validationErrors, err := NewCM().UpdateConfigMap(config)
205 t.Errorf("UpdateConfigMap failed: %v -> %v", err, validationErrors)
209 func TestValidationSuccess(t *testing.T) {
211 var cfg map[string]interface{}
212 err := json.Unmarshal([]byte(`{"active": true, "interfaceId":{"globalENBId": {"eNBId": 77, "plmnId": "6666"}}}`), &cfg)
214 err = NewCM().ReadFile("../../test/schema.json", &d)
216 t.Errorf("ReadFile failed: %v -> %v", err, d)
219 feedback, err := NewCM().doValidate(d, cfg)
221 t.Errorf("doValidate failed: %v -> %v", err, feedback)
225 func TestValidationFails(t *testing.T) {
227 var cfg map[string]interface{}
228 err := json.Unmarshal([]byte(`{"active": "INVALID", "interfaceId":{"globalENBId": {"eNBId": 77, "plmnId": "6666"}}}`), &cfg)
230 err = NewCM().ReadFile("../../test/schema.json", &d)
232 t.Errorf("ConfigMetadata failed: %v -> %v", err, d)
235 feedback, err := NewCM().doValidate(d, cfg)
237 t.Errorf("doValidate should faile but didn't: %v -> %v", err, feedback)
239 appmgr.Logger.Debug("Feedbacks: %v", feedback)