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.
29 "github.com/stretchr/testify/assert"
30 "github.com/stretchr/testify/suite"
33 func TestGetMyIP(t *testing.T) {
34 myIPAddress, err := getMyIP()
35 assert.NotEqual(t, string(""), myIPAddress)
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)
47 type VesmgrTestSuite struct {
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")
63 func (suite *VesmgrTestSuite) TestMainLoopSupervision() {
64 go suite.vesmgr.servRequest()
65 ch := make(chan string)
66 suite.vesmgr.chSupervision <- ch
68 suite.Equal("OK", reply)
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)
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
87 // check that vesmgr existed with status 1
89 e, ok := err.(*exec.ExitError)
91 suite.Equal("exit status 1", e.Error())
94 func (suite *VesmgrTestSuite) TestWaitSubscriptionLoopRespondsSupervisionAndBreaksWhenReceivedSubsNotif() {
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, ""}
105 suite.vesmgr.waitSubscriptionLoop()
108 func (suite *VesmgrTestSuite) TestEmptyNotificationChannelReadsAllMsgsFromCh() {
110 for i := 0; i < 11; i++ {
111 suite.vesmgr.chXAppNotifications <- []byte("hello")
114 time.Sleep(500 * time.Millisecond)
115 <-suite.vesmgr.chXAppNotifications
116 suite.vesmgr.emptyNotificationsChannel()
118 case <-suite.vesmgr.chXAppNotifications:
119 suite.Fail("Got unexpected notification")
125 func (suite *VesmgrTestSuite) TestVespaKilling() {
126 suite.vesmgr.vesagent = makeRunner("sleep", "20")
127 suite.vesmgr.startVesagent()
128 suite.NotNil(suite.vesmgr.killVespa())
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())
139 func TestVesMgrSuite(t *testing.T) {
140 suite.Run(t, new(VesmgrTestSuite))