44882df0f4fd42c4ab1dc6c167a43ba67726d8e5
[ric-plt/appmgr.git] / src / 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     "fmt"
24     "os"
25     "os/exec"
26         "strconv"
27     "testing"
28     "reflect"
29     "github.com/spf13/viper"
30     "io/ioutil"
31     "path"
32 )
33
34 var helmStatusOutput = `
35 LAST DEPLOYED: Sat Mar  9 06:50:45 2019
36 NAMESPACE: default
37 STATUS: DEPLOYED
38
39 RESOURCES:
40 ==> v1/Pod(related)
41 NAME                        READY  STATUS   RESTARTS  AGE
42 dummy-xapp-8984fc9fd-bkcbp  1/1    Running  0         55m
43 dummy-xapp-8984fc9fd-l6xch  1/1    Running  0         55m
44 dummy-xapp-8984fc9fd-pp4hg  1/1    Running  0         55m
45
46 ==> v1/Service
47 NAME                         TYPE       CLUSTER-IP      EXTERNAL-IP  PORT(S)  AGE
48 dummy-xapp-dummy-xapp-chart  ClusterIP  10.102.184.212  <none>       80/TCP   55m
49
50 ==> v1beta1/Deployment
51 NAME        READY  UP-TO-DATE  AVAILABLE  AGE
52 dummy-xapp  3/3    3           3          55m
53 `
54
55 var helListOutput = `Next: ""
56 Releases:
57 - AppVersion: "1.0"
58   Chart: dummy-xapp-chart-0.1.0
59   Name: dummy-xapp
60   Namespace: default
61   Revision: 1
62   Status: DEPLOYED
63   Updated: Mon Mar 11 06:55:05 2019
64 - AppVersion: "2.0"
65   Chart: dummy-xapp-chart-0.1.0
66   Name: dummy-xapp2
67   Namespace: default
68   Revision: 1
69   Status: DEPLOYED
70   Updated: Mon Mar 11 06:55:05 2019
71 - AppVersion: "1.0"
72   Chart: appmgr-0.0.1
73   Name: appmgr
74   Namespace: default
75   Revision: 1
76   Status: DEPLOYED
77   Updated: Sun Mar 24 07:17:00 2019`
78
79 var mockedExitStatus = 0
80 var mockedStdout string
81 var h = Helm{}
82
83 func fakeExecCommand(command string, args ...string) *exec.Cmd {
84     cs := []string{"-test.run=TestExecCommandHelper", "--", command}
85     cs = append(cs, args...)
86         
87         cmd := exec.Command(os.Args[0], cs...)
88     es := strconv.Itoa(mockedExitStatus)
89         cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1", "STDOUT=" + mockedStdout, "EXIT_STATUS=" + es}
90         
91     return cmd
92 }
93
94 func TestExecCommandHelper(t *testing.T) {
95     if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
96         return
97     }
98
99     fmt.Fprintf(os.Stdout, os.Getenv("STDOUT"))
100     i, _ := strconv.Atoi(os.Getenv("EXIT_STATUS"))
101     os.Exit(i)
102 }
103
104 func writeTestCreds() (err error) {
105
106     // Write test entries to helm username and password files
107     f, err := os.Create(viper.GetString("helm.helm-username-file"))
108     if err != nil {
109         return err
110     }
111
112     _, err = f.WriteString(viper.GetString("helm.secrets.username"))
113     if err != nil {
114         f.Close()
115         return (err)
116     }
117     f.Close()
118
119     f, err = os.Create(viper.GetString("helm.helm-password-file"))
120     if err != nil {
121         return err
122     }
123
124     _, err = f.WriteString(viper.GetString("helm.secrets.password"))
125     if err != nil {
126         f.Close()
127         return (err)
128     }
129     f.Close()
130     return
131 }
132
133 func TestHelmInit(t *testing.T) {
134         mockedExitStatus = 0
135     execCommand = fakeExecCommand
136     defer func() { execCommand = exec.Command }()
137
138     if err := writeTestCreds(); err != nil {
139         t.Errorf("Writing test entries failed: %s", err)
140         return
141     }
142
143     out, err := h.Init()
144     if err != nil {
145         t.Errorf("Helm init failed: %s %s", err, string(out))
146     }
147 }
148
149 func TestHelmInstall(t *testing.T) {
150     copyFile(t)
151     mockedExitStatus = 0
152         execCommand = fakeExecCommand
153         mockedStdout = helmStatusOutput
154     defer func() { execCommand = exec.Command }()
155
156     xapp, err := h.Install("dummy-xapp")
157     if err != nil {
158         t.Errorf("Helm install failed: %v", err)
159         }
160
161     x := getXappData()
162     xapp.Version = "1.0"
163
164     if !reflect.DeepEqual(xapp, x) {
165         t.Errorf("%v \n%v", xapp, x)
166     }
167 }
168
169 func TestHelmStatus(t *testing.T) {
170     copyFile(t)
171     mockedExitStatus = 0
172     mockedStdout = helmStatusOutput
173     execCommand = fakeExecCommand
174     defer func() { execCommand = exec.Command }()
175
176     xapp, err := h.Status("dummy-xapp")
177     if err != nil {
178         t.Errorf("Helm status failed: %v", err)
179         }
180
181     x := getXappData()
182     xapp.Version = "1.0"
183
184         if !reflect.DeepEqual(xapp, x) {
185         t.Errorf("%v \n%v", xapp, x)
186     }
187 }
188
189 func TestHelmStatusAll(t *testing.T) {
190     copyFile(t)
191     mockedExitStatus = 0
192     mockedStdout = helListOutput
193     execCommand = fakeExecCommand
194     defer func() { execCommand = exec.Command }()
195
196     xapp, err := h.StatusAll()
197     if err != nil {
198         t.Errorf("Helm StatusAll failed: %v - %v", err, xapp)
199         }
200
201     // Todo: check the content
202 }
203
204 func TestHelmParseAllStatus(t *testing.T) {
205     copyFile(t)
206     mockedExitStatus = 0
207     mockedStdout = helListOutput
208     execCommand = fakeExecCommand
209     defer func() { execCommand = exec.Command }()
210
211     xapp, err := h.parseAllStatus([]string{"dummy-xapp", "dummy-xapp2"})
212     if err != nil {
213         t.Errorf("Helm parseAllStatus failed: %v - %v", err, xapp)
214         }
215
216     // Todo: check the content
217 }
218
219 func TestHelmDelete(t *testing.T) {
220     copyFile(t)
221     mockedExitStatus = 0
222     mockedStdout = helListOutput
223     execCommand = fakeExecCommand
224     defer func() { execCommand = exec.Command }()
225
226     xapp, err := h.Delete("dummy-xapp")
227     if err != nil {
228         t.Errorf("Helm delete failed: %v - %v", err, xapp)
229         }
230
231     // Todo: check the content
232 }
233
234 func TestHelmLists(t *testing.T) {
235     mockedExitStatus = 0
236     mockedStdout = helListOutput
237     execCommand = fakeExecCommand
238     defer func() { execCommand = exec.Command }()
239
240     names, err := h.List()
241     if err != nil {
242         t.Errorf("Helm status failed: %v", err)
243         }
244
245     if !reflect.DeepEqual(names, []string{"dummy-xapp", "dummy-xapp2"}) {
246         t.Errorf("Helm status failed: %v", err)
247     }
248 }
249
250 func getXappData() (x Xapp) {
251     x = generateXapp("dummy-xapp", "deployed", "1.0", "dummy-xapp-8984fc9fd-bkcbp", "running", "10.102.184.212", "80")
252     x.Instances = append(x.Instances, x.Instances[0])
253     x.Instances = append(x.Instances, x.Instances[0])
254     x.Instances[1].Name = "dummy-xapp-8984fc9fd-l6xch"
255     x.Instances[2].Name = "dummy-xapp-8984fc9fd-pp4hg"
256
257     return x
258 }
259
260
261 func copyFile(t *testing.T) {
262     tarDir := path.Join(viper.GetString("xapp.tarDir"), "dummy-xapp")
263     err := os.MkdirAll(tarDir, 0777)
264     if err != nil {
265          t.Errorf("%v", err)
266     }
267
268     data, err := ioutil.ReadFile("../config/msg_type.yaml")
269     if err != nil {
270          t.Errorf("%v", err)
271     }
272
273     _ = ioutil.WriteFile(path.Join(tarDir, "msg_type.yaml"), data, 0644)
274     if err != nil {
275          t.Errorf("%v", err)
276     }
277 }