68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
package overlay
|
|
|
|
import (
|
|
"testing"
|
|
|
|
warewulfconf "github.com/warewulf/warewulf/internal/pkg/config"
|
|
)
|
|
|
|
var overlayImageTests = []struct {
|
|
description string
|
|
node string
|
|
context string
|
|
overlays []string
|
|
image string
|
|
}{
|
|
{"all empty", "", "", nil, ""},
|
|
{"empty with named context", "", "system", nil, "p/overlays/__SYSTEM__.img"},
|
|
{"empty with named overlay", "", "", []string{"o1"}, "p/overlays/o1.img"},
|
|
{"empty with two named overlays", "", "", []string{"o1", "o2"}, "p/overlays/o1-o2.img"},
|
|
{"empty node", "node1", "", nil, ""},
|
|
{"node system overlay", "node1", "system", nil, "p/overlays/node1/__SYSTEM__.img"},
|
|
{"node runtime overlay", "node1", "runtime", nil, "p/overlays/node1/__RUNTIME__.img"},
|
|
{"node single overlay", "node1", "", []string{"o1"}, "p/overlays/node1/o1.img"},
|
|
{"node two overlays", "node1", "", []string{"o1", "o2"}, "p/overlays/node1/o1-o2.img"},
|
|
{"node with context and overlays", "node1", "system", []string{"o1", "o2"}, "p/overlays/node1/__SYSTEM__.img"},
|
|
}
|
|
|
|
var validateOverlayNameTests = []struct {
|
|
name string
|
|
wantErr bool
|
|
}{
|
|
{"valid", false},
|
|
{"valid-with-dashes", false},
|
|
{"valid.with.dots", false},
|
|
{"valid_underscore", false},
|
|
{"", true},
|
|
{"../../etc", true},
|
|
{"../foo", true},
|
|
{"/etc/passwd", true},
|
|
{"foo/bar", true},
|
|
{"foo bar", true},
|
|
{"foo\x00bar", true},
|
|
}
|
|
|
|
func Test_validateOverlayName(t *testing.T) {
|
|
for _, tt := range validateOverlayNameTests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := validateOverlayName(tt.name)
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("validateOverlayName(%q) error = %v, wantErr %v", tt.name, err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func Test_OverlayImage(t *testing.T) {
|
|
conf := warewulfconf.Get()
|
|
conf.Paths.WWProvisiondir = "p"
|
|
for _, tt := range overlayImageTests {
|
|
t.Run(tt.description, func(t *testing.T) {
|
|
out := Image(tt.node, tt.context, tt.overlays)
|
|
if tt.image != out {
|
|
t.Errorf("got %q, want %q", out, tt.image)
|
|
}
|
|
})
|
|
}
|
|
}
|