package api import ( "context" "os" "path/filepath" "testing" ) // fakeDocker puts a `docker` on PATH whose `image inspect` answers with the given RepoDigests. func fakeDocker(t *testing.T, digests string) { t.Helper() dir := t.TempDir() script := "#!/bin/sh\necho '" + digests + "'\n" if err := os.WriteFile(filepath.Join(dir, "docker"), []byte(script), 0o755); err != nil { t.Fatal(err) } t.Setenv("PATH", dir) } // An update pulls the newer image: the cached registry answer must not keep calling it behind. func TestSettledSeesAPulledImage(t *testing.T) { cached := []imageCheck{ {Image: "nginx:alpine", State: "update", remote: "sha256:new"}, {Image: "redis:7", State: "current", remote: "sha256:r"}, {Image: "local/thing", State: "unknown"}, } fakeDocker(t, `["nginx@sha256:new"]`) got := settled(context.Background(), cached) if got[0].State != "current" { t.Fatalf("pulled image still marked %q", got[0].State) } if got[1].State != "current" || got[2].State != "unknown" { t.Fatalf("others changed: %+v", got) } if cached[0].State != "update" { t.Fatal("settled wrote through to the cache") } } func TestSettledKeepsAnImageStillBehind(t *testing.T) { fakeDocker(t, `["nginx@sha256:old"]`) got := settled(context.Background(), []imageCheck{{Image: "nginx:alpine", State: "update", remote: "sha256:new"}}) if got[0].State != "update" { t.Fatalf("image not yet pulled marked %q", got[0].State) } }