diff --git a/executor/linux/linux.go b/executor/linux/linux.go index 8eeb462d4..fa1b990d8 100644 --- a/executor/linux/linux.go +++ b/executor/linux/linux.go @@ -57,6 +57,11 @@ type ( // Equal returns true if the other client is the equivalent. func Equal(a, b *client) bool { + // handle any nil comparisons + if a == nil || b == nil { + return a == nil && b == nil + } + if reflect.DeepEqual(a.Logger, b.Logger) && reflect.DeepEqual(a.Vela, b.Vela) && reflect.DeepEqual(a.Runtime, b.Runtime) && diff --git a/executor/linux/linux_test.go b/executor/linux/linux_test.go index 829118676..7c442f4cc 100644 --- a/executor/linux/linux_test.go +++ b/executor/linux/linux_test.go @@ -21,6 +21,75 @@ import ( "github.com/go-vela/types/pipeline" ) +func TestEqual(t *testing.T) { + // setup types + gin.SetMode(gin.TestMode) + + s := httptest.NewServer(server.FakeHandler()) + + _client, err := vela.NewClient(s.URL, "", nil) + if err != nil { + t.Errorf("unable to create Vela API client: %v", err) + } + + _runtime, err := docker.NewMock() + if err != nil { + t.Errorf("unable to create runtime engine: %v", err) + } + + _linux, err := New( + WithBuild(testBuild()), + WithHostname("localhost"), + WithPipeline(testSteps()), + WithRepo(testRepo()), + WithRuntime(_runtime), + WithUser(testUser()), + WithVelaClient(_client), + ) + if err != nil { + t.Errorf("unable to create linux executor: %v", err) + } + + tests := []struct { + name string + a *client + b *client + want bool + }{ + { + name: "both nil", + a: nil, + b: nil, + want: true, + }, + { + name: "left nil", + a: nil, + b: _linux, + want: false, + }, + { + name: "right nil", + a: _linux, + b: nil, + want: false, + }, + { + name: "equal", + a: _linux, + b: _linux, + want: true, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := Equal(test.a, test.b); got != test.want { + t.Errorf("Equal() = %v, want %v", got, test.want) + } + }) + } +} + func TestLinux_New(t *testing.T) { // setup types gin.SetMode(gin.TestMode) diff --git a/executor/local/local.go b/executor/local/local.go index f99f34a59..9f6d7f40f 100644 --- a/executor/local/local.go +++ b/executor/local/local.go @@ -38,6 +38,11 @@ type ( // equal returns true if the other client is the equivalent. func Equal(a, b *client) bool { + // handle any nil comparisons + if a == nil || b == nil { + return a == nil && b == nil + } + if reflect.DeepEqual(a.Vela, b.Vela) && reflect.DeepEqual(a.Runtime, b.Runtime) && a.Hostname == b.Hostname && diff --git a/executor/local/local_test.go b/executor/local/local_test.go index 083a0ca3f..4e6a1989d 100644 --- a/executor/local/local_test.go +++ b/executor/local/local_test.go @@ -20,6 +20,75 @@ import ( "github.com/go-vela/types/pipeline" ) +func TestEqual(t *testing.T) { + // setup types + gin.SetMode(gin.TestMode) + + s := httptest.NewServer(server.FakeHandler()) + + _client, err := vela.NewClient(s.URL, "", nil) + if err != nil { + t.Errorf("unable to create Vela API client: %v", err) + } + + _runtime, err := docker.NewMock() + if err != nil { + t.Errorf("unable to create runtime engine: %v", err) + } + + _local, err := New( + WithBuild(testBuild()), + WithHostname("localhost"), + WithPipeline(testSteps()), + WithRepo(testRepo()), + WithRuntime(_runtime), + WithUser(testUser()), + WithVelaClient(_client), + ) + if err != nil { + t.Errorf("unable to create local executor: %v", err) + } + + tests := []struct { + name string + a *client + b *client + want bool + }{ + { + name: "both nil", + a: nil, + b: nil, + want: true, + }, + { + name: "left nil", + a: nil, + b: _local, + want: false, + }, + { + name: "right nil", + a: _local, + b: nil, + want: false, + }, + { + name: "equal", + a: _local, + b: _local, + want: true, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if got := Equal(test.a, test.b); got != test.want { + t.Errorf("Equal() = %v, want %v", got, test.want) + } + }) + } +} + func TestLocal_New(t *testing.T) { // setup types gin.SetMode(gin.TestMode)