Update logging interface
[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 TestAddTillerEnv(t *testing.T) {
103     if addTillerEnv() != nil {
104         t.Errorf("TestAddTillerEnv failed!")
105         }
106 }
107
108 func TestGetInstallArgs(t *testing.T) {
109         x := XappDeploy{Name: "dummy-xapp", Namespace: "ricxapp"}
110
111         expectedArgs := "install helm-repo/dummy-xapp --name=dummy-xapp  --namespace=ricxapp"
112         if args := getInstallArgs(x, false); args != expectedArgs {
113         t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
114         }
115
116         x.ImageRepo = "localhost:5000"
117         expectedArgs = expectedArgs + " --set global.repository=" + "localhost:5000"
118         if args := getInstallArgs(x, false); args != expectedArgs {
119         t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
120         }
121
122         x.ServiceName = "xapp"
123         expectedArgs = expectedArgs + " --set ricapp.service.name=" + "xapp"
124         if args := getInstallArgs(x, false); args != expectedArgs {
125         t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
126         }
127
128         x.ServiceName = "xapp"
129         expectedArgs = expectedArgs + " --set ricapp.appconfig.override=dummy-xapp-appconfig"
130         if args := getInstallArgs(x, true); args != expectedArgs {
131         t.Errorf("TestGetInstallArgs failed: expected %v, got %v", expectedArgs, args)
132         }
133 }
134
135 func getXappData() (x Xapp) {
136     x = generateXapp("dummy-xapp", "deployed", "1.0", "dummy-xapp-8984fc9fd-bkcbp", "running", "10.102.184.212", "80")
137     x.Instances = append(x.Instances, x.Instances[0])
138     x.Instances = append(x.Instances, x.Instances[0])
139     x.Instances[1].Name = "dummy-xapp-8984fc9fd-l6xch"
140     x.Instances[2].Name = "dummy-xapp-8984fc9fd-pp4hg"
141
142     return x
143 }
144