cloud_test.go 13 KB

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