Resolve RMR port from K8s services
[ric-plt/appmgr.git] / pkg / helm / helm_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 helm
21
22 import (
23         "os"
24         "reflect"
25         "strconv"
26         "testing"
27
28         "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/appmgr"
29         "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/models"
30         "gerrit.o-ran-sc.org/r/ric-plt/appmgr/pkg/util"
31 )
32
33 var helmStatusOutput = `
34 LAST DEPLOYED: Sat Mar  9 06:50:45 2019
35 NAMESPACE: default
36 STATUS: DEPLOYED
37
38 RESOURCES:
39 ==> v1/Pod(related)
40 NAME                        READY  STATUS   RESTARTS  AGE
41 dummy-xapp-8984fc9fd-bkcbp  1/1    Running  0         55m
42 dummy-xapp-8984fc9fd-l6xch  1/1    Running  0         55m
43 dummy-xapp-8984fc9fd-pp4hg  1/1    Running  0         55m
44
45 ==> v1/Service
46 NAME                         TYPE       CLUSTER-IP      EXTERNAL-IP  PORT(S)  AGE
47 dummy-xapp-dummy-xapp-chart  ClusterIP  10.102.184.212  <none>       80/TCP   55m
48
49 ==> v1beta1/Deployment
50 NAME        READY  UP-TO-DATE  AVAILABLE  AGE
51 dummy-xapp  3/3    3           3          55m
52 `
53
54 var helListOutput = `Next: ""
55 Releases:
56 - AppVersion: "1.0"
57   Chart: dummy-xapp-chart-0.1.0
58   Name: dummy-xapp
59   Namespace: default
60   Revision: 1
61   Status: DEPLOYED
62   Updated: Mon Mar 11 06:55:05 2019
63 - AppVersion: "2.0"
64   Chart: dummy-xapp-chart-0.1.0
65   Name: dummy-xapp2
66   Namespace: default
67   Revision: 1
68   Status: DEPLOYED
69   Updated: Mon Mar 11 06:55:05 2019
70 - AppVersion: "1.0"
71   Chart: appmgr-0.0.1
72   Name: appmgr
73   Namespace: default
74   Revision: 1
75   Status: DEPLOYED
76   Updated: Sun Mar 24 07:17:00 2019
77   `
78
79 var helmServiceOutput = `{
80     "apiVersion": "v1",
81     "kind": "Service",
82     "metadata": {
83         "creationTimestamp": "2020-03-31T12:27:12Z",
84         "labels": {
85             "app": "ricxapp-dummy-xapp",
86             "chart": "dummy-xapp-0.0.4",
87             "heritage": "Tiller",
88             "release": "dummy-xapp"
89         },
90         "name": "service-ricxapp-dummy-xapp-rmr",
91         "namespace": "ricxapp",
92         "resourceVersion": "4423380",
93         "selfLink": "/api/v1/namespaces/ricxapp/services/service-ricxapp-dummy-xapp-rmr",
94         "uid": "2254b77d-7dd6-43e0-beff-3e2a7b24c89a"
95     },
96     "spec": {
97         "clusterIP": "10.98.239.107",
98         "ports": [
99             {
100                 "name": "rmrdata",
101                 "port": 4560,
102                 "protocol": "TCP",
103                 "targetPort": "rmrdata"
104             },
105             {
106                 "name": "rmrroute",
107                 "port": 4561,
108                 "protocol": "TCP",
109                 "targetPort": "rmrroute"
110             }
111         ],
112         "selector": {
113             "app": "ricxapp-dummy-xapp",
114             "release": "dummy-xapp"
115         },
116         "sessionAffinity": "None",
117         "type": "ClusterIP"
118     },
119     "status": {
120         "loadBalancer": {}
121     }
122 }`
123
124 // Test cases
125 func TestMain(m *testing.M) {
126         appmgr.Init()
127         appmgr.Logger.SetLevel(0)
128
129         code := m.Run()
130         os.Exit(code)
131 }
132
133 func TestHelmStatus(t *testing.T) {
134         util.KubectlExec = func(args string) (out []byte, err error) {
135                 return []byte(helmServiceOutput), nil
136         }
137         xapp, err := NewHelm().ParseStatus("dummy-xapp", helmStatusOutput)
138         if err != nil {
139                 t.Errorf("Helm install failed: %v", err)
140         }
141         x := getXappData()
142         xapp.Version = "1.0"
143
144         if *x.Name != *xapp.Name || x.Status != xapp.Status || x.Version != xapp.Version {
145                 t.Errorf("\n%v \n%v", *xapp.Name, *x.Name)
146         }
147
148         if *x.Instances[0].Name != *xapp.Instances[0].Name || x.Instances[0].Status != xapp.Instances[0].Status {
149                 t.Errorf("\n1:%v 2:%v", *x.Instances[0].Name, *xapp.Instances[0].Name)
150         }
151
152         if x.Instances[0].IP != xapp.Instances[0].IP || x.Instances[0].Port != xapp.Instances[0].Port {
153                 t.Errorf("\n%v - %v, %v - %v", x.Instances[0].IP, xapp.Instances[0].IP, x.Instances[0].Port, xapp.Instances[0].Port)
154         }
155 }
156
157 func TestHelmLists(t *testing.T) {
158         names, err := NewHelm().GetNames(helListOutput)
159         if err != nil {
160                 t.Errorf("Helm status failed: %v", err)
161         }
162
163         if !reflect.DeepEqual(names, []string{"dummy-xapp", "dummy-xapp2"}) {
164                 t.Errorf("Helm status failed: %v", err)
165         }
166 }
167
168 func TestAddTillerEnv(t *testing.T) {
169         if NewHelm().AddTillerEnv() != nil {
170                 t.Errorf("TestAddTillerEnv failed!")
171         }
172 }
173
174 func TestGetInstallArgs(t *testing.T) {
175         name := "dummy-xapp"
176         x := models.XappDescriptor{XappName: &name, Namespace: "ricxapp"}
177
178         expectedArgs := "install helm-repo/dummy-xapp  --namespace=ricxapp --name=dummy-xapp"
179         if args := NewHelm().GetInstallArgs(x, false); args != expectedArgs {
180                 t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
181         }
182
183         x.HelmVersion = "1.2.3"
184         expectedArgs = "install helm-repo/dummy-xapp  --namespace=ricxapp --version=1.2.3 --name=dummy-xapp"
185         if args := NewHelm().GetInstallArgs(x, false); args != expectedArgs {
186                 t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
187         }
188
189         x.ReleaseName = "ueec-xapp"
190         expectedArgs = "install helm-repo/dummy-xapp  --namespace=ricxapp --version=1.2.3 --name=ueec-xapp"
191         if args := NewHelm().GetInstallArgs(x, false); args != expectedArgs {
192                 t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
193         }
194 }
195
196 func getXappData() (x models.Xapp) {
197         //name1 := "dummy-xapp-8984fc9fd-l6xch"
198         //name2 := "dummy-xapp-8984fc9fd-pp4hg"
199         x = generateXapp("dummy-xapp", "deployed", "1.0", "dummy-xapp-8984fc9fd-bkcbp", "running", "service-ricxapp-dummy-xapp-rmr.ricxapp", "4560")
200         //x.Instances = append(x.Instances, x.Instances[0])
201         //x.Instances = append(x.Instances, x.Instances[0])
202         //x.Instances[1].Name = &name1
203         //x.Instances[2].Name = &name2
204
205         return x
206 }
207
208 func generateXapp(name, status, ver, iname, istatus, ip, port string) (x models.Xapp) {
209         x.Name = &name
210         x.Status = status
211         x.Version = ver
212         p, _ := strconv.Atoi(port)
213         var msgs appmgr.RtmData
214
215         instance := &models.XappInstance{
216                 Name:       &iname,
217                 Status:     istatus,
218                 IP:         ip,
219                 Port:       int64(p),
220                 TxMessages: msgs.TxMessages,
221                 RxMessages: msgs.RxMessages,
222         }
223         x.Instances = append(x.Instances, instance)
224
225         return
226 }