Skip to content
Snippets Groups Projects
principalNetworkDomain_test.go 31.7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 			_, err = pnd.ChangeMNE(mne.ID(), ppb.ApiOperation_API_OPERATION_UPDATE, "system/config/hostname", "ceos3000")
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    				t.Error(err)
    				return
    			}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    			u := pnd.PendingChanges()[0]
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    			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)
    			}
    		})
    	}
    }
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    
    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
    	}
    
    
    	store := store.NewChangeStore()
    
    	pending := NewChange(mneid, &openconfig.Device{}, &openconfig.Device{}, callback)
    
    	if err := store.Add(pending); err != nil {
    		t.Error(err)
    		return
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	tests := []struct {
    		name string
    		want []uuid.UUID
    	}{
    		{
    			name: "default",
    
    			want: []uuid.UUID{pending.cuid},
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		},
    	}
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    			pnd := newPnd()
    
    			pnd.changes = store
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    			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
    	}
    
    
    	store := store.NewChangeStore()
    
    	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
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	tests := []struct {
    		name string
    		want []uuid.UUID
    	}{
    		{
    			name: "default",
    
    			want: []uuid.UUID{committed.cuid},
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		},
    	}
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    			pnd := newPnd()
    
    			pnd.changes = store
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    			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
    	}
    
    	store := store.NewChangeStore()
    
    	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
    	}
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    	tests := []struct {
    		name string
    		want []uuid.UUID
    	}{
    		{
    			name: "default",
    
    			want: []uuid.UUID{confirmed.cuid},
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    		},
    	}
    	for _, tt := range tests {
    		t.Run(tt.name, func(t *testing.T) {
    			pnd := newPnd()
    
    			pnd.changes = store
    
    Manuel Kieweg's avatar
    Manuel Kieweg committed
    			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()
    
    	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
    
    	}
    	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,
    		},
    	}
    
    
    		// 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{
    
    		//      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)
    			}
    		})
    	}
    }