mirror of
https://github.com/kemko/nomad.git
synced 2026-01-05 01:45:44 +03:00
* test: use `T.TempDir` to create temporary test directory
This commit replaces `ioutil.TempDir` with `t.TempDir` in tests. The
directory created by `t.TempDir` is automatically removed when the test
and all its subtests complete.
Prior to this commit, temporary directory created using `ioutil.TempDir`
needs to be removed manually by calling `os.RemoveAll`, which is omitted
in some tests. The error handling boilerplate e.g.
defer func() {
if err := os.RemoveAll(dir); err != nil {
t.Fatal(err)
}
}
is also tedious, but `t.TempDir` handles this for us nicely.
Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix TestLogmon_Start_restart on Windows
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
* test: fix failing TestConsul_Integration
t.TempDir fails to perform the cleanup properly because the folder is
still in use
testing.go:967: TempDir RemoveAll cleanup: unlinkat /tmp/TestConsul_Integration2837567823/002/191a6f1a-5371-cf7c-da38-220fe85d10e5/web/secrets: device or resource busy
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
117 lines
2.6 KiB
Go
117 lines
2.6 KiB
Go
//go:build !windows
|
|
// +build !windows
|
|
|
|
package hostnames
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/nomad/plugins/drivers"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGenerateEtcHostsMount(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
name string
|
|
spec *drivers.NetworkIsolationSpec
|
|
extraHosts []string
|
|
expected []string
|
|
expectedErr string
|
|
}{
|
|
{
|
|
name: "no-spec",
|
|
},
|
|
{
|
|
name: "no-hosts-config",
|
|
spec: &drivers.NetworkIsolationSpec{Mode: drivers.NetIsolationModeGroup},
|
|
},
|
|
{
|
|
name: "base-case",
|
|
spec: &drivers.NetworkIsolationSpec{
|
|
Mode: drivers.NetIsolationModeGroup,
|
|
HostsConfig: &drivers.HostsConfig{
|
|
Address: "192.168.1.1",
|
|
Hostname: "xyzzy",
|
|
},
|
|
},
|
|
expected: []string{
|
|
"192.168.1.1 xyzzy",
|
|
},
|
|
},
|
|
{
|
|
name: "with-valid-extra-hosts",
|
|
spec: &drivers.NetworkIsolationSpec{
|
|
Mode: drivers.NetIsolationModeGroup,
|
|
HostsConfig: &drivers.HostsConfig{
|
|
Address: "192.168.1.1",
|
|
Hostname: "xyzzy",
|
|
},
|
|
},
|
|
extraHosts: []string{
|
|
"apple:192.168.1.2",
|
|
"banana:2001:0db8:85a3:0000:0000:8a2e:0370:7334",
|
|
},
|
|
expected: []string{
|
|
"192.168.1.1 xyzzy",
|
|
"192.168.1.2 apple",
|
|
"2001:0db8:85a3:0000:0000:8a2e:0370:7334 banana",
|
|
},
|
|
},
|
|
{
|
|
name: "invalid-extra-hosts-syntax",
|
|
spec: &drivers.NetworkIsolationSpec{
|
|
Mode: drivers.NetIsolationModeGroup,
|
|
HostsConfig: &drivers.HostsConfig{
|
|
Address: "192.168.1.1",
|
|
Hostname: "xyzzy",
|
|
},
|
|
},
|
|
extraHosts: []string{"apple192.168.1.2"},
|
|
expectedErr: "invalid hosts entry \"apple192.168.1.2\"",
|
|
},
|
|
{
|
|
name: "invalid-extra-hosts-bad-ip",
|
|
spec: &drivers.NetworkIsolationSpec{
|
|
Mode: drivers.NetIsolationModeGroup,
|
|
HostsConfig: &drivers.HostsConfig{
|
|
Address: "192.168.1.1",
|
|
Hostname: "xyzzy",
|
|
},
|
|
},
|
|
extraHosts: []string{"apple:192.168.1.256"},
|
|
expectedErr: "invalid IP address \"apple:192.168.1.256\"",
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
taskDir := t.TempDir()
|
|
dest := filepath.Join(taskDir, "hosts")
|
|
|
|
got, err := GenerateEtcHostsMount(taskDir, tc.spec, tc.extraHosts)
|
|
|
|
if tc.expectedErr != "" {
|
|
require.EqualError(err, tc.expectedErr)
|
|
} else {
|
|
require.NoError(err)
|
|
}
|
|
if len(tc.expected) == 0 {
|
|
require.Nil(got)
|
|
} else {
|
|
require.NotNil(got)
|
|
require.FileExists(dest)
|
|
tmpHosts, err := ioutil.ReadFile(dest)
|
|
require.NoError(err)
|
|
for _, line := range tc.expected {
|
|
require.Contains(string(tmpHosts), line)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|