Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Artifactory upload and download handling #30

Open
wants to merge 53 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
1675df4
Implemented Artifactory upload and download handling
eeasaa01 Nov 5, 2024
0a01f2e
Fixed authentication error and adjusted log level to debug for succes…
eeasaa01 Nov 5, 2024
412a3aa
Conditional ARTIFACTORY_TOKEN check and removed spurious comment
eeasaa01 Nov 5, 2024
825c3ea
Restored COMMENT_PREFIX and improved error logging for resource creat…
eeasaa01 Nov 5, 2024
d6229c9
Fixed Artifactory integration and addressed PR feedback
eeasaa01 Nov 6, 2024
0ea343f
Implemented requested changes and added tests for Artifactory integra…
eeasaa01 Nov 8, 2024
082c98b
Added comprehensive tests for Artifactory cache functionality
eeasaa01 Nov 8, 2024
a484aa3
Updating delete function to also delete artifactory cache
amdurh05 Nov 11, 2024
61ec037
Implemented Artifactory upload and download handling
eeasaa01 Nov 5, 2024
e98d7af
Fixed authentication error and adjusted log level to debug for succes…
eeasaa01 Nov 5, 2024
503e529
Conditional ARTIFACTORY_TOKEN check and removed spurious comment
eeasaa01 Nov 5, 2024
f5f034e
Restored COMMENT_PREFIX and improved error logging for resource creat…
eeasaa01 Nov 5, 2024
410b85b
Fixed Artifactory integration and addressed PR feedback
eeasaa01 Nov 6, 2024
4033f12
Implemented requested changes and added tests for Artifactory integra…
eeasaa01 Nov 8, 2024
c389fb2
Added comprehensive tests for Artifactory cache functionality
eeasaa01 Nov 8, 2024
2ab9448
Cleanup: Remove binary files and add gitignore
eeasaa01 Nov 12, 2024
2090e45
Fix: Code review feedback and cleanup
eeasaa01 Nov 13, 2024
1ee74b5
Fix: Pass token as parameter to uploadToArtifactory
eeasaa01 Nov 13, 2024
f0676ab
Fix: Prefix NO_CACHE_UPLOAD with GRABIT_
eeasaa01 Nov 13, 2024
5d9a7c8
Fix: Code improvements and cleanup
eeasaa01 Nov 13, 2024
513e420
Fix: Use requests library for cache upload
eeasaa01 Nov 13, 2024
8a41679
Update cmd/add.go with feedback implementation
eeasaa01 Nov 13, 2024
f2e8c42
Update cmd/cache_test.go with cache download tests
eeasaa01 Nov 13, 2024
5ba7cdd
Update log level to warn when failing to upload to cache
eeasaa01 Nov 13, 2024
335070a
Update hash.go and lock.go based on code review
eeasaa01 Nov 13, 2024
a10e728
Update .gitignore
eeasaa01 Nov 13, 2024
d4dcfd7
Merge branch 'feature/artifactory-upload' into feature/artifactory-de…
eeasaa01 Nov 15, 2024
3396a6f
Merge pull request #1 from eeasaa01/feature/artifactory-delete
eeasaa01 Nov 15, 2024
aa45dcb
Delete cmd/6ae8a75555209fd6c44157c0aed8016e763ff435a19cf186f768631401…
eeasaa01 Nov 18, 2024
de40802
Update add.go
eeasaa01 Nov 18, 2024
932ceef
Delete cmd/cache_test.go
eeasaa01 Nov 18, 2024
60c174f
Update download.go
eeasaa01 Nov 18, 2024
c1afae0
Delete cmd/test.txt
eeasaa01 Nov 18, 2024
1623787
Update hash.go
eeasaa01 Nov 18, 2024
ee5fafe
Update lock.go
eeasaa01 Nov 18, 2024
5d77339
Update resource.go
eeasaa01 Nov 18, 2024
1acba07
Delete a640986bc257389dd379886fdae6264c8cfa56bc98b71ce3ae3dfbd8ce61dbe3
eeasaa01 Nov 18, 2024
6794f82
Delete openmpi-5.0.5.tar.bz2
eeasaa01 Nov 18, 2024
1273d45
Replace old project files with new ones
eeasaa01 Nov 18, 2024
e5aefe8
Fix typo in filename: dowload_test.go -> download_test.go
eeasaa01 Nov 18, 2024
2b68e57
Fix merge conflict
amdurh05 Nov 19, 2024
f7f3445
readding artifactory add functionality
amdurh05 Nov 21, 2024
f93213b
Updating delete function and changing Artifactory filename to SRI format
amdurh05 Nov 22, 2024
8301d12
Fix bug in AddResource
amdurh05 Nov 22, 2024
534d546
Fix Artifactory download logic and tests for resource cache behavior
eeasaa01 Nov 26, 2024
9344f52
Update resource.go
eeasaa01 Nov 27, 2024
01d5f3e
Update lock.go
eeasaa01 Nov 27, 2024
1f5cf14
Update artifactory_cache_test.go
eeasaa01 Nov 27, 2024
2d597ba
Update lock.go
eeasaa01 Nov 27, 2024
738ec4b
Delete grsbit
eeasaa01 Nov 27, 2024
ba91254
refactor: replace http client with requests library in lock.go
eeasaa01 Nov 27, 2024
fb7c978
fix: update logic in lock.go
eeasaa01 Nov 27, 2024
79d9e43
WIP: Save local changes before pulling
eeasaa01 Nov 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package cmd

import (
"fmt"
"os"

"github.com/cisco-open/grabit/internal"
"github.com/spf13/cobra"
)
Expand All @@ -18,6 +21,7 @@ func addAdd(cmd *cobra.Command) {
addCmd.Flags().String("algo", internal.RecommendedAlgo, "Integrity algorithm")
addCmd.Flags().String("filename", "", "Target file name to use when downloading the resource")
addCmd.Flags().StringArray("tag", []string{}, "Resource tags")
addCmd.Flags().String("cache", "", "Artifactory cache URL")
cmd.AddCommand(addCmd)
}

Expand All @@ -26,6 +30,20 @@ func runAdd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
// Get cache URL
cacheURL, err := cmd.Flags().GetString("cache")
if err != nil {
return err
}

// Check token if cache is requested
if cacheURL != "" {
token := os.Getenv("GRABIT_ARTIFACTORY_TOKEN")
if token == "" {
return fmt.Errorf("GRABIT_ARTIFACTORY_TOKEN environment variable is not set")
}
}

lock, err := internal.NewLock(lockFile, true)
if err != nil {
return err
Expand All @@ -42,7 +60,7 @@ func runAdd(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
err = lock.AddResource(args, algo, tags, filename)
err = lock.AddResource(args, algo, tags, filename, cacheURL)
if err != nil {
return err
}
Expand Down
24 changes: 23 additions & 1 deletion cmd/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
)

func TestRunAdd(t *testing.T) {
// Setup HTTP handler for the resource
handler := func(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte(`abcdef`))
if err != nil {
Expand All @@ -18,8 +19,29 @@ func TestRunAdd(t *testing.T) {
}
port, server := test.HttpHandler(handler)
defer server.Close()

// Setup dummy cache server
cacheHandler := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "PUT" {
w.WriteHeader(http.StatusCreated)
}
}
cachePort, cacheServer := test.HttpHandler(cacheHandler)
defer cacheServer.Close()

// Create empty lockfile
lockFile := test.TmpFile(t, "")

cmd := NewRootCmd()
cmd.SetArgs([]string{"-f", test.TmpFile(t, ""), "add", fmt.Sprintf("http://localhost:%d/test.html", port)})
// Add cache URL to the command
cacheURL := fmt.Sprintf("http://localhost:%d", cachePort)
cmd.SetArgs([]string{
"-f", lockFile,
"add",
fmt.Sprintf("http://localhost:%d/test.html", port),
"--cache", cacheURL,
})

err := cmd.Execute()
assert.Nil(t, err)
}
1 change: 1 addition & 0 deletions cmd/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestRunDelete(t *testing.T) {
Tags = ['tag1', 'tag2']
`)
cmd := NewRootCmd()
cmd.Flags().String("cache", "", "Artifactory URL for caching")
cmd.SetArgs([]string{"-f", testfilepath, "delete", "http://localhost:123456/test.html"})
err := cmd.Execute()
assert.Nil(t, err)
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is poorly named, but let's not rename it as part of this PR -- let's rename it in a separate PR.

File renamed without changes.
139 changes: 139 additions & 0 deletions internal/artifactory_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// artifactory_test.go
package internal

import (
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"
"os"
"path/filepath"
"testing"

"github.com/cisco-open/grabit/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAddWithArtifactoryCache(t *testing.T) {
t.Run("TokenNotSet", func(t *testing.T) {
os.Unsetenv("GRABIT_ARTIFACTORY_TOKEN")

handler := func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`test content`))
}
port, server := test.HttpHandler(handler)
defer server.Close()

path := test.TmpFile(t, "")
lock, err := NewLock(path, true)
require.NoError(t, err)

// Use a testing server
sourceURL := fmt.Sprintf("http://localhost:%d/test.txt", port)
cacheURL := fmt.Sprintf("http://localhost:%d", port)

err = lock.AddResource([]string{sourceURL}, "sha256", []string{}, "", cacheURL)
assert.Contains(t, err.Error(), "GRABIT_ARTIFACTORY_TOKEN environment variable is not set")
})
}
func TestDownloadWithArtifactoryCache(t *testing.T) {
t.Run("NO_CACHE_UPLOAD", func(t *testing.T) {
// Turn on NO_CACHE_UPLOAD setting
os.Setenv("NO_CACHE_UPLOAD", "1")
Copy link
Contributor

@rabadin rabadin Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use https://pkg.go.dev/testing#B.Setenv instead. This guarantees that it gets restored after the test runs.

defer os.Unsetenv("NO_CACHE_UPLOAD")

testContent := []byte("test content")
hash := sha256.Sum256(testContent)
expectedHash := "sha256-" + base64.StdEncoding.EncodeToString(hash[:])

// Start a test server
uploadAttempted := false
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "PUT" {
uploadAttempted = true
t.Error("Should not attempt upload when NO_CACHE_UPLOAD is set")
}
w.Write(testContent)
}
port, server := test.HttpHandler(handler)
defer server.Close()

// Make a test folder
tmpDir := test.TmpDir(t)

// Make a test lock file with a cache URL and correct hash
lockContent := fmt.Sprintf(`[[Resource]]
Urls = ['http://localhost:%d/test.txt']
Integrity = '%s'
CacheUri = 'http://localhost:%d/cache'`, port, expectedHash, port)

// Set up the lock file
lockPath := test.TmpFile(t, lockContent)
lock, err := NewLock(lockPath, false)
require.NoError(t, err)

// Check the download process
err = lock.Download(tmpDir, []string{}, []string{}, "")
assert.NoError(t, err)

// Make sure no upload happened
assert.False(t, uploadAttempted)

// Make sure the file downloaded properly
downloadedFile := filepath.Join(tmpDir, "test.txt")
assert.FileExists(t, downloadedFile)

// Make sure the content is the same
content, err := os.ReadFile(downloadedFile)
require.NoError(t, err)
assert.Equal(t, testContent, content)
})
}

func TestDeleteWithArtifactoryCache(t *testing.T) {
t.Run("SuccessfulDelete", func(t *testing.T) {
os.Setenv("GRABIT_ARTIFACTORY_TOKEN", "test-token")
defer os.Unsetenv("GRABIT_ARTIFACTORY_TOKEN")

// Start the test server
handler := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "DELETE" {
w.WriteHeader(http.StatusOK)
}
}
port, server := test.HttpHandler(handler)
defer server.Close()

url := fmt.Sprintf("http://localhost:%d/test.txt", port)
cacheUrl := fmt.Sprintf("http://localhost:%d", port)

tmpDir := test.TmpDir(t)
lockContent := fmt.Sprintf(`[[Resource]]
Urls = ['%s']
Integrity = 'sha256-test'
CacheUri = '%s'`, url, cacheUrl)

lockPath := filepath.Join(tmpDir, "grabit.lock")
err := os.WriteFile(lockPath, []byte(lockContent), 0644)
require.NoError(t, err)

lock, err := NewLock(lockPath, false)
require.NoError(t, err)

// Keep the starting state
err = lock.Save()
require.NoError(t, err)

lock.DeleteResource(url)

// Keep the updates
err = lock.Save()
require.NoError(t, err)

// Make sure the resource was deleted
newLock, err := NewLock(lockPath, false)
require.NoError(t, err)
assert.Equal(t, 0, len(newLock.conf.Resource))
})
}
Loading
Loading