|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package docker |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "os/exec" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/blox-eng/openblox/pkg/sandbox" |
| 12 | +) |
| 13 | + |
| 14 | +// pullTestImage is small, stable, and not the image the rest of the suite uses, |
| 15 | +// so removing it locally cannot disturb anything else. |
| 16 | +const pullTestImage = "alpine:3.19" |
| 17 | + |
| 18 | +// A sandbox host is not required to have the image already. Before this, Create |
| 19 | +// failed with a bare "No such image" and every deployment needed a manual build |
| 20 | +// or pull first. |
| 21 | +func TestCreatePullsAnAbsentImage(t *testing.T) { |
| 22 | + b := newTestBackend(t) |
| 23 | + ctx := context.Background() |
| 24 | + |
| 25 | + // Start from a genuinely absent image, or the test proves nothing. |
| 26 | + _ = exec.Command("docker", "rmi", "-f", pullTestImage).Run() |
| 27 | + if err := exec.Command("docker", "image", "inspect", pullTestImage).Run(); err == nil { |
| 28 | + t.Skipf("%s is still present (in use by another container?); cannot prove the pull", pullTestImage) |
| 29 | + } |
| 30 | + |
| 31 | + name := "openblox-test-pull" |
| 32 | + _, err := b.Create(ctx, name, sandbox.WithImage(pullTestImage)) |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("Create with an absent image = %v, want it pulled", err) |
| 35 | + } |
| 36 | + t.Cleanup(func() { _ = b.Destroy(context.Background(), name) }) |
| 37 | + |
| 38 | + if err := exec.Command("docker", "image", "inspect", pullTestImage).Run(); err != nil { |
| 39 | + t.Errorf("%s is still absent after a successful Create", pullTestImage) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// An image that cannot be obtained is its own failure, distinct from a malformed |
| 44 | +// request: the reference may be perfectly valid and the registry simply down. |
| 45 | +func TestCreateReportsAnUnobtainableImage(t *testing.T) { |
| 46 | + b := newTestBackend(t) |
| 47 | + |
| 48 | + _, err := b.Create(context.Background(), "openblox-test-noimage", |
| 49 | + sandbox.WithImage("ghcr.io/blox-eng/openblox-does-not-exist:0.0.0")) |
| 50 | + |
| 51 | + if !errors.Is(err, sandbox.ErrImageUnavailable) { |
| 52 | + t.Fatalf("Create with an unobtainable image = %v, want ErrImageUnavailable", err) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// A present image must not be re-resolved on every create. The image is the |
| 57 | +// guest's whole userland, so a mutable tag silently changing under a caller is a |
| 58 | +// supply-chain surprise, not a convenience. |
| 59 | +func TestCreateDoesNotRePullAPresentImage(t *testing.T) { |
| 60 | + b := newTestBackend(t) |
| 61 | + ctx := context.Background() |
| 62 | + |
| 63 | + before := imageID(t, testImage) |
| 64 | + name := "openblox-test-nopull" |
| 65 | + if _, err := b.Create(ctx, name, sandbox.WithImage(testImage)); err != nil { |
| 66 | + t.Fatalf("Create = %v", err) |
| 67 | + } |
| 68 | + t.Cleanup(func() { _ = b.Destroy(context.Background(), name) }) |
| 69 | + |
| 70 | + if after := imageID(t, testImage); after != before { |
| 71 | + t.Errorf("image id changed %s -> %s; a present image was re-resolved", before, after) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func imageID(t *testing.T, ref string) string { |
| 76 | + t.Helper() |
| 77 | + out, err := exec.Command("docker", "image", "inspect", "-f", "{{.Id}}", ref).Output() |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("inspect %s = %v", ref, err) |
| 80 | + } |
| 81 | + return string(out) |
| 82 | +} |
0 commit comments