-
Notifications
You must be signed in to change notification settings - Fork 6
fix(cli): parse CSV Count and EstimatedSavings strictly #2114
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
2 commits
Select commit
Hold shift + click to select a range
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,95 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func loadCSVContent(t *testing.T, content string) error { | ||
| t.Helper() | ||
| csvPath := filepath.Join(t.TempDir(), "recs.csv") | ||
| require.NoError(t, os.WriteFile(csvPath, []byte(content), 0o600)) | ||
| _, err := loadRecommendationsFromCSV(csvPath) | ||
| return err | ||
| } | ||
|
|
||
| // Count cells must parse whole or not at all (#1944): fmt.Sscanf truncated | ||
| // "3.7" to 3 and accepted trailing garbage, both feeding the purchase loop. | ||
| func TestLoadRecommendationsFromCSV_StrictCount(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| cell string | ||
| }{ | ||
| {"fractional", "3.7"}, | ||
| {"trailing garbage", "3abc"}, | ||
| {"trailing unit", "12 units"}, | ||
| {"negative", "-1"}, | ||
| {"blank", ""}, | ||
| {"whitespace only", " "}, | ||
| {"overflows int64", "99999999999999999999"}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| content := "Service,Region,ResourceType,Count\n" + | ||
| "rds,us-east-1,db.t3.micro,1\n" + | ||
| "rds,us-east-1,db.t3.small,\"" + tt.cell + "\"\n" | ||
| err := loadCSVContent(t, content) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "line 3") | ||
| assert.Contains(t, err.Error(), `column 4 "Count"`) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestLoadRecommendationsFromCSV_CountTrimmed(t *testing.T) { | ||
| csvPath := filepath.Join(t.TempDir(), "recs.csv") | ||
| require.NoError(t, os.WriteFile(csvPath, []byte("Service,Region,ResourceType,Count\nrds,us-east-1,db.t3.micro,\" 3 \"\n"), 0o600)) | ||
| recs, err := loadRecommendationsFromCSV(csvPath) | ||
| require.NoError(t, err) | ||
| require.Len(t, recs, 1) | ||
| assert.Equal(t, 3, recs[0].Count) | ||
| } | ||
|
|
||
| func TestLoadRecommendationsFromCSV_CountColumnRequired(t *testing.T) { | ||
| err := loadCSVContent(t, "Service,Region,ResourceType\nrds,us-east-1,db.t3.micro\n") | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "Count") | ||
| } | ||
|
|
||
| func TestLoadRecommendationsFromCSV_StrictEstimatedSavings(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| cell string | ||
| }{ | ||
| {"trailing currency", "1000 USD"}, | ||
| {"trailing garbage", "12.5abc"}, | ||
| {"NaN", "NaN"}, | ||
| {"infinity", "Inf"}, | ||
| {"overflows float64", "1e400"}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| content := "Service,Region,ResourceType,Count,EstimatedSavings\n" + | ||
| "rds,us-east-1,db.t3.micro,2,\"" + tt.cell + "\"\n" | ||
| err := loadCSVContent(t, content) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "line 2") | ||
| assert.Contains(t, err.Error(), `column 5 "EstimatedSavings"`) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // A blank EstimatedSavings cell stays absent-as-zero: requireRankingSignal | ||
| // relies on it to refuse a binding --max-instances cap. | ||
| func TestLoadRecommendationsFromCSV_BlankSavingsStillZero(t *testing.T) { | ||
| csvPath := filepath.Join(t.TempDir(), "recs.csv") | ||
| require.NoError(t, os.WriteFile(csvPath, []byte("Service,Region,ResourceType,Count,EstimatedSavings\nrds,us-east-1,db.t3.micro,2, \n"), 0o600)) | ||
| recs, err := loadRecommendationsFromCSV(csvPath) | ||
| require.NoError(t, err) | ||
| require.Len(t, recs, 1) | ||
| assert.Zero(t, recs[0].EstimatedSavings) | ||
| } |
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Validate the required Count column before reading records.
If a CSV contains only the header
Service,Region,ResourceType,parseCSVRecordsreaches EOF without callingparseCSVCount. The loader then accepts a file that lacks the requiredCountcolumn. Check the header inloadRecommendationsFromCSVso a header-only file also reports the missing column. (pkg.go.dev)🤖 Prompt for AI Agents