cloud_test.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. package test
  2. import (
  3. "fmt"
  4. "math"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/opencost/opencost/core/pkg/clusters"
  11. "github.com/opencost/opencost/pkg/cloud/provider"
  12. "github.com/opencost/opencost/pkg/clustercache"
  13. "github.com/opencost/opencost/pkg/config"
  14. "github.com/opencost/opencost/pkg/costmodel"
  15. v1 "k8s.io/api/core/v1"
  16. "k8s.io/apimachinery/pkg/api/resource"
  17. )
  18. const (
  19. providerIDMap = "spec.providerID"
  20. nameMap = "metadata.name"
  21. labelMapFoo = "metadata.labels.foo"
  22. labelMapFooBar = "metadata.labels.foo.bar"
  23. )
  24. func TestRegionValueFromMapField(t *testing.T) {
  25. wantRegion := "useast"
  26. wantpid := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0")
  27. providerIDWant := wantRegion + "," + wantpid
  28. n := &clustercache.Node{}
  29. n.SpecProviderID = "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0"
  30. n.Labels = make(map[string]string)
  31. n.Labels[v1.LabelTopologyRegion] = wantRegion
  32. got := provider.NodeValueFromMapField(providerIDMap, n, true)
  33. if got != providerIDWant {
  34. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
  35. }
  36. }
  37. func TestTransformedValueFromMapField(t *testing.T) {
  38. providerIDWant := "i-05445591e0d182d42"
  39. n := &clustercache.Node{}
  40. n.SpecProviderID = "aws:///us-east-1a/i-05445591e0d182d42"
  41. got := provider.NodeValueFromMapField(providerIDMap, n, false)
  42. if got != providerIDWant {
  43. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
  44. }
  45. providerIDWant2 := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0")
  46. n2 := &clustercache.Node{}
  47. n2.SpecProviderID = "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0"
  48. got2 := provider.NodeValueFromMapField(providerIDMap, n2, false)
  49. if got2 != providerIDWant2 {
  50. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant2, got2)
  51. }
  52. providerIDWant3 := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/mc_testspot_testspot_eastus/providers/Microsoft.Compute/virtualMachineScaleSets/aks-nodepool1-19213364-vmss/virtualMachines/0")
  53. n3 := &clustercache.Node{}
  54. n3.SpecProviderID = "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/mc_testspot_testspot_eastus/providers/Microsoft.Compute/virtualMachineScaleSets/aks-nodepool1-19213364-vmss/virtualMachines/0"
  55. got3 := provider.NodeValueFromMapField(providerIDMap, n3, false)
  56. if got3 != providerIDWant3 {
  57. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant3, got3)
  58. }
  59. }
  60. func TestNodeValueFromMapField(t *testing.T) {
  61. providerIDWant := "providerid"
  62. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  63. labelFooWant := "labelfoo"
  64. n := &clustercache.Node{}
  65. n.SpecProviderID = providerIDWant
  66. n.Name = nameWant
  67. n.Labels = make(map[string]string)
  68. n.Labels["foo"] = labelFooWant
  69. got := provider.NodeValueFromMapField(providerIDMap, n, false)
  70. if got != providerIDWant {
  71. t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
  72. }
  73. got = provider.NodeValueFromMapField(nameMap, n, false)
  74. if got != nameWant {
  75. t.Errorf("Assert on '%s' want '%s' got '%s'", nameMap, nameWant, got)
  76. }
  77. got = provider.NodeValueFromMapField(labelMapFoo, n, false)
  78. if got != labelFooWant {
  79. t.Errorf("Assert on '%s' want '%s' got '%s'", labelMapFoo, labelFooWant, got)
  80. }
  81. }
  82. func TestPVPriceFromCSV(t *testing.T) {
  83. nameWant := "pvc-08e1f205-d7a9-4430-90fc-7b3965a18c4d"
  84. pv := &clustercache.PersistentVolume{}
  85. pv.Name = nameWant
  86. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  87. LocalConfigPath: "./",
  88. })
  89. wantPrice := "0.1337"
  90. c := &provider.CSVProvider{
  91. CSVLocation: "../configs/pricing_schema_pv.csv",
  92. CustomProvider: &provider.CustomProvider{
  93. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  94. },
  95. }
  96. c.DownloadPricingData()
  97. k := c.GetPVKey(pv, make(map[string]string), "")
  98. resPV, err := c.PVPricing(k)
  99. if err != nil {
  100. t.Errorf("Error in NodePricing: %s", err.Error())
  101. } else {
  102. gotPrice := resPV.Cost
  103. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  104. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  105. if gotPriceFloat != wantPriceFloat {
  106. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  107. }
  108. }
  109. }
  110. func TestPVPriceFromCSVStorageClass(t *testing.T) {
  111. nameWant := "pvc-08e1f205-d7a9-4430-90fc-7b3965a18c4d"
  112. storageClassWant := "storageclass0"
  113. pv := &clustercache.PersistentVolume{}
  114. pv.Name = nameWant
  115. pv.Spec.StorageClassName = storageClassWant
  116. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  117. LocalConfigPath: "./",
  118. })
  119. wantPrice := "0.1338"
  120. c := &provider.CSVProvider{
  121. CSVLocation: "../configs/pricing_schema_pv_storageclass.csv",
  122. CustomProvider: &provider.CustomProvider{
  123. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  124. },
  125. }
  126. c.DownloadPricingData()
  127. k := c.GetPVKey(pv, make(map[string]string), "")
  128. resPV, err := c.PVPricing(k)
  129. if err != nil {
  130. t.Errorf("Error in NodePricing: %s", err.Error())
  131. } else {
  132. gotPrice := resPV.Cost
  133. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  134. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  135. if gotPriceFloat != wantPriceFloat {
  136. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  137. }
  138. }
  139. }
  140. func TestNodePriceFromCSVWithGPU(t *testing.T) {
  141. providerIDWant := "providerid"
  142. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  143. labelFooWant := "labelfoo"
  144. wantGPU := "2"
  145. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  146. LocalConfigPath: "./",
  147. })
  148. n := &clustercache.Node{}
  149. n.SpecProviderID = providerIDWant
  150. n.Name = nameWant
  151. n.Labels = make(map[string]string)
  152. n.Labels["foo"] = labelFooWant
  153. n.Labels["nvidia.com/gpu_type"] = "Quadro_RTX_4000"
  154. n.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
  155. wantPrice := "1.633700"
  156. n2 := &clustercache.Node{}
  157. n2.SpecProviderID = providerIDWant
  158. n2.Name = nameWant
  159. n2.Labels = make(map[string]string)
  160. n2.Labels["foo"] = labelFooWant
  161. n2.Labels["gpu.nvidia.com/class"] = "Quadro_RTX_4001"
  162. n2.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
  163. wantPrice2 := "1.733700"
  164. c := &provider.CSVProvider{
  165. CSVLocation: "../configs/pricing_schema.csv",
  166. CustomProvider: &provider.CustomProvider{
  167. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  168. },
  169. }
  170. c.DownloadPricingData()
  171. k := c.GetKey(n.Labels, n)
  172. resN, _, err := c.NodePricing(k)
  173. if err != nil {
  174. t.Errorf("Error in NodePricing: %s", err.Error())
  175. } else {
  176. gotGPU := resN.GPU
  177. gotPrice := resN.Cost
  178. if gotGPU != wantGPU {
  179. t.Errorf("Wanted gpu count '%s' got gpu count '%s'", wantGPU, gotGPU)
  180. }
  181. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  182. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  183. if gotPriceFloat != wantPriceFloat {
  184. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  185. }
  186. }
  187. k2 := c.GetKey(n2.Labels, n2)
  188. resN2, _, err := c.NodePricing(k2)
  189. if err != nil {
  190. t.Errorf("Error in NodePricing: %s", err.Error())
  191. } else {
  192. gotGPU := resN2.GPU
  193. gotPrice := resN2.Cost
  194. if gotGPU != wantGPU {
  195. t.Errorf("Wanted gpu count '%s' got gpu count '%s'", wantGPU, gotGPU)
  196. }
  197. wantPriceFloat, _ := strconv.ParseFloat(wantPrice2, 64)
  198. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  199. if gotPriceFloat != wantPriceFloat {
  200. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  201. }
  202. }
  203. }
  204. func TestNodePriceFromCSVWithGPULabels(t *testing.T) {
  205. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  206. wantGPUCost := "0.75"
  207. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  208. LocalConfigPath: "./",
  209. })
  210. n := &clustercache.Node{}
  211. n.SpecProviderID = "providerid"
  212. n.Name = nameWant
  213. n.Labels = make(map[string]string)
  214. n.Labels["foo"] = "labelfoo"
  215. n.Labels["nvidia.com/gpu_type"] = "Quadro_RTX_4000"
  216. n.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
  217. c := &provider.CSVProvider{
  218. CSVLocation: "../configs/pricing_schema_gpu_labels.csv",
  219. CustomProvider: &provider.CustomProvider{
  220. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  221. },
  222. }
  223. c.DownloadPricingData()
  224. fc := NewFakeNodeCache([]*clustercache.Node{n})
  225. fm := FakeClusterMap{}
  226. d, _ := time.ParseDuration("1m")
  227. model := costmodel.NewCostModel(nil, nil, fc, fm, d)
  228. nodeMap, err := model.GetNodeCost(c)
  229. if err != nil {
  230. t.Errorf("Error in NodePricing: %s", err.Error())
  231. } else {
  232. if node, ok := nodeMap[nameWant]; ok {
  233. if node.GPUCost != wantGPUCost {
  234. t.Errorf("Wanted gpu cost '%v' got gpu cost '%v'", wantGPUCost, node.GPUCost)
  235. }
  236. } else {
  237. t.Errorf("Node %s not found in node map", nameWant)
  238. }
  239. }
  240. }
  241. func TestRKE2NodePriceFromCSVWithGPULabels(t *testing.T) {
  242. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  243. wantGPUCost := "0.750000"
  244. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  245. LocalConfigPath: "./",
  246. })
  247. n := &clustercache.Node{}
  248. n.SpecProviderID = "providerid"
  249. n.Name = nameWant
  250. n.Labels = make(map[string]string)
  251. n.Labels["foo"] = "labelfoo"
  252. n.Labels["nvidia.com/gpu_type"] = "Quadro_RTX_4000"
  253. n.Labels[v1.LabelInstanceTypeStable] = "rke2"
  254. n.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
  255. c := &provider.CSVProvider{
  256. CSVLocation: "../configs/pricing_schema_gpu_labels.csv",
  257. CustomProvider: &provider.CustomProvider{
  258. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  259. },
  260. }
  261. c.DownloadPricingData()
  262. fc := NewFakeNodeCache([]*clustercache.Node{n})
  263. fm := FakeClusterMap{}
  264. d, _ := time.ParseDuration("1m")
  265. model := costmodel.NewCostModel(nil, nil, fc, fm, d)
  266. nodeMap, err := model.GetNodeCost(c)
  267. if err != nil {
  268. t.Errorf("Error in NodePricing: %s", err.Error())
  269. } else {
  270. if node, ok := nodeMap[nameWant]; ok {
  271. if node.GPUCost != wantGPUCost {
  272. t.Errorf("Wanted gpu cost '%v' got gpu cost '%v'", wantGPUCost, node.GPUCost)
  273. }
  274. } else {
  275. t.Errorf("Node %s not found in node map", nameWant)
  276. }
  277. }
  278. }
  279. func TestNodePriceFromCSVSpecialChar(t *testing.T) {
  280. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  281. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  282. LocalConfigPath: "./",
  283. })
  284. n := &clustercache.Node{}
  285. n.Name = nameWant
  286. n.Labels = make(map[string]string)
  287. n.Labels["<http://metadata.label.servers.com/label|metadata.label.servers.com/label>"] = nameWant
  288. wantPrice := "0.133700"
  289. c := &provider.CSVProvider{
  290. CSVLocation: "../configs/pricing_schema_special_char.csv",
  291. CustomProvider: &provider.CustomProvider{
  292. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  293. },
  294. }
  295. c.DownloadPricingData()
  296. k := c.GetKey(n.Labels, n)
  297. resN, _, err := c.NodePricing(k)
  298. if err != nil {
  299. t.Errorf("Error in NodePricing: %s", err.Error())
  300. } else {
  301. gotPrice := resN.Cost
  302. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  303. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  304. if gotPriceFloat != wantPriceFloat {
  305. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  306. }
  307. }
  308. }
  309. func TestNodePriceFromCSV(t *testing.T) {
  310. providerIDWant := "providerid"
  311. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  312. labelFooWant := "labelfoo"
  313. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  314. LocalConfigPath: "./",
  315. })
  316. n := &clustercache.Node{}
  317. n.SpecProviderID = providerIDWant
  318. n.Name = nameWant
  319. n.Labels = make(map[string]string)
  320. n.Labels["foo"] = labelFooWant
  321. wantPrice := "0.133700"
  322. c := &provider.CSVProvider{
  323. CSVLocation: "../configs/pricing_schema.csv",
  324. CustomProvider: &provider.CustomProvider{
  325. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  326. },
  327. }
  328. c.DownloadPricingData()
  329. k := c.GetKey(n.Labels, n)
  330. resN, _, err := c.NodePricing(k)
  331. if err != nil {
  332. t.Errorf("Error in NodePricing: %s", err.Error())
  333. } else {
  334. gotPrice := resN.Cost
  335. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  336. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  337. if gotPriceFloat != wantPriceFloat {
  338. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  339. }
  340. }
  341. unknownN := &clustercache.Node{}
  342. unknownN.SpecProviderID = providerIDWant
  343. unknownN.Name = "unknownname"
  344. unknownN.Labels = make(map[string]string)
  345. unknownN.Labels["foo"] = labelFooWant
  346. unknownN.Labels[v1.LabelTopologyRegion] = "fakeregion"
  347. k2 := c.GetKey(unknownN.Labels, unknownN)
  348. resN2, _, _ := c.NodePricing(k2)
  349. if resN2 != nil {
  350. t.Errorf("CSV provider should return nil on missing node")
  351. }
  352. c2 := &provider.CSVProvider{
  353. CSVLocation: "../configs/fake.csv",
  354. CustomProvider: &provider.CustomProvider{
  355. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  356. },
  357. }
  358. k3 := c.GetKey(n.Labels, n)
  359. resN3, _, _ := c2.NodePricing(k3)
  360. if resN3 != nil {
  361. t.Errorf("CSV provider should return nil on missing csv")
  362. }
  363. }
  364. func TestNodePriceFromCSVWithRegion(t *testing.T) {
  365. providerIDWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  366. nameWant := "foo"
  367. labelFooWant := "labelfoo"
  368. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  369. LocalConfigPath: "./",
  370. })
  371. n := &clustercache.Node{}
  372. n.SpecProviderID = providerIDWant
  373. n.Name = nameWant
  374. n.Labels = make(map[string]string)
  375. n.Labels["foo"] = labelFooWant
  376. n.Labels[v1.LabelTopologyRegion] = "regionone"
  377. wantPrice := "0.133700"
  378. n2 := &clustercache.Node{}
  379. n2.SpecProviderID = providerIDWant
  380. n2.Name = nameWant
  381. n2.Labels = make(map[string]string)
  382. n2.Labels["foo"] = labelFooWant
  383. n2.Labels[v1.LabelTopologyRegion] = "regiontwo"
  384. wantPrice2 := "0.133800"
  385. n3 := &clustercache.Node{}
  386. n3.SpecProviderID = providerIDWant
  387. n3.Name = nameWant
  388. n3.Labels = make(map[string]string)
  389. n3.Labels["foo"] = labelFooWant
  390. n3.Labels[v1.LabelTopologyRegion] = "fakeregion"
  391. wantPrice3 := "0.1339"
  392. c := &provider.CSVProvider{
  393. CSVLocation: "../configs/pricing_schema_region.csv",
  394. CustomProvider: &provider.CustomProvider{
  395. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  396. },
  397. }
  398. c.DownloadPricingData()
  399. k := c.GetKey(n.Labels, n)
  400. resN, _, err := c.NodePricing(k)
  401. if err != nil {
  402. t.Errorf("Error in NodePricing: %s", err.Error())
  403. } else {
  404. gotPrice := resN.Cost
  405. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  406. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  407. if gotPriceFloat != wantPriceFloat {
  408. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  409. }
  410. }
  411. k2 := c.GetKey(n2.Labels, n2)
  412. resN2, _, err := c.NodePricing(k2)
  413. if err != nil {
  414. t.Errorf("Error in NodePricing: %s", err.Error())
  415. } else {
  416. gotPrice := resN2.Cost
  417. wantPriceFloat, _ := strconv.ParseFloat(wantPrice2, 64)
  418. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  419. if gotPriceFloat != wantPriceFloat {
  420. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  421. }
  422. }
  423. k3 := c.GetKey(n3.Labels, n3)
  424. resN3, _, err := c.NodePricing(k3)
  425. if err != nil {
  426. t.Errorf("Error in NodePricing: %s", err.Error())
  427. } else {
  428. gotPrice := resN3.Cost
  429. wantPriceFloat, _ := strconv.ParseFloat(wantPrice3, 64)
  430. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  431. if gotPriceFloat != wantPriceFloat {
  432. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  433. }
  434. }
  435. unknownN := &clustercache.Node{}
  436. unknownN.SpecProviderID = "fake providerID"
  437. unknownN.Name = "unknownname"
  438. unknownN.Labels = make(map[string]string)
  439. unknownN.Labels[v1.LabelTopologyRegion] = "fakeregion"
  440. unknownN.Labels["foo"] = labelFooWant
  441. k4 := c.GetKey(unknownN.Labels, unknownN)
  442. resN4, _, _ := c.NodePricing(k4)
  443. if resN4 != nil {
  444. t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN4)
  445. }
  446. c2 := &provider.CSVProvider{
  447. CSVLocation: "../configs/fake.csv",
  448. CustomProvider: &provider.CustomProvider{
  449. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  450. },
  451. }
  452. k5 := c.GetKey(n.Labels, n)
  453. resN5, _, _ := c2.NodePricing(k5)
  454. if resN5 != nil {
  455. t.Errorf("CSV provider should return nil on missing csv")
  456. }
  457. }
  458. type FakeCache struct {
  459. nodes []*clustercache.Node
  460. clustercache.ClusterCache
  461. }
  462. func (f FakeCache) GetAllNodes() []*clustercache.Node {
  463. return f.nodes
  464. }
  465. func (f FakeCache) GetAllDaemonSets() []*clustercache.DaemonSet {
  466. return nil
  467. }
  468. func NewFakeNodeCache(nodes []*clustercache.Node) FakeCache {
  469. return FakeCache{
  470. nodes: nodes,
  471. }
  472. }
  473. type FakeClusterMap struct {
  474. clusters.ClusterMap
  475. }
  476. func TestNodePriceFromCSVWithBadConfig(t *testing.T) {
  477. os.Setenv("CONFIG_PATH", "../config")
  478. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  479. LocalConfigPath: "./",
  480. })
  481. c := &provider.CSVProvider{
  482. CSVLocation: "../configs/pricing_schema_case.csv",
  483. CustomProvider: &provider.CustomProvider{
  484. Config: provider.NewProviderConfig(confMan, "invalid.json"),
  485. },
  486. }
  487. c.DownloadPricingData()
  488. n := &clustercache.Node{}
  489. n.SpecProviderID = "fake"
  490. n.Name = "nameWant"
  491. n.Labels = make(map[string]string)
  492. n.Labels["foo"] = "labelFooWant"
  493. n.Labels[v1.LabelTopologyRegion] = "regionone"
  494. fc := NewFakeNodeCache([]*clustercache.Node{n})
  495. fm := FakeClusterMap{}
  496. d, _ := time.ParseDuration("1m")
  497. model := costmodel.NewCostModel(nil, nil, fc, fm, d)
  498. _, err := model.GetNodeCost(c)
  499. if err != nil {
  500. t.Errorf("Error in node pricing: %s", err)
  501. }
  502. }
  503. func TestSourceMatchesFromCSV(t *testing.T) {
  504. os.Setenv("CONFIG_PATH", "../configs")
  505. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  506. LocalConfigPath: "./",
  507. })
  508. c := &provider.CSVProvider{
  509. CSVLocation: "../configs/pricing_schema_case.csv",
  510. CustomProvider: &provider.CustomProvider{
  511. Config: provider.NewProviderConfig(confMan, "/default.json"),
  512. },
  513. }
  514. c.DownloadPricingData()
  515. n := &clustercache.Node{}
  516. n.SpecProviderID = "fake"
  517. n.Name = "nameWant"
  518. n.Labels = make(map[string]string)
  519. n.Labels["foo"] = "labelFooWant"
  520. n.Labels[v1.LabelTopologyRegion] = "regionone"
  521. n2 := &clustercache.Node{}
  522. n2.SpecProviderID = "azure:///subscriptions/123a7sd-asd-1234-578a9-123abcdef/resourceGroups/case_12_STaGe_TeSt7/providers/Microsoft.Compute/virtualMachineScaleSets/vmss-agent-worker0-12stagetest7-ezggnore/virtualMachines/7"
  523. n2.Labels = make(map[string]string)
  524. n2.Labels[v1.LabelTopologyRegion] = "eastus2"
  525. n2.Labels["foo"] = "labelFooWant"
  526. k := c.GetKey(n2.Labels, n2)
  527. resN, _, err := c.NodePricing(k)
  528. if err != nil {
  529. t.Errorf("Error in NodePricing: %s", err.Error())
  530. } else {
  531. wantPrice := "0.13370357"
  532. gotPrice := resN.Cost
  533. if gotPrice != wantPrice {
  534. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  535. }
  536. }
  537. n3 := &clustercache.Node{}
  538. n3.SpecProviderID = "fake"
  539. n3.Name = "nameWant"
  540. n3.Labels = make(map[string]string)
  541. n3.Labels[v1.LabelTopologyRegion] = "eastus2"
  542. n3.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
  543. fc := NewFakeNodeCache([]*clustercache.Node{n, n2, n3})
  544. fm := FakeClusterMap{}
  545. d, _ := time.ParseDuration("1m")
  546. model := costmodel.NewCostModel(nil, nil, fc, fm, d)
  547. _, err = model.GetNodeCost(c)
  548. if err != nil {
  549. t.Errorf("Error in node pricing: %s", err)
  550. }
  551. p, err := model.GetPricingSourceCounts()
  552. if err != nil {
  553. t.Errorf("Error in pricing source counts: %s", err)
  554. } else if p.TotalNodes != 3 {
  555. t.Errorf("Wanted 3 nodes got %d", p.TotalNodes)
  556. }
  557. if p.PricingTypeCounts[""] != 1 {
  558. t.Errorf("Wanted 1 default match got %d: %+v", p.PricingTypeCounts[""], p.PricingTypeCounts)
  559. }
  560. if p.PricingTypeCounts["csvExact"] != 1 {
  561. t.Errorf("Wanted 1 exact match got %d: %+v", p.PricingTypeCounts["csvExact"], p.PricingTypeCounts)
  562. }
  563. if p.PricingTypeCounts["csvClass"] != 1 {
  564. t.Errorf("Wanted 1 class match got %d: %+v", p.PricingTypeCounts["csvClass"], p.PricingTypeCounts)
  565. }
  566. }
  567. func TestNodePriceFromCSVWithCase(t *testing.T) {
  568. n := &clustercache.Node{}
  569. n.SpecProviderID = "azure:///subscriptions/123a7sd-asd-1234-578a9-123abcdef/resourceGroups/case_12_STaGe_TeSt7/providers/Microsoft.Compute/virtualMachineScaleSets/vmss-agent-worker0-12stagetest7-ezggnore/virtualMachines/7"
  570. n.Labels = make(map[string]string)
  571. n.Labels[v1.LabelTopologyRegion] = "eastus2"
  572. wantPrice := "0.13370357"
  573. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  574. LocalConfigPath: "./",
  575. })
  576. c := &provider.CSVProvider{
  577. CSVLocation: "../configs/pricing_schema_case.csv",
  578. CustomProvider: &provider.CustomProvider{
  579. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  580. },
  581. }
  582. c.DownloadPricingData()
  583. k := c.GetKey(n.Labels, n)
  584. resN, _, err := c.NodePricing(k)
  585. if err != nil {
  586. t.Errorf("Error in NodePricing: %s", err.Error())
  587. } else {
  588. gotPrice := resN.Cost
  589. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  590. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  591. if gotPriceFloat != wantPriceFloat {
  592. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  593. }
  594. }
  595. }
  596. func TestNodePriceFromCSVMixed(t *testing.T) {
  597. labelFooWant := "OnDemand"
  598. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  599. LocalConfigPath: "./",
  600. })
  601. n := &clustercache.Node{}
  602. n.Labels = make(map[string]string)
  603. n.Labels["TestClusterUsage"] = labelFooWant
  604. n.Labels["nvidia.com/gpu_type"] = "a100-ondemand"
  605. n.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
  606. wantPrice := "1.904110"
  607. labelFooWant2 := "Reserved"
  608. n2 := &clustercache.Node{}
  609. n2.Labels = make(map[string]string)
  610. n2.Labels["TestClusterUsage"] = labelFooWant2
  611. n2.Labels["nvidia.com/gpu_type"] = "a100-reserved"
  612. n2.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(1, 0)}
  613. wantPrice2 := "1.654795"
  614. c := &provider.CSVProvider{
  615. CSVLocation: "../configs/pricing_schema_mixed_gpu_ondemand.csv",
  616. CustomProvider: &provider.CustomProvider{
  617. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  618. },
  619. }
  620. c.DownloadPricingData()
  621. k := c.GetKey(n.Labels, n)
  622. resN, _, err := c.NodePricing(k)
  623. if err != nil {
  624. t.Errorf("Error in NodePricing: %s", err.Error())
  625. } else {
  626. gotPrice := resN.Cost
  627. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  628. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  629. if gotPriceFloat != wantPriceFloat {
  630. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  631. }
  632. }
  633. k2 := c.GetKey(n2.Labels, n2)
  634. resN2, _, err2 := c.NodePricing(k2)
  635. if err2 != nil {
  636. t.Errorf("Error in NodePricing: %s", err.Error())
  637. } else {
  638. gotPrice := resN2.Cost
  639. wantPriceFloat, _ := strconv.ParseFloat(wantPrice2, 64)
  640. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  641. if gotPriceFloat != wantPriceFloat {
  642. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  643. }
  644. }
  645. }
  646. func TestNodePriceFromCSVByClass(t *testing.T) {
  647. n := &clustercache.Node{}
  648. n.SpecProviderID = "fakeproviderid"
  649. n.Labels = make(map[string]string)
  650. n.Labels[v1.LabelTopologyRegion] = "eastus2"
  651. n.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
  652. wantpricefloat := 0.13370357
  653. wantPrice := fmt.Sprintf("%f", (math.Round(wantpricefloat*1000000) / 1000000))
  654. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  655. LocalConfigPath: "./",
  656. })
  657. c := &provider.CSVProvider{
  658. CSVLocation: "../configs/pricing_schema_case.csv",
  659. CustomProvider: &provider.CustomProvider{
  660. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  661. },
  662. }
  663. c.DownloadPricingData()
  664. k := c.GetKey(n.Labels, n)
  665. resN, _, err := c.NodePricing(k)
  666. if err != nil {
  667. t.Errorf("Error in NodePricing: %s", err.Error())
  668. } else {
  669. gotPrice := resN.Cost
  670. if gotPrice != wantPrice {
  671. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  672. }
  673. }
  674. n2 := &clustercache.Node{}
  675. n2.SpecProviderID = "fakeproviderid"
  676. n2.Labels = make(map[string]string)
  677. n2.Labels[v1.LabelTopologyRegion] = "fakeregion"
  678. n2.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
  679. k2 := c.GetKey(n2.Labels, n)
  680. c.DownloadPricingData()
  681. resN2, _, err := c.NodePricing(k2)
  682. if resN2 != nil {
  683. t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN2)
  684. }
  685. }