Support for XApp configuration update
[ric-plt/appmgr.git] / cmd / appmgr / 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 main
21
22 import (
23     "testing"
24     "reflect"
25 )
26
27
28 var helmStatusOutput = `
29 LAST DEPLOYED: Sat Mar  9 06:50:45 2019
30 NAMESPACE: default
31 STATUS: DEPLOYED
32
33 RESOURCES:
34 ==> v1/Pod(related)
35 NAME                        READY  STATUS   RESTARTS  AGE
36 dummy-xapp-8984fc9fd-bkcbp  1/1    Running  0         55m
37 dummy-xapp-8984fc9fd-l6xch  1/1    Running  0         55m
38 dummy-xapp-8984fc9fd-pp4hg  1/1    Running  0         55m
39
40 ==> v1/Service
41 NAME                         TYPE       CLUSTER-IP      EXTERNAL-IP  PORT(S)  AGE
42 dummy-xapp-dummy-xapp-chart  ClusterIP  10.102.184.212  <none>       80/TCP   55m
43
44 ==> v1beta1/Deployment
45 NAME        READY  UP-TO-DATE  AVAILABLE  AGE
46 dummy-xapp  3/3    3           3          55m
47 `
48
49 var helListOutput = `Next: ""
50 Releases:
51 - AppVersion: "1.0"
52   Chart: dummy-xapp-chart-0.1.0
53   Name: dummy-xapp
54   Namespace: default
55   Revision: 1
56   Status: DEPLOYED
57   Updated: Mon Mar 11 06:55:05 2019
58 - AppVersion: "2.0"
59   Chart: dummy-xapp-chart-0.1.0
60   Name: dummy-xapp2
61   Namespace: default
62   Revision: 1
63   Status: DEPLOYED
64   Updated: Mon Mar 11 06:55:05 2019
65 - AppVersion: "1.0"
66   Chart: appmgr-0.0.1
67   Name: appmgr
68   Namespace: default
69   Revision: 1
70   Status: DEPLOYED
71   Updated: Sun Mar 24 07:17:00 2019`
72
73
74 var h = Helm{}
75
76 func TestHelmStatus(t *testing.T) {
77         h.SetCM(&ConfigMap{})
78         xapp, err := h.ParseStatus("dummy-xapp", helmStatusOutput)
79     if err != nil {
80         t.Errorf("Helm install failed: %v", err)
81         }
82
83     x := getXappData()
84     xapp.Version = "1.0"
85
86     if !reflect.DeepEqual(xapp, x) {
87         t.Errorf("\n%v \n%v", xapp, x)
88     }
89 }
90
91 func TestHelmLists(t *testing.T) {
92     names, err := h.GetNames(helListOutput)
93     if err != nil {
94         t.Errorf("Helm status failed: %v", err)
95         }
96
97     if !reflect.DeepEqual(names, []string{"dummy-xapp", "dummy-xapp2"}) {
98         t.Errorf("Helm status failed: %v", err)
99     }
100 }
101
102 func TestHelmNamespace(t *testing.T) {
103     if getNamespace("pltxapp") != "pltxapp" {
104         t.Errorf("getNamespace failed!")
105         }
106
107     if getNamespace("") != "ricxapp" {
108         t.Errorf("getNamespace failed!")
109         }
110 }
111
112 func TestAddTillerEnv(t *testing.T) {
113     if addTillerEnv() != nil {
114         t.Errorf("TestAddTillerEnv failed!")
115         }
116 }
117
118 func TestGetInstallArgs(t *testing.T) {
119         x := XappDeploy{Name: "dummy-xapp"}
120
121         expectedArgs := "install helm-repo/dummy-xapp --name=dummy-xapp  --namespace=ricxapp"
122         if args := getInstallArgs(x, false); args != expectedArgs {
123         t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
124         }
125
126         x.ImageRepo = "localhost:5000"
127         expectedArgs = expectedArgs + " --set image.repository=" + "localhost:5000"
128         if args := getInstallArgs(x, false); args != expectedArgs {
129         t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
130         }
131
132         x.ServiceName = "xapp"
133         expectedArgs = expectedArgs + " --set service.name=" + "xapp"
134         if args := getInstallArgs(x, false); args != expectedArgs {
135         t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
136         }
137
138         x.ServiceName = "xapp"
139         expectedArgs = expectedArgs + " --set appconfig.override=true"
140         if args := getInstallArgs(x, true); args != expectedArgs {
141         t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
142         }
143 }
144
145 func getXappData() (x Xapp) {
146     x = generateXapp("dummy-xapp", "deployed", "1.0", "dummy-xapp-8984fc9fd-bkcbp", "running", "10.102.184.212", "80")
147     x.Instances = append(x.Instances, x.Instances[0])
148     x.Instances = append(x.Instances, x.Instances[0])
149     x.Instances[1].Name = "dummy-xapp-8984fc9fd-l6xch"
150     x.Instances[2].Name = "dummy-xapp-8984fc9fd-pp4hg"
151
152     return x
153 }
154