2
0

cloud_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  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 TestNodePriceFromCSVSpecialChar(t *testing.T) {
  205. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  206. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  207. LocalConfigPath: "./",
  208. })
  209. n := &clustercache.Node{}
  210. n.Name = nameWant
  211. n.Labels = make(map[string]string)
  212. n.Labels["<http://metadata.label.servers.com/label|metadata.label.servers.com/label>"] = nameWant
  213. wantPrice := "0.133700"
  214. c := &provider.CSVProvider{
  215. CSVLocation: "../configs/pricing_schema_special_char.csv",
  216. CustomProvider: &provider.CustomProvider{
  217. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  218. },
  219. }
  220. c.DownloadPricingData()
  221. k := c.GetKey(n.Labels, n)
  222. resN, _, err := c.NodePricing(k)
  223. if err != nil {
  224. t.Errorf("Error in NodePricing: %s", err.Error())
  225. } else {
  226. gotPrice := resN.Cost
  227. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  228. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  229. if gotPriceFloat != wantPriceFloat {
  230. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  231. }
  232. }
  233. }
  234. func TestNodePriceFromCSV(t *testing.T) {
  235. providerIDWant := "providerid"
  236. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  237. labelFooWant := "labelfoo"
  238. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  239. LocalConfigPath: "./",
  240. })
  241. n := &clustercache.Node{}
  242. n.SpecProviderID = providerIDWant
  243. n.Name = nameWant
  244. n.Labels = make(map[string]string)
  245. n.Labels["foo"] = labelFooWant
  246. wantPrice := "0.133700"
  247. c := &provider.CSVProvider{
  248. CSVLocation: "../configs/pricing_schema.csv",
  249. CustomProvider: &provider.CustomProvider{
  250. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  251. },
  252. }
  253. c.DownloadPricingData()
  254. k := c.GetKey(n.Labels, n)
  255. resN, _, err := c.NodePricing(k)
  256. if err != nil {
  257. t.Errorf("Error in NodePricing: %s", err.Error())
  258. } else {
  259. gotPrice := resN.Cost
  260. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  261. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  262. if gotPriceFloat != wantPriceFloat {
  263. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  264. }
  265. }
  266. unknownN := &clustercache.Node{}
  267. unknownN.SpecProviderID = providerIDWant
  268. unknownN.Name = "unknownname"
  269. unknownN.Labels = make(map[string]string)
  270. unknownN.Labels["foo"] = labelFooWant
  271. unknownN.Labels[v1.LabelTopologyRegion] = "fakeregion"
  272. k2 := c.GetKey(unknownN.Labels, unknownN)
  273. resN2, _, _ := c.NodePricing(k2)
  274. if resN2 != nil {
  275. t.Errorf("CSV provider should return nil on missing node")
  276. }
  277. c2 := &provider.CSVProvider{
  278. CSVLocation: "../configs/fake.csv",
  279. CustomProvider: &provider.CustomProvider{
  280. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  281. },
  282. }
  283. k3 := c.GetKey(n.Labels, n)
  284. resN3, _, _ := c2.NodePricing(k3)
  285. if resN3 != nil {
  286. t.Errorf("CSV provider should return nil on missing csv")
  287. }
  288. }
  289. func TestNodePriceFromCSVWithRegion(t *testing.T) {
  290. providerIDWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  291. nameWant := "foo"
  292. labelFooWant := "labelfoo"
  293. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  294. LocalConfigPath: "./",
  295. })
  296. n := &clustercache.Node{}
  297. n.SpecProviderID = providerIDWant
  298. n.Name = nameWant
  299. n.Labels = make(map[string]string)
  300. n.Labels["foo"] = labelFooWant
  301. n.Labels[v1.LabelTopologyRegion] = "regionone"
  302. wantPrice := "0.133700"
  303. n2 := &clustercache.Node{}
  304. n2.SpecProviderID = providerIDWant
  305. n2.Name = nameWant
  306. n2.Labels = make(map[string]string)
  307. n2.Labels["foo"] = labelFooWant
  308. n2.Labels[v1.LabelTopologyRegion] = "regiontwo"
  309. wantPrice2 := "0.133800"
  310. n3 := &clustercache.Node{}
  311. n3.SpecProviderID = providerIDWant
  312. n3.Name = nameWant
  313. n3.Labels = make(map[string]string)
  314. n3.Labels["foo"] = labelFooWant
  315. n3.Labels[v1.LabelTopologyRegion] = "fakeregion"
  316. wantPrice3 := "0.1339"
  317. c := &provider.CSVProvider{
  318. CSVLocation: "../configs/pricing_schema_region.csv",
  319. CustomProvider: &provider.CustomProvider{
  320. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  321. },
  322. }
  323. c.DownloadPricingData()
  324. k := c.GetKey(n.Labels, n)
  325. resN, _, err := c.NodePricing(k)
  326. if err != nil {
  327. t.Errorf("Error in NodePricing: %s", err.Error())
  328. } else {
  329. gotPrice := resN.Cost
  330. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  331. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  332. if gotPriceFloat != wantPriceFloat {
  333. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  334. }
  335. }
  336. k2 := c.GetKey(n2.Labels, n2)
  337. resN2, _, err := c.NodePricing(k2)
  338. if err != nil {
  339. t.Errorf("Error in NodePricing: %s", err.Error())
  340. } else {
  341. gotPrice := resN2.Cost
  342. wantPriceFloat, _ := strconv.ParseFloat(wantPrice2, 64)
  343. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  344. if gotPriceFloat != wantPriceFloat {
  345. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  346. }
  347. }
  348. k3 := c.GetKey(n3.Labels, n3)
  349. resN3, _, err := c.NodePricing(k3)
  350. if err != nil {
  351. t.Errorf("Error in NodePricing: %s", err.Error())
  352. } else {
  353. gotPrice := resN3.Cost
  354. wantPriceFloat, _ := strconv.ParseFloat(wantPrice3, 64)
  355. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  356. if gotPriceFloat != wantPriceFloat {
  357. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  358. }
  359. }
  360. unknownN := &clustercache.Node{}
  361. unknownN.SpecProviderID = "fake providerID"
  362. unknownN.Name = "unknownname"
  363. unknownN.Labels = make(map[string]string)
  364. unknownN.Labels[v1.LabelTopologyRegion] = "fakeregion"
  365. unknownN.Labels["foo"] = labelFooWant
  366. k4 := c.GetKey(unknownN.Labels, unknownN)
  367. resN4, _, _ := c.NodePricing(k4)
  368. if resN4 != nil {
  369. t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN4)
  370. }
  371. c2 := &provider.CSVProvider{
  372. CSVLocation: "../configs/fake.csv",
  373. CustomProvider: &provider.CustomProvider{
  374. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  375. },
  376. }
  377. k5 := c.GetKey(n.Labels, n)
  378. resN5, _, _ := c2.NodePricing(k5)
  379. if resN5 != nil {
  380. t.Errorf("CSV provider should return nil on missing csv")
  381. }
  382. }
  383. type FakeCache struct {
  384. nodes []*clustercache.Node
  385. clustercache.ClusterCache
  386. }
  387. func (f FakeCache) GetAllNodes() []*clustercache.Node {
  388. return f.nodes
  389. }
  390. func (f FakeCache) GetAllDaemonSets() []*clustercache.DaemonSet {
  391. return nil
  392. }
  393. func NewFakeNodeCache(nodes []*clustercache.Node) FakeCache {
  394. return FakeCache{
  395. nodes: nodes,
  396. }
  397. }
  398. type FakeClusterMap struct {
  399. clusters.ClusterMap
  400. }
  401. func TestNodePriceFromCSVWithBadConfig(t *testing.T) {
  402. os.Setenv("CONFIG_PATH", "../config")
  403. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  404. LocalConfigPath: "./",
  405. })
  406. c := &provider.CSVProvider{
  407. CSVLocation: "../configs/pricing_schema_case.csv",
  408. CustomProvider: &provider.CustomProvider{
  409. Config: provider.NewProviderConfig(confMan, "invalid.json"),
  410. },
  411. }
  412. c.DownloadPricingData()
  413. n := &clustercache.Node{}
  414. n.SpecProviderID = "fake"
  415. n.Name = "nameWant"
  416. n.Labels = make(map[string]string)
  417. n.Labels["foo"] = "labelFooWant"
  418. n.Labels[v1.LabelTopologyRegion] = "regionone"
  419. fc := NewFakeNodeCache([]*clustercache.Node{n})
  420. fm := FakeClusterMap{}
  421. d, _ := time.ParseDuration("1m")
  422. model := costmodel.NewCostModel(nil, nil, fc, fm, d)
  423. _, err := model.GetNodeCost(c)
  424. if err != nil {
  425. t.Errorf("Error in node pricing: %s", err)
  426. }
  427. }
  428. func TestSourceMatchesFromCSV(t *testing.T) {
  429. os.Setenv("CONFIG_PATH", "../configs")
  430. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  431. LocalConfigPath: "./",
  432. })
  433. c := &provider.CSVProvider{
  434. CSVLocation: "../configs/pricing_schema_case.csv",
  435. CustomProvider: &provider.CustomProvider{
  436. Config: provider.NewProviderConfig(confMan, "/default.json"),
  437. },
  438. }
  439. c.DownloadPricingData()
  440. n := &clustercache.Node{}
  441. n.SpecProviderID = "fake"
  442. n.Name = "nameWant"
  443. n.Labels = make(map[string]string)
  444. n.Labels["foo"] = "labelFooWant"
  445. n.Labels[v1.LabelTopologyRegion] = "regionone"
  446. n2 := &clustercache.Node{}
  447. 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"
  448. n2.Labels = make(map[string]string)
  449. n2.Labels[v1.LabelTopologyRegion] = "eastus2"
  450. n2.Labels["foo"] = "labelFooWant"
  451. k := c.GetKey(n2.Labels, n2)
  452. resN, _, err := c.NodePricing(k)
  453. if err != nil {
  454. t.Errorf("Error in NodePricing: %s", err.Error())
  455. } else {
  456. wantPrice := "0.13370357"
  457. gotPrice := resN.Cost
  458. if gotPrice != wantPrice {
  459. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  460. }
  461. }
  462. n3 := &clustercache.Node{}
  463. n3.SpecProviderID = "fake"
  464. n3.Name = "nameWant"
  465. n3.Labels = make(map[string]string)
  466. n3.Labels[v1.LabelTopologyRegion] = "eastus2"
  467. n3.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
  468. fc := NewFakeNodeCache([]*clustercache.Node{n, n2, n3})
  469. fm := FakeClusterMap{}
  470. d, _ := time.ParseDuration("1m")
  471. model := costmodel.NewCostModel(nil, nil, fc, fm, d)
  472. _, err = model.GetNodeCost(c)
  473. if err != nil {
  474. t.Errorf("Error in node pricing: %s", err)
  475. }
  476. p, err := model.GetPricingSourceCounts()
  477. if err != nil {
  478. t.Errorf("Error in pricing source counts: %s", err)
  479. } else if p.TotalNodes != 3 {
  480. t.Errorf("Wanted 3 nodes got %d", p.TotalNodes)
  481. }
  482. if p.PricingTypeCounts[""] != 1 {
  483. t.Errorf("Wanted 1 default match got %d: %+v", p.PricingTypeCounts[""], p.PricingTypeCounts)
  484. }
  485. if p.PricingTypeCounts["csvExact"] != 1 {
  486. t.Errorf("Wanted 1 exact match got %d: %+v", p.PricingTypeCounts["csvExact"], p.PricingTypeCounts)
  487. }
  488. if p.PricingTypeCounts["csvClass"] != 1 {
  489. t.Errorf("Wanted 1 class match got %d: %+v", p.PricingTypeCounts["csvClass"], p.PricingTypeCounts)
  490. }
  491. }
  492. func TestNodePriceFromCSVWithCase(t *testing.T) {
  493. n := &clustercache.Node{}
  494. 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"
  495. n.Labels = make(map[string]string)
  496. n.Labels[v1.LabelTopologyRegion] = "eastus2"
  497. wantPrice := "0.13370357"
  498. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  499. LocalConfigPath: "./",
  500. })
  501. c := &provider.CSVProvider{
  502. CSVLocation: "../configs/pricing_schema_case.csv",
  503. CustomProvider: &provider.CustomProvider{
  504. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  505. },
  506. }
  507. c.DownloadPricingData()
  508. k := c.GetKey(n.Labels, n)
  509. resN, _, err := c.NodePricing(k)
  510. if err != nil {
  511. t.Errorf("Error in NodePricing: %s", err.Error())
  512. } else {
  513. gotPrice := resN.Cost
  514. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  515. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  516. if gotPriceFloat != wantPriceFloat {
  517. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  518. }
  519. }
  520. }
  521. func TestNodePriceFromCSVMixed(t *testing.T) {
  522. labelFooWant := "OnDemand"
  523. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  524. LocalConfigPath: "./",
  525. })
  526. n := &clustercache.Node{}
  527. n.Labels = make(map[string]string)
  528. n.Labels["TestClusterUsage"] = labelFooWant
  529. n.Labels["nvidia.com/gpu_type"] = "a100-ondemand"
  530. n.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
  531. wantPrice := "1.904110"
  532. labelFooWant2 := "Reserved"
  533. n2 := &clustercache.Node{}
  534. n2.Labels = make(map[string]string)
  535. n2.Labels["TestClusterUsage"] = labelFooWant2
  536. n2.Labels["nvidia.com/gpu_type"] = "a100-reserved"
  537. n2.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(1, 0)}
  538. wantPrice2 := "1.654795"
  539. c := &provider.CSVProvider{
  540. CSVLocation: "../configs/pricing_schema_mixed_gpu_ondemand.csv",
  541. CustomProvider: &provider.CustomProvider{
  542. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  543. },
  544. }
  545. c.DownloadPricingData()
  546. k := c.GetKey(n.Labels, n)
  547. resN, _, err := c.NodePricing(k)
  548. if err != nil {
  549. t.Errorf("Error in NodePricing: %s", err.Error())
  550. } else {
  551. gotPrice := resN.Cost
  552. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  553. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  554. if gotPriceFloat != wantPriceFloat {
  555. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  556. }
  557. }
  558. k2 := c.GetKey(n2.Labels, n2)
  559. resN2, _, err2 := c.NodePricing(k2)
  560. if err2 != nil {
  561. t.Errorf("Error in NodePricing: %s", err.Error())
  562. } else {
  563. gotPrice := resN2.Cost
  564. wantPriceFloat, _ := strconv.ParseFloat(wantPrice2, 64)
  565. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  566. if gotPriceFloat != wantPriceFloat {
  567. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  568. }
  569. }
  570. }
  571. func TestNodePriceFromCSVByClass(t *testing.T) {
  572. n := &clustercache.Node{}
  573. n.SpecProviderID = "fakeproviderid"
  574. n.Labels = make(map[string]string)
  575. n.Labels[v1.LabelTopologyRegion] = "eastus2"
  576. n.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
  577. wantpricefloat := 0.13370357
  578. wantPrice := fmt.Sprintf("%f", (math.Round(wantpricefloat*1000000) / 1000000))
  579. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  580. LocalConfigPath: "./",
  581. })
  582. c := &provider.CSVProvider{
  583. CSVLocation: "../configs/pricing_schema_case.csv",
  584. CustomProvider: &provider.CustomProvider{
  585. Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
  586. },
  587. }
  588. c.DownloadPricingData()
  589. k := c.GetKey(n.Labels, n)
  590. resN, _, err := c.NodePricing(k)
  591. if err != nil {
  592. t.Errorf("Error in NodePricing: %s", err.Error())
  593. } else {
  594. gotPrice := resN.Cost
  595. if gotPrice != wantPrice {
  596. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  597. }
  598. }
  599. n2 := &clustercache.Node{}
  600. n2.SpecProviderID = "fakeproviderid"
  601. n2.Labels = make(map[string]string)
  602. n2.Labels[v1.LabelTopologyRegion] = "fakeregion"
  603. n2.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
  604. k2 := c.GetKey(n2.Labels, n)
  605. c.DownloadPricingData()
  606. resN2, _, err := c.NodePricing(k2)
  607. if resN2 != nil {
  608. t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN2)
  609. }
  610. }