Newer
Older
"code.fbi.h-da.de/danet/gosdn/config"
"github.com/google/uuid"
"github.com/openconfig/ygot/exampleoc"
"github.com/openconfig/ygot/ygot"
var commitHostname = "commit"
var rollbackHostname = "rollback"
var commitDevice = &exampleoc.Device{
System: &exampleoc.System{
},
}
var rollbackDevice = &exampleoc.Device{
System: &exampleoc.System{
func TestChange_CommitRollback(t *testing.T) {
duid: did,
timestamp: time.Now(),
previousState: rollbackDevice,
intendedState: commitDevice,
callback: func(first ygot.GoStruct, second ygot.GoStruct) error {
hostname := *first.(*exampleoc.Device).System.Hostname
t.Logf("callback in test %v", t.Name())
case rollbackHostname:
callback <- rollbackHostname
stateIn, stateOut, requestState, errChan := stateManager(c, time.Millisecond*100)
c.stateIn = stateIn
c.stateOut = stateOut
c.requestState = requestState
c.errChan = errChan
wg.Add(1)
time.Sleep(time.Millisecond * 10)
if err := c.Commit(); (err != nil) != wantErr {
t.Errorf("Commit() error = %v, wantErr %v", err, wantErr)
}
time.Sleep(config.ChangeTimeout)
}()
got := <-callback
if !reflect.DeepEqual(got, want) {
t.Errorf("Commit() = %v, want %v", got, want)
}
func TestChange_CommitRollbackError(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
wantErr := false
want := errors.New("this is an expected error")
c := &Change{
duid: did,
timestamp: time.Now(),
previousState: rollbackDevice,
intendedState: commitDevice,
callback: func(first ygot.GoStruct, second ygot.GoStruct) error {
hostname := *second.(*exampleoc.Device).System.Hostname
t.Logf("callback in test %v", t.Name())
return errors.New("this is an expected error")
}
return nil
},
}
stateIn, stateOut, requestState, errChan := stateManager(c, time.Millisecond*100)
c.stateIn = stateIn
c.stateOut = stateOut
c.requestState = requestState
c.errChan = errChan
time.Sleep(time.Millisecond * 10)
if err := c.Commit(); (err != nil) != wantErr {
t.Errorf("Commit() error = %v, wantErr %v", err, wantErr)
}
time.Sleep(config.ChangeTimeout)
}()
got := <-c.errChan
if !reflect.DeepEqual(got, want) {
t.Errorf("Commit() = %v, want %v", got, want)
}
}
func TestChange_CommitError(t *testing.T) {
want := ppb.Change_INCONSISTENT
duid: did,
timestamp: time.Now(),
previousState: rollbackDevice,
intendedState: commitDevice,
callback: func(first ygot.GoStruct, second ygot.GoStruct) error {
return errors.New("this is an expected error")
},
}
stateIn, stateOut, requestState, errChan := stateManager(c, time.Millisecond*100)
c.stateIn = stateIn
c.stateOut = stateOut
c.requestState = requestState
c.errChan = errChan
time.Sleep(time.Millisecond * 10)
if err := c.Commit(); err == nil {
t.Errorf("Commit() expected error, error = %v", err)
}
got := c.State()
if !reflect.DeepEqual(got, want) {
t.Errorf("Commit() = %v, want %v", got, want)
duid: did,
timestamp: time.Now(),
previousState: rollbackDevice,
intendedState: commitDevice,
callback: func(first ygot.GoStruct, second ygot.GoStruct) error {
t.Logf("callback in test %v", t.Name())
stateIn, stateOut, requestState, errChan := stateManager(c, time.Millisecond*100)
c.stateIn = stateIn
c.stateOut = stateOut
c.requestState = requestState
c.errChan = errChan
if err := c.Commit(); err != nil {
t.Errorf("Commit() error = %v", err)
if !reflect.DeepEqual(got, want) {
t.Errorf("Commit() = %v, want %v", got, want)
if err := c.Confirm(); err != nil {
t.Errorf("Confirm() error = %v", err)
}
}
func TestChange_Confirm(t *testing.T) {
tests := []struct {
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Change{
previousState: &exampleoc.Device{
System: &exampleoc.System{
},
},
intendedState: &exampleoc.Device{
System: &exampleoc.System{
callback: func(first ygot.GoStruct, second ygot.GoStruct) error {
t.Logf("callback in test %v", t.Name())
stateIn, stateOut, requestState, errChan := stateManager(c, time.Millisecond*100)
c.stateIn = stateIn
c.stateOut = stateOut
c.requestState = requestState
c.errChan = errChan
if tt.name == "committed" {
if err := c.Commit(); err != nil {
t.Errorf("Commit() error = %v, wantErr %v", err, tt.wantErr)
}
}
if err := c.Confirm(); (err != nil) != tt.wantErr {
t.Errorf("Confirm() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestChange_ID(t *testing.T) {
type fields struct {
cuid uuid.UUID
}
tests := []struct {
name string
fields fields
want uuid.UUID
}{
{
name: "default",
fields: fields{cuid: cuid},
want: cuid,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &Change{
cuid: tt.fields.cuid,
}
if got := c.ID(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ID() = %v, want %v", got, tt.want)
}
})
}
}
func TestChange_State(t *testing.T) {
tests := []struct {
name string
want ppb.Change_State
}{
{
name: "pending",
want: ppb.Change_PENDING,
},
{
name: "committed",
want: ppb.Change_COMMITTED,
},
{
name: "confirmed",
want: ppb.Change_CONFIRMED,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
callback := func(first ygot.GoStruct, second ygot.GoStruct) error {
t.Logf("callback in test %v", t.Name())
c := NewChange(did, rollbackDevice, commitDevice, callback)
if tt.name != "pending" {
if err := c.Commit(); err != nil {
t.Errorf("Commit() error = %v", err)
}
}
if tt.name == "confirmed" {
if err := c.Confirm(); err != nil {
t.Errorf("Confirm() error = %v", err)
}
}
if got := c.State(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Change.State() = %v, want %v", got, tt.want)
}
})
}
}