Skip to content

Commit

Permalink
Merge pull request #280 from grycap/tests
Browse files Browse the repository at this point in the history
Improve test
  • Loading branch information
micafer authored Jan 14, 2025
2 parents 23742eb + e75ff62 commit 0d6ea32
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 49 deletions.
109 changes: 60 additions & 49 deletions pkg/handlers/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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": ["[email protected]", "[email protected]"]
}
`)
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": ["[email protected]", "[email protected]"]
}`)

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)
}
}
24 changes: 24 additions & 0 deletions pkg/handlers/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -41,6 +63,8 @@ func TestMakeDeleteHandler(t *testing.T) {
Output: []types.StorageIOConfig{
{Provider: "minio." + types.DefaultProvider, Path: "/output"},
},
IsolationLevel: "USER",
AllowedUsers: []string{"[email protected]"},
StorageProviders: &types.StorageProviders{
MinIO: map[string]*types.MinIOProvider{types.DefaultProvider: {
Region: "us-east-1",
Expand Down

0 comments on commit 0d6ea32

Please sign in to comment.