Repository navigation
fix(aws/ec2): refuse an offering priced above the approved upfront #611
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
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,86 @@ | ||
| package ec2 | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/aws/aws-sdk-go-v2/aws" | ||
| "github.com/aws/aws-sdk-go-v2/service/ec2" | ||
| "github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/LeanerCloud/cloud-commitments-go/pkg/common" | ||
| ) | ||
|
|
||
| // pricedRec is a 2-instance partial-upfront rec approved at a $1000 total upfront. | ||
| func pricedRec() common.Recommendation { | ||
| r := matchRec() | ||
| r.Count = 2 | ||
| r.PaymentOption = "partial-upfront" | ||
| r.CommitmentCost = 1000 | ||
| return r | ||
| } | ||
|
|
||
| func pricedOffering(fixed float32) types.ReservedInstancesOffering { | ||
| return fullOffering("priced", "", func(o *types.ReservedInstancesOffering) { | ||
| o.OfferingType = types.OfferingTypeValuesPartialUpfront | ||
| o.FixedPrice = aws.Float32(fixed) | ||
| o.CurrencyCode = types.CurrencyCodeValuesUsd | ||
| }) | ||
| } | ||
|
|
||
| func TestPurchaseCommitment_RefusesOfferingAboveApprovedUpfront(t *testing.T) { | ||
| t.Parallel() | ||
| // 2 x 600 = 1200 against an approved 1000 (ceiling 1050). | ||
| m := offeringsMock(pricedOffering(600)) | ||
| res, err := (&Client{client: m, region: "us-east-1"}).PurchaseCommitment(context.Background(), pricedRec(), common.PurchaseOptions{}) | ||
| require.ErrorContains(t, err, "exceeds the approved upfront") | ||
| assert.False(t, res.Success) | ||
| m.AssertNotCalled(t, "PurchaseReservedInstancesOffering", mock.Anything, mock.Anything) | ||
| } | ||
|
|
||
| func TestPurchaseCommitment_BuysAtApprovedUpfront(t *testing.T) { | ||
| t.Parallel() | ||
| m := offeringsMock(pricedOffering(500)) // 2 x 500 == approved 1000 | ||
| res, err := (&Client{client: m, region: "us-east-1"}).PurchaseCommitment(context.Background(), pricedRec(), common.PurchaseOptions{}) | ||
| require.NoError(t, err) | ||
| assert.True(t, res.Success) | ||
| m.AssertCalled(t, "PurchaseReservedInstancesOffering", mock.Anything, mock.MatchedBy(func(in *ec2.PurchaseReservedInstancesOfferingInput) bool { | ||
| return in.LimitPrice == nil | ||
| }), mock.Anything) | ||
| } | ||
|
|
||
| // The MCP EC2 tool never sets CommitmentCost; zero must not block the buy. | ||
| func TestPurchaseCommitment_NoApprovedUpfrontIsNotChecked(t *testing.T) { | ||
| t.Parallel() | ||
| m := offeringsMock(pricedOffering(9000)) | ||
| rec := pricedRec() | ||
| rec.CommitmentCost = 0 | ||
| _, err := (&Client{client: m, region: "us-east-1"}).PurchaseCommitment(context.Background(), rec, common.PurchaseOptions{}) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestCheckApprovedUpfront_Currency(t *testing.T) { | ||
| t.Parallel() | ||
| off := pricedOffering(500) | ||
| for _, cur := range []string{"", "USD", "usd"} { | ||
| rec := pricedRec() | ||
| rec.CommitmentCurrency = cur | ||
| require.NoError(t, checkApprovedUpfront(rec, &off), "currency %q", cur) | ||
| } | ||
| rec := pricedRec() | ||
| rec.CommitmentCurrency = "EUR" | ||
| require.ErrorContains(t, checkApprovedUpfront(rec, &off), "EUR") | ||
| } | ||
|
|
||
| func TestCheckApprovedUpfront_RefusesMissingPriceAndNonUSDOffering(t *testing.T) { | ||
| t.Parallel() | ||
| noPrice := fullOffering("p", "") | ||
| require.ErrorContains(t, checkApprovedUpfront(pricedRec(), &noPrice), "no FixedPrice") | ||
|
|
||
| eur := pricedOffering(500) | ||
| eur.CurrencyCode = types.CurrencyCodeValues("EUR") | ||
| require.ErrorContains(t, checkApprovedUpfront(pricedRec(), &eur), "USD only") | ||
| } |
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.