github.com/hernad/nomad@v1.6.112/e2e/servicediscovery/simple_lb_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package servicediscovery 5 6 import ( 7 "context" 8 "fmt" 9 "strings" 10 "testing" 11 12 "github.com/hernad/nomad/api" 13 "github.com/hernad/nomad/e2e/e2eutil" 14 "github.com/hernad/nomad/helper/uuid" 15 "github.com/shoenig/test/must" 16 ) 17 18 func testSimpleLoadBalancing(t *testing.T) { 19 nomadClient := e2eutil.NomadClient(t) 20 21 // Generate our unique job ID which will be used for this test. 22 jobID := "nsd-simple-lb-replicas-" + 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 // Register the replicas job. 32 allocStubs := e2eutil.RegisterAndWaitForAllocs(t, nomadClient, jobSimpleLBReplicas, jobID, "") 33 must.Len(t, 3, allocStubs) 34 35 for _, stub := range allocStubs { 36 var tag string 37 switch stub.TaskGroup { 38 case "db_replica_1": 39 tag = "r1" 40 case "db_replica_2": 41 tag = "r2" 42 case "db_replica_3": 43 tag = "r3" 44 } 45 expectService := api.ServiceRegistration{ 46 ServiceName: "db", 47 Namespace: api.DefaultNamespace, 48 Datacenter: "dc1", 49 JobID: jobID, 50 AllocID: stub.ID, 51 Tags: []string{tag}, 52 } 53 filter := fmt.Sprintf("Tags contains %q", tag) 54 requireEventuallyNomadService(t, &expectService, filter) 55 } 56 57 jobID2 := "nsd-simple-lb-clients" + uuid.Short() 58 jobIDs = append(jobIDs, jobID2) 59 60 // Register the clients job. 61 allocStubs = e2eutil.RegisterAndWaitForAllocs(t, nomadClient, jobSimpleLBClients, jobID2, "") 62 must.Len(t, 2, allocStubs) 63 64 for _, stub := range allocStubs { 65 var expCount int 66 switch stub.TaskGroup { 67 case "client_1": 68 expCount = 1 69 case "client_2": 70 expCount = 2 71 } 72 must.NoError(t, e2eutil.WaitForAllocFile(stub.ID, "cat/output.txt", func(content string) bool { 73 count := strings.Count(content, "server ") 74 return count == expCount 75 }, nil)) 76 } 77 }