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 } const buildah = `{"module":"buildah","provides":[{"name":"rootless-image-build","reach":"machine"}]}` func TestOnlyBuildahProvidesTheRootlessImageBuild(t *testing.T) { root := catalogue(t, map[string]string{"buildah": buildah, "go": `{"module":"go","provides":[{"name":"go-toolchain"}]}`}) got, err := Check(root) if err != nil { t.Fatal(err) } if len(got) != 0 { t.Errorf("found %v, want none", got) } } func TestDockerAndAnySecondProviderAreRefused(t *testing.T) { root := catalogue(t, map[string]string{ "buildah": buildah, "docker": `{"module":"docker","provides":[{"name":"rootless-image-build"}]}`, "other": `{"module":"other","provides":[{"name":"x"},{"name":"rootless-image-build"}]}`, }) got, err := Check(root) if err != nil { t.Fatal(err) } want := []Finding{{Module: "docker"}, {Module: "other"}} if !reflect.DeepEqual(got, want) { t.Errorf("found %v, want %v", got, want) } } func TestBuildahMustProvideIt(t *testing.T) { root := catalogue(t, map[string]string{"buildah": `{"module":"buildah"}`}) got, err := Check(root) if err != nil { t.Fatal(err) } if want := []Finding{{Module: "buildah"}}; !reflect.DeepEqual(got, want) { t.Errorf("found %v, want %v", got, want) } } // The catalogue as it stands: this is the check itself, run here too. func TestTheCatalogueGivesTheProvisionToBuildahAlone(t *testing.T) { got, err := Check("../..") if err != nil { t.Fatal(err) } if len(got) != 0 { t.Errorf("the catalogue's providers of rootless-image-build are wrong: %v", got) } }