Repository navigation
fix(cmd): exit non-zero and count Savings Plan failures after a purchase run #2238
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
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,176 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/LeanerCloud/cloud-commitments-go/pkg/common" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // spHookFailing answers the Savings Plan calls like spHook, but fails the | ||
| // CreateSavingsPlan call for the given offering IDs. Every other call (the | ||
| // EC2 RI API included) gets a 400, so an RI purchase through it fails. | ||
| func spHookFailing(failOfferings ...string) *hookTransport { | ||
| h := &hookTransport{} | ||
| h.handle = func(r *http.Request) (int, string) { | ||
| switch { | ||
| case strings.HasSuffix(r.URL.Path, "/CreateSavingsPlan"): | ||
| body, _ := io.ReadAll(r.Body) | ||
| for _, id := range failOfferings { | ||
| if strings.Contains(string(body), `"`+id+`"`) { | ||
| return http.StatusBadRequest, `{"__type":"ValidationException","message":"offering rejected"}` | ||
| } | ||
| } | ||
| return http.StatusOK, `{"savingsPlanId":"sp-111"}` | ||
| case strings.HasSuffix(r.URL.Path, "/DescribeSavingsPlans"): | ||
| return http.StatusOK, `{"savingsPlans":[]}` | ||
| } | ||
| return http.StatusBadRequest, `{"__type":"ValidationException","message":"unexpected call"}` | ||
| } | ||
| return h | ||
| } | ||
|
|
||
| func TestExecuteAndReport_SavingsPlanFailureFailsRun(t *testing.T) { | ||
| t.Setenv("DISABLE_PURCHASE_DELAY", "true") | ||
| out := captureAppLog(t) | ||
| cfg := auditCfg(t) | ||
| h := spHookFailing("o-2") | ||
| recs := []common.Recommendation{offeringSP("o-1", 1), offeringSP("o-2", 2)} | ||
|
|
||
| err := executeAndReport(context.Background(), hookedAWSConfig(h), recs, false, "run-1", cfg, nil) | ||
|
|
||
| require.ErrorIs(t, err, errPurchasesFailed) | ||
| assert.NotErrorIs(t, err, errAuditTrailLost) | ||
| assert.Contains(t, err.Error(), "1 of 2") | ||
| assert.Equal(t, 2, h.count("/CreateSavingsPlan")) | ||
| assert.Contains(t, out.String(), "Purchases: 1 succeeded, 1 failed") | ||
| assert.Contains(t, out.String(), "1 of 2 purchase(s) failed") | ||
| } | ||
|
|
||
| func TestExecuteAndReport_AllSavingsPlansFailFailsRun(t *testing.T) { | ||
| t.Setenv("DISABLE_PURCHASE_DELAY", "true") | ||
| out := captureAppLog(t) | ||
| cfg := auditCfg(t) | ||
| h := spHookFailing("o-1", "o-2") | ||
| recs := []common.Recommendation{offeringSP("o-1", 1), offeringSP("o-2", 2)} | ||
|
|
||
| err := executeAndReport(context.Background(), hookedAWSConfig(h), recs, false, "run-1", cfg, nil) | ||
|
|
||
| require.ErrorIs(t, err, errPurchasesFailed) | ||
| assert.Contains(t, out.String(), "Purchases: 0 succeeded, 2 failed") | ||
| assert.NotContains(t, out.String(), "Purchase operations completed") | ||
| } | ||
|
|
||
| func TestExecuteAndReport_SavingsPlanAndRIFailuresCountedTogether(t *testing.T) { | ||
| t.Setenv("DISABLE_PURCHASE_DELAY", "true") | ||
| out := captureAppLog(t) | ||
| cfg := auditCfg(t) | ||
| h := spHookFailing("o-2") | ||
| recs := []common.Recommendation{offeringSP("o-1", 1), offeringSP("o-2", 2), validEC2Rec()} | ||
|
|
||
| err := executeAndReport(context.Background(), hookedAWSConfig(h), recs, false, "run-1", cfg, nil) | ||
|
|
||
| require.ErrorIs(t, err, errPurchasesFailed) | ||
| assert.Contains(t, err.Error(), "2 of 3") | ||
| assert.Contains(t, out.String(), "Purchases: 1 succeeded, 2 failed") | ||
| assert.Contains(t, out.String(), "Overall success rate: 33.3%") | ||
| } | ||
|
|
||
| func TestExecuteAndReport_AllSavingsPlansSucceedReturnsNil(t *testing.T) { | ||
| t.Setenv("DISABLE_PURCHASE_DELAY", "true") | ||
| out := captureAppLog(t) | ||
| cfg := auditCfg(t) | ||
| h := spHookFailing() | ||
| recs := []common.Recommendation{offeringSP("o-1", 1), offeringSP("o-2", 2)} | ||
|
|
||
| err := executeAndReport(context.Background(), hookedAWSConfig(h), recs, false, "run-1", cfg, nil) | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Contains(t, out.String(), "Purchases: 2 succeeded, 0 failed") | ||
| assert.Contains(t, out.String(), "Purchase operations completed") | ||
| assert.NotContains(t, out.String(), "purchase(s) failed") | ||
| } | ||
|
|
||
| func TestExecuteAndReport_DryRunPreconditionFailureReturnsNil(t *testing.T) { | ||
| out := captureAppLog(t) | ||
| cfg := auditCfg(t) | ||
| h := spHookFailing() | ||
| bad := offeringSP("o-1", 1) | ||
| bad.Details = nil // fails the purchase preconditions | ||
|
|
||
| err := executeAndReport(context.Background(), hookedAWSConfig(h), []common.Recommendation{bad}, true, "run-1", cfg, nil) | ||
|
|
||
| require.NoError(t, err, "a dry run buys nothing, so it does not fail the run") | ||
| assert.Zero(t, h.count("/CreateSavingsPlan")) | ||
| assert.Contains(t, out.String(), "cannot purchase", "the precondition failure is still reported") | ||
| } | ||
|
|
||
| func TestRunCSVPurchases_SavingsPlanFailureFailsRun(t *testing.T) { | ||
| t.Setenv("DISABLE_PURCHASE_DELAY", "true") | ||
| out := captureAppLog(t) | ||
| cfg := auditCfg(t) | ||
| h := spHookFailing("o-2") | ||
| recs := []common.Recommendation{offeringSP("o-1", 1), offeringSP("o-2", 2)} | ||
|
|
||
| err := runCSVPurchases(context.Background(), hookedAWSConfig(h), recs, false, cfg, "run-1") | ||
|
|
||
| require.ErrorIs(t, err, errPurchasesFailed) | ||
| assert.Contains(t, err.Error(), "1 of 2") | ||
| assert.Contains(t, out.String(), "Purchases: 1 succeeded, 1 failed") | ||
| } | ||
|
|
||
| func TestRunCSVPurchases_AllSucceedReturnsNil(t *testing.T) { | ||
| t.Setenv("DISABLE_PURCHASE_DELAY", "true") | ||
| out := captureAppLog(t) | ||
| cfg := auditCfg(t) | ||
| h := spHookFailing() | ||
| recs := []common.Recommendation{offeringSP("o-1", 1), offeringSP("o-2", 2)} | ||
|
|
||
| err := runCSVPurchases(context.Background(), hookedAWSConfig(h), recs, false, cfg, "run-1") | ||
|
|
||
| require.NoError(t, err) | ||
| assert.Contains(t, out.String(), "Purchases: 2 succeeded, 0 failed") | ||
| } | ||
|
|
||
| func TestPrintMultiServiceSummary_CountsSavingsPlanFailures(t *testing.T) { | ||
| stats := map[common.ServiceType]ServiceProcessingStats{ | ||
| common.ServiceEC2: {Service: common.ServiceEC2, RecommendationsSelected: 2, SuccessfulPurchases: 1, FailedPurchases: 1}, | ||
| common.ServiceSavingsPlansCompute: {Service: common.ServiceSavingsPlansCompute, RecommendationsSelected: 2, FailedPurchases: 2}, | ||
| common.ServiceSavingsPlansEC2Instance: {Service: common.ServiceSavingsPlansEC2Instance, RecommendationsSelected: 1, SuccessfulPurchases: 1}, | ||
| } | ||
|
|
||
| output := captureAppOutput(t, func() { | ||
| printMultiServiceSummary(nil, nil, stats, false) | ||
| }) | ||
|
|
||
| assert.Contains(t, output, "Purchases: 2 succeeded, 3 failed") | ||
| assert.Contains(t, output, "Overall success rate: 40.0%") | ||
| assert.Contains(t, output, "3 of 5 purchase(s) failed") | ||
| } | ||
|
|
||
| func TestPrintFinalMessage_SavingsPlanOnlySuccessShowsCompletion(t *testing.T) { | ||
| output := captureAppOutput(t, func() { | ||
| printMultiServiceSummary(nil, nil, map[common.ServiceType]ServiceProcessingStats{ | ||
| common.ServiceSavingsPlansCompute: {Service: common.ServiceSavingsPlansCompute, RecommendationsSelected: 1, SuccessfulPurchases: 1}, | ||
| }, false) | ||
| }) | ||
|
|
||
| assert.Contains(t, output, "Purchase operations completed") | ||
| assert.NotContains(t, output, "purchase(s) failed") | ||
| } | ||
|
|
||
| func TestPurchaseFailureError(t *testing.T) { | ||
| ok := common.PurchaseResult{Success: true} | ||
| bad := common.PurchaseResult{Success: false} | ||
| assert.NoError(t, purchaseFailureError(nil, false)) | ||
| assert.NoError(t, purchaseFailureError([]common.PurchaseResult{ok, ok}, false)) | ||
| assert.NoError(t, purchaseFailureError([]common.PurchaseResult{bad}, true), "dry run") | ||
| err := purchaseFailureError([]common.PurchaseResult{ok, bad, bad}, false) | ||
| require.ErrorIs(t, err, errPurchasesFailed) | ||
| assert.Contains(t, err.Error(), "2 of 3") | ||
| } |
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.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Remove RI-specific guidance from Savings Plan-only runs.
When only a Savings Plan purchase succeeds, this branch prints “Allow up to 15 minutes for RIs to appear in your account.” No RI was purchased. Show that message only after an RI success, or use guidance that applies to both purchase types.
🤖 Prompt for AI Agents
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.
Confirmed: the 15-minute RI note also prints after a Savings-Plan-only success. It is wording only, with no effect on counts, exit code or purchases, so it does not block merge under this repo's gate (block only on correctness, security or data loss). It is left as a follow-up for @IAmJSD: print the RI line only when
riAggregates.success > 0. (claude-cc-reviewer, independent reviewer)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.
Fixed in 9c24083.
printFinalMessagenow takes the RI success count (riAggregates.success) and prints the RI propagation hint only when at least one RI purchase succeeded. The completion message and the Archera pitch still appear for any success, Savings Plan only included.New test:
TestPrintMultiServiceSummary_RIHintOnlyAfterRISuccess.Evidence:
6b7ad637the two SP cases failed. With this commit they all pass.go build,go vet ./...,go test ./... -count=1(withAWS_CONFIG_FILE=/dev/null AWS_SHARED_CREDENTIALS_FILE=/dev/null), golangci-lint v2.10.1 (0 issues),gocyclo -over 10and gofmt all pass.🤖 Generated with Claude Code
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.