athenaintegration_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package aws
  2. import (
  3. "os"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/util/json"
  7. "github.com/opencost/opencost/core/pkg/util/timeutil"
  8. )
  9. func TestAthenaIntegration_GetCloudCost(t *testing.T) {
  10. athenaConfigPath := os.Getenv("ATHENA_CONFIGURATION")
  11. if athenaConfigPath == "" {
  12. t.Skip("skipping integration test, set environment variable ATHENA_CONFIGURATION")
  13. }
  14. athenaConfigBin, err := os.ReadFile(athenaConfigPath)
  15. if err != nil {
  16. t.Fatalf("failed to read config file: %s", err.Error())
  17. }
  18. var athenaConfig AthenaConfiguration
  19. err = json.Unmarshal(athenaConfigBin, &athenaConfig)
  20. if err != nil {
  21. t.Fatalf("failed to unmarshal config from JSON: %s", err.Error())
  22. }
  23. testCases := map[string]struct {
  24. integration *AthenaIntegration
  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: &AthenaIntegration{
  32. AthenaQuerier: AthenaQuerier{
  33. AthenaConfiguration: athenaConfig,
  34. },
  35. },
  36. end: time.Now(),
  37. start: time.Now().Add(-timeutil.Day),
  38. expected: true,
  39. },
  40. // CUR data should be available
  41. "last week window": {
  42. integration: &AthenaIntegration{
  43. AthenaQuerier: AthenaQuerier{
  44. AthenaConfiguration: athenaConfig,
  45. },
  46. },
  47. end: time.Now().Add(-7 * timeutil.Day),
  48. start: time.Now().Add(-8 * timeutil.Day),
  49. expected: false,
  50. },
  51. }
  52. for name, testCase := range testCases {
  53. t.Run(name, func(t *testing.T) {
  54. actual, err := testCase.integration.GetCloudCost(testCase.start, testCase.end)
  55. if err != nil {
  56. t.Errorf("Other error during testing %s", err)
  57. } else if actual.IsEmpty() != testCase.expected {
  58. t.Errorf("Incorrect result, actual emptiness: %t, expected: %t", actual.IsEmpty(), testCase.expected)
  59. }
  60. })
  61. }
  62. }