github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/internal/testing/apitests/jobs_test.go (about) 1 package apitests 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/nomad/api" 7 "github.com/hashicorp/nomad/ci" 8 "github.com/hashicorp/nomad/nomad/mock" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestJobs_Parse(t *testing.T) { 13 ci.Parallel(t) 14 c, s := makeClient(t, nil, nil) 15 defer s.Stop() 16 17 jobs := c.Jobs() 18 19 checkJob := func(job *api.Job, expectedRegion string) { 20 if job == nil { 21 t.Fatal("job should not be nil") 22 } 23 24 region := job.Region 25 26 if region == nil { 27 if expectedRegion != "" { 28 t.Fatalf("expected job region to be '%s' but was unset", expectedRegion) 29 } 30 } else { 31 if expectedRegion != *region { 32 t.Fatalf("expected job region '%s', but got '%s'", expectedRegion, *region) 33 } 34 } 35 } 36 job, err := jobs.ParseHCL(mock.HCL(), true) 37 if err != nil { 38 t.Fatalf("err: %s", err) 39 } 40 checkJob(job, "global") 41 42 job, err = jobs.ParseHCL(mock.HCL(), false) 43 if err != nil { 44 t.Fatalf("err: %s", err) 45 } 46 checkJob(job, "") 47 } 48 49 func TestJobs_Summary_WithACL(t *testing.T) { 50 ci.Parallel(t) 51 assert := assert.New(t) 52 53 c, s, root := makeACLClient(t, nil, nil) 54 defer s.Stop() 55 jobs := c.Jobs() 56 57 invalidToken := mock.ACLToken() 58 59 // Registering with an invalid token should fail 60 c.SetSecretID(invalidToken.SecretID) 61 job := testJob() 62 _, _, err := jobs.Register(job, nil) 63 assert.NotNil(err) 64 65 // Register with token should succeed 66 c.SetSecretID(root.SecretID) 67 resp2, wm, err := jobs.Register(job, nil) 68 assert.Nil(err) 69 assert.NotNil(resp2) 70 assert.NotEqual("", resp2.EvalID) 71 assertWriteMeta(t, wm) 72 73 // Query the job summary with an invalid token should fail 74 c.SetSecretID(invalidToken.SecretID) 75 result, _, err := jobs.Summary(*job.ID, nil) 76 assert.NotNil(err) 77 78 // Query the job summary with a valid token should succeed 79 c.SetSecretID(root.SecretID) 80 result, qm, err := jobs.Summary(*job.ID, nil) 81 assert.Nil(err) 82 assertQueryMeta(t, qm) 83 84 // Check that the result is what we expect 85 assert.Equal(*job.ID, result.JobID) 86 }