2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
29 "github.com/spf13/viper"
34 var helmStatusOutput = `
35 LAST DEPLOYED: Sat Mar 9 06:50:45 2019
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
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
50 ==> v1beta1/Deployment
51 NAME READY UP-TO-DATE AVAILABLE AGE
52 dummy-xapp 3/3 3 3 55m
55 var helListOutput = `Next: ""
58 Chart: dummy-xapp-chart-0.1.0
63 Updated: Mon Mar 11 06:55:05 2019
65 Chart: dummy-xapp-chart-0.1.0
70 Updated: Mon Mar 11 06:55:05 2019
77 Updated: Sun Mar 24 07:17:00 2019`
79 var mockedExitStatus = 0
80 var mockedStdout string
83 func fakeExecCommand(command string, args ...string) *exec.Cmd {
84 cs := []string{"-test.run=TestExecCommandHelper", "--", command}
85 cs = append(cs, args...)
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}
94 func TestExecCommandHelper(t *testing.T) {
95 if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" {
99 fmt.Fprintf(os.Stdout, os.Getenv("STDOUT"))
100 i, _ := strconv.Atoi(os.Getenv("EXIT_STATUS"))
104 func writeTestCreds() (err error) {
106 // Write test entries to helm username and password files
107 f, err := os.Create(viper.GetString("helm.helm-username-file"))
112 _, err = f.WriteString(viper.GetString("helm.secrets.username"))
119 f, err = os.Create(viper.GetString("helm.helm-password-file"))
124 _, err = f.WriteString(viper.GetString("helm.secrets.password"))
133 func TestHelmInit(t *testing.T) {
135 execCommand = fakeExecCommand
136 defer func() { execCommand = exec.Command }()
138 if err := writeTestCreds(); err != nil {
139 t.Errorf("Writing test entries failed: %s", err)
145 t.Errorf("Helm init failed: %s %s", err, string(out))
149 func TestHelmInstall(t *testing.T) {
152 execCommand = fakeExecCommand
153 mockedStdout = helmStatusOutput
154 defer func() { execCommand = exec.Command }()
156 xapp, err := h.Install("dummy-xapp")
158 t.Errorf("Helm install failed: %v", err)
164 if !reflect.DeepEqual(xapp, x) {
165 t.Errorf("%v \n%v", xapp, x)
169 func TestHelmStatus(t *testing.T) {
172 mockedStdout = helmStatusOutput
173 execCommand = fakeExecCommand
174 defer func() { execCommand = exec.Command }()
176 xapp, err := h.Status("dummy-xapp")
178 t.Errorf("Helm status failed: %v", err)
184 if !reflect.DeepEqual(xapp, x) {
185 t.Errorf("%v \n%v", xapp, x)
189 func TestHelmStatusAll(t *testing.T) {
192 mockedStdout = helListOutput
193 execCommand = fakeExecCommand
194 defer func() { execCommand = exec.Command }()
196 xapp, err := h.StatusAll()
198 t.Errorf("Helm StatusAll failed: %v - %v", err, xapp)
201 // Todo: check the content
204 func TestHelmParseAllStatus(t *testing.T) {
207 mockedStdout = helListOutput
208 execCommand = fakeExecCommand
209 defer func() { execCommand = exec.Command }()
211 xapp, err := h.parseAllStatus([]string{"dummy-xapp", "dummy-xapp2"})
213 t.Errorf("Helm parseAllStatus failed: %v - %v", err, xapp)
216 // Todo: check the content
219 func TestHelmDelete(t *testing.T) {
222 mockedStdout = helListOutput
223 execCommand = fakeExecCommand
224 defer func() { execCommand = exec.Command }()
226 xapp, err := h.Delete("dummy-xapp")
228 t.Errorf("Helm delete failed: %v - %v", err, xapp)
231 // Todo: check the content
234 func TestHelmLists(t *testing.T) {
236 mockedStdout = helListOutput
237 execCommand = fakeExecCommand
238 defer func() { execCommand = exec.Command }()
240 names, err := h.List()
242 t.Errorf("Helm status failed: %v", err)
245 if !reflect.DeepEqual(names, []string{"dummy-xapp", "dummy-xapp2"}) {
246 t.Errorf("Helm status failed: %v", err)
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"
261 func copyFile(t *testing.T) {
262 tarDir := path.Join(viper.GetString("xapp.tarDir"), "dummy-xapp")
263 err := os.MkdirAll(tarDir, 0777)
268 data, err := ioutil.ReadFile("../config/msg_type.yaml")
273 _ = ioutil.WriteFile(path.Join(tarDir, "msg_type.yaml"), data, 0644)