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