github.com/hernad/nomad@v1.6.112/e2e/consul/service_revert_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package consul 5 6 import ( 7 "context" 8 "testing" 9 10 "github.com/hernad/nomad/e2e/e2eutil" 11 "github.com/hernad/nomad/helper/uuid" 12 "github.com/hernad/nomad/nomad/structs" 13 "github.com/shoenig/test/must" 14 ) 15 16 // testServiceReversion asserts we can 17 // - submit a job with a service 18 // - update that job and modify service 19 // - revert the job, restoring the original service 20 func testServiceReversion(t *testing.T) { 21 const jobFile = "./input/service_reversion.nomad" 22 jobID := "service-reversion-" + uuid.Short() 23 jobIDs := []string{jobID} 24 25 // Defer a cleanup function to remove the job. This will trigger if the 26 // test fails, unless the cancel function is called. 27 ctx, cancel := context.WithCancel(context.Background()) 28 defer cancel() 29 defer e2eutil.CleanupJobsAndGCWithContext(t, ctx, &jobIDs) 30 31 // initial register of job, with service="one" 32 vars := []string{"-var", "service=one"} 33 err := e2eutil.RegisterWithArgs(jobID, jobFile, vars...) 34 must.NoError(t, err) 35 36 // wait for job to be running 37 err = e2eutil.WaitForAllocStatusExpected(jobID, "", []string{structs.AllocClientStatusRunning}) 38 must.NoError(t, err) 39 40 // get our consul client 41 consulClient := e2eutil.ConsulClient(t) 42 43 assertService := func(name string, count int) { 44 services, _, consulErr := consulClient.Catalog().Service(name, "", nil) 45 must.NoError(t, consulErr) 46 must.Len(t, count, services, must.Sprintf("expected %d instances of %s, got %d", count, name, len(services))) 47 } 48 49 // query services, assert 1 instance of "one" 50 assertService("one", 1) 51 assertService("two", 0) 52 53 // second register of job, with service="two" 54 vars = []string{"-var", "service=two"} 55 err = e2eutil.RegisterWithArgs(jobID, jobFile, vars...) 56 must.NoError(t, err) 57 58 // wait for job to be running 59 err = e2eutil.WaitForAllocStatusExpected(jobID, "", []string{structs.AllocClientStatusRunning}) 60 must.NoError(t, err) 61 62 // query services, assert 0 instance of "one" (replaced), 1 of "two" 63 assertService("one", 0) 64 assertService("two", 1) 65 66 // now revert our job back to version 0 67 err = e2eutil.Revert(jobID, jobFile, 0) 68 must.NoError(t, err) 69 70 // wait for job to be running 71 err = e2eutil.WaitForAllocStatusExpected(jobID, "", []string{structs.AllocClientStatusRunning}) 72 must.NoError(t, err) 73 74 // query services, assert 1 instance of "one" (reverted), 1 of "two" (removed) 75 assertService("one", 1) 76 assertService("two", 0) 77 }