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