blob: 19dd60d82b200375c6b3feda1fa498f0522e081c [file] [log] [blame]
Katri Turunen4b74f012019-08-15 10:49:36 +03001/*
2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-2019 Nokia.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package main
19
20import (
Katri Turunen412df962019-09-16 08:48:18 +030021 "errors"
Katri Turunen4b74f012019-08-15 10:49:36 +030022 "github.com/stretchr/testify/assert"
Katri Turunen412df962019-09-16 08:48:18 +030023 "os"
24 "os/exec"
25 "testing"
26 "time"
Katri Turunen4b74f012019-08-15 10:49:36 +030027)
28
29func init() {
30 vesagent.name = "echo" // no need to run real ves-agent
31 logger.MdcAdd("Testvesmgr", "0.0.1")
32 os.Setenv("VESMGR_HB_INTERVAL", "30s")
33 os.Setenv("VESMGR_MEAS_INTERVAL", "30s")
34 os.Setenv("VESMGR_PRICOLLECTOR_ADDR", "127.1.1.1")
35 os.Setenv("VESMGR_PRICOLLECTOR_PORT", "8443")
36 os.Setenv("VESMGR_PROMETHEUS_ADDR", "http://localhost:9090")
37}
38
39func TestStartVesagent(t *testing.T) {
40 assert.Equal(t, 0, vesagent.Pid)
Katri Turunen412df962019-09-16 08:48:18 +030041 ch := make(chan error)
42 startVesagent(ch)
Katri Turunen4b74f012019-08-15 10:49:36 +030043 assert.NotEqual(t, 0, vesagent.Pid)
44 t.Logf("VES agent pid = %d", vesagent.Pid)
45 vesagent.Pid = 0
46 err := <-ch
47 assert.Nil(t, err)
48}
49
50func TestStartVesagentFails(t *testing.T) {
Katri Turunen4b74f012019-08-15 10:49:36 +030051 vesagent.name = "Not-ves-agent"
52 assert.Equal(t, 0, vesagent.Pid)
Katri Turunen412df962019-09-16 08:48:18 +030053 ch := make(chan error)
54 startVesagent(ch)
Katri Turunen4b74f012019-08-15 10:49:36 +030055 err := <-ch
56 assert.NotNil(t, err)
57 assert.Equal(t, 0, vesagent.Pid)
58 vesagent.name = "ves-agent"
59}
Katri Turunen412df962019-09-16 08:48:18 +030060
61func TestGetMyIP(t *testing.T) {
62 vesmgr.myIPAddress = string("")
63 var err error
64 vesmgr.myIPAddress, err = getMyIP()
65 assert.NotEqual(t, string(""), vesmgr.myIPAddress)
66 assert.Equal(t, nil, err)
67}
68
69func TestMainLoopSupervision(t *testing.T) {
70 chXAppNotifications := make(chan []byte)
71 chSupervision := make(chan chan string)
72 chVesagent := make(chan error)
73 chSubscriptions := make(chan subsChannel)
74 go runVesmgr(chVesagent, chSupervision, chXAppNotifications, chSubscriptions)
75
76 ch := make(chan string)
77 chSupervision <- ch
78 reply := <-ch
79 assert.Equal(t, "OK", reply)
80}
81
82func TestMainLoopVesagentError(t *testing.T) {
83 if os.Getenv("TEST_VESPA_EXIT") == "1" {
84 // we're run in a new process, now make vesmgr main loop exit
85 chXAppNotifications := make(chan []byte)
86 chSupervision := make(chan chan string)
87 chVesagent := make(chan error)
88 chSubscriptions := make(chan subsChannel)
89 go runVesmgr(chVesagent, chSupervision, chXAppNotifications, chSubscriptions)
90
91 chVesagent <- errors.New("vesagent killed")
92 // we should never actually end up to this sleep, since the runVesmgr should exit
93 time.Sleep(3 * time.Second)
94 return
95 }
96
97 // Run the vesmgr exit test as a separate process
98 cmd := exec.Command(os.Args[0], "-test.run=TestMainLoopVesagentError")
99 cmd.Env = append(os.Environ(), "TEST_VESPA_EXIT=1")
100 cmd.Stdout = os.Stdout
101 cmd.Stderr = os.Stderr
102 err := cmd.Run()
103
104 // check that vesmgr existed with status 1
105 e, ok := err.(*exec.ExitError)
106 assert.Equal(t, true, ok)
107 assert.Equal(t, "exit status 1", e.Error())
108}