cloud_test.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. package provider_test
  2. import (
  3. "fmt"
  4. "math"
  5. "os"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/opencost/opencost/core/pkg/clusters"
  12. "github.com/opencost/opencost/core/pkg/clustercache"
  13. "github.com/opencost/opencost/pkg/cloud/provider"
  14. "github.com/opencost/opencost/pkg/config"
  15. "github.com/opencost/opencost/pkg/costmodel"
  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 := &clustercache.Node{}
  30. n.SpecProviderID = "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.LabelTopologyRegion] = 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 := &clustercache.Node{}
  41. n.SpecProviderID = "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 := &clustercache.Node{}
  48. n2.SpecProviderID = "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 := &clustercache.Node{}
  55. n3.SpecProviderID = "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 := &clustercache.Node{}
  66. n.SpecProviderID = 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 := &clustercache.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 := &clustercache.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 := &clustercache.Node{}
  150. n.SpecProviderID = 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 := &clustercache.Node{}
  158. n2.SpecProviderID = 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 TestNodePriceFromCSVWithGPULabels(t *testing.T) {
  206. const defaultConfigJson = `{"provider":"base","description":"Default prices based on GCP us-central1","CPU":"0.021811","spotCPU":"0.006543","RAM":"0.002923","spotRAM":"0.000877","GPU":"0.95","spotGPU":"0.308","storage":"0.00005479452","zoneNetworkEgress":"0.01","regionNetworkEgress":"0.01","internetNetworkEgress":"0.12","firstFiveForwardingRulesCost":"","additionalForwardingRuleCost":"","LBIngressDataCost":"","athenaBucketName":"","athenaRegion":"","athenaDatabase":"","athenaCatalog":"","athenaTable":"","athenaWorkgroup":"","masterPayerARN":"","customPricesEnabled":"false","defaultIdle":"","azureSubscriptionID":"","azureClientID":"","azureClientSecret":"","azureTenantID":"","azureBillingRegion":"","azureBillingAccount":"","azureOfferDurableID":"","azureStorageSubscriptionID":"","azureStorageAccount":"","azureStorageAccessKey":"","azureStorageContainer":"","azureContainerPath":"","azureCloud":"","currencyCode":"","discount":"","negotiatedDiscount":"","sharedOverhead":"","clusterName":"","sharedNamespaces":"","sharedLabelNames":"","sharedLabelValues":"","shareTenancyCosts":"true","readOnly":"","editorAccess":"","kubecostToken":"","googleAnalyticsTag":"","excludeProviderID":"","defaultLBPrice":""}`
  207. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  208. wantGPUCost := "0.75"
  209. tempPath := t.TempDir()
  210. currentPath, err := filepath.Abs(".")
  211. if err != nil {
  212. t.Skip(fmt.Sprintf("Unable to get absolute path for current dir: '%s' - Error: %s - Skipping test.", currentPath, err))
  213. return
  214. }
  215. configPath, err := filepath.Rel(currentPath, tempPath)
  216. if err != nil {
  217. t.Skip(fmt.Sprintf("Unable to get relative path for temp dir: '%s' - Error: %s - Skipping test.", tempPath, err))
  218. return
  219. }
  220. err = os.WriteFile(filepath.Join(configPath, "default.json"), []byte(defaultConfigJson), 0644)
  221. if err != nil {
  222. t.Skip(fmt.Sprintf("Unable to write temporary json config file: '%s' - Error: %s - Skipping test.", filepath.Join(configPath, "default.json"), err))
  223. return
  224. }
  225. t.Logf("Setting Config Path to: %s", configPath)
  226. t.Setenv("CONFIG_PATH", configPath)
  227. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  228. LocalConfigPath: "./",
  229. })
  230. n := &clustercache.Node{}
  231. n.SpecProviderID = "providerid"
  232. n.Name = nameWant
  233. n.Labels = make(map[string]string)
  234. n.Labels["foo"] = "labelfoo"
  235. n.Labels["nvidia.com/gpu_type"] = "Quadro_RTX_4000"
  236. n.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
  237. c := &provider.CSVProvider{
  238. CSVLocation: "../../../configs/pricing_schema_gpu_labels.csv",
  239. CustomProvider: &provider.CustomProvider{
  240. Config: provider.NewProviderConfig(confMan, "default.json"),
  241. },
  242. }
  243. c.DownloadPricingData()
  244. fc := NewFakeNodeCache([]*clustercache.Node{n})
  245. fm := FakeClusterMap{}
  246. d, _ := time.ParseDuration("1m")
  247. model := costmodel.NewCostModel(nil, c, fc, fm, d)
  248. nodeMap, err := model.GetNodeCost()
  249. if err != nil {
  250. t.Errorf("Error in NodePricing: %s", err.Error())
  251. } else {
  252. if node, ok := nodeMap[nameWant]; ok {
  253. if node.GPUCost != wantGPUCost {
  254. t.Errorf("Wanted gpu cost '%v' got gpu cost '%v'", wantGPUCost, node.GPUCost)
  255. }
  256. } else {
  257. t.Errorf("Node %s not found in node map", nameWant)
  258. }
  259. }
  260. }
  261. func TestRKE2NodePriceFromCSVWithGPULabels(t *testing.T) {
  262. const defaultConfigJson = `{"provider":"base","description":"Default prices based on GCP us-central1","CPU":"0.021811","spotCPU":"0.006543","RAM":"0.002923","spotRAM":"0.000877","GPU":"0.95","spotGPU":"0.308","storage":"0.00005479452","zoneNetworkEgress":"0.01","regionNetworkEgress":"0.01","internetNetworkEgress":"0.12","firstFiveForwardingRulesCost":"","additionalForwardingRuleCost":"","LBIngressDataCost":"","athenaBucketName":"","athenaRegion":"","athenaDatabase":"","athenaCatalog":"","athenaTable":"","athenaWorkgroup":"","masterPayerARN":"","customPricesEnabled":"false","defaultIdle":"","azureSubscriptionID":"","azureClientID":"","azureClientSecret":"","azureTenantID":"","azureBillingRegion":"","azureBillingAccount":"","azureOfferDurableID":"","azureStorageSubscriptionID":"","azureStorageAccount":"","azureStorageAccessKey":"","azureStorageContainer":"","azureContainerPath":"","azureCloud":"","currencyCode":"","discount":"","negotiatedDiscount":"","sharedOverhead":"","clusterName":"","sharedNamespaces":"","sharedLabelNames":"","sharedLabelValues":"","shareTenancyCosts":"true","readOnly":"","editorAccess":"","kubecostToken":"","googleAnalyticsTag":"","excludeProviderID":"","defaultLBPrice":""}`
  263. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  264. wantGPUCost := "0.750000"
  265. tempPath := t.TempDir()
  266. currentPath, err := filepath.Abs(".")
  267. if err != nil {
  268. t.Skip(fmt.Sprintf("Unable to get absolute path for current dir: '%s' - Error: %s - Skipping test.", currentPath, err))
  269. return
  270. }
  271. configPath, err := filepath.Rel(currentPath, tempPath)
  272. if err != nil {
  273. t.Skip(fmt.Sprintf("Unable to get relative path for temp dir: '%s' - Error: %s - Skipping test.", tempPath, err))
  274. return
  275. }
  276. err = os.WriteFile(filepath.Join(configPath, "default.json"), []byte(defaultConfigJson), 0644)
  277. if err != nil {
  278. t.Skip(fmt.Sprintf("Unable to write temporary json config file: '%s' - Error: %s - Skipping test.", filepath.Join(configPath, "default.json"), err))
  279. return
  280. }
  281. t.Logf("Setting Config Path to: %s", configPath)
  282. t.Setenv("CONFIG_PATH", configPath)
  283. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  284. LocalConfigPath: "./",
  285. })
  286. n := &clustercache.Node{}
  287. n.SpecProviderID = "providerid"
  288. n.Name = nameWant
  289. n.Labels = make(map[string]string)
  290. n.Labels["foo"] = "labelfoo"
  291. n.Labels["nvidia.com/gpu_type"] = "Quadro_RTX_4000"
  292. n.Labels[v1.LabelInstanceTypeStable] = "rke2"
  293. n.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
  294. c := &provider.CSVProvider{
  295. CSVLocation: "../../../configs/pricing_schema_gpu_labels.csv",
  296. CustomProvider: &provider.CustomProvider{
  297. Config: provider.NewProviderConfig(confMan, "default.json"),
  298. },
  299. }
  300. c.DownloadPricingData()
  301. fc := NewFakeNodeCache([]*clustercache.Node{n})
  302. fm := FakeClusterMap{}
  303. d, _ := time.ParseDuration("1m")
  304. model := costmodel.NewCostModel(nil, c, fc, fm, d)
  305. nodeMap, err := model.GetNodeCost()
  306. if err != nil {
  307. t.Errorf("Error in NodePricing: %s", err.Error())
  308. } else {
  309. if node, ok := nodeMap[nameWant]; ok {
  310. if node.GPUCost != wantGPUCost {
  311. t.Errorf("Wanted gpu cost '%v' got gpu cost '%v'", wantGPUCost, node.GPUCost)
  312. }
  313. } else {
  314. t.Errorf("Node %s not found in node map", nameWant)
  315. }
  316. }
  317. }
  318. func TestNodePriceFromCSVSpecialChar(t *testing.T) {
  319. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  320. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  321. LocalConfigPath: "./",
  322. })
  323. n := &clustercache.Node{}
  324. n.Name = nameWant
  325. n.Labels = make(map[string]string)
  326. n.Labels["<http://metadata.label.servers.com/label|metadata.label.servers.com/label>"] = nameWant
  327. wantPrice := "0.133700"
  328. c := &provider.CSVProvider{
  329. CSVLocation: "../../../configs/pricing_schema_special_char.csv",
  330. CustomProvider: &provider.CustomProvider{
  331. Config: provider.NewProviderConfig(confMan, "../../../configs/default.json"),
  332. },
  333. }
  334. c.DownloadPricingData()
  335. k := c.GetKey(n.Labels, n)
  336. resN, _, err := c.NodePricing(k)
  337. if err != nil {
  338. t.Errorf("Error in NodePricing: %s", err.Error())
  339. } else {
  340. gotPrice := resN.Cost
  341. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  342. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  343. if gotPriceFloat != wantPriceFloat {
  344. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  345. }
  346. }
  347. }
  348. func TestNodePriceFromCSV(t *testing.T) {
  349. providerIDWant := "providerid"
  350. nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  351. labelFooWant := "labelfoo"
  352. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  353. LocalConfigPath: "./",
  354. })
  355. n := &clustercache.Node{}
  356. n.SpecProviderID = providerIDWant
  357. n.Name = nameWant
  358. n.Labels = make(map[string]string)
  359. n.Labels["foo"] = labelFooWant
  360. wantPrice := "0.133700"
  361. c := &provider.CSVProvider{
  362. CSVLocation: "../../../configs/pricing_schema.csv",
  363. CustomProvider: &provider.CustomProvider{
  364. Config: provider.NewProviderConfig(confMan, "../../../configs/default.json"),
  365. },
  366. }
  367. c.DownloadPricingData()
  368. k := c.GetKey(n.Labels, n)
  369. resN, _, err := c.NodePricing(k)
  370. if err != nil {
  371. t.Errorf("Error in NodePricing: %s", err.Error())
  372. } else {
  373. gotPrice := resN.Cost
  374. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  375. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  376. if gotPriceFloat != wantPriceFloat {
  377. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  378. }
  379. }
  380. unknownN := &clustercache.Node{}
  381. unknownN.SpecProviderID = providerIDWant
  382. unknownN.Name = "unknownname"
  383. unknownN.Labels = make(map[string]string)
  384. unknownN.Labels["foo"] = labelFooWant
  385. unknownN.Labels[v1.LabelTopologyRegion] = "fakeregion"
  386. k2 := c.GetKey(unknownN.Labels, unknownN)
  387. resN2, _, _ := c.NodePricing(k2)
  388. if resN2 != nil {
  389. t.Errorf("CSV provider should return nil on missing node")
  390. }
  391. c2 := &provider.CSVProvider{
  392. CSVLocation: "../../../configs/fake.csv",
  393. CustomProvider: &provider.CustomProvider{
  394. Config: provider.NewProviderConfig(confMan, "../../../configs/default.json"),
  395. },
  396. }
  397. k3 := c.GetKey(n.Labels, n)
  398. resN3, _, _ := c2.NodePricing(k3)
  399. if resN3 != nil {
  400. t.Errorf("CSV provider should return nil on missing csv")
  401. }
  402. }
  403. func TestNodePriceFromCSVWithRegion(t *testing.T) {
  404. providerIDWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
  405. nameWant := "foo"
  406. labelFooWant := "labelfoo"
  407. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  408. LocalConfigPath: "./",
  409. })
  410. n := &clustercache.Node{}
  411. n.SpecProviderID = providerIDWant
  412. n.Name = nameWant
  413. n.Labels = make(map[string]string)
  414. n.Labels["foo"] = labelFooWant
  415. n.Labels[v1.LabelTopologyRegion] = "regionone"
  416. wantPrice := "0.133700"
  417. n2 := &clustercache.Node{}
  418. n2.SpecProviderID = providerIDWant
  419. n2.Name = nameWant
  420. n2.Labels = make(map[string]string)
  421. n2.Labels["foo"] = labelFooWant
  422. n2.Labels[v1.LabelTopologyRegion] = "regiontwo"
  423. wantPrice2 := "0.133800"
  424. n3 := &clustercache.Node{}
  425. n3.SpecProviderID = providerIDWant
  426. n3.Name = nameWant
  427. n3.Labels = make(map[string]string)
  428. n3.Labels["foo"] = labelFooWant
  429. n3.Labels[v1.LabelTopologyRegion] = "fakeregion"
  430. wantPrice3 := "0.1339"
  431. c := &provider.CSVProvider{
  432. CSVLocation: "../../../configs/pricing_schema_region.csv",
  433. CustomProvider: &provider.CustomProvider{
  434. Config: provider.NewProviderConfig(confMan, "../../../configs/default.json"),
  435. },
  436. }
  437. c.DownloadPricingData()
  438. k := c.GetKey(n.Labels, n)
  439. resN, _, err := c.NodePricing(k)
  440. if err != nil {
  441. t.Errorf("Error in NodePricing: %s", err.Error())
  442. } else {
  443. gotPrice := resN.Cost
  444. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  445. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  446. if gotPriceFloat != wantPriceFloat {
  447. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  448. }
  449. }
  450. k2 := c.GetKey(n2.Labels, n2)
  451. resN2, _, err := c.NodePricing(k2)
  452. if err != nil {
  453. t.Errorf("Error in NodePricing: %s", err.Error())
  454. } else {
  455. gotPrice := resN2.Cost
  456. wantPriceFloat, _ := strconv.ParseFloat(wantPrice2, 64)
  457. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  458. if gotPriceFloat != wantPriceFloat {
  459. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  460. }
  461. }
  462. k3 := c.GetKey(n3.Labels, n3)
  463. resN3, _, err := c.NodePricing(k3)
  464. if err != nil {
  465. t.Errorf("Error in NodePricing: %s", err.Error())
  466. } else {
  467. gotPrice := resN3.Cost
  468. wantPriceFloat, _ := strconv.ParseFloat(wantPrice3, 64)
  469. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  470. if gotPriceFloat != wantPriceFloat {
  471. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  472. }
  473. }
  474. unknownN := &clustercache.Node{}
  475. unknownN.SpecProviderID = "fake providerID"
  476. unknownN.Name = "unknownname"
  477. unknownN.Labels = make(map[string]string)
  478. unknownN.Labels[v1.LabelTopologyRegion] = "fakeregion"
  479. unknownN.Labels["foo"] = labelFooWant
  480. k4 := c.GetKey(unknownN.Labels, unknownN)
  481. resN4, _, _ := c.NodePricing(k4)
  482. if resN4 != nil {
  483. t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN4)
  484. }
  485. c2 := &provider.CSVProvider{
  486. CSVLocation: "../../../configs/fake.csv",
  487. CustomProvider: &provider.CustomProvider{
  488. Config: provider.NewProviderConfig(confMan, "../../../configs/default.json"),
  489. },
  490. }
  491. k5 := c.GetKey(n.Labels, n)
  492. resN5, _, _ := c2.NodePricing(k5)
  493. if resN5 != nil {
  494. t.Errorf("CSV provider should return nil on missing csv")
  495. }
  496. }
  497. type FakeCache struct {
  498. nodes []*clustercache.Node
  499. clustercache.ClusterCache
  500. }
  501. func (f FakeCache) GetAllNodes() []*clustercache.Node {
  502. return f.nodes
  503. }
  504. func (f FakeCache) GetAllDaemonSets() []*clustercache.DaemonSet {
  505. return nil
  506. }
  507. func NewFakeNodeCache(nodes []*clustercache.Node) FakeCache {
  508. return FakeCache{
  509. nodes: nodes,
  510. }
  511. }
  512. type FakeClusterMap struct {
  513. clusters.ClusterMap
  514. }
  515. func TestNodePriceFromCSVWithBadConfig(t *testing.T) {
  516. const invalidConfigJson = `{
  517. "provider":"base",
  518. "description":"Default prices based on GCP us-central1",
  519. "CPU":"0.031611",
  520. "spotCPU":"0.006655",
  521. "RAM":"0.004237",
  522. "spotRAM":"0.000892",
  523. "GPU":"0.95",
  524. "spotGPU":"0.308",
  525. "storage":"0.00005479452",
  526. "zoneNetworkEgress":"0.01",
  527. "regionNetworkEgress":"0.01",
  528. "internetNetworkEgress":"0.12",
  529. "firstFiveForwardingRulesCost":"",
  530. "additionalForwardingRuleCost":"",
  531. "LBIngressDataCost":"",
  532. "athenaBucketName":"",
  533. "athenaRegion":"",
  534. "athenaDatabase":"",
  535. "athenaTable":"",
  536. "athenaWorkgroup":"",
  537. "masterPayerARN":"",
  538. "customPricesEnabled":"false",
  539. "defaultIdle":"",
  540. "azureSubscriptionID":"",
  541. "azureClientID":"",
  542. "azureClientSecret":"",
  543. "azureTenantID":"",
  544. "azureBillingRegion":"",
  545. "azureOfferDurableID":"",
  546. "azureStorageSubscriptionID":"",
  547. "azureStorageAccount":"",
  548. "azureStorageAccessKey":"",
  549. "azureStorageContainer":"",
  550. "azureContainerPath":"",
  551. "azureCloud":"",
  552. "currencyCode":"",
  553. "discount":"",
  554. "negotiatedDiscount":"",
  555. "sharedOverhead":"",
  556. "clusterName":"",
  557. "sharedNamespaces":"",
  558. "sharedLabelNames":"",
  559. "sharedLabelValues":"",
  560. "shareTenancyCosts":"true",
  561. "readOnly":"",
  562. "editorAccess":"",
  563. "kubecostToken":"",
  564. "googleAnalyticsTag":"",
  565. "excludeProviderID":""
  566. }`
  567. tempPath := t.TempDir()
  568. currentPath, err := filepath.Abs(".")
  569. if err != nil {
  570. t.Skip(fmt.Sprintf("Unable to get absolute path for current dir: '%s' - Error: %s - Skipping test.", currentPath, err))
  571. return
  572. }
  573. configPath, err := filepath.Rel(currentPath, tempPath)
  574. if err != nil {
  575. t.Skip(fmt.Sprintf("Unable to get relative path for temp dir: '%s' - Error: %s - Skipping test.", tempPath, err))
  576. return
  577. }
  578. err = os.WriteFile(filepath.Join(configPath, "invalid.json"), []byte(invalidConfigJson), 0644)
  579. if err != nil {
  580. t.Skip(fmt.Sprintf("Unable to write temporary json config file: '%s' - Error: %s - Skipping test.", filepath.Join(configPath, "invalid.json"), err))
  581. return
  582. }
  583. t.Logf("Setting Config Path to: %s", configPath)
  584. t.Setenv("CONFIG_PATH", configPath)
  585. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  586. LocalConfigPath: "./",
  587. })
  588. c := &provider.CSVProvider{
  589. CSVLocation: "../../../configs/pricing_schema_case.csv",
  590. CustomProvider: &provider.CustomProvider{
  591. Config: provider.NewProviderConfig(confMan, "invalid.json"),
  592. },
  593. }
  594. c.DownloadPricingData()
  595. n := &clustercache.Node{}
  596. n.SpecProviderID = "fake"
  597. n.Name = "nameWant"
  598. n.Labels = make(map[string]string)
  599. n.Labels["foo"] = "labelFooWant"
  600. n.Labels[v1.LabelTopologyRegion] = "regionone"
  601. fc := NewFakeNodeCache([]*clustercache.Node{n})
  602. fm := FakeClusterMap{}
  603. d, _ := time.ParseDuration("1m")
  604. model := costmodel.NewCostModel(nil, c, fc, fm, d)
  605. _, err = model.GetNodeCost()
  606. if err != nil {
  607. t.Errorf("Error in node pricing: %s", err)
  608. }
  609. }
  610. func TestSourceMatchesFromCSV(t *testing.T) {
  611. os.Setenv("CONFIG_PATH", "../../../configs")
  612. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  613. LocalConfigPath: "./",
  614. })
  615. c := &provider.CSVProvider{
  616. CSVLocation: "../../../configs/pricing_schema_case.csv",
  617. CustomProvider: &provider.CustomProvider{
  618. Config: provider.NewProviderConfig(confMan, "default.json"),
  619. },
  620. }
  621. c.DownloadPricingData()
  622. n := &clustercache.Node{}
  623. n.SpecProviderID = "fake"
  624. n.Name = "nameWant"
  625. n.Labels = make(map[string]string)
  626. n.Labels["foo"] = "labelFooWant"
  627. n.Labels[v1.LabelTopologyRegion] = "regionone"
  628. n2 := &clustercache.Node{}
  629. 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"
  630. n2.Labels = make(map[string]string)
  631. n2.Labels[v1.LabelTopologyRegion] = "eastus2"
  632. n2.Labels["foo"] = "labelFooWant"
  633. k := c.GetKey(n2.Labels, n2)
  634. resN, _, err := c.NodePricing(k)
  635. if err != nil {
  636. t.Errorf("Error in NodePricing: %s", err.Error())
  637. } else {
  638. wantPrice := "0.13370357"
  639. gotPrice := resN.Cost
  640. if gotPrice != wantPrice {
  641. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  642. }
  643. }
  644. n3 := &clustercache.Node{}
  645. n3.SpecProviderID = "fake"
  646. n3.Name = "nameWant"
  647. n3.Labels = make(map[string]string)
  648. n3.Labels[v1.LabelTopologyRegion] = "eastus2"
  649. n3.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
  650. fc := NewFakeNodeCache([]*clustercache.Node{n, n2, n3})
  651. fm := FakeClusterMap{}
  652. d, _ := time.ParseDuration("1m")
  653. model := costmodel.NewCostModel(nil, c, fc, fm, d)
  654. _, err = model.GetNodeCost()
  655. if err != nil {
  656. t.Errorf("Error in node pricing: %s", err)
  657. }
  658. p, err := model.GetPricingSourceCounts()
  659. if err != nil {
  660. t.Errorf("Error in pricing source counts: %s", err)
  661. } else if p.TotalNodes != 3 {
  662. t.Errorf("Wanted 3 nodes got %d", p.TotalNodes)
  663. }
  664. if p.PricingTypeCounts[""] != 1 {
  665. t.Errorf("Wanted 1 default match got %d: %+v", p.PricingTypeCounts[""], p.PricingTypeCounts)
  666. }
  667. if p.PricingTypeCounts["csvExact"] != 1 {
  668. t.Errorf("Wanted 1 exact match got %d: %+v", p.PricingTypeCounts["csvExact"], p.PricingTypeCounts)
  669. }
  670. if p.PricingTypeCounts["csvClass"] != 1 {
  671. t.Errorf("Wanted 1 class match got %d: %+v", p.PricingTypeCounts["csvClass"], p.PricingTypeCounts)
  672. }
  673. }
  674. func TestNodePriceFromCSVWithCase(t *testing.T) {
  675. n := &clustercache.Node{}
  676. 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"
  677. n.Labels = make(map[string]string)
  678. n.Labels[v1.LabelTopologyRegion] = "eastus2"
  679. wantPrice := "0.13370357"
  680. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  681. LocalConfigPath: "./",
  682. })
  683. c := &provider.CSVProvider{
  684. CSVLocation: "../../../configs/pricing_schema_case.csv",
  685. CustomProvider: &provider.CustomProvider{
  686. Config: provider.NewProviderConfig(confMan, "../../../configs/default.json"),
  687. },
  688. }
  689. c.DownloadPricingData()
  690. k := c.GetKey(n.Labels, n)
  691. resN, _, err := c.NodePricing(k)
  692. if err != nil {
  693. t.Errorf("Error in NodePricing: %s", err.Error())
  694. } else {
  695. gotPrice := resN.Cost
  696. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  697. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  698. if gotPriceFloat != wantPriceFloat {
  699. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  700. }
  701. }
  702. }
  703. func TestNodePriceFromCSVMixed(t *testing.T) {
  704. labelFooWant := "OnDemand"
  705. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  706. LocalConfigPath: "./",
  707. })
  708. n := &clustercache.Node{}
  709. n.Labels = make(map[string]string)
  710. n.Labels["TestClusterUsage"] = labelFooWant
  711. n.Labels["nvidia.com/gpu_type"] = "a100-ondemand"
  712. n.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
  713. wantPrice := "1.904110"
  714. labelFooWant2 := "Reserved"
  715. n2 := &clustercache.Node{}
  716. n2.Labels = make(map[string]string)
  717. n2.Labels["TestClusterUsage"] = labelFooWant2
  718. n2.Labels["nvidia.com/gpu_type"] = "a100-reserved"
  719. n2.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(1, 0)}
  720. wantPrice2 := "1.654795"
  721. c := &provider.CSVProvider{
  722. CSVLocation: "../../../configs/pricing_schema_mixed_gpu_ondemand.csv",
  723. CustomProvider: &provider.CustomProvider{
  724. Config: provider.NewProviderConfig(confMan, "../../../configs/default.json"),
  725. },
  726. }
  727. c.DownloadPricingData()
  728. k := c.GetKey(n.Labels, n)
  729. resN, _, err := c.NodePricing(k)
  730. if err != nil {
  731. t.Errorf("Error in NodePricing: %s", err.Error())
  732. } else {
  733. gotPrice := resN.Cost
  734. wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
  735. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  736. if gotPriceFloat != wantPriceFloat {
  737. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  738. }
  739. }
  740. k2 := c.GetKey(n2.Labels, n2)
  741. resN2, _, err2 := c.NodePricing(k2)
  742. if err2 != nil {
  743. t.Errorf("Error in NodePricing: %s", err.Error())
  744. } else {
  745. gotPrice := resN2.Cost
  746. wantPriceFloat, _ := strconv.ParseFloat(wantPrice2, 64)
  747. gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
  748. if gotPriceFloat != wantPriceFloat {
  749. t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
  750. }
  751. }
  752. }
  753. func TestNodePriceFromCSVByClass(t *testing.T) {
  754. n := &clustercache.Node{}
  755. n.SpecProviderID = "fakeproviderid"
  756. n.Labels = make(map[string]string)
  757. n.Labels[v1.LabelTopologyRegion] = "eastus2"
  758. n.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
  759. wantpricefloat := 0.13370357
  760. wantPrice := fmt.Sprintf("%f", (math.Round(wantpricefloat*1000000) / 1000000))
  761. confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
  762. LocalConfigPath: "./",
  763. })
  764. c := &provider.CSVProvider{
  765. CSVLocation: "../../../configs/pricing_schema_case.csv",
  766. CustomProvider: &provider.CustomProvider{
  767. Config: provider.NewProviderConfig(confMan, "../../../configs/default.json"),
  768. },
  769. }
  770. c.DownloadPricingData()
  771. k := c.GetKey(n.Labels, n)
  772. resN, _, err := c.NodePricing(k)
  773. if err != nil {
  774. t.Errorf("Error in NodePricing: %s", err.Error())
  775. } else {
  776. gotPrice := resN.Cost
  777. if gotPrice != wantPrice {
  778. t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
  779. }
  780. }
  781. n2 := &clustercache.Node{}
  782. n2.SpecProviderID = "fakeproviderid"
  783. n2.Labels = make(map[string]string)
  784. n2.Labels[v1.LabelTopologyRegion] = "fakeregion"
  785. n2.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
  786. k2 := c.GetKey(n2.Labels, n)
  787. c.DownloadPricingData()
  788. resN2, _, err := c.NodePricing(k2)
  789. if resN2 != nil {
  790. t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN2)
  791. }
  792. }