2
0

usageapiintegration_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package oracle
  2. import (
  3. "encoding/json"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/util/timeutil"
  8. )
  9. func TestUsageAPIIntegration_GetCloudCost(t *testing.T) {
  10. usageApiConfigPath := os.Getenv("USAGEAPI_CONFIGURATION")
  11. if usageApiConfigPath == "" {
  12. t.Skip("skipping integration test, set environment variable USAGEAPI_CONFIGURATION")
  13. }
  14. usageApiConfigBin, err := os.ReadFile(usageApiConfigPath)
  15. if err != nil {
  16. t.Fatalf("failed to read config file: %s", err.Error())
  17. }
  18. var usageApiConfig UsageApiConfiguration
  19. err = json.Unmarshal(usageApiConfigBin, &usageApiConfig)
  20. if err != nil {
  21. t.Fatalf("failed to unmarshal config from JSON: %s", err.Error())
  22. }
  23. testCases := map[string]struct {
  24. integration *UsageApiIntegration
  25. start time.Time
  26. end time.Time
  27. expected bool
  28. }{
  29. // No CUR data is expected within 2 days of now
  30. "too_recent_window": {
  31. integration: &UsageApiIntegration{
  32. UsageApiConfiguration: usageApiConfig,
  33. },
  34. end: time.Now(),
  35. start: time.Now().Add(-timeutil.Day),
  36. expected: true,
  37. },
  38. // CUR data should be available
  39. "last week window": {
  40. integration: &UsageApiIntegration{
  41. UsageApiConfiguration: usageApiConfig,
  42. },
  43. end: time.Now().Add(-7 * timeutil.Day),
  44. start: time.Now().Add(-8 * timeutil.Day),
  45. expected: false,
  46. },
  47. }
  48. for name, testCase := range testCases {
  49. t.Run(name, func(t *testing.T) {
  50. actual, err := testCase.integration.GetCloudCost(testCase.start, testCase.end)
  51. if err != nil {
  52. t.Errorf("Other error during testing %s", err)
  53. } else if actual.IsEmpty() != testCase.expected {
  54. t.Errorf("Incorrect result, actual emptiness: %t, expected: %t", actual.IsEmpty(), testCase.expected)
  55. }
  56. })
  57. }
  58. }