2
0

s3selectintegration_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 TestS3Integration_GetCloudCost(t *testing.T) {
  10. s3ConfigPath := os.Getenv("S3_CONFIGURATION")
  11. if s3ConfigPath == "" {
  12. t.Skip("skipping integration test, set environment variable S3_CONFIGURATION")
  13. }
  14. s3ConfigBin, err := os.ReadFile(s3ConfigPath)
  15. if err != nil {
  16. t.Fatalf("failed to read config file: %s", err.Error())
  17. }
  18. var s3Config S3Configuration
  19. err = json.Unmarshal(s3ConfigBin, &s3Config)
  20. if err != nil {
  21. t.Fatalf("failed to unmarshal config from JSON: %s", err.Error())
  22. }
  23. testCases := map[string]struct {
  24. integration *S3SelectIntegration
  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: &S3SelectIntegration{
  32. S3SelectQuerier: S3SelectQuerier{
  33. S3Connection: S3Connection{
  34. S3Configuration: s3Config,
  35. },
  36. },
  37. },
  38. end: time.Now(),
  39. start: time.Now().Add(-timeutil.Day),
  40. expected: true,
  41. },
  42. // CUR data should be available
  43. "last week window": {
  44. integration: &S3SelectIntegration{
  45. S3SelectQuerier: S3SelectQuerier{
  46. S3Connection: S3Connection{
  47. S3Configuration: s3Config,
  48. },
  49. },
  50. },
  51. end: time.Now().Add(-7 * timeutil.Day),
  52. start: time.Now().Add(-8 * timeutil.Day),
  53. expected: false,
  54. },
  55. }
  56. for name, testCase := range testCases {
  57. t.Run(name, func(t *testing.T) {
  58. actual, err := testCase.integration.GetCloudCost(testCase.start, testCase.end)
  59. if err != nil {
  60. t.Errorf("Other error during testing %s", err)
  61. } else if actual.IsEmpty() != testCase.expected {
  62. t.Errorf("Incorrect result, actual emptiness: %t, expected: %t", actual.IsEmpty(), testCase.expected)
  63. }
  64. })
  65. }
  66. }