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 TestTeardown(t *testing.T) {
163 func CreateHTTPServer(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
164 l, err := net.Listen("tcp", "localhost:8080")
166 t.Error("Failed to create listener: " + err.Error())
168 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
169 assert.Equal(t, r.Method, method)
170 assert.Equal(t, r.URL.String(), url)
171 w.Header().Add("Content-Type", "application/json")
172 w.WriteHeader(status)
173 b, _ := json.Marshal(respData)
184 func DescMatcher(result, expected *apimodel.XappDescriptor) bool {
185 if *result.XappName == *expected.XappName && result.HelmVersion == expected.HelmVersion &&
186 result.Namespace == expected.Namespace && result.ReleaseName == expected.ReleaseName {
192 func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
193 if *result.Metadata.XappName == *expected.Metadata.XappName &&
194 *result.Metadata.Namespace == *expected.Metadata.Namespace {