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 "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"
38 "o-ran-sc-ric-ueec-config-v1:ric": {
41 "namespace": "ricxapp",
55 var XappDescriptor = `{
56 "o-ran-sc-ric-xapp-desc-v1:ric": {
61 "release-name": "ueec-xapp",
63 "namespace": "ricxapp"
73 func TestMain(M *testing.M) {
74 n = NewNbi(sbi.NewSBIClient("localhost:8080", "/ric/v1/", []string{"http"}, 5))
76 time.Sleep(time.Duration(1) * time.Second)
81 func TestModifyConfigmap(t *testing.T) {
82 ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", http.StatusOK, apimodel.ConfigValidationErrors{})
86 err := json.Unmarshal([]byte(XappConfig), &f)
87 assert.Equal(t, true, err == nil)
89 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfig, 1)
90 assert.Equal(t, true, err == nil)
93 func TestDeployXApp(t *testing.T) {
94 ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", http.StatusCreated, apimodel.Xapp{})
98 err := json.Unmarshal([]byte(XappDescriptor), &f)
99 assert.Equal(t, true, err == nil)
101 err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 0)
102 assert.Equal(t, true, err == nil)
105 func TestUnDeployXApp(t *testing.T) {
106 ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", http.StatusNoContent, apimodel.Xapp{})
110 err := json.Unmarshal([]byte(XappDescriptor), &f)
111 assert.Equal(t, true, err == nil)
113 err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 2)
114 assert.Equal(t, true, err == nil)
117 func TestGetDeployedXapps(t *testing.T) {
118 ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", http.StatusOK, apimodel.AllDeployedXapps{})
121 err := sbiClient.GetDeployedXapps()
122 assert.Equal(t, true, err == nil)
125 func TestErrorCases(t *testing.T) {
127 err := n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", "", 2)
128 assert.Equal(t, true, err == nil)
131 err = n.ManageXapps("", "{}", 2)
132 assert.Equal(t, true, err == nil)
135 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", "{}", 2)
136 assert.Equal(t, true, err == nil)
139 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 1)
140 assert.Equal(t, true, err == nil)
143 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "", 1)
144 assert.Equal(t, true, err == nil)
147 err = n.ManageConfigmaps("", "{}", 1)
148 assert.Equal(t, true, err == nil)
151 err = n.ManageConfigmaps("o-ran-sc-ric-xapp-desc-v1", "{}", 0)
152 assert.Equal(t, true, err == nil)
155 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "{}", 0)
156 assert.Equal(t, true, err != nil)
159 func TestConnStatus2Str(t *testing.T) {
160 assert.Equal(t, n.ConnStatus2Str(0), "not-specified")
161 assert.Equal(t, n.ConnStatus2Str(1), "connected")
162 assert.Equal(t, n.ConnStatus2Str(2), "disconnected")
163 assert.Equal(t, n.ConnStatus2Str(3), "setup-failed")
164 assert.Equal(t, n.ConnStatus2Str(4), "connecting")
165 assert.Equal(t, n.ConnStatus2Str(5), "shutting-down")
166 assert.Equal(t, n.ConnStatus2Str(6), "shutdown")
167 assert.Equal(t, n.ConnStatus2Str(1234), "not-specified")
170 func TestE2APProt2Str(t *testing.T) {
171 assert.Equal(t, n.E2APProt2Str(0), "not-specified")
172 assert.Equal(t, n.E2APProt2Str(1), "x2-setup-request")
173 assert.Equal(t, n.E2APProt2Str(2), "endc-x2-setup-request")
174 assert.Equal(t, n.E2APProt2Str(1111), "not-specified")
177 func TestNodeType2Str(t *testing.T) {
178 assert.Equal(t, n.NodeType2Str(0), "not-specified")
179 assert.Equal(t, n.NodeType2Str(1), "enb")
180 assert.Equal(t, n.NodeType2Str(2), "gnb")
181 assert.Equal(t, n.NodeType2Str(1111), "not-specified")
184 func TestTeardown(t *testing.T) {
188 func CreateHTTPServer(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
189 l, err := net.Listen("tcp", "localhost:8080")
191 t.Error("Failed to create listener: " + err.Error())
193 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
194 assert.Equal(t, r.Method, method)
195 assert.Equal(t, r.URL.String(), url)
196 w.Header().Add("Content-Type", "application/json")
197 w.WriteHeader(status)
198 b, _ := json.Marshal(respData)
209 func DescMatcher(result, expected *apimodel.XappDescriptor) bool {
210 if *result.XappName == *expected.XappName && result.HelmVersion == expected.HelmVersion &&
211 result.Namespace == expected.Namespace && result.ReleaseName == expected.ReleaseName {
217 func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
218 if *result.Metadata.XappName == *expected.Metadata.XappName &&
219 *result.Metadata.Namespace == *expected.Metadata.Namespace {