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"
31 "github.com/go-openapi/strfmt"
34 "github.com/prometheus/alertmanager/api/v2/models"
35 apimodel "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrmodel"
36 "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/sbi"
40 "o-ran-sc-ric-ueec-config-v1:ric": {
43 "namespace": "ricxapp",
57 var XappDescriptor = `{
58 "o-ran-sc-ric-xapp-desc-v1:ric": {
63 "release-name": "ueec-xapp",
65 "namespace": "ricxapp"
73 NAME READY STATUS RESTARTS AGE
74 ricxapp-ueec-7bfdd587db-2jl9j 1/1 Running 53 29d
75 ricxapp-anr-6748846478-8hmtz 1-1 Running 1 29d
76 ricxapp-dualco-7f76f65c99-5p6c6 0/1 Running 1 29d
82 func TestMain(M *testing.M) {
83 n = NewNbi(sbi.NewSBIClient("localhost:8080", "localhost:9093", 5))
85 time.Sleep(time.Duration(1) * time.Second)
90 func TestModifyConfigmap(t *testing.T) {
91 ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", 8080, http.StatusOK, apimodel.ConfigValidationErrors{})
95 err := json.Unmarshal([]byte(XappConfig), &f)
96 assert.Equal(t, true, err == nil)
98 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfig, 1)
99 assert.Equal(t, true, err == nil)
102 func TestDeployXApp(t *testing.T) {
103 ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", 8080, http.StatusCreated, apimodel.Xapp{})
107 err := json.Unmarshal([]byte(XappDescriptor), &f)
108 assert.Equal(t, true, err == nil)
110 err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 0)
111 assert.Equal(t, true, err == nil)
114 func TestUnDeployXApp(t *testing.T) {
115 ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", 8080, http.StatusNoContent, apimodel.Xapp{})
119 err := json.Unmarshal([]byte(XappDescriptor), &f)
120 assert.Equal(t, true, err == nil)
122 err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 2)
123 assert.Equal(t, true, err == nil)
126 func TestGetDeployedXapps(t *testing.T) {
127 ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", 8080, http.StatusOK, apimodel.AllDeployedXapps{})
130 err := sbiClient.GetDeployedXapps()
131 assert.Equal(t, true, err == nil)
134 func TestGetAlerts(t *testing.T) {
135 tim := strfmt.DateTime(time.Now())
136 fingerprint := "34c8f717936f063f"
138 alerts := []models.GettableAlert{
139 models.GettableAlert{
141 Labels: models.LabelSet{
143 "alertname": "E2 CONNECTIVITY LOST TO G-NODEB",
145 "service": "RIC:UEEC",
146 "system_name": "RIC",
149 Annotations: models.LabelSet{
151 "additional_info": "ethernet",
152 "description": "eth12",
153 "instructions": "Not defined",
154 "summary": "Communication error",
159 Fingerprint: &fingerprint,
163 url := "/api/v2/alerts?active=true&inhibited=true&silenced=true&unprocessed=true"
164 ts := CreateHTTPServer(t, "GET", url, 9093, http.StatusOK, alerts)
167 resp, err := sbiClient.GetAlerts()
169 assert.Equal(t, true, err == nil)
170 assert.Equal(t, true, resp != nil)
172 for _, alert := range resp.Payload {
173 assert.Equal(t, alert.Annotations, alerts[0].Annotations)
174 assert.Equal(t, alert.Alert, alerts[0].Alert)
175 assert.Equal(t, alert.Fingerprint, alerts[0].Fingerprint)
179 func TestGetAllPodStatus(t *testing.T) {
180 sbi.CommandExec = func(args string) (out string, err error) {
181 assert.Equal(t, "/usr/local/bin/kubectl get pod -n ricxapp", args)
182 return kpodOutput, nil
185 expectedPodList := []sbi.PodStatus{
193 Health: "unavailable",
203 podList, err := sbiClient.GetAllPodStatus("ricxapp")
204 assert.Equal(t, true, err == nil)
205 assert.Equal(t, podList, expectedPodList)
208 func TestErrorCases(t *testing.T) {
210 err := n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", "", 2)
211 assert.Equal(t, true, err == nil)
214 err = n.ManageXapps("", "{}", 2)
215 assert.Equal(t, true, err == nil)
218 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", "{}", 2)
219 assert.Equal(t, true, err == nil)
222 err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 1)
223 assert.Equal(t, true, err == nil)
226 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "", 1)
227 assert.Equal(t, true, err == nil)
230 err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "{}", 0)
231 assert.Equal(t, true, err != nil)
234 func TestConnStatus2Str(t *testing.T) {
235 assert.Equal(t, n.ConnStatus2Str(0), "not-specified")
236 assert.Equal(t, n.ConnStatus2Str(1), "connected")
237 assert.Equal(t, n.ConnStatus2Str(2), "disconnected")
238 assert.Equal(t, n.ConnStatus2Str(3), "setup-failed")
239 assert.Equal(t, n.ConnStatus2Str(4), "connecting")
240 assert.Equal(t, n.ConnStatus2Str(5), "shutting-down")
241 assert.Equal(t, n.ConnStatus2Str(6), "shutdown")
242 assert.Equal(t, n.ConnStatus2Str(1234), "not-specified")
245 func TestE2APProt2Str(t *testing.T) {
246 assert.Equal(t, n.E2APProt2Str(0), "not-specified")
247 assert.Equal(t, n.E2APProt2Str(1), "x2-setup-request")
248 assert.Equal(t, n.E2APProt2Str(2), "endc-x2-setup-request")
249 assert.Equal(t, n.E2APProt2Str(1111), "not-specified")
252 func TestNodeType2Str(t *testing.T) {
253 assert.Equal(t, n.NodeType2Str(0), "not-specified")
254 assert.Equal(t, n.NodeType2Str(1), "enb")
255 assert.Equal(t, n.NodeType2Str(2), "gnb")
256 assert.Equal(t, n.NodeType2Str(1111), "not-specified")
259 func TestTeardown(t *testing.T) {
263 func CreateHTTPServer(t *testing.T, method, url string, port, status int, respData interface{}) *httptest.Server {
264 l, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port))
266 t.Error("Failed to create listener: " + err.Error())
268 ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
269 assert.Equal(t, r.Method, method)
270 assert.Equal(t, r.URL.String(), url)
271 w.Header().Add("Content-Type", "application/json")
272 w.WriteHeader(status)
273 b, _ := json.Marshal(respData)
284 func DescMatcher(result, expected *apimodel.XappDescriptor) bool {
285 if *result.XappName == *expected.XappName && result.HelmVersion == expected.HelmVersion &&
286 result.Namespace == expected.Namespace && result.ReleaseName == expected.ReleaseName {
292 func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
293 if *result.Metadata.XappName == *expected.Metadata.XappName &&
294 *result.Metadata.Namespace == *expected.Metadata.Namespace {