85 lines
2.3 KiB
Go
85 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
// module writes modules/<name>/module.json with one image artifact from `from`, and the files named.
|
|
func module(t *testing.T, root, name, from string, files ...string) {
|
|
t.Helper()
|
|
dir := filepath.Join(root, "modules", name)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body := `{"module":"` + name + `","build":{"artifacts":[{"name":"server","kind":"image","from":"` + from + `"},{"name":"files","kind":"archive","from":"../elsewhere"}]}}`
|
|
if err := os.WriteFile(filepath.Join(dir, "module.json"), []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, f := range files {
|
|
p := filepath.Join(dir, f)
|
|
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(p, []byte("FROM scratch\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestADockerfileInTheModulesOwnDirectoryPasses(t *testing.T) {
|
|
root := t.TempDir()
|
|
module(t, root, "own", "Dockerfile", "Dockerfile")
|
|
module(t, root, "nested", "image/Dockerfile", "image/Dockerfile")
|
|
got, err := Check(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 0 {
|
|
t.Errorf("found %v, want none", got)
|
|
}
|
|
}
|
|
|
|
func TestADockerfileOutsideTheModulesDirectoryFails(t *testing.T) {
|
|
root := t.TempDir()
|
|
module(t, root, "other", "Dockerfile", "Dockerfile")
|
|
module(t, root, "leaves", "../other/Dockerfile")
|
|
module(t, root, "absolute", "/etc/Dockerfile")
|
|
got, err := Check(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var mods []string
|
|
for _, f := range got {
|
|
mods = append(mods, f.Module)
|
|
}
|
|
if want := []string{"absolute", "leaves"}; !reflect.DeepEqual(mods, want) {
|
|
t.Errorf("refused %v (%v), want %v", mods, got, want)
|
|
}
|
|
}
|
|
|
|
func TestAMissingDockerfileFails(t *testing.T) {
|
|
root := t.TempDir()
|
|
module(t, root, "gone", "Dockerfile")
|
|
got, err := Check(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 1 || got[0].Module != "gone" {
|
|
t.Errorf("found %v, want gone refused", got)
|
|
}
|
|
}
|
|
|
|
// The catalogue as it stands: this is the check itself, run here too.
|
|
func TestTheCataloguesImagesBuildFromTheirOwnDirectories(t *testing.T) {
|
|
got, err := Check("../..")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 0 {
|
|
t.Errorf("an image builds from outside its module: %v", got)
|
|
}
|
|
}
|