-
Notifications
You must be signed in to change notification settings - Fork 4
fix(scheduler): try PUT before GET for schedule pause/resume (fixes #101) #107
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
Draft
ambiorix2099
wants to merge
2
commits into
main
Choose a base branch
from
fix/scheduler-pause-resume-verb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /* | ||
| * Copyright 2026 Conductor Authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/conductor-sdk/conductor-go/sdk/client" | ||
| "github.com/conductor-sdk/conductor-go/sdk/settings" | ||
|
|
||
| "github.com/conductor-oss/conductor-cli/internal" | ||
| ) | ||
|
|
||
| // The three deployment shapes setSchedulePaused has to satisfy cannot be covered by | ||
| // any single live server, so they are modelled here as stub handlers. Each records | ||
| // the verbs it received so the test can assert not just success but that the client | ||
| // converged on PUT and only fell back when it had to. | ||
|
|
||
| type verbRecorder struct { | ||
| methods []string | ||
| } | ||
|
|
||
| func (v *verbRecorder) handler(allow map[string]int) http.HandlerFunc { | ||
| return func(w http.ResponseWriter, r *http.Request) { | ||
| v.methods = append(v.methods, r.Method) | ||
| if code, ok := allow[r.Method]; ok { | ||
| w.WriteHeader(code) | ||
| return | ||
| } | ||
| w.WriteHeader(http.StatusMethodNotAllowed) | ||
| } | ||
| } | ||
|
|
||
| func withStubServer(t *testing.T, h http.Handler) { | ||
| t.Helper() | ||
| srv := httptest.NewServer(h) | ||
| t.Cleanup(srv.Close) | ||
| internal.SetAPIClient(client.NewAPIClient( | ||
| settings.NewAuthenticationSettings("", ""), | ||
| settings.NewHttpSettings(srv.URL+"/api"), | ||
| )) | ||
| } | ||
|
|
||
| func TestSetSchedulePausedVerbNegotiation(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| allow map[string]int | ||
| wantMethods []string | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| // Upstream OSS Conductor: PUT only. One request, no fallback needed. | ||
| name: "oss accepts put", | ||
| allow: map[string]int{http.MethodPut: http.StatusOK}, | ||
| wantMethods: []string{http.MethodPut}, | ||
| }, | ||
| { | ||
| // Orkes >= 2026-07-14 accepts both; PUT must win so we converge on it. | ||
| name: "orkes accepts both, put is preferred", | ||
| allow: map[string]int{ | ||
| http.MethodPut: http.StatusOK, | ||
| http.MethodGet: http.StatusOK, | ||
| }, | ||
| wantMethods: []string{http.MethodPut}, | ||
| }, | ||
| { | ||
| // Legacy Orkes: GET only. PUT is rejected 405, then GET succeeds. | ||
| name: "legacy orkes falls back to get", | ||
| allow: map[string]int{http.MethodGet: http.StatusOK}, | ||
| wantMethods: []string{http.MethodPut, http.MethodGet}, | ||
| }, | ||
| { | ||
| // Neither verb works: surface the failure rather than reporting success. | ||
| name: "both rejected with 405 returns error", | ||
| allow: map[string]int{}, | ||
| wantMethods: []string{http.MethodPut, http.MethodGet}, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| // 405 is the only status meaning "wrong verb". A 404 means the schedule — | ||
| // or the whole scheduler module — is absent, so retrying with GET would | ||
| // produce a second 404 and report that instead of the real cause. | ||
| name: "404 does not trigger the fallback", | ||
| allow: map[string]int{http.MethodPut: http.StatusNotFound}, | ||
| wantMethods: []string{http.MethodPut}, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| // Likewise for auth failures: a fallback would just repeat the rejection. | ||
| name: "401 does not trigger the fallback", | ||
| allow: map[string]int{http.MethodPut: http.StatusUnauthorized}, | ||
| wantMethods: []string{http.MethodPut}, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| rec := &verbRecorder{} | ||
| withStubServer(t, rec.handler(tt.allow)) | ||
|
|
||
| err := setSchedulePaused(context.Background(), "probe", "pause") | ||
| if tt.wantErr && err == nil { | ||
| t.Fatal("expected an error, got nil") | ||
| } | ||
| if !tt.wantErr && err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if len(rec.methods) != len(tt.wantMethods) { | ||
| t.Fatalf("methods = %v, want %v", rec.methods, tt.wantMethods) | ||
| } | ||
| for i, m := range tt.wantMethods { | ||
| if rec.methods[i] != m { | ||
| t.Fatalf("methods = %v, want %v", rec.methods, tt.wantMethods) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // A 5xx must NOT trigger the fallback. Retrying a server fault with a different verb | ||
| // would mask it and surface a misleading "method not supported" instead. | ||
| func TestSetSchedulePausedDoesNotFallBackOnServerError(t *testing.T) { | ||
| rec := &verbRecorder{} | ||
| withStubServer(t, rec.handler(map[string]int{ | ||
| http.MethodPut: http.StatusInternalServerError, | ||
| http.MethodGet: http.StatusOK, | ||
| })) | ||
|
|
||
| if err := setSchedulePaused(context.Background(), "probe", "pause"); err == nil { | ||
| t.Fatal("expected the 500 to be returned, got nil") | ||
| } | ||
| if len(rec.methods) != 1 || rec.methods[0] != http.MethodPut { | ||
| t.Fatalf("methods = %v, want a single PUT with no GET fallback", rec.methods) | ||
| } | ||
| } | ||
|
|
||
| // The schedule name is interpolated into the path and must be escaped. | ||
| func TestSetSchedulePausedEscapesName(t *testing.T) { | ||
| var gotPath string | ||
| withStubServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| gotPath = r.URL.EscapedPath() | ||
| w.WriteHeader(http.StatusOK) | ||
| })) | ||
|
|
||
| if err := setSchedulePaused(context.Background(), "needs escaping", "resume"); err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if want := "/api/scheduler/schedules/needs%20escaping/resume"; gotPath != want { | ||
| t.Fatalf("path = %q, want %q", gotPath, want) | ||
| } | ||
| } |
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.
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.
if you are making these changes conductor-oss/go-sdk#276, let's go with that instead of
setSchedulePaused.