52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func catalogue(t *testing.T, manifests map[string]string) string {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
for name, body := range manifests {
|
|
dir := filepath.Join(root, "modules", name)
|
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "module.json"), []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
return root
|
|
}
|
|
|
|
// A fixture manifest with an image artifact is refused, the builder's is not, and an upstream image is no image.
|
|
func TestOnlyBuildAgentMayDeclareAnImageBuiltFromADockerfile(t *testing.T) {
|
|
root := catalogue(t, map[string]string{
|
|
"old": `{"module":"old","build":{"artifacts":[{"name":"server","kind":"image","from":"Dockerfile"}]}}`,
|
|
"build-agent": `{"module":"build-agent","build":{"artifacts":[{"name":"agent","kind":"image","from":"Dockerfile"}]}}`,
|
|
"new": `{"module":"new","build":{"artifacts":[{"name":"server","kind":"upstream","from":"alpine@sha256:00"},{"name":"p","kind":"bundle"}]}}`,
|
|
"none": `{"module":"none"}`,
|
|
})
|
|
got, err := Check(root)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if want := []Finding{{Module: "old", Artifact: "server"}}; !reflect.DeepEqual(got, want) {
|
|
t.Errorf("found %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
// The catalogue this check lives in passes it.
|
|
func TestTheCatalogueDeclaresNoImage(t *testing.T) {
|
|
got, err := Check("../..")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) > 0 {
|
|
t.Errorf("the catalogue declares %v", got)
|
|
}
|
|
}
|