Skip to content

Commit b858b75

Browse files
leodidoona-agent
andcommitted
feat(test): add specific error validation for legacy push test
- Add expectErrorMatch field with regex pattern matching - Validate that legacy push fails with Docker Hub auth error - Ensures test fails for the right reason (missing credentials) - Makes test more precise and maintainable - Pattern matches: 'push access denied', 'authorization failed', or 'insufficient_scope' Co-authored-by: Ona <no-reply@ona.com>
1 parent 573e830 commit b858b75

File tree

1 file changed

+42
-22
lines changed

1 file changed

+42
-22
lines changed

pkg/leeway/build_integration_test.go

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"os/exec"
1515
"path/filepath"
16+
"regexp"
1617
"strings"
1718
"testing"
1819

@@ -79,33 +80,37 @@ func TestDockerPackage_ExportToCache_Integration(t *testing.T) {
7980
}
8081

8182
tests := []struct {
82-
name string
83-
exportToCache bool
84-
hasImages bool
85-
expectFiles []string
86-
skipReason string
87-
expectError bool
83+
name string
84+
exportToCache bool
85+
hasImages bool
86+
expectFiles []string
87+
skipReason string
88+
expectError bool
89+
expectErrorMatch string // Regex pattern to match expected error
8890
}{
8991
{
90-
name: "legacy push behavior",
91-
exportToCache: false,
92-
hasImages: true,
93-
expectFiles: []string{"imgnames.txt", "metadata.yaml"},
94-
expectError: true, // Expected to fail at push step without credentials
92+
name: "legacy push behavior",
93+
exportToCache: false,
94+
hasImages: true,
95+
expectFiles: []string{"imgnames.txt", "metadata.yaml"},
96+
expectError: true,
97+
expectErrorMatch: "(?i)(push access denied|authorization failed|insufficient_scope)", // Expected Docker Hub auth error
9598
},
9699
{
97-
name: "new export behavior",
98-
exportToCache: true,
99-
hasImages: true,
100-
expectFiles: []string{"image.tar", "imgnames.txt", "docker-export-metadata.json"},
101-
expectError: false,
100+
name: "new export behavior",
101+
exportToCache: true,
102+
hasImages: true,
103+
expectFiles: []string{"image.tar", "imgnames.txt", "docker-export-metadata.json"},
104+
expectError: false,
105+
expectErrorMatch: "",
102106
},
103107
{
104-
name: "export without image config",
105-
exportToCache: true,
106-
hasImages: false,
107-
expectFiles: []string{"content"},
108-
expectError: false,
108+
name: "export without image config",
109+
exportToCache: true,
110+
hasImages: false,
111+
expectFiles: []string{"content"},
112+
expectError: false,
113+
expectErrorMatch: "",
109114
},
110115
}
111116

@@ -196,7 +201,22 @@ CMD ["echo", "test"]`
196201
if err == nil {
197202
t.Fatal("Expected build to fail but it succeeded")
198203
}
199-
t.Logf("Build failed as expected: %v", err)
204+
205+
// Validate error matches expected pattern
206+
if tt.expectErrorMatch != "" {
207+
matched, regexErr := regexp.MatchString(tt.expectErrorMatch, err.Error())
208+
if regexErr != nil {
209+
t.Fatalf("Invalid error regex pattern: %v", regexErr)
210+
}
211+
if !matched {
212+
t.Fatalf("Error doesn't match expected pattern.\nExpected pattern: %s\nActual error: %v",
213+
tt.expectErrorMatch, err)
214+
}
215+
t.Logf("Build failed as expected with correct error: %v", err)
216+
} else {
217+
t.Logf("Build failed as expected: %v", err)
218+
}
219+
200220
// For legacy push test, we expect it to fail at push step
201221
// but the image should still be built locally
202222
// Skip further validation for this test case

0 commit comments

Comments
 (0)