Skip to content

Commit

Permalink
Fix Artifactory delete test
Browse files Browse the repository at this point in the history
  • Loading branch information
amdurh05 committed Dec 6, 2024
1 parent 6d0d769 commit 90ea47b
Showing 1 changed file with 48 additions and 42 deletions.
90 changes: 48 additions & 42 deletions internal/artifactory_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,49 +91,55 @@ func TestDownloadWithArtifactoryCache(t *testing.T) {

// TestDeleteWithArtifactoryCache verifies deleting a resource with caching enabled.
func TestDeleteWithArtifactoryCache(t *testing.T) {
// Sub-test to verify successful deletion of a resource.
t.Run("SuccessfulDelete", func(t *testing.T) {
// Set the GRABIT_ARTIFACTORY_TOKEN environment variable.
t.Setenv("GRABIT_ARTIFACTORY_TOKEN", "test-token")
// Set the GRABIT_ARTIFACTORY_TOKEN environment variable.
t.Setenv("GRABIT_ARTIFACTORY_TOKEN", "test-token")

// Setup an HTTP handler to handle DELETE requests.
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "DELETE" { // Respond with OK for DELETE requests.
w.WriteHeader(http.StatusOK)
}
// Setup an HTTP handler to handle DELETE requests.
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "DELETE" { // Respond with OK for DELETE requests.
w.WriteHeader(http.StatusOK)
}
// Start the HTTP server and get the port it runs on.
port, server := test.HttpHandler(handler)
defer server.Close() // Ensure the server is stopped after the test.

// Set up URLs for the source file and cache.
sourceURL := fmt.Sprintf("http://localhost:%d/test.txt", port)
cacheURL := fmt.Sprintf("http://localhost:%d", port)

// Create a lock file with the resource and cache information.
lockContent := fmt.Sprintf(`[[Resource]]
Urls = ['%s']
Integrity = 'sha256-test'
CacheUri = '%s'`, sourceURL, cacheURL)

lockPath := test.TmpFile(t, lockContent)
lock, err := NewLock(lockPath, false)
require.NoError(t, err) // Fail the test if the lock cannot be created.

// Save the lock file before modifying it.
err = lock.Save()
require.NoError(t, err) // Fail the test if saving fails.

// Delete the resource from the lock file.
lock.DeleteResource(sourceURL)

// Save the lock file again after deletion.
err = lock.Save()
require.NoError(t, err) // Fail the test if saving fails.

// Reload the lock file and verify the resource is gone.
newLock, err := NewLock(lockPath, false)
}
// Start the HTTP server and get the port it runs on.
port, server := test.HttpHandler(handler)
defer server.Close() // Ensure the server is stopped after the test.

// Set up URLs for the source file and cache.
sourceURL := fmt.Sprintf("http://localhost:%d/test.txt", port)
cacheURL := fmt.Sprintf("http://localhost:%d", port)

// Create a lock file with the resource and cache information.
lockContent := fmt.Sprintf(`[[Resource]]
Urls = ['%s']
Integrity = 'sha256-test'
CacheUri = '%s'`, sourceURL, cacheURL)

lockPath := test.TmpFile(t, lockContent)
lock, err := NewLock(lockPath, false)
require.NoError(t, err) // Fail the test if the lock cannot be created.

// Save the lock file before modifying it.
err = lock.Save()
require.NoError(t, err) // Fail the test if saving fails.

// Delete the resource from the lock file.
lock.DeleteResource(sourceURL)

// Checks that http.StatusOK was returned meaning the delete was successful
resp, err := http.Get(server.URL)
if err != nil {
require.NoError(t, err)
assert.Equal(t, 0, len(newLock.conf.Resource)) // Ensure no resources remain.
})
}
if resp.StatusCode != http.StatusOK {
t.Errorf("expected 200 but got %d", resp.StatusCode)
}

// Save the lock file again after deletion.
err = lock.Save()
require.NoError(t, err) // Fail the test if saving fails.

// Reload the lock file and verify the resource is gone.
newLock, err := NewLock(lockPath, false)
require.NoError(t, err)
assert.Equal(t, 0, len(newLock.conf.Resource)) // Ensure no resources remain.
}

0 comments on commit 90ea47b

Please sign in to comment.