Katri Turunen | 4b74f01 | 2019-08-15 10:49:36 +0300 | [diff] [blame] | 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. |
Roni Riska | 6ffba08 | 2019-11-27 10:59:54 +0200 | [diff] [blame] | 16 | * |
| 17 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | * platform project (RICP). |
| 19 | * |
Katri Turunen | 4b74f01 | 2019-08-15 10:49:36 +0300 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | package main |
| 23 | |
| 24 | import ( |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 25 | "errors" |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 26 | "os" |
| 27 | "os/exec" |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 28 | "path/filepath" |
| 29 | "strconv" |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 30 | "testing" |
| 31 | "time" |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 32 | |
| 33 | "github.com/stretchr/testify/assert" |
| 34 | "github.com/stretchr/testify/suite" |
Katri Turunen | 4b74f01 | 2019-08-15 10:49:36 +0300 | [diff] [blame] | 35 | ) |
| 36 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 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") |
Katri Turunen | 4b74f01 | 2019-08-15 10:49:36 +0300 | [diff] [blame] | 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 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 67 | func (suite *VesmgrTestSuite) TestMainLoopSupervision() { |
| 68 | go suite.vesmgr.servRequest() |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 69 | ch := make(chan string) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 70 | suite.vesmgr.chSupervision <- ch |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 71 | reply := <-ch |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 72 | suite.Equal("OK", reply) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 73 | } |
| 74 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 75 | func (suite *VesmgrTestSuite) TestMainLoopVesagentError() { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 76 | if os.Getenv("TEST_VESPA_EXIT") == "1" { |
| 77 | // we're run in a new process, now make vesmgr main loop exit |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 78 | go suite.vesmgr.servRequest() |
| 79 | suite.vesmgr.chVesagent <- errors.New("vesagent killed") |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 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 |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 86 | cmd := exec.Command(os.Args[0], "-test.run", "TestVesMgrSuite", "-testify.m", "TestMainLoopVesagentError") |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 87 | cmd.Env = append(os.Environ(), "TEST_VESPA_EXIT=1") |
| 88 | cmd.Stdout = os.Stdout |
| 89 | cmd.Stderr = os.Stderr |
| 90 | err := cmd.Run() |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 91 | // check that vesmgr existed with status 1 |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 92 | |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 93 | e, ok := err.(*exec.ExitError) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 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)) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 145 | } |