cloud_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package test
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/kubecost/cost-model/pkg/cloud"
  6. v1 "k8s.io/api/core/v1"
  7. )
  8. const (
  9. providerIDMap = "spec.providerID"
  10. nameMap = "metadata.name"
  11. labelMapFoo = "metadata.labels.foo"
  12. )
  13. func TestRegionValueFromMapField(t *testing.T) {
  14. wantRegion := "useast"
  15. wantpid := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0")
  16. providerIDWant := wantRegion + "," + wantpid
  17. n := &v1.Node{}
  18. n.Spec.ProviderID = "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0"
  19. n.Labels = make(map[string]string)
  20. n.Labels[v1.LabelZoneRegion] = wantRegion
  21. got := cloud.NodeValueFromMapField(providerIDMap, n, true)
  22. if got != providerIDWant {
  23. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
  24. }
  25. }
  26. func TestTransformedValueFromMapField(t *testing.T) {
  27. providerIDWant := "i-05445591e0d182d42"
  28. n := &v1.Node{}
  29. n.Spec.ProviderID = "aws:///us-east-1a/i-05445591e0d182d42"
  30. got := cloud.NodeValueFromMapField(providerIDMap, n, false)
  31. if got != providerIDWant {
  32. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
  33. }
  34. providerIDWant2 := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0")
  35. n2 := &v1.Node{}
  36. n2.Spec.ProviderID = "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0"
  37. got2 := cloud.NodeValueFromMapField(providerIDMap, n2, false)
  38. if got2 != providerIDWant2 {
  39. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant2, got2)
  40. }
  41. providerIDWant3 := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/mc_testspot_testspot_eastus/providers/Microsoft.Compute/virtualMachineScaleSets/aks-nodepool1-19213364-vmss/virtualMachines/0")
  42. n3 := &v1.Node{}
  43. n3.Spec.ProviderID = "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/mc_testspot_testspot_eastus/providers/Microsoft.Compute/virtualMachineScaleSets/aks-nodepool1-19213364-vmss/virtualMachines/0"
  44. got3 := cloud.NodeValueFromMapField(providerIDMap, n3, false)
  45. if got3 != providerIDWant3 {
  46. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant3, got3)
  47. }
  48. }
  49. func TestNodeValueFromMapField(t *testing.T) {
  50. providerIDWant := "providerid"
  51. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  52. labelFooWant := "labelfoo"
  53. n := &v1.Node{}
  54. n.Spec.ProviderID = providerIDWant
  55. n.Name = nameWant
  56. n.Labels = make(map[string]string)
  57. n.Labels["foo"] = labelFooWant
  58. got := cloud.NodeValueFromMapField(providerIDMap, n, false)
  59. if got != providerIDWant {
  60. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
  61. }
  62. got = cloud.NodeValueFromMapField(nameMap, n, false)
  63. if got != nameWant {
  64. t.Errorf("Assert on '%s' want '%s' got '%s'", nameMap, nameWant, got)
  65. }
  66. got = cloud.NodeValueFromMapField(labelMapFoo, n, false)
  67. if got != labelFooWant {
  68. t.Errorf("Assert on '%s' want '%s' got '%s'", labelMapFoo, labelFooWant, got)
  69. }
  70. }
  71. func TestNodePriceFromCSV(t *testing.T) {
  72. providerIDWant := "providerid"
  73. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  74. labelFooWant := "labelfoo"
  75. n := &v1.Node{}
  76. n.Spec.ProviderID = providerIDWant
  77. n.Name = nameWant
  78. n.Labels = make(map[string]string)
  79. n.Labels["foo"] = labelFooWant
  80. wantPrice := "0.1337"
  81. c := &cloud.CSVProvider{
  82. CSVLocation: "../configs/pricing_schema.csv",
  83. CustomProvider: &cloud.CustomProvider{
  84. Config: cloud.NewProviderConfig("../configs/default.json"),
  85. },
  86. }
  87. c.DownloadPricingData()
  88. k := c.GetKey(n.Labels, n)
  89. resN, err := c.NodePricing(k)
  90. if err != nil {
  91. t.Errorf("Error in NodePricing: %s", err.Error())
  92. } else {
  93. gotPrice := resN.Cost
  94. if gotPrice != wantPrice {
  95. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  96. }
  97. }
  98. unknownN := &v1.Node{}
  99. unknownN.Spec.ProviderID = providerIDWant
  100. unknownN.Name = "unknownname"
  101. unknownN.Labels = make(map[string]string)
  102. unknownN.Labels["foo"] = labelFooWant
  103. k2 := c.GetKey(n.Labels, unknownN)
  104. resN2, _ := c.NodePricing(k2)
  105. if resN2 != nil {
  106. t.Errorf("CSV provider should return nil on missing node")
  107. }
  108. c2 := &cloud.CSVProvider{
  109. CSVLocation: "../configs/fake.csv",
  110. CustomProvider: &cloud.CustomProvider{
  111. Config: cloud.NewProviderConfig("../configs/default.json"),
  112. },
  113. }
  114. k3 := c.GetKey(n.Labels, n)
  115. resN3, _ := c2.NodePricing(k3)
  116. if resN3 != nil {
  117. t.Errorf("CSV provider should return nil on missing csv")
  118. }
  119. }
  120. func TestNodePriceFromCSVWithRegion(t *testing.T) {
  121. providerIDWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  122. nameWant := "foo"
  123. labelFooWant := "labelfoo"
  124. n := &v1.Node{}
  125. n.Spec.ProviderID = providerIDWant
  126. n.Name = nameWant
  127. n.Labels = make(map[string]string)
  128. n.Labels["foo"] = labelFooWant
  129. n.Labels[v1.LabelZoneRegion] = "regionone"
  130. wantPrice := "0.1337"
  131. n2 := &v1.Node{}
  132. n2.Spec.ProviderID = providerIDWant
  133. n2.Name = nameWant
  134. n2.Labels = make(map[string]string)
  135. n2.Labels["foo"] = labelFooWant
  136. n2.Labels[v1.LabelZoneRegion] = "regiontwo"
  137. wantPrice2 := "0.1338"
  138. n3 := &v1.Node{}
  139. n3.Spec.ProviderID = providerIDWant
  140. n3.Name = nameWant
  141. n3.Labels = make(map[string]string)
  142. n3.Labels["foo"] = labelFooWant
  143. n3.Labels[v1.LabelZoneRegion] = "fakeregion"
  144. wantPrice3 := "0.1339"
  145. c := &cloud.CSVProvider{
  146. CSVLocation: "../configs/pricing_schema_region.csv",
  147. CustomProvider: &cloud.CustomProvider{
  148. Config: cloud.NewProviderConfig("../configs/default.json"),
  149. },
  150. }
  151. c.DownloadPricingData()
  152. k := c.GetKey(n.Labels, n)
  153. resN, err := c.NodePricing(k)
  154. if err != nil {
  155. t.Errorf("Error in NodePricing: %s", err.Error())
  156. } else {
  157. gotPrice := resN.Cost
  158. if gotPrice != wantPrice {
  159. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  160. }
  161. }
  162. k2 := c.GetKey(n2.Labels, n2)
  163. resN2, err := c.NodePricing(k2)
  164. if err != nil {
  165. t.Errorf("Error in NodePricing: %s", err.Error())
  166. } else {
  167. gotPrice := resN2.Cost
  168. if gotPrice != wantPrice2 {
  169. t.Errorf("Wanted price '%s' got price '%s'", wantPrice2, gotPrice)
  170. }
  171. }
  172. k3 := c.GetKey(n3.Labels, n3)
  173. resN3, err := c.NodePricing(k3)
  174. if err != nil {
  175. t.Errorf("Error in NodePricing: %s", err.Error())
  176. } else {
  177. gotPrice := resN3.Cost
  178. if gotPrice != wantPrice3 {
  179. t.Errorf("Wanted price '%s' got price '%s'", wantPrice3, gotPrice)
  180. }
  181. }
  182. unknownN := &v1.Node{}
  183. unknownN.Spec.ProviderID = "fake providerID"
  184. unknownN.Name = "unknownname"
  185. unknownN.Labels = make(map[string]string)
  186. unknownN.Labels["foo"] = labelFooWant
  187. k4 := c.GetKey(unknownN.Labels, unknownN)
  188. resN4, _ := c.NodePricing(k4)
  189. if resN4 != nil {
  190. t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN4)
  191. }
  192. c2 := &cloud.CSVProvider{
  193. CSVLocation: "../configs/fake.csv",
  194. CustomProvider: &cloud.CustomProvider{
  195. Config: cloud.NewProviderConfig("../configs/default.json"),
  196. },
  197. }
  198. k5 := c.GetKey(n.Labels, n)
  199. resN5, _ := c2.NodePricing(k5)
  200. if resN5 != nil {
  201. t.Errorf("CSV provider should return nil on missing csv")
  202. }
  203. }
  204. func TestNodePriceFromCSVWithCase(t *testing.T) {
  205. n := &v1.Node{}
  206. n.Spec.ProviderID = "azure:///subscriptions/123a7sd-asd-1234-578a9-123abcdef/resourceGroups/case_12_STaGe_TeSt7/providers/Microsoft.Compute/virtualMachineScaleSets/vmss-agent-worker0-12stagetest7-ezggnore/virtualMachines/7"
  207. n.Labels = make(map[string]string)
  208. n.Labels[v1.LabelZoneRegion] = "eastus2"
  209. wantPrice := "0.13370357"
  210. c := &cloud.CSVProvider{
  211. CSVLocation: "../configs/pricing_schema_case.csv",
  212. CustomProvider: &cloud.CustomProvider{
  213. Config: cloud.NewProviderConfig("../configs/default.json"),
  214. },
  215. }
  216. c.DownloadPricingData()
  217. k := c.GetKey(n.Labels, n)
  218. resN, err := c.NodePricing(k)
  219. if err != nil {
  220. t.Errorf("Error in NodePricing: %s", err.Error())
  221. } else {
  222. gotPrice := resN.Cost
  223. if gotPrice != wantPrice {
  224. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  225. }
  226. }
  227. }