blob: 21c69c0230e5fe261712fad2a4d8e7790c62176d [file] [log] [blame]
Katri Turunen4b74f012019-08-15 10:49:36 +03001/*
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.
Roni Riska6ffba082019-11-27 10:59:54 +020016 *
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
19 *
Katri Turunen4b74f012019-08-15 10:49:36 +030020 */
21
22package main
23
24import (
Katri Turunen412df962019-09-16 08:48:18 +030025 "errors"
Katri Turunen412df962019-09-16 08:48:18 +030026 "os"
27 "os/exec"
Roni Riskafc77ebb2019-09-26 08:20:44 +030028 "path/filepath"
29 "strconv"
Katri Turunen412df962019-09-16 08:48:18 +030030 "testing"
31 "time"
Roni Riskafc77ebb2019-09-26 08:20:44 +030032
33 "github.com/stretchr/testify/assert"
34 "github.com/stretchr/testify/suite"
Katri Turunen4b74f012019-08-15 10:49:36 +030035)
36
Roni Riskafc77ebb2019-09-26 08:20:44 +030037func TestGetMyIP(t *testing.T) {
38 myIPAddress, err := getMyIP()
39 assert.NotEqual(t, string(""), myIPAddress)
40 assert.Nil(t, err)
41}
42
43func 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
51type VesmgrTestSuite struct {
52 suite.Suite
53 vesmgr VesMgr
54}
55
56func (suite *VesmgrTestSuite) SetupSuite() {
57 suite.vesmgr = VesMgr{}
58 suite.vesmgr.Init("0")
Katri Turunen4b74f012019-08-15 10:49:36 +030059 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
Roni Riskafc77ebb2019-09-26 08:20:44 +030067func (suite *VesmgrTestSuite) TestMainLoopSupervision() {
68 go suite.vesmgr.servRequest()
Katri Turunen412df962019-09-16 08:48:18 +030069 ch := make(chan string)
Roni Riskafc77ebb2019-09-26 08:20:44 +030070 suite.vesmgr.chSupervision <- ch
Katri Turunen412df962019-09-16 08:48:18 +030071 reply := <-ch
Roni Riskafc77ebb2019-09-26 08:20:44 +030072 suite.Equal("OK", reply)
Katri Turunen412df962019-09-16 08:48:18 +030073}
74
Roni Riskafc77ebb2019-09-26 08:20:44 +030075func (suite *VesmgrTestSuite) TestMainLoopVesagentError() {
Katri Turunen412df962019-09-16 08:48:18 +030076 if os.Getenv("TEST_VESPA_EXIT") == "1" {
77 // we're run in a new process, now make vesmgr main loop exit
Roni Riskafc77ebb2019-09-26 08:20:44 +030078 go suite.vesmgr.servRequest()
79 suite.vesmgr.chVesagent <- errors.New("vesagent killed")
Katri Turunen412df962019-09-16 08:48:18 +030080 // 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
Roni Riskafc77ebb2019-09-26 08:20:44 +030086 cmd := exec.Command(os.Args[0], "-test.run", "TestVesMgrSuite", "-testify.m", "TestMainLoopVesagentError")
Katri Turunen412df962019-09-16 08:48:18 +030087 cmd.Env = append(os.Environ(), "TEST_VESPA_EXIT=1")
88 cmd.Stdout = os.Stdout
89 cmd.Stderr = os.Stderr
90 err := cmd.Run()
Katri Turunen412df962019-09-16 08:48:18 +030091 // check that vesmgr existed with status 1
Roni Riskafc77ebb2019-09-26 08:20:44 +030092
Katri Turunen412df962019-09-16 08:48:18 +030093 e, ok := err.(*exec.ExitError)
Roni Riskafc77ebb2019-09-26 08:20:44 +030094 suite.True(ok)
95 suite.Equal("exit status 1", e.Error())
96}
97
98func (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
112func (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
129func (suite *VesmgrTestSuite) TestVespaKilling() {
130 suite.vesmgr.vesagent = makeRunner("sleep", "20")
131 suite.vesmgr.startVesagent()
132 suite.NotNil(suite.vesmgr.killVespa())
133}
134
135func (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
143func TestVesMgrSuite(t *testing.T) {
144 suite.Run(t, new(VesmgrTestSuite))
Katri Turunen412df962019-09-16 08:48:18 +0300145}