costmodelenv_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 TestIsCacheDisabled(t *testing.T) {
  43. tests := []struct {
  44. name string
  45. want bool
  46. pre func()
  47. }{
  48. {
  49. name: "Ensure the default value is false",
  50. want: false,
  51. },
  52. {
  53. name: "Ensure the value is false when DISABLE_AGGREGATE_COST_MODEL_CACHE is set to false",
  54. want: false,
  55. pre: func() {
  56. os.Setenv("DISABLE_AGGREGATE_COST_MODEL_CACHE", "false")
  57. },
  58. },
  59. {
  60. name: "Ensure the value is true when DISABLE_AGGREGATE_COST_MODEL_CACHE is set to true",
  61. want: true,
  62. pre: func() {
  63. os.Setenv("DISABLE_AGGREGATE_COST_MODEL_CACHE", "true")
  64. },
  65. },
  66. }
  67. for _, tt := range tests {
  68. if tt.pre != nil {
  69. tt.pre()
  70. }
  71. t.Run(tt.name, func(t *testing.T) {
  72. if got := IsAggregateCostModelCacheDisabled(); got != tt.want {
  73. t.Errorf("IsAggregateCostModelCacheDisabled() = %v, want %v", got, tt.want)
  74. }
  75. })
  76. }
  77. }
  78. func TestGetExportCSVMaxDays(t *testing.T) {
  79. tests := []struct {
  80. name string
  81. want int
  82. pre func()
  83. }{
  84. {
  85. name: "Ensure the default value is 90d",
  86. want: 90,
  87. },
  88. {
  89. name: "Ensure the value is 30 when EXPORT_CSV_MAX_DAYS is set to 30",
  90. want: 30,
  91. pre: func() {
  92. os.Setenv("EXPORT_CSV_MAX_DAYS", "30")
  93. },
  94. },
  95. {
  96. name: "Ensure the value is 90 when EXPORT_CSV_MAX_DAYS is set to empty string",
  97. want: 90,
  98. pre: func() {
  99. os.Setenv("EXPORT_CSV_MAX_DAYS", "")
  100. },
  101. },
  102. {
  103. name: "Ensure the value is 90 when EXPORT_CSV_MAX_DAYS is set to invalid value",
  104. want: 90,
  105. pre: func() {
  106. os.Setenv("EXPORT_CSV_MAX_DAYS", "foo")
  107. },
  108. },
  109. }
  110. for _, tt := range tests {
  111. if tt.pre != nil {
  112. tt.pre()
  113. }
  114. t.Run(tt.name, func(t *testing.T) {
  115. if got := GetExportCSVMaxDays(); got != tt.want {
  116. t.Errorf("GetExportCSVMaxDays() = %v, want %v", got, tt.want)
  117. }
  118. })
  119. }
  120. }
  121. func TestGetKubernetesEnabled(t *testing.T) {
  122. tests := []struct {
  123. name string
  124. want bool
  125. pre func()
  126. }{
  127. {
  128. name: "Ensure the default value is false",
  129. want: false,
  130. },
  131. {
  132. name: "Ensure the value is true when KUBERNETES_PORT has a value",
  133. want: true,
  134. pre: func() {
  135. os.Setenv("KUBERNETES_PORT", "tcp://10.43.0.1:443")
  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 := IsKubernetesEnabled(); got != tt.want {
  145. t.Errorf("IsKubernetesEnabled() = %v, want %v", got, tt.want)
  146. }
  147. })
  148. }
  149. }
  150. func TestGetCloudCostConfigPath(t *testing.T) {
  151. tests := []struct {
  152. name string
  153. want string
  154. pre func()
  155. }{
  156. {
  157. name: "Ensure the default value is 'cloud-integration.json'",
  158. want: "cloud-integration.json",
  159. },
  160. {
  161. name: "Ensure the value is 'cloud-integration.json' when CLOUD_COST_CONFIG_PATH is set to ''",
  162. want: "cloud-integration.json",
  163. pre: func() {
  164. os.Setenv("CLOUD_COST_CONFIG_PATH", "")
  165. },
  166. },
  167. {
  168. name: "Ensure the value is 'flying-pig.json' when CLOUD_COST_CONFIG_PATH is set to 'flying-pig.json'",
  169. want: "flying-pig.json",
  170. pre: func() {
  171. os.Setenv("CLOUD_COST_CONFIG_PATH", "flying-pig.json")
  172. },
  173. },
  174. }
  175. for _, tt := range tests {
  176. if tt.pre != nil {
  177. tt.pre()
  178. }
  179. t.Run(tt.name, func(t *testing.T) {
  180. if got := GetCloudCostConfigPath(); got != tt.want {
  181. t.Errorf("GetCloudCostConfigPath() = %v, want %v", got, tt.want)
  182. }
  183. })
  184. }
  185. }