pipelineservice_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package customcost
  2. import (
  3. "fmt"
  4. "io"
  5. "net/http"
  6. "os"
  7. "runtime"
  8. "testing"
  9. "time"
  10. "github.com/opencost/opencost/core/pkg/log"
  11. "github.com/opencost/opencost/core/pkg/util/timeutil"
  12. )
  13. func TestPipelineService(t *testing.T) {
  14. // establish temporary test assets dir
  15. dir := t.TempDir()
  16. err := os.MkdirAll(dir+"/config", 0777)
  17. if err != nil {
  18. t.Fatalf("error creating temp config dir: %v", err)
  19. }
  20. err = os.MkdirAll(dir+"/executable", 0777)
  21. if err != nil {
  22. t.Fatalf("error creating temp exec dir: %v", err)
  23. }
  24. // write DD secrets to config files
  25. // write config file to temp dir
  26. writeDDConfig(dir+"/config", t)
  27. // set env vars for plugin config and executable
  28. err = os.Setenv("PLUGIN_CONFIG_DIR", dir+"/config")
  29. if err != nil {
  30. t.Fatalf("error setting config dir env var: %v", err)
  31. }
  32. err = os.Setenv("PLUGIN_EXECUTABLE_DIR", dir+"/executable")
  33. if err != nil {
  34. t.Fatalf("error setting config dir env var: %v", err)
  35. }
  36. // download amd plugin, store in tmp executable dir
  37. downloadLatestPluginExec(dir+"/executable", t)
  38. // set up repos
  39. hourlyRepo := NewMemoryRepository()
  40. dailyRepo := NewMemoryRepository()
  41. // set up ingestor config
  42. config := DefaultIngestorConfiguration()
  43. config.DailyDuration = 7 * timeutil.Day
  44. config.HourlyDuration = 16 * time.Hour
  45. pipeline, err := NewPipelineService(hourlyRepo, dailyRepo, config)
  46. if err != nil {
  47. t.Fatalf("error starting pipeline: %v", err)
  48. }
  49. // wait until coverage is complete, then stop ingestor
  50. ingestionComplete := false
  51. maxLoops := 10
  52. loopCount := 0
  53. for !ingestionComplete && loopCount < maxLoops {
  54. status := pipeline.Status()
  55. log.Debugf("got status: %v", status)
  56. coverageHourly, foundHourly := status.CoverageHourly["datadog"]
  57. coverageDaily, foundDaily := status.CoverageDaily["datadog"]
  58. if foundHourly && foundDaily {
  59. // check coverage
  60. minTime := time.Now().UTC().Add(-6 * timeutil.Day)
  61. maxTime := time.Now().UTC().Add(-3 * time.Hour)
  62. if coverageDaily.Start().Before(minTime) && coverageHourly.End().After(maxTime) {
  63. log.Infof("good coverage, breaking out of loop")
  64. ingestionComplete = true
  65. break
  66. } else {
  67. log.Infof("coverage not within range. Looking for coverage %v to %v, but current coverage is %v/%v", minTime, maxTime, coverageDaily, coverageHourly)
  68. }
  69. } else {
  70. log.Debugf("no coverage info ready yet for datadog")
  71. }
  72. log.Infof("sleeping 10s...")
  73. time.Sleep(10 * time.Second)
  74. loopCount++
  75. }
  76. if !ingestionComplete {
  77. t.Fatal("ingestor never completed within allocated time")
  78. }
  79. pipeline.hourlyIngestor.Stop()
  80. pipeline.dailyIngestor.Stop()
  81. // inspect data from yesterday
  82. targetTime := time.Now().UTC().Add(-1 * timeutil.Day).Truncate(timeutil.Day)
  83. log.Infof("querying for data with window start: %v", targetTime)
  84. // check for presence of hosts in DD response
  85. ddCosts, err := dailyRepo.Get(targetTime, "datadog")
  86. if err != nil {
  87. t.Fatalf("error getting results for targetTime")
  88. }
  89. foundInfraHosts := false
  90. for _, cost := range ddCosts.Costs {
  91. if cost.ResourceType == "infra_hosts" {
  92. foundInfraHosts = true
  93. }
  94. }
  95. if !foundInfraHosts {
  96. t.Fatal("expecting infra_hosts costs in daily response")
  97. }
  98. // query data from 4 hours ago (hourly)
  99. targetTime = time.Now().UTC().Add(-13 * time.Hour).Truncate(time.Hour)
  100. log.Infof("querying for data with window start: %v", targetTime)
  101. // check for presence of hosts in DD response
  102. ddCosts, err = hourlyRepo.Get(targetTime, "datadog")
  103. if err != nil {
  104. t.Fatalf("error getting results for targetTime")
  105. }
  106. foundInfraHosts = false
  107. for _, cost := range ddCosts.Costs {
  108. if cost.ResourceType == "infra_hosts" {
  109. foundInfraHosts = true
  110. }
  111. }
  112. if !foundInfraHosts {
  113. t.Fatal("expecting infra_hosts costs in hourly response")
  114. }
  115. }
  116. func downloadLatestPluginExec(dirName string, t *testing.T) {
  117. ddPluginURL := "https://github.com/opencost/opencost-plugins/releases/download/v0.0.3/datadog.ocplugin." + runtime.GOOS + "." + runtime.GOARCH
  118. out, err := os.OpenFile(dirName+"/datadog.ocplugin."+runtime.GOOS+"."+runtime.GOARCH, 0755|os.O_CREATE, 0755)
  119. if err != nil {
  120. t.Fatalf("error creating executable file: %v", err)
  121. }
  122. resp, err := http.Get(ddPluginURL)
  123. if err != nil {
  124. t.Fatalf("error downloading: %v", err)
  125. }
  126. defer resp.Body.Close()
  127. defer out.Close()
  128. _, err = io.Copy(out, resp.Body)
  129. if err != nil {
  130. t.Fatalf("error copying: %v", err)
  131. }
  132. }
  133. func writeDDConfig(pluginConfigDir string, t *testing.T) {
  134. // read necessary env vars. If any are missing, log warning and skip test
  135. ddSite := os.Getenv("DD_SITE")
  136. ddApiKey := os.Getenv("DD_API_KEY")
  137. ddAppKey := os.Getenv("DD_APPLICATION_KEY")
  138. if ddSite == "" {
  139. log.Warnf("DD_SITE undefined, this needs to have the URL of your DD instance, skipping test")
  140. t.Skip()
  141. return
  142. }
  143. if ddApiKey == "" {
  144. log.Warnf("DD_API_KEY undefined, skipping test")
  145. t.Skip()
  146. return
  147. }
  148. if ddAppKey == "" {
  149. log.Warnf("DD_APPLICATION_KEY undefined, skipping test")
  150. t.Skip()
  151. return
  152. }
  153. // write out config to temp file using contents of env vars
  154. ddConf := fmt.Sprintf(`{"datadog_site": "%s", "datadog_api_key": "%s", "datadog_app_key": "%s"}`, ddSite, ddApiKey, ddAppKey)
  155. // set up custom cost request
  156. file, err := os.CreateTemp(pluginConfigDir, "datadog_config.json")
  157. if err != nil {
  158. t.Fatalf("could not create temp config dir: %v", err)
  159. }
  160. err = os.WriteFile(file.Name(), []byte(ddConf), 0777)
  161. if err != nil {
  162. t.Fatalf("could not write file: %v", err)
  163. }
  164. }