Newer
Older
"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) {
wantErr := false
errChan := make(chan error, 10)
commit, confirm, out := stateManager(time.Millisecond * 100)
commit: commit,
confirm: confirm,
errChan: errChan,
out: out,
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("hostname: %v", hostname)
switch hostname {
case rollbackHostname:
callback <- rollbackHostname
}
go func() {
time.Sleep(time.Millisecond * 10)
if err := c.Commit(); (err != nil) != wantErr {
t.Errorf("Commit() error = %v, wantErr %v", err, wantErr)
}
}()
got := <-callback
if !reflect.DeepEqual(got, want) {
t.Errorf("Commit() = %v, want %v", got, want)
}
}
func TestChange_CommitRollbackError(t *testing.T) {
wantErr := false
want := errors.New("this is an expected error")
commit, confirm, out := stateManager(time.Millisecond * 100)
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("hostname: %v", hostname)
switch hostname {
return errors.New("this is an expected error")
}
return nil
},
errChan: make(chan error),
}
go func() {
time.Sleep(time.Millisecond * 10)
if err := c.Commit(); (err != nil) != wantErr {
t.Errorf("Commit() error = %v, wantErr %v", err, wantErr)
}
}()
got := <-c.errChan
if !reflect.DeepEqual(got, want) {
t.Errorf("Commit() = %v, want %v", got, want)
}
}
func TestChange_CommitError(t *testing.T) {
wantErr := true
commit, confirm, out := stateManager(time.Millisecond * 100)
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")
},
}
go func() {
time.Sleep(time.Millisecond * 10)
if err := c.Commit(); (err != nil) != wantErr {
t.Errorf("Commit() error = %v, wantErr %v", err, wantErr)
}
}()
got := c.committed
if !reflect.DeepEqual(got, false) {
t.Errorf("Commit() = %v, want %v", got, false)
}
}
commit, confirm, out := stateManager(time.Millisecond * 100)
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("hostname: %v", hostname)
return nil
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) {
errChan := make(chan error, 10)
commit, confirm, out := stateManager(time.Millisecond * 100)
commit: commit,
confirm: confirm,
errChan: errChan,
out: out,
previousState: &exampleoc.Device{
System: &exampleoc.System{
},
},
intendedState: &exampleoc.Device{
System: &exampleoc.System{
callback: func(first ygot.GoStruct, second ygot.GoStruct) error {
hostname := *first.(*exampleoc.Device).System.Hostname
t.Logf("hostname: %v", hostname)
return nil
},
}
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)
}
})
}
}
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
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 {
hostname := *first.(*exampleoc.Device).System.Hostname
t.Logf("hostname: %v", hostname)
return nil
}
errChan := make(chan error)
c := NewChange(did, rollbackDevice, commitDevice, callback, errChan)
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)
}
})
}
}