Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for consistent workUnitIDs #1207

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 48 additions & 18 deletions pkg/workceptor/workceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"os"
"testing"

"github.com/ansible/receptor/pkg/logger"
Expand Down Expand Up @@ -302,6 +303,7 @@ func TestAllocateRemoteUnit(t *testing.T) {

testCases := []struct {
name string
workUnitID string
tlsClient string
ttl string
signWork bool
Expand All @@ -310,37 +312,52 @@ func TestAllocateRemoteUnit(t *testing.T) {
expectedCalls func()
}{
{
name: "get client tls config error",
tlsClient: "something",
errorMsg: "terminated",
name: "get client tls config error",
workUnitID: "",
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",
tlsClient: "",
params: map[string]string{"secret_": "secret"},
errorMsg: "cannot send secrets over a non-TLS connection",
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",
expectedCalls: func() {
// For testing purposes
},
},
{
name: "invalid duration error",
tlsClient: "",
ttl: "ttl",
errorMsg: "time: invalid duration \"ttl\"",
name: "invalid duration error",
workUnitID: "",
tlsClient: "",
ttl: "ttl",
errorMsg: "time: invalid duration \"ttl\"",
expectedCalls: func() {
// For testing purposes
},
},
{
name: "normal case",
tlsClient: "",
ttl: "1.5h",
errorMsg: "",
signWork: true,
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: "",
signWork: true,
expectedCalls: func() {
// For testing purposes
},
Expand All @@ -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)
}
}
})
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/functional/mesh/work_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
})
}
}
Expand Down
Loading