19dd60d82b200375c6b3feda1fa498f0522e081c
[ric-plt/vespamgr.git] / cmd / vesmgr / vesmgr_test.go
1 /*
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
18 package main
19
20 import (
21         "errors"
22         "github.com/stretchr/testify/assert"
23         "os"
24         "os/exec"
25         "testing"
26         "time"
27 )
28
29 func 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
39 func TestStartVesagent(t *testing.T) {
40         assert.Equal(t, 0, vesagent.Pid)
41         ch := make(chan error)
42         startVesagent(ch)
43         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
50 func TestStartVesagentFails(t *testing.T) {
51         vesagent.name = "Not-ves-agent"
52         assert.Equal(t, 0, vesagent.Pid)
53         ch := make(chan error)
54         startVesagent(ch)
55         err := <-ch
56         assert.NotNil(t, err)
57         assert.Equal(t, 0, vesagent.Pid)
58         vesagent.name = "ves-agent"
59 }
60
61 func 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
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)
75
76         ch := make(chan string)
77         chSupervision <- ch
78         reply := <-ch
79         assert.Equal(t, "OK", reply)
80 }
81
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)
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 }