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

Improve test #280

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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