Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated UT for event service #1010

Merged
merged 6 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions svc-events/consumer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/ODIM-Project/ODIM/lib-utilities/common"
"github.com/ODIM-Project/ODIM/lib-utilities/config"
)

func TestKafkaSubscriber(t *testing.T) {
Expand Down Expand Up @@ -63,4 +64,75 @@ func TestKafkaSubscriber(t *testing.T) {
if currentData != 1 {
t.Errorf("error: expected count is 1 but got %v", currentData)
}
EventSubscriber("invalidJson")
}

func TestConsume(t *testing.T) {
config.SetUpMockConfig(t)
type args struct {
topicName string
}
tests := []struct {
name string
args args
}{
{
name: "Positive Test",
args: args{
topicName: "demo",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Consume(tt.args.topicName)
})
}
}

func TestSubscribeCtrlMsgQueue(t *testing.T) {
config.SetUpMockConfig(t)
type args struct {
topicName string
}
tests := []struct {
name string
args args
}{
{
name: "Positive Test",
args: args{
topicName: "demo",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
SubscribeCtrlMsgQueue(tt.args.topicName)
})
}
}

func Test_consumeCtrlMsg(t *testing.T) {
config.SetUpMockConfig(t)

type args struct {
event interface{}
}
tests := []struct {
name string
args args
}{
{
name: "Empty Test",
args: args{
event: "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
consumeCtrlMsg(tt.args.event)
})
}
}
7 changes: 6 additions & 1 deletion svc-events/evcommon/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ type StartUpInteraface struct {
UpdateDeviceSubscriptionLocation func(evmodel.DeviceSubscription) error
}

//GetAllPluginsFunc ...
var (
GetAllPluginsFunc = evmodel.GetAllPlugins
)

// EmbTopic hold the list all consuming topics after
type EmbTopic struct {
TopicsList map[string]bool
Expand Down Expand Up @@ -520,7 +525,7 @@ func ProcessCtrlMsg(data interface{}) bool {
// SubscribePluginEMB is for subscribing to plugin EMB
func (st *StartUpInteraface) SubscribePluginEMB() {
time.Sleep(time.Second * 2)
pluginList, err := evmodel.GetAllPlugins()
pluginList, err := GetAllPluginsFunc()
if err != nil {
log.Error(err.Error())
return
Expand Down
80 changes: 78 additions & 2 deletions svc-events/evcommon/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import (
"reflect"
"testing"

log "github.com/sirupsen/logrus"

"github.com/ODIM-Project/ODIM/lib-utilities/common"
"github.com/ODIM-Project/ODIM/lib-utilities/config"
"github.com/ODIM-Project/ODIM/lib-utilities/errors"
"github.com/ODIM-Project/ODIM/lib-utilities/response"
"github.com/ODIM-Project/ODIM/svc-events/evmodel"
"github.com/ODIM-Project/ODIM/svc-events/evresponse"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -398,3 +398,79 @@ func TestGetPluginStatusandStartUP(t *testing.T) {
}
assert.Equal(t, "https://odim.2.com/EventService/Subscriptions/1", deviceSubscription.Location, "should be same")
}

func TestStartUpInteraface_SubscribePluginEMB(t *testing.T) {
pc := StartUpInteraface{}
GetAllPluginsFunc = func() ([]evmodel.Plugin, *errors.Error) { return nil, &errors.Error{} }
pc.SubscribePluginEMB()

GetAllPluginsFunc = func() ([]evmodel.Plugin, *errors.Error) {
return []evmodel.Plugin{evmodel.Plugin{IP: ""}}, &errors.Error{}
}
pc.SubscribePluginEMB()
getTypes("[alert statuschange]")
getTypes("[]")

callPlugin(PluginContactRequest{Plugin: &evmodel.Plugin{PreferredAuthType: "BasicAuth"}})

common.SetUpMockConfig()
defer func() {
err := common.TruncateDB(common.OnDisk)
if err != nil {
t.Fatalf("error: %v", err)
}
}()

var devSubscription = evmodel.DeviceSubscription{
EventHostIP: "10.10.0.1",
Location: "https://10.10.10.23/redfish/v1/EventService/Subscriptions/123",
OriginResources: []string{"/redfish/v1/Systems/uuid.1"},
}
if cerr := evmodel.SaveDeviceSubscription(devSubscription); cerr != nil {
t.Errorf("Error while saving device suscription: %v\n", cerr.Error())
}

err := updateDeviceSubscriptionLocation(map[string]string{"10.10.0.1": "Test"})
assert.Nil(t, err)
err = updateDeviceSubscriptionLocation(map[string]string{"location": "Test"})
assert.Nil(t, err)

}

func TestProcessCtrlMsg(t *testing.T) {
type args struct {
data interface{}
}
tests := []struct {
name string
args args
want bool
}{
{
name: "Test case for Nil value",
args: args{
data: nil,
},
want: false,
},
{
name: "Test case for Nil",
args: args{
data: common.ControlMessageData{
MessageType: common.SubscribeEMB,
Data: common.SubscribeEMBData{
EMBQueues: []string{},
},
},
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ProcessCtrlMsg(tt.args.data); got != tt.want {
t.Errorf("ProcessCtrlMsg() = %v, want %v", got, tt.want)
}
})
}
}
33 changes: 32 additions & 1 deletion svc-events/evcommon/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func MockContactClient(url, method, token string, odataID string, body interface
return response, nil
}

return nil, fmt.Errorf("InvalidRequest")
return &http.Response{StatusCode: 0}, fmt.Errorf("InvalidRequest")
}

// MockCreateChildTask is for mocking up of crete child task
Expand Down Expand Up @@ -230,6 +230,14 @@ func MockGetTarget(uuid string) (*evmodel.Target, error) {
DeviceUUID: "abab09db-e7a9-4352-8df0-5e41315a2a4c",
PluginID: "ILO",
}
case "6d4a0a66-7efa-578e-83cf-44dc68d2874d":
target = &evmodel.Target{
ManagerAddress: "100.100.100.101",
Password: encryptedData,
UserName: "admin",
DeviceUUID: "6d4a0a66-7efa-578e-83cf-44dc68d2874d",
PluginID: "GRF",
}
default:
return target, fmt.Errorf("UUID not found")
}
Expand Down Expand Up @@ -397,6 +405,23 @@ func MockGetEvtSubscriptions(searchKey string) ([]evmodel.Subscription, error) {
SubordinateResources: true,
},
}
case "/redfish/v1/Systems/6d4a0a66-7efa-578e-83cf-44dc68d2874d.1", "[^0-9]100.100.100.101[^0-9]":
subarr = []evmodel.Subscription{
{
UserName: "admin",
SubscriptionID: "81de0110-c35a-4859-984c-072d6c5a32d7",
Destination: "https://odim.destination.com:9090/events",
Name: "Subscription",
Location: "https://odim.2.com/EventService/Subscriptions/1",
Context: "context",
EventTypes: []string{"Alert", "ResourceAdded"},
MessageIds: []string{"IndicatorChanged"},
ResourceTypes: []string{"ComputerSystem"},
OriginResources: []string{"/redfish/v1/Systems/6d4a0a66-7efa-578e-83cf-44dc68d2874e.1"},
Hosts: []string{"100.100.100.100", "100.100.100.101"},
SubordinateResources: true,
},
}
case "11081de0-4859-984c-c35a-6c50732d72da", "/redfish/v1/Systems", "https://odim.destination.com:9090/events", "*", "":
subarr = []evmodel.Subscription{
{
Expand Down Expand Up @@ -515,6 +540,12 @@ func MockGetDeviceSubscriptions(hostIP string) (*evmodel.DeviceSubscription, err
EventHostIP: "100.100.100.100",
OriginResources: []string{"/redfish/v1/Systems/6d4a0a66-7efa-578e-83cf-44dc68d2874e.1"},
}
} else if strings.Contains(hostIP, "100.100.100.101") || hostIP == "*" {
deviceSub = &evmodel.DeviceSubscription{
Location: "https://odim.2.com/EventService/Subscriptions/1",
EventHostIP: "100.100.100.100",
OriginResources: []string{"/redfish/v1/Systems/6d4a0a66-7efa-578e-83cf-44dc68d2874e.1"},
}
} else if strings.Contains(hostIP, "10.10.1.3") {
deviceSub = &evmodel.DeviceSubscription{
Location: "https://10.10.10.3/EventService/Subscriptions/1",
Expand Down
Loading