X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=agent%2Fpkg%2Fnbi%2Fnbi_test.go;h=e1795ca0c44d073fcfc3587392e8ee2ceba8619a;hb=d36089fd7e360dbbdd3e3f4ea5c39e3cee27b6d9;hp=3812fd6e2e1c72c5a961c1396bef10626943bcc7;hpb=bad83880ceaeeff5b73e9e3aa15cdd302829b5fc;p=ric-plt%2Fo1.git diff --git a/agent/pkg/nbi/nbi_test.go b/agent/pkg/nbi/nbi_test.go index 3812fd6..e1795ca 100755 --- a/agent/pkg/nbi/nbi_test.go +++ b/agent/pkg/nbi/nbi_test.go @@ -20,18 +20,21 @@ package nbi import ( - "os" - "time" "encoding/json" - "testing" - "net" - "net/http" - "net/http/httptest" + "fmt" "github.com/stretchr/testify/assert" + "net" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" + "errors" + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" apimodel "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/appmgrmodel" "gerrit.oran-osc.org/r/ric-plt/o1mediator/pkg/sbi" - + "github.com/stretchr/testify/mock" ) var XappConfig = `{ @@ -52,6 +55,10 @@ var XappConfig = `{ } }` +var XappConfigErr = `{ + "ric": { + }` + var XappDescriptor = `{ "o-ran-sc-ric-xapp-desc-v1:ric": { "xapps": { @@ -68,10 +75,13 @@ var XappDescriptor = `{ }` var n *Nbi +var rnibM *rnibMock // Test cases func TestMain(M *testing.M) { - n = NewNbi(sbi.NewSBIClient("localhost:8080", "/ric/v1/", []string{"http"}, 5)) + rnibM = new(rnibMock) + rnib = rnibM + n = NewNbi(sbi.NewSBIClient("localhost:8080", "localhost:9093", 5)) go n.Start() time.Sleep(time.Duration(1) * time.Second) @@ -79,7 +89,7 @@ func TestMain(M *testing.M) { } func TestModifyConfigmap(t *testing.T) { - ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", http.StatusOK, apimodel.ConfigValidationErrors{}) + ts := CreateHTTPServer(t, "PUT", "/ric/v1/config", 8080, http.StatusOK, apimodel.ConfigValidationErrors{}) defer ts.Close() var f interface{} @@ -90,8 +100,71 @@ func TestModifyConfigmap(t *testing.T) { assert.Equal(t, true, err == nil) } +func TestXappDescModuleChangeCB(t *testing.T) { + ok := n.testModuleChangeCB("o-ran-sc-ric-xapp-desc-v1") + assert.True(t, ok) +} + +func TestUeecConfigModuleChangeCB(t *testing.T) { + ok := n.testModuleChangeCB("o-ran-sc-ric-ueec-config-v1") + assert.True(t, ok) +} + +func TestUeecConfigDoneModuleChangeCB(t *testing.T) { + ok := n.testModuleChangeCBDone("o-ran-sc-ric-ueec-config-v1") + assert.True(t, ok) +} + +func TestXappDescGnbStateCB(t *testing.T) { + ok := n.testGnbStateCB("o-ran-sc-ric-xapp-desc-v1") + assert.True(t, ok) +} + +func TestAlarmGnbStateCB(t *testing.T) { + ok := n.testGnbStateCB("o-ran-sc-ric-alarm-v1") + assert.True(t, ok) +} + +func TestGnbStateCB(t *testing.T) { + var rnibOk xapp.RNIBIRNibError + var gNbIDs []*xapp.RNIBNbIdentity + gNbID := xapp.RNIBNbIdentity{ + InventoryName: "test-gnb", + } + gNbIDs = append(gNbIDs, &gNbID) + nodeInfo := xapp.RNIBNodebInfo{} + + rnibM.On("GetListGnbIds").Return(gNbIDs, rnibOk).Once() + rnibM.On("GetNodeb", mock.Anything).Return(&nodeInfo, rnibOk).Once() + ok := n.testGnbStateCB("") + assert.True(t, ok) +} + +func TestGnbStateCBWhenRnibGetListGnbIdsFails(t *testing.T) { + var rnibErr xapp.RNIBIRNibError = errors.New("Some RNIB Error") + + rnibM.On("GetListGnbIds").Return(nil, rnibErr).Once() + ok := n.testGnbStateCB("") + assert.True(t, ok) +} + +func TestGnbStateCBWhenRnibGetNodebFails(t *testing.T) { + var rnibOk xapp.RNIBIRNibError + var rnibErr xapp.RNIBIRNibError = errors.New("Some RNIB Error") + var gNbIDs []*xapp.RNIBNbIdentity + gNbID := xapp.RNIBNbIdentity{ + InventoryName: "test-gnb", + } + gNbIDs = append(gNbIDs, &gNbID) + + rnibM.On("GetListGnbIds").Return(gNbIDs, rnibOk).Once() + rnibM.On("GetNodeb", mock.Anything).Return(nil, rnibErr).Once() + ok := n.testGnbStateCB("") + assert.True(t, ok) +} + func TestDeployXApp(t *testing.T) { - ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", http.StatusCreated, apimodel.Xapp{}) + ts := CreateHTTPServer(t, "POST", "/ric/v1/xapps", 8080, http.StatusCreated, apimodel.Xapp{}) defer ts.Close() var f interface{} @@ -103,7 +176,7 @@ func TestDeployXApp(t *testing.T) { } func TestUnDeployXApp(t *testing.T) { - ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", http.StatusNoContent, apimodel.Xapp{}) + ts := CreateHTTPServer(t, "DELETE", "/ric/v1/xapps/ueec-xapp", 8080, http.StatusNoContent, apimodel.Xapp{}) defer ts.Close() var f interface{} @@ -115,7 +188,7 @@ func TestUnDeployXApp(t *testing.T) { } func TestGetDeployedXapps(t *testing.T) { - ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", http.StatusOK, apimodel.AllDeployedXapps{}) + ts := CreateHTTPServer(t, "GET", "/ric/v1/xapps", 8080, http.StatusOK, apimodel.AllDeployedXapps{}) defer ts.Close() err := sbiClient.GetDeployedXapps() @@ -143,17 +216,21 @@ func TestErrorCases(t *testing.T) { err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "", 1) assert.Equal(t, true, err == nil) - // Invalid module - err = n.ManageConfigmaps("", "{}", 1) - assert.Equal(t, true, err == nil) - - // Unexpected module - err = n.ManageConfigmaps("o-ran-sc-ric-xapp-desc-v1", "{}", 0) - assert.Equal(t, true, err == nil) - // Invalid operation err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", "{}", 0) assert.Equal(t, true, err != nil) + + //Invalid json + err = n.ManageConfigmaps("o-ran-sc-ric-ueec-config-v1", XappConfigErr, 1) + assert.Equal(t, true, err != nil) + + // Invalid operation + err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappDescriptor, 5) + assert.Equal(t, true, err == nil) + + //Invalid json + err = n.ManageXapps("o-ran-sc-ric-ueec-config-v1", XappConfigErr, 1) + assert.Equal(t, true, err != nil) } func TestConnStatus2Str(t *testing.T) { @@ -185,10 +262,10 @@ func TestTeardown(t *testing.T) { n.Stop() } -func CreateHTTPServer(t *testing.T, method, url string, status int, respData interface{}) *httptest.Server { - l, err := net.Listen("tcp", "localhost:8080") +func CreateHTTPServer(t *testing.T, method, url string, port, status int, respData interface{}) *httptest.Server { + l, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", port)) if err != nil { - t.Error("Failed to create listener: " + err.Error()) + t.Error("Failed to create listener: " + err.Error()) } ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, r.Method, method) @@ -216,8 +293,28 @@ func DescMatcher(result, expected *apimodel.XappDescriptor) bool { func ConfigMatcher(result, expected *apimodel.XAppConfig) bool { if *result.Metadata.XappName == *expected.Metadata.XappName && - *result.Metadata.Namespace == *expected.Metadata.Namespace { + *result.Metadata.Namespace == *expected.Metadata.Namespace { return true } return false } + +type rnibMock struct { + mock.Mock +} + +func (m *rnibMock) GetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) { + a := m.Called() + if a.Get(0) == nil { + return nil, a.Error(1) + } + return a.Get(0).([]*xapp.RNIBNbIdentity), a.Error(1) +} + +func (m *rnibMock) GetNodeb(invName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) { + a := m.Called(invName) + if a.Get(0) == nil { + return nil, a.Error(1) + } + return a.Get(0).(*xapp.RNIBNodebInfo), a.Error(1) +}