github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/e2e/networking/networking.go (about)

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