From 7b2a1bae63760ac32770f806718d0add74c5c8dc Mon Sep 17 00:00:00 2001 From: matoval Date: Tue, 12 Nov 2024 19:08:20 -0800 Subject: [PATCH 1/6] Add tests for consistent workUnitIDs --- pkg/workceptor/workceptor_test.go | 36 +++++++++++++++++++++++++++--- tests/functional/mesh/work_test.go | 4 ++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pkg/workceptor/workceptor_test.go b/pkg/workceptor/workceptor_test.go index b4ada1290..0b730fde6 100644 --- a/pkg/workceptor/workceptor_test.go +++ b/pkg/workceptor/workceptor_test.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "errors" "fmt" + "os" "testing" "github.com/ansible/receptor/pkg/logger" @@ -302,6 +303,7 @@ func TestAllocateRemoteUnit(t *testing.T) { testCases := []struct { name string + workUnitID string tlsClient string ttl string signWork bool @@ -311,6 +313,7 @@ func TestAllocateRemoteUnit(t *testing.T) { }{ { name: "get client tls config error", + workUnitID: "", tlsClient: "something", errorMsg: "terminated", expectedCalls: func() { @@ -319,6 +322,7 @@ func TestAllocateRemoteUnit(t *testing.T) { }, { name: "sending secrets over non tls connection error", + workUnitID: "", tlsClient: "", params: map[string]string{"secret_": "secret"}, errorMsg: "cannot send secrets over a non-TLS connection", @@ -328,6 +332,7 @@ func TestAllocateRemoteUnit(t *testing.T) { }, { name: "invalid duration error", + workUnitID: "", tlsClient: "", ttl: "ttl", errorMsg: "time: invalid duration \"ttl\"", @@ -337,6 +342,18 @@ func TestAllocateRemoteUnit(t *testing.T) { }, { name: "normal case", + workUnitID: "", + tlsClient: "", + ttl: "1.5h", + errorMsg: "", + signWork: true, + expectedCalls: func() { + // For testing purposes + }, + }, + { + name: "pass workUnitID", + workUnitID: "testID12345678", tlsClient: "", ttl: "1.5h", errorMsg: "", @@ -350,15 +367,28 @@ func TestAllocateRemoteUnit(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { tc.expectedCalls() - _, err := w.AllocateRemoteUnit("", "", "", tc.tlsClient, tc.ttl, tc.signWork, tc.params) - + wu, err := w.AllocateRemoteUnit("", "", tc.workUnitID, tc.tlsClient, tc.ttl, tc.signWork, tc.params) if tc.errorMsg != "" && tc.errorMsg != err.Error() && err != nil { t.Errorf("expected: %s, received: %s", tc.errorMsg, err) } - + if tc.errorMsg == "" && err != nil { t.Error(err) } + if tc.workUnitID != "" { + wuID := wu.ID() + if tc.workUnitID != wuID { + t.Errorf("expected workUnitID to equal %s but got %s", tc.workUnitID, wuID) + } + } + }) + t.Cleanup(func() { + if tc.workUnitID != "" { + err := os.RemoveAll(fmt.Sprintf("/tmp/test/%s", tc.workUnitID)) + if err != nil { + t.Errorf("removal of test directory /tmp/test/%s failed", tc.workUnitID) + } + } }) } } diff --git a/tests/functional/mesh/work_test.go b/tests/functional/mesh/work_test.go index 50b8fb0bc..736f5769e 100644 --- a/tests/functional/mesh/work_test.go +++ b/tests/functional/mesh/work_test.go @@ -45,6 +45,10 @@ func TestWorkSubmitWithTLSClient(t *testing.T) { if err != nil { t.Fatal(err, m.GetDataDir()) } + err = controllers["node2"].AssertWorkResults(unitID, expectedResults) + if err != nil { + t.Fatal(err, m.GetDataDir()) + } }) } } From a8d149f6353496b8d1cd517fe4fb3e36d6af4849 Mon Sep 17 00:00:00 2001 From: matoval Date: Tue, 12 Nov 2024 19:09:42 -0800 Subject: [PATCH 2/6] Remove extra spaces --- pkg/workceptor/workceptor_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/workceptor/workceptor_test.go b/pkg/workceptor/workceptor_test.go index 0b730fde6..4cf6f906a 100644 --- a/pkg/workceptor/workceptor_test.go +++ b/pkg/workceptor/workceptor_test.go @@ -371,7 +371,7 @@ func TestAllocateRemoteUnit(t *testing.T) { if tc.errorMsg != "" && tc.errorMsg != err.Error() && err != nil { t.Errorf("expected: %s, received: %s", tc.errorMsg, err) } - + if tc.errorMsg == "" && err != nil { t.Error(err) } From 5b45c70bf7ef29fdeb062293f2100de73a9fb4dc Mon Sep 17 00:00:00 2001 From: matoval Date: Tue, 12 Nov 2024 19:12:36 -0800 Subject: [PATCH 3/6] Fix lint --- pkg/workceptor/workceptor_test.go | 40 +++++++++++++++---------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/pkg/workceptor/workceptor_test.go b/pkg/workceptor/workceptor_test.go index 4cf6f906a..8e43ff2e4 100644 --- a/pkg/workceptor/workceptor_test.go +++ b/pkg/workceptor/workceptor_test.go @@ -312,51 +312,51 @@ func TestAllocateRemoteUnit(t *testing.T) { expectedCalls func() }{ { - name: "get client tls config error", + name: "get client tls config error", workUnitID: "", - tlsClient: "something", - errorMsg: "terminated", + tlsClient: "something", + errorMsg: "terminated", expectedCalls: func() { mockNetceptor.EXPECT().GetClientTLSConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(&tls.Config{}, errors.New("terminated")) }, }, { - name: "sending secrets over non tls connection error", + name: "sending secrets over non tls connection error", workUnitID: "", - tlsClient: "", - params: map[string]string{"secret_": "secret"}, - errorMsg: "cannot send secrets over a non-TLS connection", + tlsClient: "", + params: map[string]string{"secret_": "secret"}, + errorMsg: "cannot send secrets over a non-TLS connection", expectedCalls: func() { // For testing purposes }, }, { - name: "invalid duration error", + name: "invalid duration error", workUnitID: "", - tlsClient: "", - ttl: "ttl", - errorMsg: "time: invalid duration \"ttl\"", + tlsClient: "", + ttl: "ttl", + errorMsg: "time: invalid duration \"ttl\"", expectedCalls: func() { // For testing purposes }, }, { - name: "normal case", + name: "normal case", workUnitID: "", - tlsClient: "", - ttl: "1.5h", - errorMsg: "", - signWork: true, + tlsClient: "", + ttl: "1.5h", + errorMsg: "", + signWork: true, expectedCalls: func() { // For testing purposes }, }, { - name: "pass workUnitID", + name: "pass workUnitID", workUnitID: "testID12345678", - tlsClient: "", - ttl: "1.5h", - errorMsg: "", + tlsClient: "", + ttl: "1.5h", + errorMsg: "", signWork: true, expectedCalls: func() { // For testing purposes From a053fc0c48d50caeb7660ed455a237892f3ba94f Mon Sep 17 00:00:00 2001 From: matoval Date: Tue, 12 Nov 2024 19:17:48 -0800 Subject: [PATCH 4/6] fix golangci-lint --- pkg/workceptor/workceptor_test.go | 42 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/pkg/workceptor/workceptor_test.go b/pkg/workceptor/workceptor_test.go index 8e43ff2e4..9a5a4a007 100644 --- a/pkg/workceptor/workceptor_test.go +++ b/pkg/workceptor/workceptor_test.go @@ -312,52 +312,52 @@ func TestAllocateRemoteUnit(t *testing.T) { expectedCalls func() }{ { - name: "get client tls config error", + name: "get client tls config error", workUnitID: "", - tlsClient: "something", - errorMsg: "terminated", + tlsClient: "something", + errorMsg: "terminated", expectedCalls: func() { mockNetceptor.EXPECT().GetClientTLSConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(&tls.Config{}, errors.New("terminated")) }, }, { - name: "sending secrets over non tls connection error", + name: "sending secrets over non tls connection error", workUnitID: "", - tlsClient: "", - params: map[string]string{"secret_": "secret"}, - errorMsg: "cannot send secrets over a non-TLS connection", + tlsClient: "", + params: map[string]string{"secret_": "secret"}, + errorMsg: "cannot send secrets over a non-TLS connection", expectedCalls: func() { // For testing purposes }, }, { - name: "invalid duration error", + name: "invalid duration error", workUnitID: "", - tlsClient: "", - ttl: "ttl", - errorMsg: "time: invalid duration \"ttl\"", + tlsClient: "", + ttl: "ttl", + errorMsg: "time: invalid duration \"ttl\"", expectedCalls: func() { // For testing purposes }, }, { - name: "normal case", + name: "normal case", workUnitID: "", - tlsClient: "", - ttl: "1.5h", - errorMsg: "", - signWork: true, + tlsClient: "", + ttl: "1.5h", + errorMsg: "", + signWork: true, expectedCalls: func() { // For testing purposes }, }, { - name: "pass workUnitID", + name: "pass workUnitID", workUnitID: "testID12345678", - tlsClient: "", - ttl: "1.5h", - errorMsg: "", - signWork: true, + tlsClient: "", + ttl: "1.5h", + errorMsg: "", + signWork: true, expectedCalls: func() { // For testing purposes }, From 23cbb92b41c3d25760482a9ea241675c8b8c197a Mon Sep 17 00:00:00 2001 From: matoval Date: Wed, 13 Nov 2024 08:14:02 -0800 Subject: [PATCH 5/6] Remove cleanup --- pkg/workceptor/workceptor_test.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/workceptor/workceptor_test.go b/pkg/workceptor/workceptor_test.go index 9a5a4a007..fa33acf90 100644 --- a/pkg/workceptor/workceptor_test.go +++ b/pkg/workceptor/workceptor_test.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "errors" "fmt" - "os" "testing" "github.com/ansible/receptor/pkg/logger" @@ -382,14 +381,14 @@ func TestAllocateRemoteUnit(t *testing.T) { } } }) - t.Cleanup(func() { - if tc.workUnitID != "" { - err := os.RemoveAll(fmt.Sprintf("/tmp/test/%s", tc.workUnitID)) - if err != nil { - t.Errorf("removal of test directory /tmp/test/%s failed", tc.workUnitID) - } - } - }) + // t.Cleanup(func() { + // if tc.workUnitID != "" { + // err := os.RemoveAll(fmt.Sprintf("/tmp/test/%s", tc.workUnitID)) + // if err != nil { + // t.Errorf("removal of test directory /tmp/test/%s failed", tc.workUnitID) + // } + // } + // }) } } From b1e96145d86c94f62e61056dd020419fbac71b85 Mon Sep 17 00:00:00 2001 From: matoval Date: Wed, 13 Nov 2024 08:24:28 -0800 Subject: [PATCH 6/6] Add cleanup back in --- pkg/workceptor/workceptor_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/pkg/workceptor/workceptor_test.go b/pkg/workceptor/workceptor_test.go index fa33acf90..9a5a4a007 100644 --- a/pkg/workceptor/workceptor_test.go +++ b/pkg/workceptor/workceptor_test.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "errors" "fmt" + "os" "testing" "github.com/ansible/receptor/pkg/logger" @@ -381,14 +382,14 @@ func TestAllocateRemoteUnit(t *testing.T) { } } }) - // t.Cleanup(func() { - // if tc.workUnitID != "" { - // err := os.RemoveAll(fmt.Sprintf("/tmp/test/%s", tc.workUnitID)) - // if err != nil { - // t.Errorf("removal of test directory /tmp/test/%s failed", tc.workUnitID) - // } - // } - // }) + t.Cleanup(func() { + if tc.workUnitID != "" { + err := os.RemoveAll(fmt.Sprintf("/tmp/test/%s", tc.workUnitID)) + if err != nil { + t.Errorf("removal of test directory /tmp/test/%s failed", tc.workUnitID) + } + } + }) } }