Skip to content

Commit 7b2a1ba

Browse files
committed
Add tests for consistent workUnitIDs
1 parent 25ff7aa commit 7b2a1ba

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

pkg/workceptor/workceptor_test.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"errors"
77
"fmt"
8+
"os"
89
"testing"
910

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

303304
testCases := []struct {
304305
name string
306+
workUnitID string
305307
tlsClient string
306308
ttl string
307309
signWork bool
@@ -311,6 +313,7 @@ func TestAllocateRemoteUnit(t *testing.T) {
311313
}{
312314
{
313315
name: "get client tls config error",
316+
workUnitID: "",
314317
tlsClient: "something",
315318
errorMsg: "terminated",
316319
expectedCalls: func() {
@@ -319,6 +322,7 @@ func TestAllocateRemoteUnit(t *testing.T) {
319322
},
320323
{
321324
name: "sending secrets over non tls connection error",
325+
workUnitID: "",
322326
tlsClient: "",
323327
params: map[string]string{"secret_": "secret"},
324328
errorMsg: "cannot send secrets over a non-TLS connection",
@@ -328,6 +332,7 @@ func TestAllocateRemoteUnit(t *testing.T) {
328332
},
329333
{
330334
name: "invalid duration error",
335+
workUnitID: "",
331336
tlsClient: "",
332337
ttl: "ttl",
333338
errorMsg: "time: invalid duration \"ttl\"",
@@ -337,6 +342,18 @@ func TestAllocateRemoteUnit(t *testing.T) {
337342
},
338343
{
339344
name: "normal case",
345+
workUnitID: "",
346+
tlsClient: "",
347+
ttl: "1.5h",
348+
errorMsg: "",
349+
signWork: true,
350+
expectedCalls: func() {
351+
// For testing purposes
352+
},
353+
},
354+
{
355+
name: "pass workUnitID",
356+
workUnitID: "testID12345678",
340357
tlsClient: "",
341358
ttl: "1.5h",
342359
errorMsg: "",
@@ -350,15 +367,28 @@ func TestAllocateRemoteUnit(t *testing.T) {
350367
for _, tc := range testCases {
351368
t.Run(tc.name, func(t *testing.T) {
352369
tc.expectedCalls()
353-
_, err := w.AllocateRemoteUnit("", "", "", tc.tlsClient, tc.ttl, tc.signWork, tc.params)
354-
370+
wu, err := w.AllocateRemoteUnit("", "", tc.workUnitID, tc.tlsClient, tc.ttl, tc.signWork, tc.params)
355371
if tc.errorMsg != "" && tc.errorMsg != err.Error() && err != nil {
356372
t.Errorf("expected: %s, received: %s", tc.errorMsg, err)
357373
}
358-
374+
359375
if tc.errorMsg == "" && err != nil {
360376
t.Error(err)
361377
}
378+
if tc.workUnitID != "" {
379+
wuID := wu.ID()
380+
if tc.workUnitID != wuID {
381+
t.Errorf("expected workUnitID to equal %s but got %s", tc.workUnitID, wuID)
382+
}
383+
}
384+
})
385+
t.Cleanup(func() {
386+
if tc.workUnitID != "" {
387+
err := os.RemoveAll(fmt.Sprintf("/tmp/test/%s", tc.workUnitID))
388+
if err != nil {
389+
t.Errorf("removal of test directory /tmp/test/%s failed", tc.workUnitID)
390+
}
391+
}
362392
})
363393
}
364394
}

tests/functional/mesh/work_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func TestWorkSubmitWithTLSClient(t *testing.T) {
4545
if err != nil {
4646
t.Fatal(err, m.GetDataDir())
4747
}
48+
err = controllers["node2"].AssertWorkResults(unitID, expectedResults)
49+
if err != nil {
50+
t.Fatal(err, m.GetDataDir())
51+
}
4852
})
4953
}
5054
}

0 commit comments

Comments
 (0)