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 ==================================================================================
24 "github.com/stretchr/testify/assert"
32 apimodel "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrmodel"
33 "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/sbi"
37 "o-ran-sc-ric-ueec-config-v1:ric": {
40 "namespace": "ricxapp",
54 var XappDescriptor = `{
55 "o-ran-sc-ric-xapp-desc-v1:ric": {
60 "release-name": "ueec-xapp",
62 "namespace": "ricxapp"
70 NAME READY STATUS RESTARTS AGE
71 ricxapp-ueec-7bfdd587db-2jl9j 1/1 Running 53 29d
72 ricxapp-anr-6748846478-8hmtz 1-1 Running 1 29d
73 ricxapp-dualco-7f76f65c99-5p6c6 0/1 Running 1 29d
79 func TestMain(M *testing.M) {
80 n = NewNbi(sbi.NewSBIClient("localhost:8080", "/ric/v1/", []string{"http"}, 5))
82 time.Sleep(time.Duration(1) * time.Second)
87 func TestModifyConfigmap(t *testing.T) {
88 ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", http.StatusOK, apimodel.ConfigValidationErrors{})
92 err := json.Unmarshal([]byte(XappConfig), &f)
93 assert.Equal(t, true, err == nil)
95 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfig, 1)
96 assert.Equal(t, true, err == nil)
99 func TestDeployXApp(t *testing.T) {
100 ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", http.StatusCreated, apimodel.Xapp{})
104 err := json.Unmarshal([]byte(XappDescriptor), &f)
105 assert.Equal(t, true, err == nil)
107 err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 0)
108 assert.Equal(t, true, err == nil)
111 func TestUnDeployXApp(t *testing.T) {
112 ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", http.StatusNoContent, apimodel.Xapp{})
116 err := json.Unmarshal([]byte(XappDescriptor), &f)
117 assert.Equal(t, true, err == nil)
119 err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 2)
120 assert.Equal(t, true, err == nil)
123 func TestGetDeployedXapps(t *testing.T) {
124 ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", http.StatusOK, apimodel.AllDeployedXapps{})
127 err := sbiClient.GetDeployedXapps()
128 assert.Equal(t, true, err == nil)
131 func TestGetAllPodStatus(t *testing.T) {
132 sbi.CommandExec = func(args string) (out string, err error) {
133 assert.Equal(t, "/usr/local/bin/kubectl get pod -n ricxapp", args)
134 return kpodOutput, nil
137 expectedPodList := []sbi.PodStatus{
145 Health: "unavailable",
155 podList, err := sbiClient.GetAllPodStatus("ricxapp")
156 assert.Equal(t, true, err == nil)
157 assert.Equal(t, podList, expectedPodList)
160 func TestErrorCases(t *testing.T) {
162 err := n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", "", 2)
163 assert.Equal(t, true, err == nil)
166 err = n.ManageXapps("", "{}", 2)
167 assert.Equal(t, true, err == nil)
170 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", "{}", 2)
171 assert.Equal(t, true, err == nil)
174 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 1)
175 assert.Equal(t, true, err == nil)
178 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "", 1)
179 assert.Equal(t, true, err == nil)
182 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "{}", 0)
183 assert.Equal(t, true, err != nil)
186 func TestConnStatus2Str(t *testing.T) {
187 assert.Equal(t, n.ConnStatus2Str(0), "not-specified")
188 assert.Equal(t, n.ConnStatus2Str(1), "connected")
189 assert.Equal(t, n.ConnStatus2Str(2), "disconnected")
190 assert.Equal(t, n.ConnStatus2Str(3), "setup-failed")
191 assert.Equal(t, n.ConnStatus2Str(4), "connecting")
192 assert.Equal(t, n.ConnStatus2Str(5), "shutting-down")
193 assert.Equal(t, n.ConnStatus2Str(6), "shutdown")
194 assert.Equal(t, n.ConnStatus2Str(1234), "not-specified")
197 func TestE2APProt2Str(t *testing.T) {
198 assert.Equal(t, n.E2APProt2Str(0), "not-specified")
199 assert.Equal(t, n.E2APProt2Str(1), "x2-setup-request")
200 assert.Equal(t, n.E2APProt2Str(2), "endc-x2-setup-request")
201 assert.Equal(t, n.E2APProt2Str(1111), "not-specified")
204 func TestNodeType2Str(t *testing.T) {
205 assert.Equal(t, n.NodeType2Str(0), "not-specified")
206 assert.Equal(t, n.NodeType2Str(1), "enb")
207 assert.Equal(t, n.NodeType2Str(2), "gnb")
208 assert.Equal(t, n.NodeType2Str(1111), "not-specified")
211 func TestTeardown(t *testing.T) {
215 func CreateHTTPServer(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
216 l, err := net.Listen("tcp", "localhost:8080")
218 t.Error("Failed to create listener: " + err.Error())
220 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
221 assert.Equal(t, r.Method, method)
222 assert.Equal(t, r.URL.String(), url)
223 w.Header().Add("Content-Type", "application/json")
224 w.WriteHeader(status)
225 b, _ := json.Marshal(respData)
236 func DescMatcher(result, expected *apimodel.XappDescriptor) bool {
237 if *result.XappName == *expected.XappName && result.HelmVersion == expected.HelmVersion &&
238 result.Namespace == expected.Namespace && result.ReleaseName == expected.ReleaseName {
244 func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
245 if *result.Metadata.XappName == *expected.Metadata.XappName &&
246 *result.Metadata.Namespace == *expected.Metadata.Namespace {