Refactor the code
[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         "os"
23         "os/exec"
24         "path/filepath"
25         "strconv"
26         "testing"
27         "time"
28
29         "github.com/stretchr/testify/assert"
30         "github.com/stretchr/testify/suite"
31 )
32
33 func TestGetMyIP(t *testing.T) {
34         myIPAddress, err := getMyIP()
35         assert.NotEqual(t, string(""), myIPAddress)
36         assert.Nil(t, err)
37 }
38
39 func TestConfCreate(t *testing.T) {
40         tmpfile := filepath.Join(os.TempDir(), "vestest."+strconv.Itoa(os.Getpid()))
41         defer os.Remove(tmpfile) // clean up
42         createConf(tmpfile, []byte("{}"))
43         _, err := os.Stat(tmpfile)
44         assert.Nil(t, err)
45 }
46
47 type VesmgrTestSuite struct {
48         suite.Suite
49         vesmgr VesMgr
50 }
51
52 func (suite *VesmgrTestSuite) SetupSuite() {
53         suite.vesmgr = VesMgr{}
54         suite.vesmgr.Init("0")
55         logger.MdcAdd("Testvesmgr", "0.0.1")
56         os.Setenv("VESMGR_HB_INTERVAL", "30s")
57         os.Setenv("VESMGR_MEAS_INTERVAL", "30s")
58         os.Setenv("VESMGR_PRICOLLECTOR_ADDR", "127.1.1.1")
59         os.Setenv("VESMGR_PRICOLLECTOR_PORT", "8443")
60         os.Setenv("VESMGR_PROMETHEUS_ADDR", "http://localhost:9090")
61 }
62
63 func (suite *VesmgrTestSuite) TestMainLoopSupervision() {
64         go suite.vesmgr.servRequest()
65         ch := make(chan string)
66         suite.vesmgr.chSupervision <- ch
67         reply := <-ch
68         suite.Equal("OK", reply)
69 }
70
71 func (suite *VesmgrTestSuite) TestMainLoopVesagentError() {
72         if os.Getenv("TEST_VESPA_EXIT") == "1" {
73                 // we're run in a new process, now make vesmgr main loop exit
74                 go suite.vesmgr.servRequest()
75                 suite.vesmgr.chVesagent <- errors.New("vesagent killed")
76                 // we should never actually end up to this sleep, since the runVesmgr should exit
77                 time.Sleep(3 * time.Second)
78                 return
79         }
80
81         // Run the vesmgr exit test as a separate process
82         cmd := exec.Command(os.Args[0], "-test.run", "TestVesMgrSuite", "-testify.m", "TestMainLoopVesagentError")
83         cmd.Env = append(os.Environ(), "TEST_VESPA_EXIT=1")
84         cmd.Stdout = os.Stdout
85         cmd.Stderr = os.Stderr
86         err := cmd.Run()
87         // check that vesmgr existed with status 1
88
89         e, ok := err.(*exec.ExitError)
90         suite.True(ok)
91         suite.Equal("exit status 1", e.Error())
92 }
93
94 func (suite *VesmgrTestSuite) TestWaitSubscriptionLoopRespondsSupervisionAndBreaksWhenReceivedSubsNotif() {
95         go func() {
96                 time.Sleep(time.Second)
97                 ch := make(chan string)
98                 suite.vesmgr.chSupervision <- ch
99                 suite.Equal("OK", <-ch)
100                 suite.vesmgr.chSupervision <- ch
101                 suite.Equal("OK", <-ch)
102                 suite.vesmgr.chXAppSubscriptions <- subscriptionNotification{true, nil, ""}
103         }()
104
105         suite.vesmgr.waitSubscriptionLoop()
106 }
107
108 func (suite *VesmgrTestSuite) TestEmptyNotificationChannelReadsAllMsgsFromCh() {
109         go func() {
110                 for i := 0; i < 11; i++ {
111                         suite.vesmgr.chXAppNotifications <- []byte("hello")
112                 }
113         }()
114         time.Sleep(500 * time.Millisecond)
115         <-suite.vesmgr.chXAppNotifications
116         suite.vesmgr.emptyNotificationsChannel()
117         select {
118         case <-suite.vesmgr.chXAppNotifications:
119                 suite.Fail("Got unexpected notification")
120         default:
121                 // ok
122         }
123 }
124
125 func (suite *VesmgrTestSuite) TestVespaKilling() {
126         suite.vesmgr.vesagent = makeRunner("sleep", "20")
127         suite.vesmgr.startVesagent()
128         suite.NotNil(suite.vesmgr.killVespa())
129 }
130
131 func (suite *VesmgrTestSuite) TestVespaKillingAlreadyKilled() {
132         suite.vesmgr.vesagent = makeRunner("sleep", "20")
133         suite.vesmgr.startVesagent()
134         suite.NotNil(suite.vesmgr.killVespa())
135         // Just check that second kill does not block execution
136         suite.NotNil(suite.vesmgr.killVespa())
137 }
138
139 func TestVesMgrSuite(t *testing.T) {
140         suite.Run(t, new(VesmgrTestSuite))
141 }