| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708 |
- package test
- import (
- "fmt"
- "math"
- "os"
- "strconv"
- "strings"
- "testing"
- "time"
- "github.com/opencost/opencost/core/pkg/clusters"
- "github.com/opencost/opencost/pkg/cloud/provider"
- "github.com/opencost/opencost/pkg/clustercache"
- "github.com/opencost/opencost/pkg/config"
- "github.com/opencost/opencost/pkg/costmodel"
- appsv1 "k8s.io/api/apps/v1"
- v1 "k8s.io/api/core/v1"
- "k8s.io/apimachinery/pkg/api/resource"
- )
- const (
- providerIDMap = "spec.providerID"
- nameMap = "metadata.name"
- labelMapFoo = "metadata.labels.foo"
- labelMapFooBar = "metadata.labels.foo.bar"
- )
- func TestRegionValueFromMapField(t *testing.T) {
- wantRegion := "useast"
- wantpid := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0")
- providerIDWant := wantRegion + "," + wantpid
- n := &v1.Node{}
- n.Spec.ProviderID = "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0"
- n.Labels = make(map[string]string)
- n.Labels[v1.LabelTopologyRegion] = wantRegion
- got := provider.NodeValueFromMapField(providerIDMap, n, true)
- if got != providerIDWant {
- t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
- }
- }
- func TestTransformedValueFromMapField(t *testing.T) {
- providerIDWant := "i-05445591e0d182d42"
- n := &v1.Node{}
- n.Spec.ProviderID = "aws:///us-east-1a/i-05445591e0d182d42"
- got := provider.NodeValueFromMapField(providerIDMap, n, false)
- if got != providerIDWant {
- t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
- }
- providerIDWant2 := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0")
- n2 := &v1.Node{}
- n2.Spec.ProviderID = "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/MC_test_test_eastus/providers/Microsoft.Compute/virtualMachines/aks-agentpool-20139558-0"
- got2 := provider.NodeValueFromMapField(providerIDMap, n2, false)
- if got2 != providerIDWant2 {
- t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant2, got2)
- }
- providerIDWant3 := strings.ToLower("/subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/mc_testspot_testspot_eastus/providers/Microsoft.Compute/virtualMachineScaleSets/aks-nodepool1-19213364-vmss/virtualMachines/0")
- n3 := &v1.Node{}
- 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"
- got3 := provider.NodeValueFromMapField(providerIDMap, n3, false)
- if got3 != providerIDWant3 {
- t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant3, got3)
- }
- }
- func TestNodeValueFromMapField(t *testing.T) {
- providerIDWant := "providerid"
- nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
- labelFooWant := "labelfoo"
- n := &v1.Node{}
- n.Spec.ProviderID = providerIDWant
- n.Name = nameWant
- n.Labels = make(map[string]string)
- n.Labels["foo"] = labelFooWant
- got := provider.NodeValueFromMapField(providerIDMap, n, false)
- if got != providerIDWant {
- t.Errorf("Assert on '%s' want '%s' got '%s'", providerIDMap, providerIDWant, got)
- }
- got = provider.NodeValueFromMapField(nameMap, n, false)
- if got != nameWant {
- t.Errorf("Assert on '%s' want '%s' got '%s'", nameMap, nameWant, got)
- }
- got = provider.NodeValueFromMapField(labelMapFoo, n, false)
- if got != labelFooWant {
- t.Errorf("Assert on '%s' want '%s' got '%s'", labelMapFoo, labelFooWant, got)
- }
- }
- func TestPVPriceFromCSV(t *testing.T) {
- nameWant := "pvc-08e1f205-d7a9-4430-90fc-7b3965a18c4d"
- pv := &v1.PersistentVolume{}
- pv.Name = nameWant
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- wantPrice := "0.1337"
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema_pv.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- c.DownloadPricingData()
- k := c.GetPVKey(pv, make(map[string]string), "")
- resPV, err := c.PVPricing(k)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resPV.Cost
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- }
- func TestPVPriceFromCSVStorageClass(t *testing.T) {
- nameWant := "pvc-08e1f205-d7a9-4430-90fc-7b3965a18c4d"
- storageClassWant := "storageclass0"
- pv := &v1.PersistentVolume{}
- pv.Name = nameWant
- pv.Spec.StorageClassName = storageClassWant
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- wantPrice := "0.1338"
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema_pv_storageclass.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- c.DownloadPricingData()
- k := c.GetPVKey(pv, make(map[string]string), "")
- resPV, err := c.PVPricing(k)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resPV.Cost
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- }
- func TestNodePriceFromCSVWithGPU(t *testing.T) {
- providerIDWant := "providerid"
- nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
- labelFooWant := "labelfoo"
- wantGPU := "2"
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- n := &v1.Node{}
- n.Spec.ProviderID = providerIDWant
- n.Name = nameWant
- n.Labels = make(map[string]string)
- n.Labels["foo"] = labelFooWant
- n.Labels["nvidia.com/gpu_type"] = "Quadro_RTX_4000"
- n.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
- wantPrice := "1.633700"
- n2 := &v1.Node{}
- n2.Spec.ProviderID = providerIDWant
- n2.Name = nameWant
- n2.Labels = make(map[string]string)
- n2.Labels["foo"] = labelFooWant
- n2.Labels["gpu.nvidia.com/class"] = "Quadro_RTX_4001"
- n2.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
- wantPrice2 := "1.733700"
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- c.DownloadPricingData()
- k := c.GetKey(n.Labels, n)
- resN, _, err := c.NodePricing(k)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotGPU := resN.GPU
- gotPrice := resN.Cost
- if gotGPU != wantGPU {
- t.Errorf("Wanted gpu count '%s' got gpu count '%s'", wantGPU, gotGPU)
- }
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- k2 := c.GetKey(n2.Labels, n2)
- resN2, _, err := c.NodePricing(k2)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotGPU := resN2.GPU
- gotPrice := resN2.Cost
- if gotGPU != wantGPU {
- t.Errorf("Wanted gpu count '%s' got gpu count '%s'", wantGPU, gotGPU)
- }
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice2, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- }
- func TestNodePriceFromCSVSpecialChar(t *testing.T) {
- nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- n := &v1.Node{}
- n.Name = nameWant
- n.Labels = make(map[string]string)
- n.Labels["<http://metadata.label.servers.com/label|metadata.label.servers.com/label>"] = nameWant
- wantPrice := "0.133700"
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema_special_char.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- c.DownloadPricingData()
- k := c.GetKey(n.Labels, n)
- resN, _, err := c.NodePricing(k)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resN.Cost
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- }
- func TestNodePriceFromCSV(t *testing.T) {
- providerIDWant := "providerid"
- nameWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
- labelFooWant := "labelfoo"
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- n := &v1.Node{}
- n.Spec.ProviderID = providerIDWant
- n.Name = nameWant
- n.Labels = make(map[string]string)
- n.Labels["foo"] = labelFooWant
- wantPrice := "0.133700"
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- c.DownloadPricingData()
- k := c.GetKey(n.Labels, n)
- resN, _, err := c.NodePricing(k)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resN.Cost
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- unknownN := &v1.Node{}
- unknownN.Spec.ProviderID = providerIDWant
- unknownN.Name = "unknownname"
- unknownN.Labels = make(map[string]string)
- unknownN.Labels["foo"] = labelFooWant
- unknownN.Labels[v1.LabelTopologyRegion] = "fakeregion"
- k2 := c.GetKey(unknownN.Labels, unknownN)
- resN2, _, _ := c.NodePricing(k2)
- if resN2 != nil {
- t.Errorf("CSV provider should return nil on missing node")
- }
- c2 := &provider.CSVProvider{
- CSVLocation: "../configs/fake.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- k3 := c.GetKey(n.Labels, n)
- resN3, _, _ := c2.NodePricing(k3)
- if resN3 != nil {
- t.Errorf("CSV provider should return nil on missing csv")
- }
- }
- func TestNodePriceFromCSVWithRegion(t *testing.T) {
- providerIDWant := "gke-standard-cluster-1-pool-1-91dc432d-cg69"
- nameWant := "foo"
- labelFooWant := "labelfoo"
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- n := &v1.Node{}
- n.Spec.ProviderID = providerIDWant
- n.Name = nameWant
- n.Labels = make(map[string]string)
- n.Labels["foo"] = labelFooWant
- n.Labels[v1.LabelTopologyRegion] = "regionone"
- wantPrice := "0.133700"
- n2 := &v1.Node{}
- n2.Spec.ProviderID = providerIDWant
- n2.Name = nameWant
- n2.Labels = make(map[string]string)
- n2.Labels["foo"] = labelFooWant
- n2.Labels[v1.LabelTopologyRegion] = "regiontwo"
- wantPrice2 := "0.133800"
- n3 := &v1.Node{}
- n3.Spec.ProviderID = providerIDWant
- n3.Name = nameWant
- n3.Labels = make(map[string]string)
- n3.Labels["foo"] = labelFooWant
- n3.Labels[v1.LabelTopologyRegion] = "fakeregion"
- wantPrice3 := "0.1339"
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema_region.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- c.DownloadPricingData()
- k := c.GetKey(n.Labels, n)
- resN, _, err := c.NodePricing(k)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resN.Cost
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- k2 := c.GetKey(n2.Labels, n2)
- resN2, _, err := c.NodePricing(k2)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resN2.Cost
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice2, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- k3 := c.GetKey(n3.Labels, n3)
- resN3, _, err := c.NodePricing(k3)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resN3.Cost
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice3, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- unknownN := &v1.Node{}
- unknownN.Spec.ProviderID = "fake providerID"
- unknownN.Name = "unknownname"
- unknownN.Labels = make(map[string]string)
- unknownN.Labels[v1.LabelTopologyRegion] = "fakeregion"
- unknownN.Labels["foo"] = labelFooWant
- k4 := c.GetKey(unknownN.Labels, unknownN)
- resN4, _, _ := c.NodePricing(k4)
- if resN4 != nil {
- t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN4)
- }
- c2 := &provider.CSVProvider{
- CSVLocation: "../configs/fake.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- k5 := c.GetKey(n.Labels, n)
- resN5, _, _ := c2.NodePricing(k5)
- if resN5 != nil {
- t.Errorf("CSV provider should return nil on missing csv")
- }
- }
- type FakeCache struct {
- nodes []*v1.Node
- clustercache.ClusterCache
- }
- func (f FakeCache) GetAllNodes() []*v1.Node {
- return f.nodes
- }
- func (f FakeCache) GetAllDaemonSets() []*appsv1.DaemonSet {
- return nil
- }
- func NewFakeNodeCache(nodes []*v1.Node) FakeCache {
- return FakeCache{
- nodes: nodes,
- }
- }
- type FakeClusterMap struct {
- clusters.ClusterMap
- }
- func TestNodePriceFromCSVWithBadConfig(t *testing.T) {
- os.Setenv("CONFIG_PATH", "../config")
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema_case.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "invalid.json"),
- },
- }
- c.DownloadPricingData()
- n := &v1.Node{}
- n.Spec.ProviderID = "fake"
- n.Name = "nameWant"
- n.Labels = make(map[string]string)
- n.Labels["foo"] = "labelFooWant"
- n.Labels[v1.LabelTopologyRegion] = "regionone"
- fc := NewFakeNodeCache([]*v1.Node{n})
- fm := FakeClusterMap{}
- d, _ := time.ParseDuration("1m")
- model := costmodel.NewCostModel(nil, nil, fc, fm, d)
- _, err := model.GetNodeCost(c)
- if err != nil {
- t.Errorf("Error in node pricing: %s", err)
- }
- }
- func TestSourceMatchesFromCSV(t *testing.T) {
- os.Setenv("CONFIG_PATH", "../configs")
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema_case.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "/default.json"),
- },
- }
- c.DownloadPricingData()
- n := &v1.Node{}
- n.Spec.ProviderID = "fake"
- n.Name = "nameWant"
- n.Labels = make(map[string]string)
- n.Labels["foo"] = "labelFooWant"
- n.Labels[v1.LabelTopologyRegion] = "regionone"
- n2 := &v1.Node{}
- 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"
- n2.Labels = make(map[string]string)
- n2.Labels[v1.LabelTopologyRegion] = "eastus2"
- n2.Labels["foo"] = "labelFooWant"
- k := c.GetKey(n2.Labels, n2)
- resN, _, err := c.NodePricing(k)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- wantPrice := "0.13370357"
- gotPrice := resN.Cost
- if gotPrice != wantPrice {
- t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
- }
- }
- n3 := &v1.Node{}
- n3.Spec.ProviderID = "fake"
- n3.Name = "nameWant"
- n3.Labels = make(map[string]string)
- n3.Labels[v1.LabelTopologyRegion] = "eastus2"
- n3.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
- fc := NewFakeNodeCache([]*v1.Node{n, n2, n3})
- fm := FakeClusterMap{}
- d, _ := time.ParseDuration("1m")
- model := costmodel.NewCostModel(nil, nil, fc, fm, d)
- _, err = model.GetNodeCost(c)
- if err != nil {
- t.Errorf("Error in node pricing: %s", err)
- }
- p, err := model.GetPricingSourceCounts()
- if err != nil {
- t.Errorf("Error in pricing source counts: %s", err)
- } else if p.TotalNodes != 3 {
- t.Errorf("Wanted 3 nodes got %d", p.TotalNodes)
- }
- if p.PricingTypeCounts[""] != 1 {
- t.Errorf("Wanted 1 default match got %d: %+v", p.PricingTypeCounts[""], p.PricingTypeCounts)
- }
- if p.PricingTypeCounts["csvExact"] != 1 {
- t.Errorf("Wanted 1 exact match got %d: %+v", p.PricingTypeCounts["csvExact"], p.PricingTypeCounts)
- }
- if p.PricingTypeCounts["csvClass"] != 1 {
- t.Errorf("Wanted 1 class match got %d: %+v", p.PricingTypeCounts["csvClass"], p.PricingTypeCounts)
- }
- }
- func TestNodePriceFromCSVWithCase(t *testing.T) {
- n := &v1.Node{}
- 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"
- n.Labels = make(map[string]string)
- n.Labels[v1.LabelTopologyRegion] = "eastus2"
- wantPrice := "0.13370357"
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema_case.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- c.DownloadPricingData()
- k := c.GetKey(n.Labels, n)
- resN, _, err := c.NodePricing(k)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resN.Cost
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- }
- func TestNodePriceFromCSVMixed(t *testing.T) {
- labelFooWant := "OnDemand"
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- n := &v1.Node{}
- n.Labels = make(map[string]string)
- n.Labels["TestClusterUsage"] = labelFooWant
- n.Labels["nvidia.com/gpu_type"] = "a100-ondemand"
- n.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(2, 0)}
- wantPrice := "1.904110"
- labelFooWant2 := "Reserved"
- n2 := &v1.Node{}
- n2.Labels = make(map[string]string)
- n2.Labels["TestClusterUsage"] = labelFooWant2
- n2.Labels["nvidia.com/gpu_type"] = "a100-reserved"
- n2.Status.Capacity = v1.ResourceList{"nvidia.com/gpu": *resource.NewScaledQuantity(1, 0)}
- wantPrice2 := "1.654795"
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema_mixed_gpu_ondemand.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- c.DownloadPricingData()
- k := c.GetKey(n.Labels, n)
- resN, _, err := c.NodePricing(k)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resN.Cost
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- k2 := c.GetKey(n2.Labels, n2)
- resN2, _, err2 := c.NodePricing(k2)
- if err2 != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resN2.Cost
- wantPriceFloat, _ := strconv.ParseFloat(wantPrice2, 64)
- gotPriceFloat, _ := strconv.ParseFloat(gotPrice, 64)
- if gotPriceFloat != wantPriceFloat {
- t.Errorf("Wanted price '%f' got price '%f'", wantPriceFloat, gotPriceFloat)
- }
- }
- }
- func TestNodePriceFromCSVByClass(t *testing.T) {
- n := &v1.Node{}
- n.Spec.ProviderID = "fakeproviderid"
- n.Labels = make(map[string]string)
- n.Labels[v1.LabelTopologyRegion] = "eastus2"
- n.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
- wantpricefloat := 0.13370357
- wantPrice := fmt.Sprintf("%f", (math.Round(wantpricefloat*1000000) / 1000000))
- confMan := config.NewConfigFileManager(&config.ConfigFileManagerOpts{
- LocalConfigPath: "./",
- })
- c := &provider.CSVProvider{
- CSVLocation: "../configs/pricing_schema_case.csv",
- CustomProvider: &provider.CustomProvider{
- Config: provider.NewProviderConfig(confMan, "../configs/default.json"),
- },
- }
- c.DownloadPricingData()
- k := c.GetKey(n.Labels, n)
- resN, _, err := c.NodePricing(k)
- if err != nil {
- t.Errorf("Error in NodePricing: %s", err.Error())
- } else {
- gotPrice := resN.Cost
- if gotPrice != wantPrice {
- t.Errorf("Wanted price '%s' got price '%s'", wantPrice, gotPrice)
- }
- }
- n2 := &v1.Node{}
- n2.Spec.ProviderID = "fakeproviderid"
- n2.Labels = make(map[string]string)
- n2.Labels[v1.LabelTopologyRegion] = "fakeregion"
- n2.Labels[v1.LabelInstanceTypeStable] = "Standard_F32s_v2"
- k2 := c.GetKey(n2.Labels, n)
- c.DownloadPricingData()
- resN2, _, err := c.NodePricing(k2)
- if resN2 != nil {
- t.Errorf("CSV provider should return nil on missing node, instead returned %+v", resN2)
- }
- }
|