5
5
"crypto/tls"
6
6
"errors"
7
7
"fmt"
8
+ "os"
8
9
"testing"
9
10
10
11
"github.com/ansible/receptor/pkg/logger"
@@ -302,6 +303,7 @@ func TestAllocateRemoteUnit(t *testing.T) {
302
303
303
304
testCases := []struct {
304
305
name string
306
+ workUnitID string
305
307
tlsClient string
306
308
ttl string
307
309
signWork bool
@@ -311,6 +313,7 @@ func TestAllocateRemoteUnit(t *testing.T) {
311
313
}{
312
314
{
313
315
name : "get client tls config error" ,
316
+ workUnitID : "" ,
314
317
tlsClient : "something" ,
315
318
errorMsg : "terminated" ,
316
319
expectedCalls : func () {
@@ -319,6 +322,7 @@ func TestAllocateRemoteUnit(t *testing.T) {
319
322
},
320
323
{
321
324
name : "sending secrets over non tls connection error" ,
325
+ workUnitID : "" ,
322
326
tlsClient : "" ,
323
327
params : map [string ]string {"secret_" : "secret" },
324
328
errorMsg : "cannot send secrets over a non-TLS connection" ,
@@ -328,6 +332,7 @@ func TestAllocateRemoteUnit(t *testing.T) {
328
332
},
329
333
{
330
334
name : "invalid duration error" ,
335
+ workUnitID : "" ,
331
336
tlsClient : "" ,
332
337
ttl : "ttl" ,
333
338
errorMsg : "time: invalid duration \" ttl\" " ,
@@ -337,6 +342,18 @@ func TestAllocateRemoteUnit(t *testing.T) {
337
342
},
338
343
{
339
344
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" ,
340
357
tlsClient : "" ,
341
358
ttl : "1.5h" ,
342
359
errorMsg : "" ,
@@ -350,15 +367,28 @@ func TestAllocateRemoteUnit(t *testing.T) {
350
367
for _ , tc := range testCases {
351
368
t .Run (tc .name , func (t * testing.T ) {
352
369
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 )
355
371
if tc .errorMsg != "" && tc .errorMsg != err .Error () && err != nil {
356
372
t .Errorf ("expected: %s, received: %s" , tc .errorMsg , err )
357
373
}
358
-
374
+
359
375
if tc .errorMsg == "" && err != nil {
360
376
t .Error (err )
361
377
}
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
+ }
362
392
})
363
393
}
364
394
}
0 commit comments