bigqueryintegration_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package gcp
  2. import (
  3. "encoding/json"
  4. "os"
  5. "testing"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/opencost"
  8. "github.com/opencost/opencost/core/pkg/util/timeutil"
  9. )
  10. func TestBigQueryIntegration_GetCloudCost(t *testing.T) {
  11. bigQueryConfigPath := os.Getenv("BIGQUERY_CONFIGURATION")
  12. if bigQueryConfigPath == "" {
  13. t.Skip("skipping integration test, set environment variable BIGQUERY_CONFIGURATION\"")
  14. }
  15. bigQueryConfigBin, err := os.ReadFile(bigQueryConfigPath)
  16. if err != nil {
  17. t.Fatalf("failed to read config file: %s", err.Error())
  18. }
  19. var bigQueryConfig BigQueryConfiguration
  20. err = json.Unmarshal(bigQueryConfigBin, &bigQueryConfig)
  21. if err != nil {
  22. t.Fatalf("failed to unmarshal config from JSON: %s", err.Error())
  23. }
  24. today := opencost.RoundBack(time.Now().UTC(), timeutil.Day)
  25. testCases := map[string]struct {
  26. integration *BigQueryIntegration
  27. start time.Time
  28. end time.Time
  29. expected bool
  30. }{
  31. "last week window": {
  32. integration: &BigQueryIntegration{
  33. BigQueryQuerier: BigQueryQuerier{
  34. BigQueryConfiguration: bigQueryConfig,
  35. },
  36. },
  37. end: today.Add(-7 * timeutil.Day),
  38. start: today.Add(-8 * timeutil.Day),
  39. expected: false,
  40. },
  41. }
  42. for name, testCase := range testCases {
  43. t.Run(name, func(t *testing.T) {
  44. actual, err := testCase.integration.GetCloudCost(testCase.start, testCase.end)
  45. if err != nil {
  46. t.Errorf("Other error during testing %s", err)
  47. } else if actual.IsEmpty() != testCase.expected {
  48. t.Errorf("Incorrect result, actual emptiness: %t, expected: %t", actual.IsEmpty(), testCase.expected)
  49. }
  50. })
  51. }
  52. }