From c8604bc2000b9e3713eaddf948c9b1a96bc51ad4 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Sat, 25 Nov 2023 23:50:01 -0700 Subject: [PATCH] Remove testenv.WriteFileAbs Writing absolute file names in testenv breaks safety that files in the test environment are written to the designated temporary path. Adjusted the relevant tests to use testenv.WriteFile. Signed-off-by: Jonathon Anderson --- internal/app/wwctl/overlay/list/main_test.go | 4 +- internal/app/wwctl/overlay/show/main_test.go | 4 +- internal/pkg/testenv/testenv.go | 59 ++++++++++---------- 3 files changed, 32 insertions(+), 35 deletions(-) diff --git a/internal/app/wwctl/overlay/list/main_test.go b/internal/app/wwctl/overlay/list/main_test.go index 55e9d34e..7fdbb694 100644 --- a/internal/app/wwctl/overlay/list/main_test.go +++ b/internal/app/wwctl/overlay/list/main_test.go @@ -5,7 +5,6 @@ import ( "path" "testing" - warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/stretchr/testify/assert" @@ -15,8 +14,7 @@ import ( func Test_Overlay_List(t *testing.T) { env := testenv.New(t) - wwconf := warewulfconf.Get() - env.WriteFileAbs(t, path.Join(wwconf.Paths.WWOverlaydir, "testoverlay/email.ww"), ` + env.WriteFile(t, path.Join(testenv.WWOverlaydir, "testoverlay/email.ww"), ` {{ if .Tags.email }}eMail: {{ .Tags.email }}{{else}} noMail{{- end }} `) defer env.RemoveAll(t) diff --git a/internal/app/wwctl/overlay/show/main_test.go b/internal/app/wwctl/overlay/show/main_test.go index 3bd63a84..de8db114 100644 --- a/internal/app/wwctl/overlay/show/main_test.go +++ b/internal/app/wwctl/overlay/show/main_test.go @@ -5,7 +5,6 @@ import ( "path" "testing" - warewulfconf "github.com/hpcng/warewulf/internal/pkg/config" "github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/stretchr/testify/assert" @@ -21,7 +20,6 @@ var ( func Test_Overlay_List(t *testing.T) { env := testenv.New(t) - wwconf := warewulfconf.Get() env.WriteFile(t, "etc/warewulf/nodes.conf", `WW_INTERNAL: 43 nodeprofiles: @@ -39,7 +37,7 @@ nodes: - empty `) - env.WriteFileAbs(t, path.Join(wwconf.Paths.WWOverlaydir, "testoverlay/email.ww"), overlayCont) + env.WriteFile(t, path.Join(testenv.WWOverlaydir, "testoverlay/email.ww"), overlayCont) defer env.RemoveAll(t) warewulfd.SetNoDaemon() t.Run("overlay show raw", func(t *testing.T) { diff --git a/internal/pkg/testenv/testenv.go b/internal/pkg/testenv/testenv.go index 9d86eb40..7303d326 100644 --- a/internal/pkg/testenv/testenv.go +++ b/internal/pkg/testenv/testenv.go @@ -29,6 +29,19 @@ type TestEnv struct { BaseDir string } +const Sysconfdir = "etc" +const Bindir = "bin" +const Datadir = "share" +const Localstatedir = "var/local" +const Srvdir = "srv" +const Tftpdir = "srv/tftp" +const Firewallddir = "usr/lib/firewalld/services" +const Systemddir = "usr/lib/systemd/system" +const WWOverlaydir = "var/local/warewulf/overlays" +const WWChrootdir = "var/local/warewulf/chroots" +const WWProvisiondir = "srv/warewulf" +const WWClientdir = "warewulf" + // New creates a test environment in a temporary directory and configures // Warewulf to use it. // @@ -44,27 +57,27 @@ func New(t *testing.T) (env *TestEnv) { assert.NoError(t, err) env.BaseDir = tmpDir - env.WriteFile(t, "etc/warewulf/nodes.conf", initNodesConf) - env.WriteFile(t, "etc/warewulf/warewulf.conf", initWarewulfConf) - env.WriteFile(t, "share/warewulf/defaults.conf", initDefaultsConf) + env.WriteFile(t, path.Join(Sysconfdir, "warewulf/nodes.conf"), initNodesConf) + env.WriteFile(t, path.Join(Sysconfdir, "warewulf/warewulf.conf"), initWarewulfConf) + env.WriteFile(t, path.Join(Datadir, "warewulf/defaults.conf"), initDefaultsConf) // re-read warewulf.conf conf := config.New() - err = conf.Read(env.GetPath("etc/warewulf/warewulf.conf")) + err = conf.Read(env.GetPath(path.Join(Sysconfdir, "warewulf/warewulf.conf"))) assert.NoError(t, err) - conf.Paths.Sysconfdir = env.GetPath("etc") - conf.Paths.Bindir = env.GetPath("bin") - conf.Paths.Datadir = env.GetPath("share") - conf.Paths.Localstatedir = env.GetPath("var/local") - conf.Paths.Srvdir = env.GetPath("srv") - conf.Paths.Tftpdir = env.GetPath("srv/tftp") - conf.Paths.Firewallddir = env.GetPath("usr/lib/firewalld/services") - conf.Paths.Systemddir = env.GetPath("usr/lib/systemd/system") - conf.Paths.WWOverlaydir = env.GetPath(path.Join(conf.Paths.Localstatedir, "warewulfoverlays")) - conf.Paths.WWChrootdir = env.GetPath(path.Join(conf.Paths.Localstatedir, "warewulf/chroots")) - conf.Paths.WWProvisiondir = env.GetPath(path.Join(conf.Paths.Srvdir, "warewulf")) - conf.Paths.WWClientdir = env.GetPath("warewulf") + conf.Paths.Sysconfdir = env.GetPath(Sysconfdir) + conf.Paths.Bindir = env.GetPath(Bindir) + conf.Paths.Datadir = env.GetPath(Datadir) + conf.Paths.Localstatedir = env.GetPath(Localstatedir) + conf.Paths.Srvdir = env.GetPath(Srvdir) + conf.Paths.Tftpdir = env.GetPath(Tftpdir) + conf.Paths.Firewallddir = env.GetPath(Firewallddir) + conf.Paths.Systemddir = env.GetPath(Systemddir) + conf.Paths.WWOverlaydir = env.GetPath(WWOverlaydir) + conf.Paths.WWChrootdir = env.GetPath(WWChrootdir) + conf.Paths.WWProvisiondir = env.GetPath(WWProvisiondir) + conf.Paths.WWClientdir = env.GetPath(WWClientdir) for _, confPath := range []string{ conf.Paths.Sysconfdir, @@ -84,7 +97,7 @@ func New(t *testing.T) (env *TestEnv) { } // node.init() has already run, so set the config path again - node.ConfigFile = env.GetPath("etc/warewulf/nodes.conf") + node.ConfigFile = env.GetPath(path.Join(Sysconfdir, "warewulf/nodes.conf")) return } @@ -119,18 +132,6 @@ func (env *TestEnv) WriteFile(t *testing.T, fileName string, content string) { assert.NoError(t, err) } -// WriteFileAbs uses an absloute path in opposite to WriteFile -func (env *TestEnv) WriteFileAbs(t *testing.T, fileName string, content string) { - dirName := filepath.Dir(fileName) - err := os.MkdirAll(dirName, 0755) - assert.NoError(t, err) - f, err := os.Create(fileName) - assert.NoError(t, err) - defer f.Close() - _, err = f.WriteString(content) - assert.NoError(t, err) -} - // ReadFile returns the content of fileName as converted to a // string. //