cloud_test.go 9.2 KB

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