2
0

clusterinfo_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package costmodel
  2. import (
  3. "testing"
  4. "github.com/opencost/opencost/core/pkg/clusters"
  5. "github.com/opencost/opencost/core/pkg/util/json"
  6. "github.com/opencost/opencost/core/pkg/util/promutil"
  7. )
  8. func TestClusterInfoLabels(t *testing.T) {
  9. expected := map[string]bool{"clusterprofile": true, "errorreporting": true, "id": true, "logcollection": true, "name": true, "productanalytics": true, "provider": true, "provisioner": true, "remotereadenabled": true, "thanosenabled": true, "valuesreporting": true, "version": true}
  10. clusterInfo := `{"clusterProfile":"production","errorReporting":"true","id":"cluster-one","logCollection":"true","name":"bolt-3","productAnalytics":"true","provider":"GCP","provisioner":"GKE","remoteReadEnabled":"false","thanosEnabled":"false","valuesReporting":"true","version":"1.14+"}`
  11. var m map[string]interface{}
  12. err := json.Unmarshal([]byte(clusterInfo), &m)
  13. if err != nil {
  14. t.Errorf("Error: %s", err)
  15. return
  16. }
  17. labels := promutil.MapToLabels(m)
  18. for k := range expected {
  19. if _, ok := labels[k]; !ok {
  20. t.Errorf("Failed to locate key: \"%s\" in labels.", k)
  21. return
  22. }
  23. }
  24. }
  25. func TestWriteReportingFlags(t *testing.T) {
  26. clusterInfo := make(map[string]string)
  27. writeReportingFlags(clusterInfo)
  28. expectedKeys := []string{
  29. clusters.ClusterInfoLogCollectionKey,
  30. clusters.ClusterInfoProductAnalyticsKey,
  31. clusters.ClusterInfoErrorReportingKey,
  32. clusters.ClusterInfoValuesReportingKey,
  33. }
  34. for _, key := range expectedKeys {
  35. if _, ok := clusterInfo[key]; !ok {
  36. t.Errorf("Missing key: %s", key)
  37. }
  38. }
  39. }
  40. func TestWriteClusterProfile(t *testing.T) {
  41. clusterInfo := make(map[string]string)
  42. writeClusterProfile(clusterInfo)
  43. if _, ok := clusterInfo[clusters.ClusterInfoProfileKey]; !ok {
  44. t.Errorf("Expected profile key %s to be present", clusters.ClusterInfoProfileKey)
  45. }
  46. }