azurestorageintegration_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package azure
  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 GetCloudCost_Test(t *testing.T) {
  10. azureConfigPath := os.Getenv("AZURE_CONFIGURATION")
  11. if azureConfigPath == "" {
  12. t.Skip("skipping integration test, set environment variable AZURE_CONFIGURATION")
  13. }
  14. azureConfigBin, err := os.ReadFile(azureConfigPath)
  15. if err != nil {
  16. t.Fatalf("failed to read config file: %s", err.Error())
  17. }
  18. var azureConfig StorageConfiguration
  19. err = json.Unmarshal(azureConfigBin, &azureConfig)
  20. if err != nil {
  21. t.Fatalf("failed to unmarshal config from JSON: %s", err.Error())
  22. }
  23. testCases := map[string]struct {
  24. integration *AzureStorageIntegration
  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: &AzureStorageIntegration{
  32. AzureStorageBillingParser: AzureStorageBillingParser{
  33. StorageConnection: StorageConnection{
  34. StorageConfiguration: azureConfig,
  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: &AzureStorageIntegration{
  45. AzureStorageBillingParser: AzureStorageBillingParser{
  46. StorageConnection: StorageConnection{
  47. StorageConfiguration: azureConfig,
  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. }