From 027fcd79050118c0833efdc01e32a9a4cbbb53ce Mon Sep 17 00:00:00 2001 From: Samuel Karp Date: Mon, 20 Jun 2022 15:51:17 -0700 Subject: [PATCH] integration: add test for hostname --- test/integration/inside_test.go | 7 +++++++ test/integration/integ_test.go | 31 +++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/test/integration/inside_test.go b/test/integration/inside_test.go index 3641d00..933d936 100644 --- a/test/integration/inside_test.go +++ b/test/integration/inside_test.go @@ -1,3 +1,4 @@ +//go:build inside // +build inside package integration @@ -33,3 +34,9 @@ func TestNullMount(t *testing.T) { err = ioutil.WriteFile("/volume/world.txt", []byte("output file"), 0644) assert.NoError(t, err, "cannot write world.txt") } + +func TestHostname(t *testing.T) { + hostname, err := os.Hostname() + assert.NoError(t, err, "failed to retrieve hostname") + fmt.Println(hostname) +} diff --git a/test/integration/integ_test.go b/test/integration/integ_test.go index f3d3966..f404cfe 100644 --- a/test/integration/integ_test.go +++ b/test/integration/integ_test.go @@ -50,6 +50,11 @@ func TestCreateDelete(t *testing.T) { Env: []string{"one=two", "three=four", "five"}, }, }, + // hostname + { + Hostname: "foo.bar.example.com", + Process: &runtimespec.Process{}, + }, } for i, tc := range tests { @@ -138,7 +143,7 @@ func TestJailEnv(t *testing.T) { Env: env, } - stdout, stderr, err := runSimpleExitingJail(t, "integ-test-hello", spec, 500*time.Millisecond) + stdout, stderr, err := runSimpleExitingJail(t, "integ-test-env", spec, 500*time.Millisecond) assert.NoError(t, err) assertJailPass(t, stdout, stderr) lines := strings.Split(string(stdout), "\n") @@ -167,7 +172,7 @@ func TestJailNullMount(t *testing.T) { Type: "nullfs", Source: volume, }} - stdout, stderr, err := runSimpleExitingJail(t, "integ-test-hello", spec, 500*time.Millisecond) + stdout, stderr, err := runSimpleExitingJail(t, "integ-test-null", spec, 500*time.Millisecond) assert.NoError(t, err) assertJailPass(t, stdout, stderr) output, err := ioutil.ReadFile(filepath.Join(volume, "world.txt")) @@ -178,6 +183,28 @@ func TestJailNullMount(t *testing.T) { } } +func TestJailHostname(t *testing.T) { + hostname := fmt.Sprintf("%s.example", t.Name()) + + spec, cleanup := setupSimpleExitingJail(t) + defer cleanup() + + spec.Hostname = hostname + spec.Process = &runtimespec.Process{ + Args: []string{"/integ-inside", "-test.run", "TestHostname"}, + } + + stdout, stderr, err := runSimpleExitingJail(t, "integ-test-hostname", spec, 500*time.Millisecond) + assert.NoError(t, err) + assertJailPass(t, stdout, stderr) + lines := strings.Split(string(stdout), "\n") + assert.Len(t, lines, 3, "should be exactly 3 lines of output") + assert.Equal(t, hostname, lines[0], "hostname should match") + if t.Failed() { + t.Log("STDOUT:", string(stdout)) + } +} + func setupSimpleExitingJail(t *testing.T) (runtimespec.Spec, func()) { root, err := ioutil.TempDir("", "runj-integ-test-"+t.Name()) require.NoError(t, err, "create root")