github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/e2e/servicediscovery/nomad_checks_test.go (about) 1 package servicediscovery 2 3 import ( 4 "context" 5 "regexp" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/hashicorp/nomad/e2e/e2eutil" 11 "github.com/hashicorp/nomad/helper/uuid" 12 "github.com/shoenig/test/must" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func testChecksHappy(t *testing.T) { 17 nomadClient := e2eutil.NomadClient(t) 18 19 // Generate our unique job ID which will be used for this test. 20 jobID := "nsd-check-happy-" + uuid.Short() 21 jobIDs := []string{jobID} 22 23 // Defer a cleanup function to remove the job. This will trigger if the 24 // test fails, unless the cancel function is called. 25 ctx, cancel := context.WithCancel(context.Background()) 26 defer cancel() 27 defer e2eutil.CleanupJobsAndGCWithContext(t, ctx, &jobIDs) 28 29 // Register the happy checks job. 30 allocStubs := e2eutil.RegisterAndWaitForAllocs(t, nomadClient, jobChecksHappy, jobID, "") 31 must.Len(t, 1, allocStubs) 32 33 // wait for the alloc to be running 34 e2eutil.WaitForAllocRunning(t, nomadClient, allocStubs[0].ID) 35 36 // Get and test the output of 'nomad alloc checks'. 37 require.Eventually(t, func() bool { 38 output, err := e2eutil.AllocChecks(allocStubs[0].ID) 39 if err != nil { 40 return false 41 } 42 43 // assert the output contains success 44 statusRe := regexp.MustCompile(`Status\s+=\s+success`) 45 if !statusRe.MatchString(output) { 46 return false 47 } 48 49 // assert the output contains 200 status code 50 statusCodeRe := regexp.MustCompile(`StatusCode\s+=\s+200`) 51 if !statusCodeRe.MatchString(output) { 52 return false 53 } 54 55 // assert output contains nomad's success string 56 return strings.Contains(output, `nomad: http ok`) 57 }, 5*time.Second, 200*time.Millisecond) 58 } 59 60 func testChecksSad(t *testing.T) { 61 nomadClient := e2eutil.NomadClient(t) 62 63 // Generate our unique job ID which will be used for this test. 64 jobID := "nsd-check-sad-" + uuid.Short() 65 jobIDs := []string{jobID} 66 67 // Defer a cleanup function to remove the job. This will trigger if the 68 // test fails, unless the cancel function is called. 69 ctx, cancel := context.WithCancel(context.Background()) 70 defer cancel() 71 defer e2eutil.CleanupJobsAndGCWithContext(t, ctx, &jobIDs) 72 73 // Register the sad checks job. 74 allocStubs := e2eutil.RegisterAndWaitForAllocs(t, nomadClient, jobChecksSad, jobID, "") 75 must.Len(t, 1, allocStubs) 76 77 // wait for the alloc to be running 78 e2eutil.WaitForAllocRunning(t, nomadClient, allocStubs[0].ID) 79 80 // Get and test the output of 'nomad alloc checks'. 81 require.Eventually(t, func() bool { 82 output, err := e2eutil.AllocChecks(allocStubs[0].ID) 83 if err != nil { 84 return false 85 } 86 87 // assert the output contains failure 88 statusRe := regexp.MustCompile(`Status\s+=\s+failure`) 89 if !statusRe.MatchString(output) { 90 return false 91 } 92 93 // assert the output contains 501 status code 94 statusCodeRe := regexp.MustCompile(`StatusCode\s+=\s+501`) 95 if !statusCodeRe.MatchString(output) { 96 return false 97 } 98 99 // assert output contains error output from python http.server 100 return strings.Contains(output, `<p>Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.</p>`) 101 }, 5*time.Second, 200*time.Millisecond) 102 }