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.
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
33 "github.com/stretchr/testify/assert"
34 "github.com/stretchr/testify/suite"
37 func TestGetMyIP(t *testing.T) {
38 myIPAddress, err := getMyIP()
39 assert.NotEqual(t, string(""), myIPAddress)
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)
51 type VesmgrTestSuite struct {
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")
67 func (suite *VesmgrTestSuite) TestMainLoopSupervision() {
68 go suite.vesmgr.servRequest()
69 ch := make(chan string)
70 suite.vesmgr.chSupervision <- ch
72 suite.Equal("OK", reply)
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)
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
91 // check that vesmgr existed with status 1
93 e, ok := err.(*exec.ExitError)
95 suite.Equal("exit status 1", e.Error())
98 func (suite *VesmgrTestSuite) TestWaitSubscriptionLoopRespondsSupervisionAndBreaksWhenReceivedSubsNotif() {
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, ""}
109 suite.vesmgr.waitSubscriptionLoop()
112 func (suite *VesmgrTestSuite) TestEmptyNotificationChannelReadsAllMsgsFromCh() {
114 for i := 0; i < 11; i++ {
115 suite.vesmgr.chXAppNotifications <- []byte("hello")
118 time.Sleep(500 * time.Millisecond)
119 <-suite.vesmgr.chXAppNotifications
120 suite.vesmgr.emptyNotificationsChannel()
122 case <-suite.vesmgr.chXAppNotifications:
123 suite.Fail("Got unexpected notification")
129 func (suite *VesmgrTestSuite) TestVespaKilling() {
130 suite.vesmgr.vesagent = makeRunner("sleep", "20")
131 suite.vesmgr.startVesagent()
132 suite.NotNil(suite.vesmgr.killVespa())
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())
143 func TestVesMgrSuite(t *testing.T) {
144 suite.Run(t, new(VesmgrTestSuite))