-
Notifications
You must be signed in to change notification settings - Fork 1
fix(wfctl): fix Spaces bucket bootstrap to use correct DO API endpoint #424
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestBootstrapDOSpacesBucket_AlreadyExists(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method == http.MethodGet { | ||
| w.WriteHeader(http.StatusOK) | ||
| return | ||
| } | ||
| http.Error(w, "unexpected method", http.StatusInternalServerError) | ||
| })) | ||
|
Comment on lines
+12
to
+18
|
||
| defer srv.Close() | ||
|
|
||
| t.Setenv("DIGITALOCEAN_TOKEN", "test-token") | ||
| if err := bootstrapDOSpacesBucketAt(context.Background(), "my-bucket", "nyc3", srv.URL); err != nil { | ||
| t.Fatalf("expected no error when bucket already exists, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestBootstrapDOSpacesBucket_CreatesNew(t *testing.T) { | ||
| var postCalled bool | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| switch r.Method { | ||
| case http.MethodGet: | ||
| w.WriteHeader(http.StatusNotFound) | ||
| case http.MethodPost: | ||
| postCalled = true | ||
| w.WriteHeader(http.StatusCreated) | ||
| default: | ||
| http.Error(w, "unexpected: "+r.Method, http.StatusInternalServerError) | ||
| } | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| t.Setenv("DIGITALOCEAN_TOKEN", "test-token") | ||
| if err := bootstrapDOSpacesBucketAt(context.Background(), "my-bucket", "nyc3", srv.URL); err != nil { | ||
| t.Fatalf("expected no error creating new bucket, got: %v", err) | ||
| } | ||
| if !postCalled { | ||
| t.Error("expected POST to be called when bucket does not exist") | ||
| } | ||
|
Comment on lines
+28
to
+48
|
||
| } | ||
|
|
||
| func TestBootstrapDOSpacesBucket_CreateErrorIncludesBody(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| switch r.Method { | ||
| case http.MethodGet: | ||
| w.WriteHeader(http.StatusNotFound) | ||
| case http.MethodPost: | ||
| w.Header().Set("Content-Type", "application/json") | ||
| w.WriteHeader(http.StatusUnprocessableEntity) | ||
| w.Write([]byte(`{"id":"unprocessable_entity","message":"region is invalid"}`)) //nolint:errcheck | ||
| default: | ||
| http.Error(w, "unexpected: "+r.Method, http.StatusInternalServerError) | ||
| } | ||
| })) | ||
| defer srv.Close() | ||
|
|
||
| t.Setenv("DIGITALOCEAN_TOKEN", "test-token") | ||
| err := bootstrapDOSpacesBucketAt(context.Background(), "bad-bucket", "invalid-region", srv.URL) | ||
| if err == nil { | ||
| t.Fatal("expected error on 4xx create response") | ||
| } | ||
| if !strings.Contains(err.Error(), "region is invalid") { | ||
| t.Errorf("expected response body in error, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestBootstrapDOSpacesBucket_MissingToken(t *testing.T) { | ||
| t.Setenv("DIGITALOCEAN_TOKEN", "") | ||
| err := bootstrapDOSpacesBucketAt(context.Background(), "my-bucket", "nyc3", "http://unused") | ||
| if err == nil { | ||
| t.Fatal("expected error when DIGITALOCEAN_TOKEN unset") | ||
| } | ||
| if !strings.Contains(err.Error(), "DIGITALOCEAN_TOKEN") { | ||
| t.Errorf("expected DIGITALOCEAN_TOKEN in error, got: %v", err) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On create failures, io.ReadAll(createResp.Body) reads the full response body without bounds and ignores the read error. A large/streaming error response could cause excessive memory usage or hang. Consider reading with a size limit (e.g., io.LimitReader) and handling the read error so the returned message is reliable.