github.com/hernad/nomad@v1.6.112/e2e/networking/networking.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package networking 5 6 import ( 7 "os" 8 "strings" 9 10 "github.com/hernad/nomad/e2e/e2eutil" 11 "github.com/hernad/nomad/e2e/framework" 12 "github.com/hernad/nomad/helper/uuid" 13 ) 14 15 type NetworkingE2ETest struct { 16 framework.TC 17 jobIDs []string 18 } 19 20 func init() { 21 framework.AddSuites(&framework.TestSuite{ 22 Component: "Networking", 23 CanRunLocal: true, 24 Cases: []framework.TestCase{ 25 e2eutil.NewE2EJob("networking/inputs/basic.nomad"), 26 new(NetworkingE2ETest), 27 }, 28 }) 29 } 30 31 func (tc *NetworkingE2ETest) BeforeAll(f *framework.F) { 32 e2eutil.WaitForLeader(f.T(), tc.Nomad()) 33 e2eutil.WaitForNodesReady(f.T(), tc.Nomad(), 1) 34 } 35 36 func (tc *NetworkingE2ETest) AfterEach(f *framework.F) { 37 if os.Getenv("NOMAD_TEST_SKIPCLEANUP") == "1" { 38 return 39 } 40 41 for _, jobID := range tc.jobIDs { 42 err := e2eutil.StopJob(jobID, "-purge") 43 f.NoError(err) 44 } 45 tc.jobIDs = []string{} 46 47 _, err := e2eutil.Command("nomad", "system", "gc") 48 f.NoError(err) 49 } 50 51 func (tc *NetworkingE2ETest) TestNetworking_DockerBridgedHostname(f *framework.F) { 52 53 jobID := "test-networking-" + uuid.Generate()[0:8] 54 f.NoError(e2eutil.Register(jobID, "networking/inputs/docker_bridged_hostname.nomad")) 55 tc.jobIDs = append(tc.jobIDs, jobID) 56 f.NoError(e2eutil.WaitForAllocStatusExpected(jobID, "default", []string{"running"}), 57 "job should be running with 1 alloc") 58 59 // Grab the allocations for the job. 60 allocs, _, err := tc.Nomad().Jobs().Allocations(jobID, false, nil) 61 f.NoError(err, "failed to get allocs for job") 62 f.Len(allocs, 1, "job should have one alloc") 63 64 // Run the hostname command within the allocation. 65 hostnameOutput, err := e2eutil.AllocExec(allocs[0].ID, "sleep", "hostname", "default", nil) 66 f.NoError(err, "failed to run hostname exec command") 67 f.Equal("mylittlepony", strings.TrimSpace(hostnameOutput), "incorrect hostname set within container") 68 69 // Check the /etc/hosts file for the correct IP address and hostname entry. 70 hostsOutput, err := e2eutil.AllocExec(allocs[0].ID, "sleep", "cat /etc/hosts", "default", nil) 71 f.NoError(err, "failed to run hostname exec command") 72 f.Contains(hostsOutput, "mylittlepony", "/etc/hosts doesn't contain hostname entry") 73 } 74 75 func (tc *NetworkingE2ETest) TestNetworking_DockerBridgedHostnameInterpolation(f *framework.F) { 76 77 jobID := "test-networking-" + uuid.Generate()[0:8] 78 f.NoError(e2eutil.Register(jobID, "networking/inputs/docker_bridged_hostname_interpolation.nomad")) 79 tc.jobIDs = append(tc.jobIDs, jobID) 80 f.NoError(e2eutil.WaitForAllocStatusExpected(jobID, "default", []string{"running"}), 81 "job should be running with 1 alloc") 82 83 // Grab the allocations for the job. 84 allocs, _, err := tc.Nomad().Jobs().Allocations(jobID, false, nil) 85 f.NoError(err, "failed to get allocs for job") 86 f.Len(allocs, 1, "job should have one alloc") 87 88 // Run the hostname command within the allocation. 89 hostnameOutput, err := e2eutil.AllocExec(allocs[0].ID, "sleep", "hostname", "default", nil) 90 f.NoError(err, "failed to run hostname exec command") 91 f.Equal("mylittlepony-0", strings.TrimSpace(hostnameOutput), "incorrect hostname set within container") 92 93 // Check the /etc/hosts file for the correct IP address and hostname entry. 94 hostsOutput, err := e2eutil.AllocExec(allocs[0].ID, "sleep", "cat /etc/hosts", "default", nil) 95 f.NoError(err, "failed to run hostname exec command") 96 f.Contains(hostsOutput, "mylittlepony-0", "/etc/hosts doesn't contain hostname entry") 97 }