| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package oracle
- import (
- "encoding/json"
- "os"
- "testing"
- "time"
- "github.com/opencost/opencost/core/pkg/util/timeutil"
- )
- func TestParseAttributedCost(t *testing.T) {
- strPtr := func(s string) *string { return &s }
- cases := map[string]struct {
- input *string
- want float64
- wantErr bool
- }{
- "nil": {nil, 0, false},
- "empty": {strPtr(""), 0, false},
- "zero": {strPtr("0"), 0, false},
- "valid": {strPtr("1.23"), 1.23, false},
- "negative": {strPtr("-0.5"), -0.5, false},
- "unparsable": {strPtr("abc"), 0, true},
- }
- for name, c := range cases {
- t.Run(name, func(t *testing.T) {
- got, err := parseAttributedCost(c.input)
- if (err != nil) != c.wantErr {
- t.Fatalf("err = %v, wantErr = %v", err, c.wantErr)
- }
- if got != c.want {
- t.Errorf("got %v, want %v", got, c.want)
- }
- })
- }
- }
- func TestUsageAPIIntegration_GetCloudCost(t *testing.T) {
- usageApiConfigPath := os.Getenv("USAGEAPI_CONFIGURATION")
- if usageApiConfigPath == "" {
- t.Skip("skipping integration test, set environment variable USAGEAPI_CONFIGURATION")
- }
- usageApiConfigBin, err := os.ReadFile(usageApiConfigPath)
- if err != nil {
- t.Fatalf("failed to read config file: %s", err.Error())
- }
- var usageApiConfig UsageApiConfiguration
- err = json.Unmarshal(usageApiConfigBin, &usageApiConfig)
- if err != nil {
- t.Fatalf("failed to unmarshal config from JSON: %s", err.Error())
- }
- testCases := map[string]struct {
- integration *UsageApiIntegration
- start time.Time
- end time.Time
- expected bool
- }{
- // No CUR data is expected within 2 days of now
- "too_recent_window": {
- integration: &UsageApiIntegration{
- UsageApiConfiguration: usageApiConfig,
- },
- end: time.Now(),
- start: time.Now().Add(-timeutil.Day),
- expected: true,
- },
- // CUR data should be available
- "last week window": {
- integration: &UsageApiIntegration{
- UsageApiConfiguration: usageApiConfig,
- },
- end: time.Now().Add(-7 * timeutil.Day),
- start: time.Now().Add(-8 * timeutil.Day),
- expected: false,
- },
- }
- for name, testCase := range testCases {
- t.Run(name, func(t *testing.T) {
- actual, err := testCase.integration.GetCloudCost(testCase.start, testCase.end)
- if err != nil {
- t.Errorf("Other error during testing %s", err)
- } else if actual.IsEmpty() != testCase.expected {
- t.Errorf("Incorrect result, actual emptiness: %t, expected: %t", actual.IsEmpty(), testCase.expected)
- }
- })
- }
- }
|