2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-2019 Nokia.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 "github.com/stretchr/testify/assert"
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")
39 func TestStartVesagent(t *testing.T) {
40 assert.Equal(t, 0, vesagent.Pid)
41 ch := make(chan error)
43 assert.NotEqual(t, 0, vesagent.Pid)
44 t.Logf("VES agent pid = %d", vesagent.Pid)
50 func TestStartVesagentFails(t *testing.T) {
51 vesagent.name = "Not-ves-agent"
52 assert.Equal(t, 0, vesagent.Pid)
53 ch := make(chan error)
57 assert.Equal(t, 0, vesagent.Pid)
58 vesagent.name = "ves-agent"
61 func TestGetMyIP(t *testing.T) {
62 vesmgr.myIPAddress = string("")
64 vesmgr.myIPAddress, err = getMyIP()
65 assert.NotEqual(t, string(""), vesmgr.myIPAddress)
66 assert.Equal(t, nil, err)
69 func 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)
76 ch := make(chan string)
79 assert.Equal(t, "OK", reply)
82 func 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)
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)
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
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())