Newer
Older
_, err = pnd.ChangeMNE(mne.ID(), ppb.ApiOperation_API_OPERATION_UPDATE, "system/config/hostname", "ceos3000")
if tt.name != "uncommitted" {
if err := pnd.Commit(u); (err != nil) != tt.wantErr {
t.Errorf("Confirm() error = %v, wantErr %v", err, tt.wantErr)
return
}
}
if err := pnd.Confirm(u); (err != nil) != tt.wantErr {
t.Errorf("Confirm() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func Test_pndImplementation_PendingChanges(t *testing.T) {
testName := t.Name()
callback := func(first ygot.GoStruct, second ygot.GoStruct) error {
log.Infof("callback in test %v", testName)
return nil
}
pending := NewChange(mneid, &openconfig.Device{}, &openconfig.Device{}, callback)
if err := store.Add(pending); err != nil {
t.Error(err)
return
}
tests := []struct {
name string
want []uuid.UUID
}{
{
name: "default",
want: []uuid.UUID{pending.cuid},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if got := pnd.PendingChanges(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("pndImplementation.PendingChanges() = %v, want %v", got, tt.want)
}
})
}
}
func Test_pndImplementation_CommittedChanges(t *testing.T) {
testName := t.Name()
callback := func(first ygot.GoStruct, second ygot.GoStruct) error {
log.Infof("callback in test %v", testName)
return nil
}
committed := NewChange(mneid, &openconfig.Device{}, &openconfig.Device{}, callback)
if err := committed.Commit(); err != nil {
t.Error(err)
return
}
if err := store.Add(committed); err != nil {
t.Error(err)
return
}
tests := []struct {
name string
want []uuid.UUID
}{
{
name: "default",
want: []uuid.UUID{committed.cuid},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if got := pnd.CommittedChanges(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("pndImplementation.CommittedChanges() = %v, want %v", got, tt.want)
}
})
}
}
func Test_pndImplementation_ConfirmedChanges(t *testing.T) {
testName := t.Name()
callback := func(first ygot.GoStruct, second ygot.GoStruct) error {
log.Infof("callback in test %v", testName)
return nil
}
confirmed := NewChange(mneid, &openconfig.Device{}, &openconfig.Device{}, callback)
if err := confirmed.Commit(); err != nil {
t.Error(err)
return
}
if err := confirmed.Confirm(); err != nil {
t.Error(err)
return
}
if err := store.Add(confirmed); err != nil {
t.Error(err)
return
}
tests := []struct {
name string
want []uuid.UUID
}{
{
name: "default",
want: []uuid.UUID{confirmed.cuid},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := newPnd()
if got := pnd.ConfirmedChanges(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("pndImplementation.ConfirmedChanges() = %v, want %v", got, tt.want)
}
})
}
}
func Test_pndImplementation_saveGoStructsToFile(t *testing.T) {
defer removeTestGoStructs()
Fabian Seidl
committed
type genericGoStructClientArg struct {
fn string
rtrn []interface{}
times int
}
// Create a new mock for GenericGoStructClient. With
// genericGoStructClientArg it is possible to set the Return values of the
// mocks methods.
newGenericGoStructClient := func(args ...genericGoStructClientArg) *mocks.GenericGoStructClient {
ggsc := &mocks.GenericGoStructClient{}
for _, arg := range args {
ggsc.On(arg.fn).Return(arg.rtrn...).Times(arg.times)
}
ggsc.On("CloseSend").Return(nil)
return ggsc
}
type args struct {
id uuid.UUID
client StreamClient
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default",
args: args{
id: uuid.New(),
client: newGenericGoStructClient(
[]genericGoStructClientArg{
{
fn: "Recv",
rtrn: []interface{}{
&csbi.Payload{Chunk: []byte("test")},
nil,
},
times: 3,
},
{
fn: "Recv",
rtrn: []interface{}{
&csbi.Payload{Chunk: nil},
io.EOF,
},
times: 1,
},
}...),
},
wantErr: false,
},
{
name: "unexpected EOF error",
args: args{
id: uuid.New(),
client: newGenericGoStructClient(
[]genericGoStructClientArg{
{
fn: "Recv",
rtrn: []interface{}{
&csbi.Payload{Chunk: nil},
io.ErrUnexpectedEOF,
},
times: 1,
},
{
fn: "CloseSend",
rtrn: []interface{}{
nil,
},
times: 1,
},
}...),
},
wantErr: true,
},
}
var wg sync.WaitGroup
for _, tt := range tests {
// waitgroup and extra func needed to be able to clean up generated pnd directories while running test cases in parallel mode,
// var x is just a dummy to be able to call the func,
// outer t.Run() required for defered wg.Done().
// reference: https://stackoverflow.com/a/63609718
x := func() {
defer wg.Done()
t.Run("parallel waiting func", func(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := saveStreamToFile(tt.args.client, util.GoStructName, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("saveGoStructsToFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
})
}
x()
}
}
func Test_pndImplementation_SubscribePath(t *testing.T) {
type fields struct {
Name string
Description string
southboundService southbound.Service
networkElementService networkelement.Service
changes *store.ChangeStore
ID uuid.UUID
csbiClient cpb.CsbiServiceClient
callback func(uuid.UUID, chan networkelement.Details)
eventService eventInterfaces.Service
}
type args struct {
uuid uuid.UUID
subList *ppb.SubscriptionList
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
//TODO: Implement proper test here!
// {
// name: "default",
// args: args{
// uuid: mneid,
// subList: &ppb.SubscriptionList{
// Subscription: []*ppb.Subscription{
// {
// Path: "",
// StreamMode: ppb.StreamMode_STREAM_MODE_SAMPLE,
// SampleInterval: 1000000000, // 1 second
// },
// },
// Mode: ppb.SubscriptionMode_SUBSCRIPTION_MODE_STREAM,
// },
// },
// },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pnd := &pndImplementation{
Name: tt.fields.Name,
Description: tt.fields.Description,
southboundService: tt.fields.southboundService,
networkElementService: tt.fields.networkElementService,
changes: tt.fields.changes,
Id: tt.fields.ID,
csbiClient: tt.fields.csbiClient,
callback: tt.fields.callback,
eventService: tt.fields.eventService,
}
if err := pnd.SubscribePath(tt.args.uuid, tt.args.subList); (err != nil) != tt.wantErr {
t.Errorf("pndImplementation.SubscribePath() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}