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