Initial commit
[ric-plt/o1.git] / agent / pkg / nbi / nbi_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
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
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
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 ==================================================================================
18 */
19
20 package nbi
21
22 import (
23         "os"    
24         "time"
25         "encoding/json"
26         "testing"
27         "net"
28     "net/http"
29     "net/http/httptest"
30         "github.com/stretchr/testify/assert"
31
32         apimodel "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrmodel"
33         "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/sbi"
34         
35 )
36
37 var XappConfig = `{
38         "o-ran-sc-ric-ueec-config-v1:ric": {
39           "config": {
40                 "name": "ueec",
41                 "namespace": "ricxapp",
42                 "control": {
43                   "active": true,
44                   "interfaceId": {
45                         "globalENBId": {
46                           "plmnId": "1234",
47                           "eNBId": "55"
48                         }
49                   }
50                 }
51           }
52         }
53   }`
54
55 var XappDescriptor = `{
56         "o-ran-sc-ric-xapp-desc-v1:ric": {
57           "xapps": {
58                 "xapp": [
59                   {
60                         "name": "ueec",
61                         "release-name": "ueec-xapp",
62                         "version": "0.0.1",
63                         "namespace": "ricxapp"
64                   }
65                 ]
66           }
67         }
68   }`
69
70 var n *Nbi
71
72 // Test cases
73 func TestMain(M *testing.M) {
74         n = NewNbi(sbi.NewSBIClient("localhost:8080", "/ric/v1/", []string{"http"}, 5))
75         go n.Start()
76         time.Sleep(time.Duration(1) * time.Second)
77
78         os.Exit(M.Run())
79 }
80
81 func TestModifyConfigmap(t *testing.T) {
82         ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", http.StatusOK, apimodel.ConfigValidationErrors{})
83         defer ts.Close()
84
85         var f interface{}
86         err := json.Unmarshal([]byte(XappConfig), &f)
87         assert.Equal(t, true, err == nil)
88
89         err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfig, 1)
90         assert.Equal(t, true, err == nil)
91 }
92
93 func TestDeployXApp(t *testing.T) {
94         ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", http.StatusCreated, apimodel.Xapp{})
95         defer ts.Close()
96
97         var f interface{}
98         err := json.Unmarshal([]byte(XappDescriptor), &f)
99         assert.Equal(t, true, err == nil)
100
101         err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 0)
102         assert.Equal(t, true, err == nil)
103 }
104
105 func TestUnDeployXApp(t *testing.T) {
106         ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", http.StatusNoContent, apimodel.Xapp{})
107         defer ts.Close()
108
109         var f interface{}
110         err := json.Unmarshal([]byte(XappDescriptor), &f)
111         assert.Equal(t, true, err == nil)
112
113         err = n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", XappDescriptor, 2)
114         assert.Equal(t, true, err == nil)
115 }
116
117 func TestGetDeployedXapps(t *testing.T) {
118         ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", http.StatusOK, apimodel.AllDeployedXapps{})
119         defer ts.Close()
120
121         err := sbiClient.GetDeployedXapps()
122         assert.Equal(t, true, err == nil)
123 }
124
125 func TestErrorCases(t *testing.T) {
126         // Invalid config
127         err := n.ManageXapps("o-ran-sc-ric-xapp-desc-v1", "", 2)
128         assert.Equal(t, true, err == nil)
129
130         // Invalid module
131         err = n.ManageXapps("", "{}", 2)
132         assert.Equal(t, true, err == nil)
133
134         // Unexpected module
135         err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", "{}", 2)
136         assert.Equal(t, true, err == nil)
137
138         // Invalid operation
139         err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 1)
140         assert.Equal(t, true, err == nil)
141
142         // Invalid config
143         err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "", 1)
144         assert.Equal(t, true, err == nil)
145
146         // Invalid module
147         err = n.ManageConfigmaps("", "{}", 1)
148         assert.Equal(t, true, err == nil)
149
150         // Unexpected module
151         err = n.ManageConfigmaps("o-ran-sc-ric-xapp-desc-v1", "{}", 0)
152         assert.Equal(t, true, err == nil)
153
154         // Invalid operation
155         err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "{}", 0)
156         assert.Equal(t, true, err != nil)
157 }
158
159 func TestTeardown(t *testing.T) {
160         n.Stop()
161 }
162
163 func CreateHTTPServer(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server {
164         l, err := net.Listen("tcp", "localhost:8080")
165         if err != nil {
166                         t.Error("Failed to create listener: " + err.Error())
167         }
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)
174                 w.Write(b)
175         }))
176         ts.Listener.Close()
177         ts.Listener = l
178
179         ts.Start()
180
181         return ts
182 }
183
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 {
187                 return true
188         }
189         return false
190 }
191
192 func ConfigMatcher(result, expected *apimodel.XAppConfig) bool {
193         if *result.Metadata.XappName == *expected.Metadata.XappName &&
194            *result.Metadata.Namespace == *expected.Metadata.Namespace {
195                 return true
196         }
197         return false
198 }