X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=cmd%2Fvesmgr%2Fvesmgr_test.go;h=21c69c0230e5fe261712fad2a4d8e7790c62176d;hb=6ffba08cc1c469c1ae04a6819445760ec75653b1;hp=19dd60d82b200375c6b3feda1fa498f0522e081c;hpb=412df96a23a30a82d2a031556888aeaf9604ada8;p=ric-plt%2Fvespamgr.git diff --git a/cmd/vesmgr/vesmgr_test.go b/cmd/vesmgr/vesmgr_test.go index 19dd60d..21c69c0 100644 --- a/cmd/vesmgr/vesmgr_test.go +++ b/cmd/vesmgr/vesmgr_test.go @@ -13,96 +13,133 @@ * 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" - "github.com/stretchr/testify/assert" "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 - logger.MdcAdd("Testvesmgr", "0.0.1") - os.Setenv("VESMGR_HB_INTERVAL", "30s") - os.Setenv("VESMGR_MEAS_INTERVAL", "30s") - os.Setenv("VESMGR_PRICOLLECTOR_ADDR", "127.1.1.1") - os.Setenv("VESMGR_PRICOLLECTOR_PORT", "8443") - os.Setenv("VESMGR_PROMETHEUS_ADDR", "http://localhost:9090") +func TestGetMyIP(t *testing.T) { + myIPAddress, err := getMyIP() + assert.NotEqual(t, string(""), myIPAddress) + assert.Nil(t, err) } -func TestStartVesagent(t *testing.T) { - assert.Equal(t, 0, vesagent.Pid) - ch := make(chan error) - startVesagent(ch) - assert.NotEqual(t, 0, vesagent.Pid) - t.Logf("VES agent pid = %d", vesagent.Pid) - vesagent.Pid = 0 - err := <-ch +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) } -func TestStartVesagentFails(t *testing.T) { - vesagent.name = "Not-ves-agent" - assert.Equal(t, 0, vesagent.Pid) - ch := make(chan error) - startVesagent(ch) - err := <-ch - assert.NotNil(t, err) - assert.Equal(t, 0, vesagent.Pid) - vesagent.name = "ves-agent" +type VesmgrTestSuite struct { + suite.Suite + vesmgr VesMgr } -func TestGetMyIP(t *testing.T) { - vesmgr.myIPAddress = string("") - var err error - vesmgr.myIPAddress, err = getMyIP() - assert.NotEqual(t, string(""), vesmgr.myIPAddress) - assert.Equal(t, nil, err) +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") + os.Setenv("VESMGR_PRICOLLECTOR_ADDR", "127.1.1.1") + os.Setenv("VESMGR_PRICOLLECTOR_PORT", "8443") + os.Setenv("VESMGR_PROMETHEUS_ADDR", "http://localhost:9090") } -func TestMainLoopSupervision(t *testing.T) { - chXAppNotifications := make(chan []byte) - chSupervision := make(chan chan string) - chVesagent := make(chan error) - chSubscriptions := make(chan subsChannel) - go runVesmgr(chVesagent, chSupervision, chXAppNotifications, chSubscriptions) - +func (suite *VesmgrTestSuite) TestMainLoopSupervision() { + go suite.vesmgr.servRequest() ch := make(chan string) - chSupervision <- ch + suite.vesmgr.chSupervision <- ch reply := <-ch - assert.Equal(t, "OK", reply) + suite.Equal("OK", reply) } -func TestMainLoopVesagentError(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 - chXAppNotifications := make(chan []byte) - chSupervision := make(chan chan string) - chVesagent := make(chan error) - chSubscriptions := make(chan subsChannel) - go runVesmgr(chVesagent, chSupervision, chXAppNotifications, chSubscriptions) - - chVesagent <- errors.New("vesagent killed") + 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=TestMainLoopVesagentError") + 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) - assert.Equal(t, true, ok) - assert.Equal(t, "exit status 1", e.Error()) + 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()) +} + +func TestVesMgrSuite(t *testing.T) { + suite.Run(t, new(VesmgrTestSuite)) }