| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297 |
- package aws
- import (
- "encoding/json"
- "errors"
- "io"
- "net/http"
- "net/url"
- "os"
- "reflect"
- "testing"
- "time"
- "github.com/opencost/opencost/core/pkg/clustercache"
- "github.com/opencost/opencost/pkg/cloud/models"
- "github.com/opencost/opencost/pkg/config"
- v1 "k8s.io/api/core/v1"
- )
- func Test_awsKey_getUsageType(t *testing.T) {
- type fields struct {
- Labels map[string]string
- ProviderID string
- }
- type args struct {
- labels map[string]string
- }
- tests := []struct {
- name string
- fields fields
- args args
- want string
- }{
- {
- // test with no labels should return false
- name: "Label does not have the capacityType label associated with it",
- args: args{
- labels: map[string]string{},
- },
- want: "",
- },
- {
- name: "EKS label with a capacityType set to empty string should return empty string",
- args: args{
- labels: map[string]string{
- EKSCapacityTypeLabel: "",
- },
- },
- want: "",
- },
- {
- name: "EKS label with capacityType set to a random value should return empty string",
- args: args{
- labels: map[string]string{
- EKSCapacityTypeLabel: "TEST_ME",
- },
- },
- want: "",
- },
- {
- name: "EKS label with capacityType set to spot should return spot",
- args: args{
- labels: map[string]string{
- EKSCapacityTypeLabel: EKSCapacitySpotTypeValue,
- },
- },
- want: PreemptibleType,
- },
- {
- name: "Karpenter label with a capacityType set to empty string should return empty string",
- args: args{
- labels: map[string]string{
- models.KarpenterCapacityTypeLabel: "",
- },
- },
- want: "",
- },
- {
- name: "Karpenter label with capacityType set to a random value should return empty string",
- args: args{
- labels: map[string]string{
- models.KarpenterCapacityTypeLabel: "TEST_ME",
- },
- },
- want: "",
- },
- {
- name: "Karpenter label with capacityType set to spot should return spot",
- args: args{
- labels: map[string]string{
- models.KarpenterCapacityTypeLabel: models.KarpenterCapacitySpotTypeValue,
- },
- },
- want: PreemptibleType,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- k := &awsKey{
- Labels: tt.fields.Labels,
- ProviderID: tt.fields.ProviderID,
- }
- if got := k.getUsageType(tt.args.labels); got != tt.want {
- t.Errorf("getUsageType() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- // Test_PricingData_Regression
- //
- // Objective: To test the pricing data download and validate the schema is still
- // as expected
- //
- // These tests may take a long time to complete. It is downloading AWS Pricing
- // data files (~500MB) for each region.
- func Test_PricingData_Regression(t *testing.T) {
- if os.Getenv("INTEGRATION") == "" {
- t.Skip("skipping integration tests, set environment variable INTEGRATION")
- }
- awsRegions := []string{"us-east-1", "eu-west-1"}
- // Check pricing data produced for each region
- for _, region := range awsRegions {
- awsTest := AWS{}
- res, _, err := awsTest.getRegionPricing([]*clustercache.Node{
- {
- Labels: map[string]string{"topology.kubernetes.io/region": region},
- }})
- if err != nil {
- t.Errorf("Failed to download pricing data for region %s: %v", region, err)
- }
- // Unmarshal pricing data into PriceListEC2Response
- var pricingData PriceListEC2Response
- body, err := io.ReadAll(res.Body)
- if err != nil {
- t.Errorf("Failed to read pricing data for region %s: %v", region, err)
- }
- err = json.Unmarshal(body, &pricingData)
- if err != nil {
- t.Errorf("Failed to unmarshal pricing data for region %s: %v", region, err)
- }
- // ASSERTION. We only anticipate "OnDemand" or "CapacityBlock" in the
- // pricing data.
- //
- // Failing this test does not necessarily mean we have regressed. Just
- // that we need to revisit this code to ensure OnDemand pricing is still
- // functioning as expected.
- for _, product := range pricingData.Products {
- if product.Attributes.MarketOption != "OnDemand" && product.Attributes.MarketOption != "CapacityBlock" && product.Attributes.MarketOption != "" {
- t.Errorf("Invalid marketOption for product %s: %s", product.Sku, product.Attributes.MarketOption)
- }
- }
- }
- }
- // Test_populate_pricing
- //
- // Objective: To test core pricing population logic for AWS
- //
- // Case 0: US endpoints
- // Take a portion of json returned from ondemand terms in us endpoints load the
- // request into the http response and give it to the function inspect the
- // resulting aws object after the function returns and validate fields
- //
- // Case 1: Ensure marketOption=OnDemand
- // AWS introduced the field marketOption. We need to further filter for
- // marketOption=OnDemand to ensure we are not getting pricing from a line item
- // such as marketOption=CapacityBlock
- //
- // Case 2: Chinese endpoints
- // Same as above US test case, except using CN PV offer codes. Validate
- // populated fields in AWS object
- func Test_populate_pricing(t *testing.T) {
- awsTest := AWS{
- ValidPricingKeys: map[string]bool{},
- ClusterRegion: "us-east-2",
- }
- inputkeys := map[string]bool{
- "us-east-2,m5.large,linux": true,
- }
- fixture, err := os.Open("testdata/pricing-us-east-2.json")
- if err != nil {
- t.Fatalf("failed to load pricing fixture: %s", err)
- }
- testResponse := http.Response{
- Body: io.NopCloser(fixture),
- Request: &http.Request{
- URL: &url.URL{
- Scheme: "https",
- Host: "test-aws-http-endpoint:443",
- },
- },
- }
- awsTest.populatePricing(&testResponse, inputkeys)
- expectedProdTermsDisk := &AWSProductTerms{
- Sku: "M6UGCCQ3CDJQAA37",
- Memory: "",
- Storage: "",
- VCpu: "",
- GPU: "",
- OnDemand: &PriceListEC2Term{
- Sku: "M6UGCCQ3CDJQAA37",
- OfferTermCode: "JRTCKXETXF",
- PriceDimensions: map[string]*PriceListEC2PriceDimension{
- "M6UGCCQ3CDJQAA37.JRTCKXETXF.6YS6EN2CT7": {
- Unit: "GB-Mo",
- PricePerUnit: PriceListEC2PricePerUnit{
- USD: "0.0800000000",
- CNY: "",
- },
- },
- },
- },
- PV: &models.PV{
- Cost: "0.00010958904109589041",
- CostPerIO: "",
- Class: "gp3",
- Size: "",
- Region: "us-east-2",
- ProviderID: "",
- },
- }
- expectedProdTermsInstanceOndemand := &AWSProductTerms{
- Sku: "8D49XP354UEYTHGM",
- Memory: "8 GiB",
- Storage: "EBS only",
- VCpu: "2",
- GPU: "",
- OnDemand: &PriceListEC2Term{
- Sku: "8D49XP354UEYTHGM",
- OfferTermCode: "MZU6U2429S",
- PriceDimensions: map[string]*PriceListEC2PriceDimension{
- "8D49XP354UEYTHGM.MZU6U2429S.2TG2D8R56U": {
- Unit: "Quantity",
- PricePerUnit: PriceListEC2PricePerUnit{
- USD: "1161",
- CNY: "",
- },
- },
- },
- },
- }
- expectedProdTermsInstanceSpot := &AWSProductTerms{
- Sku: "8D49XP354UEYTHGM",
- Memory: "8 GiB",
- Storage: "EBS only",
- VCpu: "2",
- GPU: "",
- OnDemand: &PriceListEC2Term{
- Sku: "8D49XP354UEYTHGM",
- OfferTermCode: "MZU6U2429S",
- PriceDimensions: map[string]*PriceListEC2PriceDimension{
- "8D49XP354UEYTHGM.MZU6U2429S.2TG2D8R56U": {
- Unit: "Quantity",
- PricePerUnit: PriceListEC2PricePerUnit{
- USD: "1161",
- CNY: "",
- },
- },
- },
- },
- }
- expectedProdTermsLoadbalancer := &AWSProductTerms{
- Sku: "Y9RYMSE644KDSV4S",
- OnDemand: &PriceListEC2Term{
- Sku: "Y9RYMSE644KDSV4S",
- OfferTermCode: "JRTCKXETXF",
- PriceDimensions: map[string]*PriceListEC2PriceDimension{
- "Y9RYMSE644KDSV4S.JRTCKXETXF.6YS6EN2CT7": {
- Unit: "Hrs",
- PricePerUnit: PriceListEC2PricePerUnit{
- USD: "0.0225000000",
- CNY: "",
- },
- },
- },
- },
- LoadBalancer: &models.LoadBalancer{
- Cost: 0.0225,
- },
- }
- expectedPricing := map[string]*AWSProductTerms{
- "us-east-2,EBS:VolumeUsage.gp3": expectedProdTermsDisk,
- "us-east-2,EBS:VolumeUsage.gp3,preemptible": expectedProdTermsDisk,
- "us-east-2,m5.large,linux": expectedProdTermsInstanceOndemand,
- "us-east-2,m5.large,linux,preemptible": expectedProdTermsInstanceSpot,
- "us-east-2,LoadBalancerUsage": expectedProdTermsLoadbalancer,
- }
- if !reflect.DeepEqual(expectedPricing, awsTest.Pricing) {
- t.Fatalf("expected parsed pricing did not match actual parsed result (us-east-2)")
- }
- lbPricing, _ := awsTest.LoadBalancerPricing()
- if lbPricing.Cost != 0.0225 {
- t.Fatalf("expected loadbalancer pricing of 0.0225 but got %f (us-east-2)", lbPricing.Cost)
- }
- // Case 1 - Only accept `"marketoption":"OnDemand"`
- inputkeysCase1 := map[string]bool{
- "us-east-1,p4d.24xlarge,linux": true,
- }
- fixture, err = os.Open("testdata/pricing-us-east-1.json")
- if err != nil {
- t.Fatalf("failed to load pricing fixture: %s", err)
- }
- testResponseCase1 := http.Response{
- Body: io.NopCloser(fixture),
- Request: &http.Request{
- URL: &url.URL{
- Scheme: "https",
- Host: "test-aws-http-endpoint:443",
- },
- },
- }
- awsTest.populatePricing(&testResponseCase1, inputkeysCase1)
- expectedProdTermsInstanceOndemandCase1 := &AWSProductTerms{
- Sku: "H7NGEAC6UEHNTKSJ",
- Memory: "1152 GiB",
- Storage: "8 x 1000 SSD",
- VCpu: "96",
- GPU: "8",
- OnDemand: &PriceListEC2Term{
- Sku: "H7NGEAC6UEHNTKSJ",
- OfferTermCode: "JRTCKXETXF",
- PriceDimensions: map[string]*PriceListEC2PriceDimension{
- "H7NGEAC6UEHNTKSJ.JRTCKXETXF.6YS6EN2CT7": {
- Unit: "Hrs",
- PricePerUnit: PriceListEC2PricePerUnit{
- USD: "32.7726000000",
- },
- },
- },
- },
- }
- expectedPricingCase1 := map[string]*AWSProductTerms{
- "us-east-1,p4d.24xlarge,linux": expectedProdTermsInstanceOndemandCase1,
- "us-east-1,p4d.24xlarge,linux,preemptible": expectedProdTermsInstanceOndemandCase1,
- }
- if !reflect.DeepEqual(expectedPricingCase1, awsTest.Pricing) {
- expectedJsonString, _ := json.MarshalIndent(expectedPricingCase1, "", " ")
- resultJsonString, _ := json.MarshalIndent(awsTest.Pricing, "", " ")
- t.Logf("Expected: %s", string(expectedJsonString))
- t.Logf("Result: %s", string(resultJsonString))
- t.Fatalf("expected parsed pricing did not match actual parsed result (us-east-1)")
- }
- // Case 2
- awsTest = AWS{
- ValidPricingKeys: map[string]bool{},
- }
- fixture, err = os.Open("testdata/pricing-cn-northwest-1.json")
- if err != nil {
- t.Fatalf("failed to load pricing fixture: %s", err)
- }
- testResponse = http.Response{
- Body: io.NopCloser(fixture),
- Request: &http.Request{
- URL: &url.URL{
- Scheme: "https",
- Host: "test-aws-http-endpoint:443",
- },
- },
- }
- awsTest.populatePricing(&testResponse, inputkeys)
- expectedProdTermsDisk = &AWSProductTerms{
- Sku: "R83VXG9NAPDASEGN",
- Memory: "",
- Storage: "",
- VCpu: "",
- GPU: "",
- OnDemand: &PriceListEC2Term{
- Sku: "R83VXG9NAPDASEGN",
- OfferTermCode: "5Y9WH78GDR",
- PriceDimensions: map[string]*PriceListEC2PriceDimension{
- "R83VXG9NAPDASEGN.5Y9WH78GDR.Q7UJUT2CE6": {
- Unit: "GB-Mo",
- PricePerUnit: PriceListEC2PricePerUnit{
- USD: "",
- CNY: "0.5312000000",
- },
- },
- },
- },
- PV: &models.PV{
- Cost: "0.0007276712328767123",
- CostPerIO: "",
- Class: "gp3",
- Size: "",
- Region: "cn-northwest-1",
- ProviderID: "",
- },
- }
- expectedPricing = map[string]*AWSProductTerms{
- "cn-northwest-1,EBS:VolumeUsage.gp3": expectedProdTermsDisk,
- "cn-northwest-1,EBS:VolumeUsage.gp3,preemptible": expectedProdTermsDisk,
- }
- if !reflect.DeepEqual(expectedPricing, awsTest.Pricing) {
- t.Fatalf("expected parsed pricing did not match actual parsed result (cn)")
- }
- }
- func TestFeatures(t *testing.T) {
- testCases := map[string]struct {
- aws awsKey
- expected string
- }{
- "Spot from custom labels": {
- aws: awsKey{
- SpotLabelName: "node-type",
- SpotLabelValue: "node-spot",
- Labels: map[string]string{
- "node-type": "node-spot",
- v1.LabelOSStable: "linux",
- v1.LabelHostname: "my-hostname",
- v1.LabelTopologyRegion: "us-west-2",
- v1.LabelTopologyZone: "us-west-2b",
- v1.LabelInstanceTypeStable: "m5.large",
- },
- },
- expected: "us-west-2,m5.large,linux,preemptible",
- },
- }
- for name, tc := range testCases {
- t.Run(name, func(t *testing.T) {
- features := tc.aws.Features()
- if features != tc.expected {
- t.Errorf("expected %s, got %s", tc.expected, features)
- }
- })
- }
- }
- func Test_getStorageClassTypeFrom(t *testing.T) {
- tests := []struct {
- name string
- provisioner string
- want string
- }{
- {
- name: "empty-provisioner",
- provisioner: "",
- want: "",
- },
- {
- name: "ebs-default-provisioner",
- provisioner: "kubernetes.io/aws-ebs",
- want: "gp2",
- },
- {
- name: "ebs-csi-provisioner",
- provisioner: "ebs.csi.aws.com",
- want: "gp3",
- },
- {
- name: "unknown-provisioner",
- provisioner: "unknown",
- want: "",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if got := getStorageClassTypeFrom(tt.provisioner); got != tt.want {
- t.Errorf("getStorageClassTypeFrom() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_awsKey_isFargateNode(t *testing.T) {
- tests := []struct {
- name string
- labels map[string]string
- want bool
- }{
- {
- name: "fargate node with correct label",
- labels: map[string]string{
- eksComputeTypeLabel: "fargate",
- },
- want: true,
- },
- {
- name: "ec2 node with different compute type",
- labels: map[string]string{
- eksComputeTypeLabel: "ec2",
- },
- want: false,
- },
- {
- name: "node without compute type label",
- labels: map[string]string{
- "some.other.label": "value",
- },
- want: false,
- },
- {
- name: "node with empty labels",
- labels: map[string]string{},
- want: false,
- },
- {
- name: "node with nil labels",
- labels: nil,
- want: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- k := &awsKey{
- Labels: tt.labels,
- }
- if got := k.isFargateNode(); got != tt.want {
- t.Errorf("awsKey.isFargateNode() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestGetPricingListURL(t *testing.T) {
- tests := []struct {
- name string
- serviceCode string
- nodeList []*clustercache.Node
- expected string
- }{
- {
- name: "AmazonEC2 service with us-east-1 region",
- serviceCode: "AmazonEC2",
- nodeList: []*clustercache.Node{
- {
- Name: "test-node",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- },
- },
- },
- expected: "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/us-east-1/index.json",
- },
- {
- name: "AmazonECS service with us-west-2 region",
- serviceCode: "AmazonECS",
- nodeList: []*clustercache.Node{
- {
- Name: "test-node",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-west-2",
- },
- },
- },
- expected: "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonECS/current/us-west-2/index.json",
- },
- {
- name: "Chinese region cn-north-1",
- serviceCode: "AmazonEC2",
- nodeList: []*clustercache.Node{
- {
- Name: "test-node",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "cn-north-1",
- },
- },
- },
- expected: "https://pricing.cn-north-1.amazonaws.com.cn/offers/v1.0/cn/AmazonEC2/current/cn-north-1/index.json",
- },
- {
- name: "Chinese region cn-northwest-1",
- serviceCode: "AmazonECS",
- nodeList: []*clustercache.Node{
- {
- Name: "test-node",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "cn-northwest-1",
- },
- },
- },
- expected: "https://pricing.cn-north-1.amazonaws.com.cn/offers/v1.0/cn/AmazonECS/current/cn-northwest-1/index.json",
- },
- {
- name: "empty node list - multiregion",
- serviceCode: "AmazonEC2",
- nodeList: []*clustercache.Node{},
- expected: "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/index.json",
- },
- {
- name: "multiple regions - multiregion",
- serviceCode: "AmazonECS",
- nodeList: []*clustercache.Node{
- {
- Name: "test-node-1",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- },
- },
- {
- Name: "test-node-2",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-west-2",
- },
- },
- },
- expected: "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonECS/current/index.json",
- },
- {
- name: "node without region label",
- serviceCode: "AmazonEC2",
- nodeList: []*clustercache.Node{
- {
- Name: "test-node",
- Labels: map[string]string{
- "some.other.label": "value",
- },
- },
- },
- expected: "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/index.json",
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := getPricingListURL(tt.serviceCode, tt.nodeList)
- if result != tt.expected {
- t.Errorf("getPricingListURL() = %v, expected %v", result, tt.expected)
- }
- })
- }
- }
- func Test_configUpdaterWithReaderAndType_forSpotValues(t *testing.T) {
- fixture, err := os.Open("testdata/aws-config.json")
- if err != nil {
- t.Fatalf("failed to load aws config fixture: %s", err)
- }
- defer fixture.Close()
- c := &models.CustomPricing{}
- callback := configUpdaterWithReaderAndType(fixture, "otherupdatetype")
- err = callback(c)
- if err != nil {
- t.Fatalf("failed to load aws config: %s", err)
- }
- if c.AwsSpotDataBucket != "mybucket" {
- t.Fatalf("Expected %s but got %s", "mybucket", c.AwsSpotDataBucket)
- }
- if c.AwsSpotDataPrefix != "myprefix" {
- t.Fatalf("Expected %s but got %s", "myprefix", c.AwsSpotDataPrefix)
- }
- if c.AwsSpotDataRegion != "us-east-1" {
- t.Fatalf("Expected %s but got %s", "us-east-1", c.AwsSpotDataRegion)
- }
- fixture2, err := os.Open("testdata/aws-config-empty.json")
- if err != nil {
- t.Fatalf("failed to load aws config fixture: %s", err)
- }
- defer fixture2.Close()
- c = &models.CustomPricing{}
- callback = configUpdaterWithReaderAndType(fixture2, "otherupdatetype")
- err = callback(c)
- if err != nil {
- t.Fatalf("failed to load aws config: %s", err)
- }
- if c.AwsSpotDataBucket != "" {
- t.Fatalf("Expected empty string but got %s", c.AwsSpotDataBucket)
- }
- if c.AwsSpotDataPrefix != "" {
- t.Fatalf("Expected empty string but got %s", c.AwsSpotDataPrefix)
- }
- if c.AwsSpotDataRegion != "" {
- t.Fatalf("Expected empty string but got %s", c.AwsSpotDataRegion)
- }
- }
- func TestAWS_getFargatePod(t *testing.T) {
- tests := []struct {
- name string
- pods []*clustercache.Pod
- awsKey *awsKey
- wantPod *clustercache.Pod
- wantBool bool
- }{
- {
- name: "pod found for node",
- pods: []*clustercache.Pod{
- {
- Name: "test-pod",
- Spec: clustercache.PodSpec{
- NodeName: "fargate-node-1",
- },
- },
- },
- awsKey: &awsKey{
- Name: "fargate-node-1",
- },
- wantPod: &clustercache.Pod{
- Name: "test-pod",
- Spec: clustercache.PodSpec{
- NodeName: "fargate-node-1",
- },
- },
- wantBool: true,
- },
- {
- name: "pod not found for node",
- pods: []*clustercache.Pod{
- {
- Name: "test-pod",
- Spec: clustercache.PodSpec{
- NodeName: "different-node",
- },
- },
- },
- awsKey: &awsKey{
- Name: "fargate-node-1",
- },
- wantPod: nil,
- wantBool: false,
- },
- {
- name: "no pods in cluster",
- pods: []*clustercache.Pod{},
- awsKey: &awsKey{
- Name: "fargate-node-1",
- },
- wantPod: nil,
- wantBool: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- aws := &AWS{
- Clientset: &clustercache.MockClusterCache{Pods: tt.pods},
- }
- gotPod, gotBool := aws.getFargatePod(tt.awsKey)
- if gotBool != tt.wantBool {
- t.Errorf("AWS.getFargatePod() gotBool = %v, want %v", gotBool, tt.wantBool)
- }
- if tt.wantPod == nil && gotPod != nil {
- t.Errorf("AWS.getFargatePod() gotPod = %v, want nil", gotPod)
- } else if tt.wantPod != nil && gotPod == nil {
- t.Errorf("AWS.getFargatePod() gotPod = nil, want %v", tt.wantPod)
- } else if tt.wantPod != nil && gotPod != nil {
- if gotPod.Name != tt.wantPod.Name || gotPod.Spec.NodeName != tt.wantPod.Spec.NodeName {
- t.Errorf("AWS.getFargatePod() gotPod = %v, want %v", gotPod, tt.wantPod)
- }
- }
- })
- }
- }
- // fakeProviderConfig implements models.ProviderConfig for testing
- type fakeProviderConfig struct {
- customPricing *models.CustomPricing
- }
- func (f *fakeProviderConfig) GetCustomPricingData() (*models.CustomPricing, error) {
- if f.customPricing != nil {
- return f.customPricing, nil
- }
- return &models.CustomPricing{}, nil
- }
- func (f *fakeProviderConfig) Update(func(*models.CustomPricing) error) (*models.CustomPricing, error) {
- return f.customPricing, nil
- }
- func (f *fakeProviderConfig) UpdateFromMap(map[string]string) (*models.CustomPricing, error) {
- return f.customPricing, nil
- }
- func (f *fakeProviderConfig) ConfigFileManager() *config.ConfigFileManager {
- return nil
- }
- func TestAWS_SpotFeedRefreshEnabled(t *testing.T) {
- tests := []struct {
- name string
- spotDataBucket string
- spotDataRegion string
- projectID string
- spotDataFeedEnabled string
- want bool
- }{
- {
- name: "disabled via config - with bucket",
- spotDataBucket: "my-bucket",
- spotDataRegion: "us-east-1",
- projectID: "123456789",
- spotDataFeedEnabled: "false",
- want: false,
- },
- {
- name: "disabled via config - with projectID only",
- projectID: "123456789",
- spotDataFeedEnabled: "false",
- want: false,
- },
- {
- name: "enabled by default - with bucket",
- spotDataBucket: "my-bucket",
- spotDataRegion: "us-east-1",
- projectID: "123456789",
- spotDataFeedEnabled: "",
- want: true,
- },
- {
- name: "enabled explicitly - with bucket",
- spotDataBucket: "my-bucket",
- spotDataRegion: "us-east-1",
- projectID: "123456789",
- spotDataFeedEnabled: "true",
- want: true,
- },
- {
- name: "no spot config - disabled",
- spotDataBucket: "",
- spotDataRegion: "",
- projectID: "",
- spotDataFeedEnabled: "",
- want: false,
- },
- {
- name: "no spot config - but explicitly enabled",
- spotDataBucket: "",
- spotDataRegion: "",
- projectID: "",
- spotDataFeedEnabled: "true",
- want: false,
- },
- {
- name: "only projectID set - enabled by default",
- projectID: "123456789",
- spotDataFeedEnabled: "",
- want: true,
- },
- {
- name: "only bucket set - enabled by default",
- spotDataBucket: "my-bucket",
- spotDataFeedEnabled: "",
- want: true,
- },
- {
- name: "only region set - enabled by default",
- spotDataRegion: "us-east-1",
- spotDataFeedEnabled: "",
- want: true,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- aws := &AWS{
- SpotDataBucket: tt.spotDataBucket,
- SpotDataRegion: tt.spotDataRegion,
- ProjectID: tt.projectID,
- Config: &fakeProviderConfig{
- customPricing: &models.CustomPricing{
- SpotDataFeedEnabled: tt.spotDataFeedEnabled,
- },
- },
- }
- got := aws.SpotFeedRefreshEnabled()
- if got != tt.want {
- t.Errorf("AWS.SpotFeedRefreshEnabled() = %v, want %v", got, tt.want)
- }
- })
- }
- // Test nil Config scenario to ensure no panic
- t.Run("nil config - falls back to field check", func(t *testing.T) {
- aws := &AWS{
- SpotDataBucket: "my-bucket",
- SpotDataRegion: "us-east-1",
- ProjectID: "123456789",
- Config: nil, // nil Config should not cause panic
- }
- got := aws.SpotFeedRefreshEnabled()
- want := true // Should fall back to field-based check
- if got != want {
- t.Errorf("AWS.SpotFeedRefreshEnabled() with nil Config = %v, want %v", got, want)
- }
- })
- t.Run("nil config - no spot fields", func(t *testing.T) {
- aws := &AWS{
- SpotDataBucket: "",
- SpotDataRegion: "",
- ProjectID: "",
- Config: nil, // nil Config should not cause panic
- }
- got := aws.SpotFeedRefreshEnabled()
- want := false // No fields set, should return false
- if got != want {
- t.Errorf("AWS.SpotFeedRefreshEnabled() with nil Config and no fields = %v, want %v", got, want)
- }
- })
- }
- func TestAWS_spotPricingFromHistory(t *testing.T) {
- t.Run("nil cache returns false", func(t *testing.T) {
- aws := &AWS{}
- key := &awsKey{
- ProviderID: "aws:///us-east-1a/i-0123456789abcdef0",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- "topology.kubernetes.io/zone": "us-east-1a",
- "node.kubernetes.io/instance-type": "m5.large",
- "kubernetes.io/os": "linux",
- "eks.amazonaws.com/capacityType": "SPOT",
- },
- }
- _, ok := aws.spotPricingFromHistory(key)
- if ok {
- t.Error("Expected false when cache is nil")
- }
- })
- t.Run("missing region label returns false", func(t *testing.T) {
- mockFetcher := &mockSpotPriceHistoryFetcher{}
- aws := &AWS{
- SpotPriceHistoryCache: NewSpotPriceHistoryCache(mockFetcher),
- }
- key := &awsKey{
- ProviderID: "aws:///us-east-1a/i-0123456789abcdef0",
- Labels: map[string]string{
- "topology.kubernetes.io/zone": "us-east-1a",
- "node.kubernetes.io/instance-type": "m5.large",
- },
- }
- _, ok := aws.spotPricingFromHistory(key)
- if ok {
- t.Error("Expected false when region label is missing")
- }
- })
- t.Run("missing instance type label returns false", func(t *testing.T) {
- mockFetcher := &mockSpotPriceHistoryFetcher{}
- aws := &AWS{
- SpotPriceHistoryCache: NewSpotPriceHistoryCache(mockFetcher),
- }
- key := &awsKey{
- ProviderID: "aws:///us-east-1a/i-0123456789abcdef0",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- "topology.kubernetes.io/zone": "us-east-1a",
- },
- }
- _, ok := aws.spotPricingFromHistory(key)
- if ok {
- t.Error("Expected false when instance type label is missing")
- }
- })
- t.Run("missing zone label returns false", func(t *testing.T) {
- mockFetcher := &mockSpotPriceHistoryFetcher{}
- aws := &AWS{
- SpotPriceHistoryCache: NewSpotPriceHistoryCache(mockFetcher),
- }
- key := &awsKey{
- ProviderID: "aws:///us-east-1a/i-0123456789abcdef0",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- "node.kubernetes.io/instance-type": "m5.large",
- },
- }
- _, ok := aws.spotPricingFromHistory(key)
- if ok {
- t.Error("Expected false when zone label is missing")
- }
- })
- t.Run("fetcher error returns false", func(t *testing.T) {
- mockFetcher := &mockSpotPriceHistoryFetcher{
- fetchFunc: func(key SpotPriceHistoryKey) (*SpotPriceHistoryEntry, error) {
- return nil, errors.New("api error")
- },
- }
- aws := &AWS{
- SpotPriceHistoryCache: NewSpotPriceHistoryCache(mockFetcher),
- }
- key := &awsKey{
- ProviderID: "aws:///us-east-1a/i-0123456789abcdef0",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- "topology.kubernetes.io/zone": "us-east-1a",
- "node.kubernetes.io/instance-type": "m5.large",
- },
- }
- _, ok := aws.spotPricingFromHistory(key)
- if ok {
- t.Error("Expected false when fetcher returns error")
- }
- })
- t.Run("successful lookup returns entry", func(t *testing.T) {
- mockFetcher := &mockSpotPriceHistoryFetcher{
- fetchFunc: func(key SpotPriceHistoryKey) (*SpotPriceHistoryEntry, error) {
- if key.Region != "us-east-1" || key.InstanceType != "m5.large" || key.AvailabilityZone != "us-east-1a" {
- t.Errorf("Unexpected key: %v", key)
- }
- return &SpotPriceHistoryEntry{
- SpotPrice: 0.042,
- Timestamp: time.Now(),
- RetrievedAt: time.Now(),
- }, nil
- },
- }
- aws := &AWS{
- SpotPriceHistoryCache: NewSpotPriceHistoryCache(mockFetcher),
- }
- key := &awsKey{
- ProviderID: "aws:///us-east-1a/i-0123456789abcdef0",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- "topology.kubernetes.io/zone": "us-east-1a",
- "node.kubernetes.io/instance-type": "m5.large",
- },
- }
- entry, ok := aws.spotPricingFromHistory(key)
- if !ok {
- t.Fatal("Expected true for successful lookup")
- }
- if entry.SpotPrice != 0.042 {
- t.Errorf("Expected spot price 0.042, got %f", entry.SpotPrice)
- }
- })
- }
- func TestAWS_createNode_spotHistoryFallback(t *testing.T) {
- // Helper to build AWSProductTerms with on-demand pricing
- makeTerms := func(sku, offerTermCode, cost string) *AWSProductTerms {
- priceKey := sku + "." + offerTermCode + "." + HourlyRateCode
- return &AWSProductTerms{
- Sku: sku,
- OnDemand: &PriceListEC2Term{
- Sku: sku,
- OfferTermCode: offerTermCode,
- PriceDimensions: map[string]*PriceListEC2PriceDimension{
- priceKey: {
- Unit: "Hrs",
- PricePerUnit: PriceListEC2PricePerUnit{USD: cost},
- },
- },
- },
- VCpu: "4",
- Memory: "16",
- }
- }
- t.Run("preemptible node uses spot history when available", func(t *testing.T) {
- mockFetcher := &mockSpotPriceHistoryFetcher{
- fetchFunc: func(key SpotPriceHistoryKey) (*SpotPriceHistoryEntry, error) {
- return &SpotPriceHistoryEntry{
- SpotPrice: 0.035,
- Timestamp: time.Now(),
- RetrievedAt: time.Now(),
- }, nil
- },
- }
- aws := &AWS{
- SpotPriceHistoryCache: NewSpotPriceHistoryCache(mockFetcher),
- BaseCPUPrice: "0.04",
- BaseRAMPrice: "0.01",
- BaseGPUPrice: "0.95",
- }
- terms := makeTerms("SKU123", "JRTCKXETXF", "0.096")
- // Key with PreemptibleType suffix to trigger isPreemptible
- key := &awsKey{
- ProviderID: "aws:///us-east-1a/i-0123456789abcdef0",
- SpotLabelName: "eks.amazonaws.com/capacityType",
- SpotLabelValue: "SPOT",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- "topology.kubernetes.io/zone": "us-east-1a",
- "node.kubernetes.io/instance-type": "m5.large",
- "kubernetes.io/os": "linux",
- "eks.amazonaws.com/capacityType": "SPOT",
- },
- }
- node, meta, err := aws.createNode(terms, PreemptibleType, key)
- if err != nil {
- t.Fatalf("Unexpected error: %v", err)
- }
- if node.Cost != "0.035000" {
- t.Errorf("Expected spot history cost 0.035000, got %s", node.Cost)
- }
- if node.UsageType != PreemptibleType {
- t.Errorf("Expected usage type %s, got %s", PreemptibleType, node.UsageType)
- }
- if meta.Source != SpotPriceHistorySource {
- t.Errorf("Expected source %s, got %s", SpotPriceHistorySource, meta.Source)
- }
- })
- t.Run("preemptible node falls back to on-demand when history unavailable", func(t *testing.T) {
- mockFetcher := &mockSpotPriceHistoryFetcher{
- fetchFunc: func(key SpotPriceHistoryKey) (*SpotPriceHistoryEntry, error) {
- return nil, errors.New("no data")
- },
- }
- aws := &AWS{
- SpotPriceHistoryCache: NewSpotPriceHistoryCache(mockFetcher),
- BaseCPUPrice: "0.04",
- BaseRAMPrice: "0.01",
- BaseGPUPrice: "0.95",
- }
- terms := makeTerms("SKU123", "JRTCKXETXF", "0.096")
- key := &awsKey{
- ProviderID: "aws:///us-east-1a/i-0123456789abcdef0",
- SpotLabelName: "eks.amazonaws.com/capacityType",
- SpotLabelValue: "SPOT",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- "topology.kubernetes.io/zone": "us-east-1a",
- "node.kubernetes.io/instance-type": "m5.large",
- "kubernetes.io/os": "linux",
- "eks.amazonaws.com/capacityType": "SPOT",
- },
- }
- node, _, err := aws.createNode(terms, PreemptibleType, key)
- if err != nil {
- t.Fatalf("Unexpected error: %v", err)
- }
- if node.Cost != "0.096" {
- t.Errorf("Expected on-demand cost 0.096, got %s", node.Cost)
- }
- if node.UsageType != PreemptibleType {
- t.Errorf("Expected usage type %s, got %s", PreemptibleType, node.UsageType)
- }
- })
- t.Run("preemptible node with nil cache falls back to on-demand", func(t *testing.T) {
- aws := &AWS{
- BaseCPUPrice: "0.04",
- BaseRAMPrice: "0.01",
- BaseGPUPrice: "0.95",
- }
- terms := makeTerms("SKU123", "JRTCKXETXF", "0.096")
- key := &awsKey{
- ProviderID: "aws:///us-east-1a/i-0123456789abcdef0",
- SpotLabelName: "eks.amazonaws.com/capacityType",
- SpotLabelValue: "SPOT",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- "topology.kubernetes.io/zone": "us-east-1a",
- "node.kubernetes.io/instance-type": "m5.large",
- "kubernetes.io/os": "linux",
- "eks.amazonaws.com/capacityType": "SPOT",
- },
- }
- node, _, err := aws.createNode(terms, PreemptibleType, key)
- if err != nil {
- t.Fatalf("Unexpected error: %v", err)
- }
- if node.Cost != "0.096" {
- t.Errorf("Expected on-demand cost 0.096, got %s", node.Cost)
- }
- })
- t.Run("preemptible node uses base spot prices when no public pricing", func(t *testing.T) {
- mockFetcher := &mockSpotPriceHistoryFetcher{
- fetchFunc: func(key SpotPriceHistoryKey) (*SpotPriceHistoryEntry, error) {
- return nil, errors.New("no data")
- },
- }
- aws := &AWS{
- SpotPriceHistoryCache: NewSpotPriceHistoryCache(mockFetcher),
- BaseCPUPrice: "0.04",
- BaseRAMPrice: "0.01",
- BaseGPUPrice: "0.95",
- BaseSpotCPUPrice: "0.02",
- BaseSpotRAMPrice: "0.005",
- }
- // Terms without valid pricing dimensions
- terms := &AWSProductTerms{
- Sku: "SKU123",
- OnDemand: &PriceListEC2Term{
- Sku: "SKU123",
- OfferTermCode: "JRTCKXETXF",
- PriceDimensions: map[string]*PriceListEC2PriceDimension{},
- },
- VCpu: "4",
- Memory: "16",
- }
- key := &awsKey{
- ProviderID: "aws:///us-east-1a/i-0123456789abcdef0",
- SpotLabelName: "eks.amazonaws.com/capacityType",
- SpotLabelValue: "SPOT",
- Labels: map[string]string{
- "topology.kubernetes.io/region": "us-east-1",
- "topology.kubernetes.io/zone": "us-east-1a",
- "node.kubernetes.io/instance-type": "m5.large",
- "kubernetes.io/os": "linux",
- "eks.amazonaws.com/capacityType": "SPOT",
- },
- }
- node, _, err := aws.createNode(terms, PreemptibleType, key)
- if err != nil {
- t.Fatalf("Unexpected error: %v", err)
- }
- if node.VCPUCost != "0.02" {
- t.Errorf("Expected base spot CPU price 0.02, got %s", node.VCPUCost)
- }
- if node.RAMCost != "0.005" {
- t.Errorf("Expected base spot RAM price 0.005, got %s", node.RAMCost)
- }
- })
- }
- func TestAWS_PricingSourceStatus_spotPriceHistory(t *testing.T) {
- t.Run("not yet initialized", func(t *testing.T) {
- aws := &AWS{
- Config: &fakeProviderConfig{
- customPricing: &models.CustomPricing{},
- },
- }
- sources := aws.PricingSourceStatus()
- sphs, ok := sources[SpotPriceHistorySource]
- if !ok {
- t.Fatal("Expected SpotPriceHistorySource in sources")
- }
- if sphs.Available {
- t.Error("Expected Available=false when cache not initialized")
- }
- if sphs.Error != "Not yet initialized" {
- t.Errorf("Expected 'Not yet initialized' error, got %q", sphs.Error)
- }
- })
- t.Run("initialization error", func(t *testing.T) {
- aws := &AWS{
- SpotPriceHistoryError: errors.New("no cluster region configured"),
- Config: &fakeProviderConfig{
- customPricing: &models.CustomPricing{},
- },
- }
- sources := aws.PricingSourceStatus()
- sphs := sources[SpotPriceHistorySource]
- if sphs.Available {
- t.Error("Expected Available=false on error")
- }
- if sphs.Error != "no cluster region configured" {
- t.Errorf("Expected error message, got %q", sphs.Error)
- }
- })
- t.Run("successfully initialized", func(t *testing.T) {
- mockFetcher := &mockSpotPriceHistoryFetcher{}
- aws := &AWS{
- SpotPriceHistoryCache: NewSpotPriceHistoryCache(mockFetcher),
- Config: &fakeProviderConfig{
- customPricing: &models.CustomPricing{},
- },
- }
- sources := aws.PricingSourceStatus()
- sphs := sources[SpotPriceHistorySource]
- if !sphs.Available {
- t.Error("Expected Available=true when cache initialized")
- }
- })
- }
|