CLI to operate archives managed by Storage Service (#2450)

* Add API for listing storage service archives
If id is mentioned we allow user to download specific
archive. If id is not mentioned we list all archives
present with storage service.
This will allow us to build CLI with storage service
and help users to debug storage service.
* Added commands for storagesvc cli and functionalities
* Reusing code and added geturl and download.
* Fixed geturl for localstorage.
* Fixed description of fission archive command
* Added unit tests for function getstorageurl
* Added integration test for archive cli




Co-authored-by: Sanket Sudake <sanketsudake@gmail.com>
This commit is contained in:
Ankit Chawla
2022-06-15 18:53:36 +05:30
committed by GitHub
co-authored by Sanket Sudake
parent 7f21b708e7
commit 21ea35b02a
17 changed files with 636 additions and 31 deletions
+27
View File
@@ -123,6 +123,33 @@ func (c *Client) GetUrl(id string) string {
return fmt.Sprintf("%v/archive?id=%v", c.url, url.PathEscape(id))
}
func (c *Client) List(ctx context.Context) ([]string, error) {
req, err := http.NewRequest(http.MethodGet, c.url+"/archive", nil)
if err != nil {
return []string{}, err
}
resp, err := ctxhttp.Do(ctx, c.httpClient, req)
if err != nil {
return []string{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return []string{}, err
}
if resp.StatusCode != http.StatusOK {
msg := fmt.Sprintf("List error %v", resp.Status)
return []string{}, errors.New(msg)
}
var ids []string
err = json.Unmarshal(body, &ids)
if err != nil {
return []string{}, err
}
return ids, nil
}
// Download fetches the file identified by ID to the local file path.
// filePath must not exist.
func (c *Client) Download(ctx context.Context, id string, filePath string) error {
+40 -25
View File
@@ -42,20 +42,25 @@ const (
minioRegion = "ap-south-1"
)
func panicIf(err error) {
func failTest(t *testing.T, err error) {
if err != nil {
log.Panicf("Error: %v", err)
t.Fatalf("%v", err)
}
}
func MakeTestFile(size int) *os.File {
func MakeTestFile(size int) (*os.File, error) {
f, err := os.CreateTemp("", "storagesvc_test_")
panicIf(err)
if err != nil {
return nil, err
}
_, err = f.Write(bytes.Repeat([]byte("."), size))
panicIf(err)
return f
if err != nil {
return nil, err
}
return f, nil
}
func runMinioDockerContainer(pool *dockertest.Pool) *dockertest.Resource {
@@ -120,7 +125,7 @@ func TestS3StorageService(t *testing.T) {
config := zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logger, err := config.Build()
panicIf(err)
failTest(t, err)
log.Println("starting storage svc")
os.Setenv("STORAGE_S3_ENDPOINT", endpoint)
@@ -139,27 +144,28 @@ func TestS3StorageService(t *testing.T) {
client := MakeClient(fmt.Sprintf("http://localhost:%v/", 8081))
// generate a test file
tmpfile := MakeTestFile(10 * 1024)
tmpfile, err := MakeTestFile(10 * 1024)
failTest(t, err)
defer os.Remove(tmpfile.Name())
// store it
metadata := make(map[string]string)
fileID, err := client.Upload(ctx, tmpfile.Name(), &metadata)
panicIf(err)
failTest(t, err)
time.Sleep(10 * time.Second)
// Retrieve file through minioClient
reader, err := minioClient.GetObject(bucketName, fileID, minio.GetObjectOptions{})
panicIf(err)
failTest(t, err)
defer reader.Close()
retThroughMinio, err := os.CreateTemp("", "storagesvc_verify_minio_")
panicIf(err)
failTest(t, err)
defer os.Remove(retThroughMinio.Name())
stat, err := reader.Stat()
panicIf(err)
failTest(t, err)
if _, err := io.CopyN(retThroughMinio, reader, stat.Size); err != nil {
log.Fatalln(err)
@@ -167,25 +173,25 @@ func TestS3StorageService(t *testing.T) {
// Retrieve file through API
retThroughAPI, err := os.CreateTemp("", "storagesvc_verify_")
panicIf(err)
failTest(t, err)
os.Remove(retThroughAPI.Name())
err = client.Download(ctx, fileID, retThroughAPI.Name())
panicIf(err)
failTest(t, err)
defer os.Remove(retThroughAPI.Name())
// compare contents
contentsMinio, err := os.ReadFile(retThroughMinio.Name())
panicIf(err)
failTest(t, err)
contentsAPI, err := os.ReadFile(retThroughAPI.Name())
panicIf(err)
failTest(t, err)
if !bytes.Equal(contentsMinio, contentsAPI) {
log.Panic("Contents don't match")
}
// delete uploaded file
err = client.Delete(ctx, fileID)
panicIf(err)
failTest(t, err)
// make sure download fails
err = client.Download(ctx, fileID, "xxx")
@@ -201,7 +207,7 @@ func TestLocalStorageService(t *testing.T) {
config := zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logger, err := config.Build()
panicIf(err)
failTest(t, err)
log.Println("starting storage svc")
localPath := fmt.Sprintf("/tmp/%v", testID)
@@ -216,36 +222,45 @@ func TestLocalStorageService(t *testing.T) {
client := MakeClient(fmt.Sprintf("http://localhost:%v/", port))
// generate a test file
tmpfile := MakeTestFile(10 * 1024)
tmpfile, err := MakeTestFile(10 * 1024)
failTest(t, err)
defer os.Remove(tmpfile.Name())
// store it
metadata := make(map[string]string)
fileID, err := client.Upload(ctx, tmpfile.Name(), &metadata)
panicIf(err)
failTest(t, err)
ids, err := client.List(ctx)
if err != nil {
t.Fatalf("Could not list files: %s", err)
}
if len(ids) != 1 {
t.Fatalf("Expected 1 file, got %v", len(ids))
}
// make a temp file for verification
retrievedfile, err := os.CreateTemp("", "storagesvc_verify_")
panicIf(err)
failTest(t, err)
os.Remove(retrievedfile.Name())
// retrieve uploaded file
err = client.Download(ctx, fileID, retrievedfile.Name())
panicIf(err)
failTest(t, err)
defer os.Remove(retrievedfile.Name())
// compare contents
contents1, err := os.ReadFile(tmpfile.Name())
panicIf(err)
failTest(t, err)
contents2, err := os.ReadFile(retrievedfile.Name())
panicIf(err)
failTest(t, err)
if !bytes.Equal(contents1, contents2) {
log.Panic("Contents don't match")
}
// delete uploaded file
err = client.Delete(ctx, fileID)
panicIf(err)
failTest(t, err)
// make sure download fails
err = client.Download(ctx, fileID, "xxx")