|
| 1 | +package contracttest |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/exec" |
| 6 | + "path/filepath" |
| 7 | + "slices" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestDockerPrunePreservesNamedRollbackImages(t *testing.T) { |
| 13 | + root := repositoryRoot(t) |
| 14 | + script := filepath.Join(root, "rootfs", "home", "cloud-compose", "docker-prune.sh") |
| 15 | + temporary := t.TempDir() |
| 16 | + profile := filepath.Join(temporary, "profile.sh") |
| 17 | + lock := filepath.Join(temporary, "docker-prune.lock") |
| 18 | + logPath := filepath.Join(temporary, "docker.log") |
| 19 | + binDirectory := filepath.Join(temporary, "bin") |
| 20 | + |
| 21 | + if err := os.Mkdir(binDirectory, 0o755); err != nil { |
| 22 | + t.Fatalf("create mock binary directory: %v", err) |
| 23 | + } |
| 24 | + if err := os.WriteFile(profile, []byte("# test profile\n"), 0o600); err != nil { |
| 25 | + t.Fatalf("write test profile: %v", err) |
| 26 | + } |
| 27 | + mockDocker := []byte("#!/usr/bin/env bash\nset -euo pipefail\nprintf '%s\\n' \"$*\" >> \"${DOCKER_PRUNE_TEST_LOG:?}\"\n") |
| 28 | + if err := os.WriteFile(filepath.Join(binDirectory, "docker"), mockDocker, 0o755); err != nil { |
| 29 | + t.Fatalf("write mock Docker binary: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + command := exec.Command("bash", script) |
| 33 | + command.Env = append(os.Environ(), |
| 34 | + "CLOUD_COMPOSE_DOCKER_PRUNE_ENABLED=true", |
| 35 | + "CLOUD_COMPOSE_DOCKER_PRUNE_LOCK_PATH="+lock, |
| 36 | + "CLOUD_COMPOSE_DOCKER_PRUNE_UNTIL=72h", |
| 37 | + "CLOUD_COMPOSE_PROFILE_PATH="+profile, |
| 38 | + "DOCKER_PRUNE_TEST_LOG="+logPath, |
| 39 | + "PATH="+binDirectory+":"+os.Getenv("PATH"), |
| 40 | + ) |
| 41 | + if output, err := command.CombinedOutput(); err != nil { |
| 42 | + t.Fatalf("run Docker prune policy: %v\n%s", err, output) |
| 43 | + } |
| 44 | + |
| 45 | + logBytes, err := os.ReadFile(logPath) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("read Docker invocation log: %v", err) |
| 48 | + } |
| 49 | + commands := strings.Split(strings.TrimSpace(string(logBytes)), "\n") |
| 50 | + want := []string{ |
| 51 | + "container prune --force --filter until=72h", |
| 52 | + "network prune --force --filter until=72h", |
| 53 | + "image prune --force --filter until=72h", |
| 54 | + "builder prune --force --filter until=72h", |
| 55 | + } |
| 56 | + if !slices.Equal(commands, want) { |
| 57 | + t.Fatalf("Docker prune commands = %#v, want %#v", commands, want) |
| 58 | + } |
| 59 | + for _, invocation := range commands { |
| 60 | + if strings.Contains(invocation, "system prune") || strings.Contains(invocation, "image prune --all") { |
| 61 | + t.Fatalf("destructive Docker prune invocation remains: %s", invocation) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestDockerPruneIsDisabledByDefault(t *testing.T) { |
| 67 | + root := repositoryRoot(t) |
| 68 | + script := filepath.Join(root, "rootfs", "home", "cloud-compose", "docker-prune.sh") |
| 69 | + temporary := t.TempDir() |
| 70 | + profile := filepath.Join(temporary, "profile.sh") |
| 71 | + if err := os.WriteFile(profile, []byte("# test profile\n"), 0o600); err != nil { |
| 72 | + t.Fatalf("write test profile: %v", err) |
| 73 | + } |
| 74 | + |
| 75 | + command := exec.Command("bash", script) |
| 76 | + command.Env = append(os.Environ(), |
| 77 | + "CLOUD_COMPOSE_DOCKER_PRUNE_ENABLED=", |
| 78 | + "CLOUD_COMPOSE_PROFILE_PATH="+profile, |
| 79 | + ) |
| 80 | + output, err := command.CombinedOutput() |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("run disabled Docker prune policy: %v\n%s", err, output) |
| 83 | + } |
| 84 | + if !strings.Contains(string(output), "Docker pruning is disabled") { |
| 85 | + t.Fatalf("disabled Docker prune output = %q", output) |
| 86 | + } |
| 87 | +} |
0 commit comments