-
Notifications
You must be signed in to change notification settings - Fork 22
Added empty window tests for Assets and Allocations api #102
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
Open
HardingEthan15
wants to merge
2
commits into
opencost:main
Choose a base branch
from
e-300:add-empty-window-tests
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.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package allocation | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/opencost/opencost-integration-tests/pkg/api" | ||
| ) | ||
|
|
||
| // TestAllocationEmptyWindow validates that allocation API returns a clean, well-formed | ||
| // empty response for valid but data-less windows. | ||
| // | ||
| // Expected response shape for empty windows: | ||
| // - HTTP Code: 200 (success) | ||
| // - Data: empty slice [] or list of empty allocations | ||
| // - No panic or 5xx errors | ||
| func TestAllocationEmptyWindow(t *testing.T) { | ||
| apiObj := api.NewAPI() | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| window string | ||
| aggregate string | ||
| }{ | ||
| { | ||
| name: "FarPastWindow", | ||
| window: "1970-01-01T00:00:00Z,1970-01-02T00:00:00Z", | ||
| aggregate: "namespace", | ||
|
|
||
| }, | ||
| { | ||
| name: "FutureWindow", | ||
| window: "2099-01-01T00:00:00Z,2099-01-02T00:00:00Z", | ||
| aggregate: "namespace", | ||
| }, | ||
| { | ||
| name: "BeyondRetentionWindow", | ||
| window: "2000-01-01T00:00:00Z,2000-01-02T00:00:00Z", | ||
| aggregate: "cluster", | ||
| }, | ||
| { | ||
| name: "TestPodEmptyWindow", | ||
| window: "1970-01-01T00:00:00Z,1970-01-02T00:00:00Z", | ||
| aggregate: "pod", | ||
| }, | ||
| { | ||
| name: "TestContainerEmptyWindow", | ||
| window: "1970-01-01T00:00:00Z,1970-01-02T00:00:00Z", | ||
| aggregate: "container", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Logf("Testing: %s - %s", tc.name, tc.window) | ||
|
|
||
| response, err := apiObj.GetAllocation(api.AllocationRequest{ | ||
| Window: tc.window, | ||
| Aggregate: tc.aggregate, | ||
| }) | ||
|
|
||
| // Validate no errors occurred | ||
| if err != nil { | ||
| t.Fatalf("Unexpected error calling allocation API: %v", err) | ||
| } | ||
|
|
||
| // Validate HTTP 200 | ||
| if response.Code != 200 { | ||
| t.Fatalf("Expected HTTP 200, got %d", response.Code) | ||
| } | ||
|
|
||
| // Validate response structure is well-formed | ||
| if response.Data == nil { | ||
| t.Fatalf("Expected non-nil data slice, got nil") | ||
| } | ||
|
|
||
| // Empty window should have empty data or a single empty map | ||
| // The exact format depends on the step parameter, but we should | ||
| // have at least one "window" entry even if no allocations | ||
| if len(response.Data) == 0 { | ||
| t.Logf("Data is empty slice (expected for empty window): %v", response.Data) | ||
| } else { | ||
| // If data has entries, each should be a map | ||
| for i, dataMap := range response.Data { | ||
| if len(dataMap) == 0 { | ||
| t.Logf("Data[%d] is empty map (expected for empty window)", i) | ||
| } else { | ||
| t.Fatalf("expected empty allocation data for empty window, data[%d] contained allocations: %v", i, dataMap) | ||
| } | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
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,78 @@ | ||
| package assets | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/opencost/opencost-integration-tests/pkg/api" | ||
| ) | ||
|
|
||
| // TestAssetsEmptyWindow validates that assets API returns a clean, well-formed | ||
| // empty response for valid but data-less windows. | ||
| // | ||
| // Expected response shape for empty windows: | ||
| // - HTTP Code: 200 (success) | ||
| // - Data: empty map {} (no assets in the window) | ||
| // - No panic or 5xx errors | ||
| func TestAssetsEmptyWindow(t *testing.T) { | ||
| apiObj := api.NewAPI() | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| window string | ||
| assetType string | ||
| }{ | ||
| { | ||
| name: "FarPastWindow", | ||
| window: "1970-01-01T00:00:00Z,1970-01-02T00:00:00Z", | ||
| assetType: "node", | ||
| }, | ||
| { | ||
| name: "FutureWindow", | ||
| window: "2099-01-01T00:00:00Z,2099-01-02T00:00:00Z", | ||
| assetType: "node", | ||
| }, | ||
| { | ||
| name: "BeyondRetentionWindow", | ||
| window: "2000-01-01T00:00:00Z,2000-01-02T00:00:00Z", | ||
| assetType: "disk", | ||
| }, | ||
| { | ||
| name: "EmptyWindowPVC", | ||
| window: "1970-01-01T00:00:00Z,1970-01-02T00:00:00Z", | ||
| assetType: "pvc", | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Logf("Testing: %s - %s", tc.name, tc.window) | ||
|
|
||
| response, err := apiObj.GetAssets(api.AssetsRequest{ | ||
| Window: tc.window, | ||
| Filter: tc.assetType, | ||
| }) | ||
|
|
||
| // Validate no errors occurred | ||
| if err != nil { | ||
| t.Fatalf("Unexpected error calling assets API: %v", err) | ||
| } | ||
|
|
||
| // Validate HTTP 200 | ||
| if response.Code != 200 { | ||
| t.Fatalf("Expected HTTP 200, got %d", response.Code) | ||
| } | ||
|
|
||
| // Validate response structure is well-formed | ||
| if response.Data == nil { | ||
| t.Fatalf("Expected non-nil data map, got nil") | ||
| } | ||
|
|
||
| // Empty window should return empty map | ||
| if len(response.Data) == 0 { | ||
| t.Logf("Data is empty map (expected for empty window)") | ||
| } else { | ||
| t.Fatalf("Data has %d entries (filter: %s)", len(response.Data), tc.assetType) | ||
| } | ||
| }) | ||
| } | ||
| } |
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.