cloud_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. package test
  2. import (
  3. "fmt"
  4. "math"
  5. "os"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/kubecost/cost-model/pkg/cloud"
  10. "github.com/kubecost/cost-model/pkg/clustercache"
  11. "github.com/kubecost/cost-model/pkg/costmodel"
  12. "github.com/kubecost/cost-model/pkg/costmodel/clusters"
  13. v1 "k8s.io/api/core/v1"
  14. )
  15. const (
  16. providerIDMap = "spec.providerID"
  17. nameMap = "metadata.name"
  18. labelMapFoo = "metadata.labels.foo"
  19. )
  20. func TestRegionValueFromMapField(t *testing.T) {
  21. wantRegion := "useast"
  22. wantpid := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0")
  23. providerIDWant := wantRegion + "," + wantpid
  24. n := &v1.Node{}
  25. n.Spec.ProviderID = "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0"
  26. n.Labels = make(map[string]string)
  27. n.Labels[v1.LabelZoneRegion] = wantRegion
  28. got := cloud.NodeValueFromMapField(providerIDMap, n, true)
  29. if got != providerIDWant {
  30. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
  31. }
  32. }
  33. func TestTransformedValueFromMapField(t *testing.T) {
  34. providerIDWant := "i-05445591e0d182d42"
  35. n := &v1.Node{}
  36. n.Spec.ProviderID = "aws:///us-east-1a/i-05445591e0d182d42"
  37. got := cloud.NodeValueFromMapField(providerIDMap, n, false)
  38. if got != providerIDWant {
  39. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
  40. }
  41. providerIDWant2 := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0")
  42. n2 := &v1.Node{}
  43. n2.Spec.ProviderID = "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0"
  44. got2 := cloud.NodeValueFromMapField(providerIDMap, n2, false)
  45. if got2 != providerIDWant2 {
  46. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant2, got2)
  47. }
  48. providerIDWant3 := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/mc_testspot_testspot_eastus/providers/Microsoft.Compute/virtualMachineScaleSets/aks-nodepool1-19213364-vmss/virtualMachines/0")
  49. n3 := &v1.Node{}
  50. 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"
  51. got3 := cloud.NodeValueFromMapField(providerIDMap, n3, false)
  52. if got3 != providerIDWant3 {
  53. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant3, got3)
  54. }
  55. }
  56. func TestNodeValueFromMapField(t *testing.T) {
  57. providerIDWant := "providerid"
  58. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  59. labelFooWant := "labelfoo"
  60. n := &v1.Node{}
  61. n.Spec.ProviderID = providerIDWant
  62. n.Name = nameWant
  63. n.Labels = make(map[string]string)
  64. n.Labels["foo"] = labelFooWant
  65. got := cloud.NodeValueFromMapField(providerIDMap, n, false)
  66. if got != providerIDWant {
  67. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
  68. }
  69. got = cloud.NodeValueFromMapField(nameMap, n, false)
  70. if got != nameWant {
  71. t.Errorf("Assert on '%s' want '%s' got '%s'", nameMap, nameWant, got)
  72. }
  73. got = cloud.NodeValueFromMapField(labelMapFoo, n, false)
  74. if got != labelFooWant {
  75. t.Errorf("Assert on '%s' want '%s' got '%s'", labelMapFoo, labelFooWant, got)
  76. }
  77. }
  78. func TestNodePriceFromCSV(t *testing.T) {
  79. providerIDWant := "providerid"
  80. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  81. labelFooWant := "labelfoo"
  82. n := &v1.Node{}
  83. n.Spec.ProviderID = providerIDWant
  84. n.Name = nameWant
  85. n.Labels = make(map[string]string)
  86. n.Labels["foo"] = labelFooWant
  87. wantPrice := "0.1337"
  88. c := &cloud.CSVProvider{
  89. CSVLocation: "../configs/pricing_schema.csv",
  90. CustomProvider: &cloud.CustomProvider{
  91. Config: cloud.NewProviderConfig("../configs/default.json"),
  92. },
  93. }
  94. c.DownloadPricingData()
  95. k := c.GetKey(n.Labels, n)
  96. resN, err := c.NodePricing(k)
  97. if err != nil {
  98. t.Errorf("Error in NodePricing: %s", err.Error())
  99. } else {
  100. gotPrice := resN.Cost
  101. if gotPrice != wantPrice {
  102. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  103. }
  104. }
  105. unknownN := &v1.Node{}
  106. unknownN.Spec.ProviderID = providerIDWant
  107. unknownN.Name = "unknownname"
  108. unknownN.Labels = make(map[string]string)
  109. unknownN.Labels["foo"] = labelFooWant
  110. unknownN.Labels["topology.kubernetes.io/region"] = "fakeregion"
  111. k2 := c.GetKey(unknownN.Labels, unknownN)
  112. resN2, _ := c.NodePricing(k2)
  113. if resN2 != nil {
  114. t.Errorf("CSV provider should return nil on missing node")
  115. }
  116. c2 := &cloud.CSVProvider{
  117. CSVLocation: "../configs/fake.csv",
  118. CustomProvider: &cloud.CustomProvider{
  119. Config: cloud.NewProviderConfig("../configs/default.json"),
  120. },
  121. }
  122. k3 := c.GetKey(n.Labels, n)
  123. resN3, _ := c2.NodePricing(k3)
  124. if resN3 != nil {
  125. t.Errorf("CSV provider should return nil on missing csv")
  126. }
  127. }
  128. func TestNodePriceFromCSVWithRegion(t *testing.T) {
  129. providerIDWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  130. nameWant := "foo"
  131. labelFooWant := "labelfoo"
  132. n := &v1.Node{}
  133. n.Spec.ProviderID = providerIDWant
  134. n.Name = nameWant
  135. n.Labels = make(map[string]string)
  136. n.Labels["foo"] = labelFooWant
  137. n.Labels[v1.LabelZoneRegion] = "regionone"
  138. wantPrice := "0.1337"
  139. n2 := &v1.Node{}
  140. n2.Spec.ProviderID = providerIDWant
  141. n2.Name = nameWant
  142. n2.Labels = make(map[string]string)
  143. n2.Labels["foo"] = labelFooWant
  144. n2.Labels[v1.LabelZoneRegion] = "regiontwo"
  145. wantPrice2 := "0.1338"
  146. n3 := &v1.Node{}
  147. n3.Spec.ProviderID = providerIDWant
  148. n3.Name = nameWant
  149. n3.Labels = make(map[string]string)
  150. n3.Labels["foo"] = labelFooWant
  151. n3.Labels[v1.LabelZoneRegion] = "fakeregion"
  152. wantPrice3 := "0.1339"
  153. c := &cloud.CSVProvider{
  154. CSVLocation: "../configs/pricing_schema_region.csv",
  155. CustomProvider: &cloud.CustomProvider{
  156. Config: cloud.NewProviderConfig("../configs/default.json"),
  157. },
  158. }
  159. c.DownloadPricingData()
  160. k := c.GetKey(n.Labels, n)
  161. resN, err := c.NodePricing(k)
  162. if err != nil {
  163. t.Errorf("Error in NodePricing: %s", err.Error())
  164. } else {
  165. gotPrice := resN.Cost
  166. if gotPrice != wantPrice {
  167. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  168. }
  169. }
  170. k2 := c.GetKey(n2.Labels, n2)
  171. resN2, err := c.NodePricing(k2)
  172. if err != nil {
  173. t.Errorf("Error in NodePricing: %s", err.Error())
  174. } else {
  175. gotPrice := resN2.Cost
  176. if gotPrice != wantPrice2 {
  177. t.Errorf("Wanted price '%s' got price '%s'", wantPrice2, gotPrice)
  178. }
  179. }
  180. k3 := c.GetKey(n3.Labels, n3)
  181. resN3, err := c.NodePricing(k3)
  182. if err != nil {
  183. t.Errorf("Error in NodePricing: %s", err.Error())
  184. } else {
  185. gotPrice := resN3.Cost
  186. if gotPrice != wantPrice3 {
  187. t.Errorf("Wanted price '%s' got price '%s'", wantPrice3, gotPrice)
  188. }
  189. }
  190. unknownN := &v1.Node{}
  191. unknownN.Spec.ProviderID = "fake providerID"
  192. unknownN.Name = "unknownname"
  193. unknownN.Labels = make(map[string]string)
  194. unknownN.Labels["topology.kubernetes.io/region"] = "fakeregion"
  195. unknownN.Labels["foo"] = labelFooWant
  196. k4 := c.GetKey(unknownN.Labels, unknownN)
  197. resN4, _ := c.NodePricing(k4)
  198. if resN4 != nil {
  199. t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN4)
  200. }
  201. c2 := &cloud.CSVProvider{
  202. CSVLocation: "../configs/fake.csv",
  203. CustomProvider: &cloud.CustomProvider{
  204. Config: cloud.NewProviderConfig("../configs/default.json"),
  205. },
  206. }
  207. k5 := c.GetKey(n.Labels, n)
  208. resN5, _ := c2.NodePricing(k5)
  209. if resN5 != nil {
  210. t.Errorf("CSV provider should return nil on missing csv")
  211. }
  212. }
  213. type FakeCache struct {
  214. nodes []*v1.Node
  215. clustercache.ClusterCache
  216. }
  217. func (f FakeCache) GetAllNodes() []*v1.Node {
  218. return f.nodes
  219. }
  220. func NewFakeNodeCache(nodes []*v1.Node) FakeCache {
  221. return FakeCache{
  222. nodes: nodes,
  223. }
  224. }
  225. type FakeClusterMap struct {
  226. clusters.ClusterMap
  227. }
  228. func TestNodePriceFromCSVWithBadConfig(t *testing.T) {
  229. os.Setenv("CONFIG_PATH", "../config")
  230. c := &cloud.CSVProvider{
  231. CSVLocation: "../configs/pricing_schema_case.csv",
  232. CustomProvider: &cloud.CustomProvider{
  233. Config: cloud.NewProviderConfig("invalid.json"),
  234. },
  235. }
  236. c.DownloadPricingData()
  237. n := &v1.Node{}
  238. n.Spec.ProviderID = "fake"
  239. n.Name = "nameWant"
  240. n.Labels = make(map[string]string)
  241. n.Labels["foo"] = "labelFooWant"
  242. n.Labels[v1.LabelZoneRegion] = "regionone"
  243. fc := NewFakeNodeCache([]*v1.Node{n})
  244. fm := FakeClusterMap{}
  245. d, _ := time.ParseDuration("1m")
  246. model := costmodel.NewCostModel(nil, nil, fc, fm, d)
  247. _, err := model.GetNodeCost(c)
  248. if err != nil {
  249. t.Errorf("Error in node pricing: %s", err)
  250. }
  251. }
  252. func TestSourceMatchesFromCSV(t *testing.T) {
  253. os.Setenv("CONFIG_PATH", "../configs")
  254. c := &cloud.CSVProvider{
  255. CSVLocation: "../configs/pricing_schema_case.csv",
  256. CustomProvider: &cloud.CustomProvider{
  257. Config: cloud.NewProviderConfig("/default.json"),
  258. },
  259. }
  260. c.DownloadPricingData()
  261. n := &v1.Node{}
  262. n.Spec.ProviderID = "fake"
  263. n.Name = "nameWant"
  264. n.Labels = make(map[string]string)
  265. n.Labels["foo"] = "labelFooWant"
  266. n.Labels[v1.LabelZoneRegion] = "regionone"
  267. n2 := &v1.Node{}
  268. n2.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"
  269. n2.Labels = make(map[string]string)
  270. n2.Labels[v1.LabelZoneRegion] = "eastus2"
  271. n2.Labels["foo"] = "labelFooWant"
  272. k := c.GetKey(n2.Labels, n2)
  273. resN, err := c.NodePricing(k)
  274. if err != nil {
  275. t.Errorf("Error in NodePricing: %s", err.Error())
  276. } else {
  277. wantPrice := "0.13370357"
  278. gotPrice := resN.Cost
  279. if gotPrice != wantPrice {
  280. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  281. }
  282. }
  283. n3 := &v1.Node{}
  284. n3.Spec.ProviderID = "fake"
  285. n3.Name = "nameWant"
  286. n3.Labels = make(map[string]string)
  287. n.Labels[v1.LabelZoneRegion] = "eastus2"
  288. n.Labels[v1.LabelInstanceType] = "Standard_F32s_v2"
  289. fc := NewFakeNodeCache([]*v1.Node{n, n2, n3})
  290. fm := FakeClusterMap{}
  291. d, _ := time.ParseDuration("1m")
  292. model := costmodel.NewCostModel(nil, nil, fc, fm, d)
  293. _, err = model.GetNodeCost(c)
  294. if err != nil {
  295. t.Errorf("Error in node pricing: %s", err)
  296. }
  297. p, err := model.GetPricingSourceCounts()
  298. if err != nil {
  299. t.Errorf("Error in pricing source counts: %s", err)
  300. } else if p.TotalNodes != 3 {
  301. t.Errorf("Wanted 3 nodes got %d", p.TotalNodes)
  302. }
  303. if p.PricingTypeCounts[""] != 1 {
  304. t.Errorf("Wanted 1 default match got %d: %+v", p.PricingTypeCounts[""], p.PricingTypeCounts)
  305. }
  306. if p.PricingTypeCounts["csvExact"] != 1 {
  307. t.Errorf("Wanted 1 exact match got %d: %+v", p.PricingTypeCounts["csvExact"], p.PricingTypeCounts)
  308. }
  309. if p.PricingTypeCounts["csvClass"] != 1 {
  310. t.Errorf("Wanted 1 class match got %d: %+v", p.PricingTypeCounts["csvClass"], p.PricingTypeCounts)
  311. }
  312. }
  313. func TestNodePriceFromCSVWithCase(t *testing.T) {
  314. n := &v1.Node{}
  315. 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"
  316. n.Labels = make(map[string]string)
  317. n.Labels[v1.LabelZoneRegion] = "eastus2"
  318. wantPrice := "0.13370357"
  319. c := &cloud.CSVProvider{
  320. CSVLocation: "../configs/pricing_schema_case.csv",
  321. CustomProvider: &cloud.CustomProvider{
  322. Config: cloud.NewProviderConfig("../configs/default.json"),
  323. },
  324. }
  325. c.DownloadPricingData()
  326. k := c.GetKey(n.Labels, n)
  327. resN, err := c.NodePricing(k)
  328. if err != nil {
  329. t.Errorf("Error in NodePricing: %s", err.Error())
  330. } else {
  331. gotPrice := resN.Cost
  332. if gotPrice != wantPrice {
  333. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  334. }
  335. }
  336. }
  337. func TestNodePriceFromCSVByClass(t *testing.T) {
  338. n := &v1.Node{}
  339. n.Spec.ProviderID = "fakeproviderid"
  340. n.Labels = make(map[string]string)
  341. n.Labels[v1.LabelZoneRegion] = "eastus2"
  342. n.Labels[v1.LabelInstanceType] = "Standard_F32s_v2"
  343. wantpricefloat := 0.13370357
  344. wantPrice := fmt.Sprintf("%f", (math.Round(wantpricefloat*1000000) / 1000000))
  345. c := &cloud.CSVProvider{
  346. CSVLocation: "../configs/pricing_schema_case.csv",
  347. CustomProvider: &cloud.CustomProvider{
  348. Config: cloud.NewProviderConfig("../configs/default.json"),
  349. },
  350. }
  351. c.DownloadPricingData()
  352. k := c.GetKey(n.Labels, n)
  353. resN, err := c.NodePricing(k)
  354. if err != nil {
  355. t.Errorf("Error in NodePricing: %s", err.Error())
  356. } else {
  357. gotPrice := resN.Cost
  358. if gotPrice != wantPrice {
  359. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  360. }
  361. }
  362. n2 := &v1.Node{}
  363. n2.Spec.ProviderID = "fakeproviderid"
  364. n2.Labels = make(map[string]string)
  365. n2.Labels[v1.LabelZoneRegion] = "fakeregion"
  366. n2.Labels[v1.LabelInstanceType] = "Standard_F32s_v2"
  367. k2 := c.GetKey(n2.Labels, n)
  368. c.DownloadPricingData()
  369. resN2, err := c.NodePricing(k2)
  370. if resN2 != nil {
  371. t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN2)
  372. }
  373. }