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