X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=cmd%2Fvesmgr%2Fvesmgr_test.go;h=21c69c0230e5fe261712fad2a4d8e7790c62176d;hb=refs%2Fchanges%2F05%2F4905%2F3;hp=0d90f7a2e57c044b0c52698087ed2c56cf4ae53a;hpb=4b74f01111b3b14fbb3832d8aaf4946cded374a0;p=ric-plt%2Fvespamgr.git diff --git a/cmd/vesmgr/vesmgr_test.go b/cmd/vesmgr/vesmgr_test.go index 0d90f7a..21c69c0 100644 --- a/cmd/vesmgr/vesmgr_test.go +++ b/cmd/vesmgr/vesmgr_test.go @@ -13,19 +13,49 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * This source code is part of the near-RT RIC (RAN Intelligent Controller) + * platform project (RICP). + * */ package main import ( + "errors" "os" + "os/exec" + "path/filepath" + "strconv" "testing" + "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/suite" ) -func init() { - vesagent.name = "echo" // no need to run real ves-agent +func TestGetMyIP(t *testing.T) { + myIPAddress, err := getMyIP() + assert.NotEqual(t, string(""), myIPAddress) + assert.Nil(t, err) +} + +func TestConfCreate(t *testing.T) { + tmpfile := filepath.Join(os.TempDir(), "vestest."+strconv.Itoa(os.Getpid())) + defer os.Remove(tmpfile) // clean up + createConf(tmpfile, []byte("{}")) + _, err := os.Stat(tmpfile) + assert.Nil(t, err) +} + +type VesmgrTestSuite struct { + suite.Suite + vesmgr VesMgr +} + +func (suite *VesmgrTestSuite) SetupSuite() { + suite.vesmgr = VesMgr{} + suite.vesmgr.Init("0") logger.MdcAdd("Testvesmgr", "0.0.1") os.Setenv("VESMGR_HB_INTERVAL", "30s") os.Setenv("VESMGR_MEAS_INTERVAL", "30s") @@ -34,23 +64,82 @@ func init() { os.Setenv("VESMGR_PROMETHEUS_ADDR", "http://localhost:9090") } -func TestStartVesagent(t *testing.T) { - assert.Equal(t, 0, vesagent.Pid) - ch := startVesagent() - assert.NotEqual(t, 0, vesagent.Pid) - t.Logf("VES agent pid = %d", vesagent.Pid) - vesagent.Pid = 0 - err := <-ch - assert.Nil(t, err) +func (suite *VesmgrTestSuite) TestMainLoopSupervision() { + go suite.vesmgr.servRequest() + ch := make(chan string) + suite.vesmgr.chSupervision <- ch + reply := <-ch + suite.Equal("OK", reply) } -func TestStartVesagentFails(t *testing.T) { +func (suite *VesmgrTestSuite) TestMainLoopVesagentError() { + if os.Getenv("TEST_VESPA_EXIT") == "1" { + // we're run in a new process, now make vesmgr main loop exit + go suite.vesmgr.servRequest() + suite.vesmgr.chVesagent <- errors.New("vesagent killed") + // we should never actually end up to this sleep, since the runVesmgr should exit + time.Sleep(3 * time.Second) + return + } + + // Run the vesmgr exit test as a separate process + cmd := exec.Command(os.Args[0], "-test.run", "TestVesMgrSuite", "-testify.m", "TestMainLoopVesagentError") + cmd.Env = append(os.Environ(), "TEST_VESPA_EXIT=1") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + // check that vesmgr existed with status 1 + + e, ok := err.(*exec.ExitError) + suite.True(ok) + suite.Equal("exit status 1", e.Error()) +} + +func (suite *VesmgrTestSuite) TestWaitSubscriptionLoopRespondsSupervisionAndBreaksWhenReceivedSubsNotif() { + go func() { + time.Sleep(time.Second) + ch := make(chan string) + suite.vesmgr.chSupervision <- ch + suite.Equal("OK", <-ch) + suite.vesmgr.chSupervision <- ch + suite.Equal("OK", <-ch) + suite.vesmgr.chXAppSubscriptions <- subscriptionNotification{true, nil, ""} + }() + + suite.vesmgr.waitSubscriptionLoop() +} + +func (suite *VesmgrTestSuite) TestEmptyNotificationChannelReadsAllMsgsFromCh() { + go func() { + for i := 0; i < 11; i++ { + suite.vesmgr.chXAppNotifications <- []byte("hello") + } + }() + time.Sleep(500 * time.Millisecond) + <-suite.vesmgr.chXAppNotifications + suite.vesmgr.emptyNotificationsChannel() + select { + case <-suite.vesmgr.chXAppNotifications: + suite.Fail("Got unexpected notification") + default: + // ok + } +} + +func (suite *VesmgrTestSuite) TestVespaKilling() { + suite.vesmgr.vesagent = makeRunner("sleep", "20") + suite.vesmgr.startVesagent() + suite.NotNil(suite.vesmgr.killVespa()) +} + +func (suite *VesmgrTestSuite) TestVespaKillingAlreadyKilled() { + suite.vesmgr.vesagent = makeRunner("sleep", "20") + suite.vesmgr.startVesagent() + suite.NotNil(suite.vesmgr.killVespa()) + // Just check that second kill does not block execution + suite.NotNil(suite.vesmgr.killVespa()) +} - vesagent.name = "Not-ves-agent" - assert.Equal(t, 0, vesagent.Pid) - ch := startVesagent() - err := <-ch - assert.NotNil(t, err) - assert.Equal(t, 0, vesagent.Pid) - vesagent.name = "ves-agent" +func TestVesMgrSuite(t *testing.T) { + suite.Run(t, new(VesmgrTestSuite)) }