From 7bcb9425632fcdb53b3073fb89f7855f44643ab1 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 14 Jan 2025 10:16:09 +0100 Subject: [PATCH 1/2] Improve test --- pkg/handlers/create_test.go | 109 ++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 49 deletions(-) diff --git a/pkg/handlers/create_test.go b/pkg/handlers/create_test.go index 1a8039c6..cc24519a 100644 --- a/pkg/handlers/create_test.go +++ b/pkg/handlers/create_test.go @@ -37,7 +37,7 @@ func TestMakeCreateHandler(t *testing.T) { // Create a fake MinIO server server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, hreq *http.Request) { - if hreq.URL.Path != "/test" && hreq.URL.Path != "/test/input/" && hreq.URL.Path != "/test/output/" && hreq.URL.Path != "/test/mount/" && !strings.HasPrefix(hreq.URL.Path, "/minio/admin/v3/") { + if hreq.URL.Path != "/test" && hreq.URL.Path != "/test/input/" && hreq.URL.Path != "/test/output/" && hreq.URL.Path != "/test/mount/" && !strings.HasPrefix(hreq.URL.Path, "/minio/admin/v3/") && !strings.HasPrefix(hreq.URL.Path, "/test-somelongui") { t.Errorf("Unexpected path in request, got: %s", hreq.URL.Path) } @@ -68,56 +68,67 @@ func TestMakeCreateHandler(t *testing.T) { }) r.POST("/system/services", MakeCreateHandler(&cfg, back)) - w := httptest.NewRecorder() - body := strings.NewReader(` - { - "name": "cowsay", - "cluster_id": "oscar", - "memory": "1Gi", - "cpu": "1.0", - "log_level": "CRITICAL", - "image": "ghcr.io/grycap/cowsay", - "alpine": false, - "script": "test", - "input": [ - { - "storage_provider": "minio", - "path": "/test/input/" - } - ], - "output": [ - { - "storage_provider": "minio", - "path": "/test/output" - } - ], - "mount": { - "storage_provider": "minio", - "path": "/test/mount" - }, - "storage_providers": { - "webdav": { - "id": { - "hostname": "` + server.URL + `", - "login": "user", - "password": "pass" - } - } - }, - "isolation_level": "SERVICE", - "bucket_list": [], - "allowed_users": ["somelonguid@egi.eu", "somelonguid2@egi.eu"] - } - `) + scenarios := []struct { + name string + isolationLevel string + }{ + {"Service", "SERVICE"}, + {"User", "USER"}, + } - req, _ := http.NewRequest("POST", "/system/services", body) - req.Header.Add("Authorization", "Bearer token") - r.ServeHTTP(w, req) + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + w := httptest.NewRecorder() + body := strings.NewReader(` + { + "name": "cowsay", + "cluster_id": "oscar", + "memory": "1Gi", + "cpu": "1.0", + "log_level": "CRITICAL", + "image": "ghcr.io/grycap/cowsay", + "alpine": false, + "script": "test", + "input": [ + { + "storage_provider": "minio", + "path": "/test/input/" + } + ], + "output": [ + { + "storage_provider": "minio", + "path": "/test/output" + } + ], + "mount": { + "storage_provider": "minio", + "path": "/test/mount" + }, + "storage_providers": { + "webdav": { + "id": { + "hostname": "` + server.URL + `", + "login": "user", + "password": "pass" + } + } + }, + "isolation_level": "` + s.isolationLevel + `", + "bucket_list": [], + "allowed_users": ["somelonguid@egi.eu", "somelonguid2@egi.eu"] + }`) + + req, _ := http.NewRequest("POST", "/system/services", body) + req.Header.Add("Authorization", "Bearer token") + r.ServeHTTP(w, req) + + if w.Code != http.StatusCreated { + t.Errorf("expecting code %d, got %d", http.StatusCreated, w.Code) + } + }) + } // Close the fake MinIO server defer server.Close() - - if w.Code != http.StatusCreated { - t.Errorf("expecting code %d, got %d", http.StatusCreated, w.Code) - } } From e75ff62a49d4e639f7a2a45c61c9aac7cf666c1a Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Tue, 14 Jan 2025 11:32:33 +0100 Subject: [PATCH 2/2] Improve test --- pkg/handlers/delete_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/handlers/delete_test.go b/pkg/handlers/delete_test.go index 2f6bb0ca..25c580ce 100644 --- a/pkg/handlers/delete_test.go +++ b/pkg/handlers/delete_test.go @@ -20,6 +20,28 @@ func TestMakeDeleteHandler(t *testing.T) { if hreq.URL.Path != "/input" && hreq.URL.Path != "/output" && !strings.HasPrefix(hreq.URL.Path, "/minio/admin/v3/") { t.Errorf("Unexpected path in request, got: %s", hreq.URL.Path) } + if hreq.URL.Path == "/minio/admin/v3/info" { + rw.WriteHeader(http.StatusOK) + rw.Write([]byte(`{"Mode": "local", "Region": "us-east-1"}`)) + } else if hreq.URL.Path == "/minio/admin/v3/info-canned-policy" { + rw.WriteHeader(http.StatusOK) + rw.Write([]byte(`{ + "PolicyName": "input", + "Policy": { + "Version": "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": ["s3:GetObject"], + "Resource": ["arn:aws:s3:::example-bucket/*"] + } + ] + } + }`)) + } else { + rw.WriteHeader(http.StatusOK) + rw.Write([]byte(`{"status": "success"}`)) + } })) // and set the MinIO endpoint to the fake server @@ -41,6 +63,8 @@ func TestMakeDeleteHandler(t *testing.T) { Output: []types.StorageIOConfig{ {Provider: "minio." + types.DefaultProvider, Path: "/output"}, }, + IsolationLevel: "USER", + AllowedUsers: []string{"somelonguid1@egi.eu"}, StorageProviders: &types.StorageProviders{ MinIO: map[string]*types.MinIOProvider{types.DefaultProvider: { Region: "us-east-1",