costmodelenv_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package env
  2. import (
  3. "os"
  4. "testing"
  5. )
  6. func TestGetAPIPort(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. want int
  10. pre func()
  11. }{
  12. {
  13. name: "Ensure the default API port '9003'",
  14. want: 9003,
  15. },
  16. {
  17. name: "Ensure the default API port '9003' when API_PORT is set to ''",
  18. want: 9003,
  19. pre: func() {
  20. os.Setenv("API_PORT", "")
  21. },
  22. },
  23. {
  24. name: "Ensure the API port '9004' when API_PORT is set to '9004'",
  25. want: 9004,
  26. pre: func() {
  27. os.Setenv("API_PORT", "9004")
  28. },
  29. },
  30. }
  31. for _, tt := range tests {
  32. if tt.pre != nil {
  33. tt.pre()
  34. }
  35. t.Run(tt.name, func(t *testing.T) {
  36. if got := GetAPIPort(); got != tt.want {
  37. t.Errorf("GetAPIPort() = %v, want %v", got, tt.want)
  38. }
  39. })
  40. }
  41. }
  42. func TestGetExportCSVMaxDays(t *testing.T) {
  43. tests := []struct {
  44. name string
  45. want int
  46. pre func()
  47. }{
  48. {
  49. name: "Ensure the default value is 90d",
  50. want: 90,
  51. },
  52. {
  53. name: "Ensure the value is 30 when EXPORT_CSV_MAX_DAYS is set to 30",
  54. want: 30,
  55. pre: func() {
  56. os.Setenv("EXPORT_CSV_MAX_DAYS", "30")
  57. },
  58. },
  59. {
  60. name: "Ensure the value is 90 when EXPORT_CSV_MAX_DAYS is set to empty string",
  61. want: 90,
  62. pre: func() {
  63. os.Setenv("EXPORT_CSV_MAX_DAYS", "")
  64. },
  65. },
  66. {
  67. name: "Ensure the value is 90 when EXPORT_CSV_MAX_DAYS is set to invalid value",
  68. want: 90,
  69. pre: func() {
  70. os.Setenv("EXPORT_CSV_MAX_DAYS", "foo")
  71. },
  72. },
  73. }
  74. for _, tt := range tests {
  75. if tt.pre != nil {
  76. tt.pre()
  77. }
  78. t.Run(tt.name, func(t *testing.T) {
  79. if got := GetExportCSVMaxDays(); got != tt.want {
  80. t.Errorf("GetExportCSVMaxDays() = %v, want %v", got, tt.want)
  81. }
  82. })
  83. }
  84. }
  85. func TestGetKubernetesEnabled(t *testing.T) {
  86. tests := []struct {
  87. name string
  88. want bool
  89. pre func()
  90. }{
  91. {
  92. name: "Ensure the default value is false",
  93. want: false,
  94. },
  95. {
  96. name: "Ensure the value is true when KUBERNETES_PORT has a value",
  97. want: true,
  98. pre: func() {
  99. os.Setenv("KUBERNETES_PORT", "tcp://10.43.0.1:443")
  100. },
  101. },
  102. }
  103. for _, tt := range tests {
  104. if tt.pre != nil {
  105. tt.pre()
  106. }
  107. t.Run(tt.name, func(t *testing.T) {
  108. if got := IsKubernetesEnabled(); got != tt.want {
  109. t.Errorf("IsKubernetesEnabled() = %v, want %v", got, tt.want)
  110. }
  111. })
  112. }
  113. }
  114. func TestGetCloudCostConfigPath(t *testing.T) {
  115. tests := []struct {
  116. name string
  117. want string
  118. pre func()
  119. }{
  120. {
  121. name: "Ensure the default value is 'cloud-integration.json'",
  122. want: "cloud-integration.json",
  123. },
  124. {
  125. name: "Ensure the value is 'cloud-integration.json' when CLOUD_COST_CONFIG_PATH is set to ''",
  126. want: "cloud-integration.json",
  127. pre: func() {
  128. os.Setenv("CLOUD_COST_CONFIG_PATH", "")
  129. },
  130. },
  131. {
  132. name: "Ensure the value is 'flying-pig.json' when CLOUD_COST_CONFIG_PATH is set to 'flying-pig.json'",
  133. want: "flying-pig.json",
  134. pre: func() {
  135. os.Setenv("CLOUD_COST_CONFIG_PATH", "flying-pig.json")
  136. },
  137. },
  138. }
  139. for _, tt := range tests {
  140. if tt.pre != nil {
  141. tt.pre()
  142. }
  143. t.Run(tt.name, func(t *testing.T) {
  144. if got := GetCloudCostConfigPath(); got != tt.want {
  145. t.Errorf("GetCloudCostConfigPath() = %v, want %v", got, tt.want)
  146. }
  147. })
  148. }
  149. }
  150. func TestEnvVarsWithBackup(t *testing.T) {
  151. t.Run("test install namespace env var", func(t *testing.T) {
  152. t.Setenv(InstallNamespaceEnvVar, "test-namespace")
  153. t.Setenv(KubecostNamespaceEnvVar, "kubecost-test-namespace")
  154. ns := GetInstallNamespace()
  155. if ns != "test-namespace" {
  156. t.Errorf("Expected install namespace to be 'test-namespace', got '%s'", ns)
  157. }
  158. })
  159. t.Run("test kubecost namespace env var", func(t *testing.T) {
  160. t.Setenv(KubecostNamespaceEnvVar, "kc-test-namespace")
  161. ns := GetInstallNamespace()
  162. if ns != "kc-test-namespace" {
  163. t.Errorf("Expected install namespace to be 'kc-test-namespace', got '%s'", ns)
  164. }
  165. })
  166. t.Run("test default install namespace", func(t *testing.T) {
  167. t.Setenv(InstallNamespaceEnvVar, "test-namespace")
  168. ns := GetInstallNamespace()
  169. if ns != "test-namespace" {
  170. t.Errorf("Expected default install namespace to be 'test-namespace', got '%s'", ns)
  171. }
  172. })
  173. t.Run("test default install namespace", func(t *testing.T) {
  174. ns := GetInstallNamespace()
  175. if ns != "opencost" {
  176. t.Errorf("Expected default install namespace to be 'opencost', got '%s'", ns)
  177. }
  178. })
  179. t.Run("test config bucket file with both", func(t *testing.T) {
  180. t.Setenv(ConfigBucketEnvVar, "test-bucket")
  181. t.Setenv(KubecostConfigBucketEnvVar, "kc-test-bucket")
  182. configBucketFile := GetConfigBucketFile()
  183. if configBucketFile != "test-bucket" {
  184. t.Errorf("Expected config bucket file to be 'test-bucket', got '%s'", configBucketFile)
  185. }
  186. })
  187. t.Run("test config bucket file with kc", func(t *testing.T) {
  188. t.Setenv(KubecostConfigBucketEnvVar, "kc-test-bucket")
  189. configBucketFile := GetConfigBucketFile()
  190. if configBucketFile != "kc-test-bucket" {
  191. t.Errorf("Expected config bucket file to be 'kc-test-bucket', got '%s'", configBucketFile)
  192. }
  193. })
  194. t.Run("test config bucket file with single", func(t *testing.T) {
  195. t.Setenv(ConfigBucketEnvVar, "test-bucket")
  196. configBucketFile := GetConfigBucketFile()
  197. if configBucketFile != "test-bucket" {
  198. t.Errorf("Expected config bucket file to be 'test-bucket', got '%s'", configBucketFile)
  199. }
  200. })
  201. t.Run("test config bucket file with both", func(t *testing.T) {
  202. configBucketFile := GetConfigBucketFile()
  203. if configBucketFile != "" {
  204. t.Errorf("Expected config bucket file to be '', got '%s'", configBucketFile)
  205. }
  206. })
  207. }