Repository navigation
🤖 fix: report an unreadable 2xx create answer as an uncertain 504 #241
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
aebc777
🤖 fix: report an unreadable 2xx create answer as an uncertain 504 (Re…
ThomasK33 0350910
🤖 fix: assert create-error arguments first and narrow the uncertain-c…
ThomasK33 52d51bc
🤖 docs: describe every 2xx create answer that maps to the uncertain 504
ThomasK33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/google/uuid" | ||
|
|
||
| apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apiserver/pkg/registry/rest" | ||
|
|
||
| aggregationv1alpha1 "github.com/coder/coder-k8s/api/aggregation/v1alpha1" | ||
| ) | ||
|
|
||
| // creator is the Create method of the workspace and template storages. | ||
| type creator interface { | ||
| Create(context.Context, runtime.Object, rest.ValidateObjectFunc, *metav1.CreateOptions) (runtime.Object, error) | ||
| } | ||
|
|
||
| // proxyAfterCreate fronts the mock Coder like a misbehaving proxy: Coder handles the create POST | ||
| // at createPath, but the client gets status instead of Coder's 201. With keepJSON the body is | ||
| // Coder's JSON answer, otherwise an HTML page. | ||
| func proxyAfterCreate(t *testing.T, coder *httptest.Server, createPath string, status int, keepJSON bool) *httptest.Server { | ||
| t.Helper() | ||
|
|
||
| proxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| if r.Method != http.MethodPost || r.URL.Path != createPath { | ||
| coder.Config.Handler.ServeHTTP(w, r) | ||
| return | ||
| } | ||
| recorder := httptest.NewRecorder() | ||
| coder.Config.Handler.ServeHTTP(recorder, r) | ||
| if recorder.Code != http.StatusCreated { | ||
| t.Errorf("mock Coder answered the create with %d: %s", recorder.Code, recorder.Body.String()) | ||
| } | ||
| if keepJSON { | ||
| w.Header().Set("Content-Type", recorder.Header().Get("Content-Type")) | ||
| w.WriteHeader(status) | ||
| _, _ = w.Write(recorder.Body.Bytes()) | ||
| return | ||
| } | ||
| w.Header().Set("Content-Type", "text/html") | ||
| w.WriteHeader(status) | ||
| _, _ = w.Write([]byte("<html>proxy page</html>")) | ||
| })) | ||
| t.Cleanup(proxy.Close) | ||
| return proxy | ||
| } | ||
|
|
||
| func TestCreateWithUnreadable2xxAnswerIsUncertain(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| for _, tc := range []struct { | ||
| name string | ||
| createPath string | ||
| storage func(t *testing.T, url string) creator | ||
| object runtime.Object | ||
| created func(state *mockCoderServerState) bool | ||
| }{ | ||
| { | ||
| name: "workspace", | ||
| createPath: "/api/v2/users/alice/workspaces", | ||
| storage: func(t *testing.T, url string) creator { | ||
| return NewWorkspaceStorage(newTestClientProvider(t, url)) | ||
| }, | ||
| object: &aggregationv1alpha1.CoderWorkspace{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "acme.alice.proxied"}, | ||
| Spec: aggregationv1alpha1.CoderWorkspaceSpec{Organization: "acme", TemplateName: "starter-template", Running: true}, | ||
| }, | ||
| created: func(state *mockCoderServerState) bool { return state.hasWorkspace("alice", "proxied") }, | ||
| }, | ||
| { | ||
| name: "template without files", | ||
| storage: func(t *testing.T, url string) creator { | ||
| return NewTemplateStorage(newTestClientProvider(t, url)) | ||
| }, | ||
| object: &aggregationv1alpha1.CoderTemplate{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "acme.proxied"}, | ||
| Spec: aggregationv1alpha1.CoderTemplateSpec{Organization: "acme", VersionID: uuid.NewString()}, | ||
| }, | ||
| created: func(state *mockCoderServerState) bool { | ||
| _, ok := state.templateActiveVersionID("acme", "proxied") | ||
| return ok | ||
| }, | ||
| }, | ||
| { | ||
| name: "template with files", | ||
| storage: func(t *testing.T, url string) creator { | ||
| return NewTemplateStorage(newTestClientProvider(t, url)) | ||
| }, | ||
| object: &aggregationv1alpha1.CoderTemplate{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "acme.proxied"}, | ||
| Spec: aggregationv1alpha1.CoderTemplateSpec{ | ||
| Organization: "acme", | ||
| Files: map[string]string{"main.tf": `resource "null_resource" "proxied" {}`}, | ||
| }, | ||
| }, | ||
| created: func(state *mockCoderServerState) bool { | ||
| _, ok := state.templateActiveVersionID("acme", "proxied") | ||
| return ok | ||
| }, | ||
| }, | ||
| } { | ||
| for _, answer := range []struct { | ||
| status int | ||
| keepJSON bool | ||
| }{{http.StatusOK, false}, {http.StatusCreated, false}, {http.StatusOK, true}} { | ||
| status := answer.status | ||
| coderServer, state := newMockCoderServer(t) | ||
| t.Cleanup(coderServer.Close) | ||
| createPath := tc.createPath | ||
| if createPath == "" { | ||
| createPath = "/api/v2/organizations/" + state.organization.ID.String() + "/templates" | ||
| } | ||
| proxy := proxyAfterCreate(t, coderServer, createPath, status, answer.keepJSON) | ||
| storage := tc.storage(t, proxy.URL) | ||
|
|
||
| _, err := storage.Create(namespacedContext("control-plane"), tc.object.DeepCopyObject(), rest.ValidateAllObjectFunc, nil) | ||
| if !apierrors.IsTimeout(err) || !strings.Contains(err.Error(), "uncertain") || strings.Contains(err.Error(), proxy.URL) { | ||
| t.Fatalf("%s, proxy answer %+v: expected an uncertain 504 without the Coder URL, got %v", tc.name, answer, err) | ||
| } | ||
| if !tc.created(state) { | ||
| t.Fatalf("%s, proxy answer %+v: the mock Coder must have created the object", tc.name, answer) | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.