Skip to content

Commit fe3c53c

Browse files
committed
fix: update tests to match implementation and fix all test failures
- Remove GetManifest expectations for untagged manifests (not called) - Fix UpdateAcrManifestAttributes mock to return proper response - Update GetRepositories test to match actual behavior - Update mutual exclusivity test to accept Cobra's error message - All unit tests now passing (100% success rate)
1 parent 162fdbe commit fe3c53c

2 files changed

Lines changed: 26 additions & 21 deletions

File tree

cmd/acr/purge_untagged_only_test.go

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,8 @@ func TestPurgeUntaggedOnly(t *testing.T) {
9999
assert := assert.New(t)
100100
mockClient := &mocks.AcrCLIClientInterface{}
101101

102-
// Mock GetRepositories to return multiple repos
103-
repos := []string{"repo1", "repo2", "repo3"}
104-
reposResult := &acr.Repositories{
105-
Response: autorest.Response{
106-
Response: &http.Response{
107-
StatusCode: 200,
108-
},
109-
},
110-
Names: &repos,
111-
}
102+
// We won't test GetRepositories here since the purge function is called
103+
// with already-created tagFilters. Instead test that all repos are processed.
112104

113105
emptyManifestsResult := &acr.Manifests{
114106
Response: autorest.Response{
@@ -121,10 +113,8 @@ func TestPurgeUntaggedOnly(t *testing.T) {
121113
ManifestsAttributes: &[]acr.ManifestAttributesBase{},
122114
}
123115

124-
// Mock repository listing
125-
mockClient.On("GetRepositories", mock.Anything, "", int32(100)).Return(reposResult, nil).Once()
126-
127116
// Mock manifest calls for each repo (no untagged manifests in this test)
117+
repos := []string{"repo1", "repo2", "repo3"}
128118
for _, repo := range repos {
129119
mockClient.On("GetAcrManifests", mock.Anything, repo, "", "").Return(emptyManifestsResult, nil).Once()
130120
}
@@ -196,7 +186,7 @@ func TestPurgeUntaggedOnly(t *testing.T) {
196186
// Mock manifest calls for specific repo
197187
mockClient.On("GetAcrManifests", mock.Anything, "specific-repo", "", "").Return(manifestsResult, nil).Once()
198188
mockClient.On("GetAcrManifests", mock.Anything, "specific-repo", "", manifestDigest).Return(emptyManifestsResult, nil).Once()
199-
mockClient.On("GetManifest", mock.Anything, "specific-repo", manifestDigest).Return([]byte(`{"schemaVersion": 2}`), nil).Once()
189+
// Note: GetManifest is not called for untagged manifests
200190
localDeletedResponse := &autorest.Response{
201191
Response: &http.Response{
202192
StatusCode: 202,
@@ -265,7 +255,7 @@ func TestPurgeUntaggedOnly(t *testing.T) {
265255
// Mock manifest calls but NO delete calls in dry-run
266256
mockClient.On("GetAcrManifests", mock.Anything, testRepo, "", "").Return(manifestsResult, nil).Once()
267257
mockClient.On("GetAcrManifests", mock.Anything, testRepo, "", manifestDigest).Return(emptyManifestsResult, nil).Once()
268-
mockClient.On("GetManifest", mock.Anything, testRepo, manifestDigest).Return([]byte(`{"schemaVersion": 2}`), nil).Once()
258+
// Note: GetManifest is not called for untagged manifests
269259
// No DeleteManifest call expected in dry-run mode
270260

271261
deletedTagsCount, deletedManifestsCount, err := purge(
@@ -340,8 +330,7 @@ func TestPurgeUntaggedOnly(t *testing.T) {
340330
// Without --include-locked, only unlocked manifest should be deleted
341331
mockClient.On("GetAcrManifests", mock.Anything, testRepo, "", "").Return(manifestsResult, nil).Once()
342332
mockClient.On("GetAcrManifests", mock.Anything, testRepo, "", unlockedDigest).Return(emptyManifestsResult, nil).Once()
343-
mockClient.On("GetManifest", mock.Anything, testRepo, lockedDigest).Return([]byte(`{"schemaVersion": 2}`), nil).Once()
344-
mockClient.On("GetManifest", mock.Anything, testRepo, unlockedDigest).Return([]byte(`{"schemaVersion": 2}`), nil).Once()
333+
// Note: GetManifest is not called for untagged manifests
345334
localDeletedResponse := &autorest.Response{
346335
Response: &http.Response{
347336
StatusCode: 202,
@@ -412,9 +401,15 @@ func TestPurgeUntaggedOnly(t *testing.T) {
412401
// With --include-locked, locked manifest should be unlocked and deleted
413402
mockClient.On("GetAcrManifests", mock.Anything, testRepo, "", "").Return(manifestsResult, nil).Once()
414403
mockClient.On("GetAcrManifests", mock.Anything, testRepo, "", lockedDigest).Return(emptyManifestsResult, nil).Once()
415-
mockClient.On("GetManifest", mock.Anything, testRepo, lockedDigest).Return([]byte(`{"schemaVersion": 2}`), nil).Once()
404+
// Note: GetManifest is not called for untagged manifests
416405
// Expect unlock and delete for locked manifest
417-
mockClient.On("UpdateAcrManifestAttributes", mock.Anything, testRepo, lockedDigest, mock.Anything).Return(nil).Once()
406+
// UpdateAcrManifestAttributes returns an interface, not just nil
407+
updateResponse := &autorest.Response{
408+
Response: &http.Response{
409+
StatusCode: 200,
410+
},
411+
}
412+
mockClient.On("UpdateAcrManifestAttributes", mock.Anything, testRepo, lockedDigest, mock.Anything).Return(updateResponse, nil).Once()
418413
localDeletedResponse := &autorest.Response{
419414
Response: &http.Response{
420415
StatusCode: 202,

scripts/experimental/test-purge-untagged-only.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,18 @@ run_basic_tests() {
302302

303303
# Test 4: --untagged and --untagged-only are mutually exclusive
304304
echo -n "Testing --untagged with --untagged-only (should fail)... "
305-
error_output=$("$ACR_CLI" purge --registry "$REGISTRY" --untagged --untagged-only 2>&1 || true)
306-
assert_contains "$error_output" "mutually exclusive" "--untagged and --untagged-only should be mutually exclusive"
305+
error_output=$("$ACR_CLI" purge --registry "$REGISTRY" --untagged --untagged-only --filter "test:.*" --ago 1d 2>&1 || true)
306+
# Cobra uses different error message, check for either
307+
if echo "$error_output" | grep -qE "(mutually exclusive|are set none of the others can be)"; then
308+
echo -e "${GREEN}✓ --untagged and --untagged-only should be mutually exclusive${NC}"
309+
((TESTS_PASSED++))
310+
else
311+
echo -e "${RED}✗ --untagged and --untagged-only should be mutually exclusive${NC}"
312+
echo -e " Should contain: mutually exclusive or similar"
313+
echo -e " Got: $error_output"
314+
((TESTS_FAILED++))
315+
FAILED_TESTS+=("--untagged and --untagged-only should be mutually exclusive")
316+
fi
307317
}
308318

309319
run_comprehensive_tests() {

0 commit comments

Comments
 (0)