| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- package kubecost
- import (
- "testing"
- "time"
- "github.com/opencost/opencost/pkg/util/timeutil"
- )
- var cciProperties1 = CloudCostItemProperties{
- ProviderID: "providerid1",
- Provider: "provider1",
- WorkGroupID: "workgroup1",
- BillingID: "billing1",
- Service: "service1",
- Category: "category1",
- Labels: map[string]string{
- "label1": "value1",
- "label2": "value2",
- },
- }
- // TestCloudCostItem_LoadCloudCostItem checks that loaded CloudCostItems end up in the correct set in the
- // correct proportions
- func TestCloudCostItem_LoadCloudCostItem(t *testing.T) {
- // create values for 3 day Range tests
- end := RoundBack(time.Now().UTC(), timeutil.Day)
- start := end.Add(-3 * timeutil.Day)
- dayWindows, _ := GetWindows(start, end, timeutil.Day)
- emtpyCCISR, _ := NewCloudCostItemSetRange(start, end, timeutil.Day, "integration")
- testCases := map[string]struct {
- cci []*CloudCostItem
- ccisr *CloudCostItemSetRange
- expected []*CloudCostItemSet
- }{
- "Load Single Day On Grid": {
- cci: []*CloudCostItem{
- {
- Properties: cciProperties1,
- Window: dayWindows[0],
- IsKubernetes: true,
- Cost: 100,
- NetCost: 80,
- },
- },
- ccisr: emtpyCCISR.Clone(),
- expected: []*CloudCostItemSet{
- {
- Integration: "integration",
- Window: dayWindows[0],
- CloudCostItems: map[string]*CloudCostItem{
- cciProperties1.Key(): {
- Properties: cciProperties1,
- Window: dayWindows[0],
- IsKubernetes: true,
- Cost: 100,
- NetCost: 80,
- },
- },
- },
- {
- Integration: "integration",
- Window: dayWindows[1],
- CloudCostItems: map[string]*CloudCostItem{},
- },
- {
- Integration: "integration",
- Window: dayWindows[2],
- CloudCostItems: map[string]*CloudCostItem{},
- },
- },
- },
- "Load Single Day Off Grid": {
- cci: []*CloudCostItem{
- {
- Properties: cciProperties1,
- Window: NewClosedWindow(start.Add(12*time.Hour), start.Add(36*time.Hour)),
- IsKubernetes: true,
- Cost: 100,
- NetCost: 80,
- },
- },
- ccisr: emtpyCCISR.Clone(),
- expected: []*CloudCostItemSet{
- {
- Integration: "integration",
- Window: dayWindows[0],
- CloudCostItems: map[string]*CloudCostItem{
- cciProperties1.Key(): {
- Properties: cciProperties1,
- Window: NewClosedWindow(start.Add(12*time.Hour), start.Add(24*time.Hour)),
- IsKubernetes: true,
- Cost: 50,
- NetCost: 40,
- },
- },
- },
- {
- Integration: "integration",
- Window: dayWindows[1],
- CloudCostItems: map[string]*CloudCostItem{
- cciProperties1.Key(): {
- Properties: cciProperties1,
- Window: NewClosedWindow(start.Add(24*time.Hour), start.Add(36*time.Hour)),
- IsKubernetes: true,
- Cost: 50,
- NetCost: 40,
- },
- },
- },
- {
- Integration: "integration",
- Window: dayWindows[2],
- CloudCostItems: map[string]*CloudCostItem{},
- },
- },
- },
- "Load Single Day Off Grid Before Range Window": {
- cci: []*CloudCostItem{
- {
- Properties: cciProperties1,
- Window: NewClosedWindow(start.Add(-12*time.Hour), start.Add(12*time.Hour)),
- IsKubernetes: true,
- Cost: 100,
- NetCost: 80,
- },
- },
- ccisr: emtpyCCISR.Clone(),
- expected: []*CloudCostItemSet{
- {
- Integration: "integration",
- Window: dayWindows[0],
- CloudCostItems: map[string]*CloudCostItem{
- cciProperties1.Key(): {
- Properties: cciProperties1,
- Window: NewClosedWindow(start, start.Add(12*time.Hour)),
- IsKubernetes: true,
- Cost: 50,
- NetCost: 40,
- },
- },
- },
- {
- Integration: "integration",
- Window: dayWindows[1],
- CloudCostItems: map[string]*CloudCostItem{},
- },
- {
- Integration: "integration",
- Window: dayWindows[2],
- CloudCostItems: map[string]*CloudCostItem{},
- },
- },
- },
- "Load Single Day Off Grid After Range Window": {
- cci: []*CloudCostItem{
- {
- Properties: cciProperties1,
- Window: NewClosedWindow(end.Add(-12*time.Hour), end.Add(12*time.Hour)),
- IsKubernetes: true,
- Cost: 100,
- NetCost: 80,
- },
- },
- ccisr: emtpyCCISR.Clone(),
- expected: []*CloudCostItemSet{
- {
- Integration: "integration",
- Window: dayWindows[0],
- CloudCostItems: map[string]*CloudCostItem{},
- },
- {
- Integration: "integration",
- Window: dayWindows[1],
- CloudCostItems: map[string]*CloudCostItem{},
- },
- {
- Integration: "integration",
- Window: dayWindows[2],
- CloudCostItems: map[string]*CloudCostItem{
- cciProperties1.Key(): {
- Properties: cciProperties1,
- Window: NewClosedWindow(end.Add(-12*time.Hour), end),
- IsKubernetes: true,
- Cost: 50,
- NetCost: 40,
- },
- },
- },
- },
- },
- "Single Day Kubecost Percent": {
- cci: []*CloudCostItem{
- {
- Properties: cciProperties1,
- Window: dayWindows[1],
- IsKubernetes: true,
- Cost: 75,
- NetCost: 60,
- },
- {
- Properties: cciProperties1,
- Window: dayWindows[1],
- IsKubernetes: true,
- Cost: 25,
- NetCost: 20,
- },
- },
- ccisr: emtpyCCISR.Clone(),
- expected: []*CloudCostItemSet{
- {
- Integration: "integration",
- Window: dayWindows[0],
- CloudCostItems: map[string]*CloudCostItem{},
- },
- {
- Integration: "integration",
- Window: dayWindows[1],
- CloudCostItems: map[string]*CloudCostItem{
- cciProperties1.Key(): {
- Properties: cciProperties1,
- Window: dayWindows[1],
- IsKubernetes: true,
- Cost: 100,
- NetCost: 80,
- },
- },
- },
- {
- Integration: "integration",
- Window: dayWindows[2],
- CloudCostItems: map[string]*CloudCostItem{},
- },
- },
- },
- }
- for name, tc := range testCases {
- t.Run(name, func(t *testing.T) {
- // load Cloud Cost Items
- for _, cci := range tc.cci {
- tc.ccisr.LoadCloudCostItem(cci)
- }
- if len(tc.ccisr.CloudCostItemSets) != len(tc.expected) {
- t.Errorf("the CloudCostItemSetRanges did not have the expected length")
- }
- for i, ccis := range tc.ccisr.CloudCostItemSets {
- if !ccis.Equal(tc.expected[i]) {
- t.Errorf("CloudCostItemSet at index: %d did not match expected", i)
- }
- }
- })
- }
- }
- func TestGetAWSClusterFromCCI(t *testing.T) {
- awsCCIWithLabeleksClusterName, eksClusterName := GenerateAWSMockCCIAndPID(1, 1, AWSMatchLabel1, ComputeCategory)
- awsCCIWithLabeleksCtlClusterName, eksCtlClusterName := GenerateAWSMockCCIAndPID(2, 2, AWSMatchLabel2, ComputeCategory)
- awsCCIWithLabelWithRandomLabel, _ := GenerateAWSMockCCIAndPID(1, 1, "randomLabel", ComputeCategory)
- awsCCINetworkCategory, _ := GenerateAWSMockCCIAndPID(1, 1, AWSMatchLabel1, NetworkCategory)
- alibabaCCI, _ := GenerateAlibabaMockCCIAndPID(4, 4, AlibabaMatchLabel1, ComputeCategory)
- testCases := map[string]struct {
- testcci *CloudCostItem
- expected string
- }{
- "cluster in label eks_cluster_name": {
- testcci: awsCCIWithLabeleksClusterName,
- expected: eksClusterName,
- },
- "cluster in label alpha_eksctl_io_cluster_name": {
- testcci: awsCCIWithLabeleksCtlClusterName,
- expected: eksCtlClusterName,
- },
- "cluster name in random label either not eks_cluster_name or eks_cluster_name": {
- testcci: awsCCIWithLabelWithRandomLabel,
- expected: "",
- },
- "Not a AWS provider": {
- testcci: alibabaCCI,
- expected: "",
- },
- "Not a compute resource": {
- testcci: awsCCINetworkCategory,
- expected: "",
- },
- }
- for name, testCase := range testCases {
- t.Run(name, func(t *testing.T) {
- actual := testCase.testcci.GetAWSCluster()
- if actual != testCase.expected {
- t.Errorf("incorrect result: Actual: '%s', Expected: '%s", actual, testCase.expected)
- }
- })
- }
- }
- func TestGetAzureClusterFromCCI(t *testing.T) {
- testCases := map[string]struct {
- testcci *CloudCostItem
- expected string
- }{
- "cluster in ProviderID complete": {
- testcci: &CloudCostItem{
- IsKubernetes: true,
- Window: Window{},
- Properties: CloudCostItemProperties{
- Labels: map[string]string{
- "randomLabel": "value1",
- },
- Provider: AzureProvider,
- Category: ComputeCategory,
- ProviderID: "azure:///subscriptions/0bd50fdf-c923-4e1e-850c-196dd3dcc5d3/resourceGroups/mc_dev_dev-1_eastus/providers/Microsoft.Compute/virtualMachineScaleSets/aks-devsysz1-24570986-vmss/virtualMachines/0",
- },
- },
- expected: "mc_dev_dev-1_eastus",
- },
- "cluster in ProviderID complete but missing some values": {
- testcci: &CloudCostItem{
- IsKubernetes: true,
- Window: Window{},
- Properties: CloudCostItemProperties{
- Labels: map[string]string{
- "randomLabel": "value1",
- },
- Provider: AzureProvider,
- Category: ComputeCategory,
- ProviderID: "azure:///subscriptions//resourceGroups/mc_dev_dev-1_eastus/providers/Microsoft.Compute/virtualMachineScaleSets/aks-devsysz1-XXXXX-vmss/virtualMachines/0",
- },
- },
- expected: "mc_dev_dev-1_eastus",
- },
- "Not having enough split content in providerID": {
- testcci: &CloudCostItem{
- IsKubernetes: true,
- Window: Window{},
- Properties: CloudCostItemProperties{
- Labels: map[string]string{
- "randomLabel": "value1",
- },
- Provider: AzureProvider,
- Category: ComputeCategory,
- ProviderID: "test1",
- },
- },
- expected: "",
- },
- "Not a Azure provider": {
- testcci: &CloudCostItem{
- IsKubernetes: true,
- Window: Window{},
- Properties: CloudCostItemProperties{
- Labels: map[string]string{
- "randomLabel": "value1",
- },
- Provider: AWSProvider,
- Category: ComputeCategory,
- ProviderID: "test1",
- },
- },
- expected: "",
- },
- "Not a compute resource": {
- testcci: &CloudCostItem{
- IsKubernetes: true,
- Window: Window{},
- Properties: CloudCostItemProperties{
- Labels: map[string]string{
- "randomLabel": "value1",
- },
- Provider: AzureProvider,
- Category: StorageCategory,
- ProviderID: "pvc-xyz",
- },
- },
- expected: "",
- },
- }
- for name, testCase := range testCases {
- t.Run(name, func(t *testing.T) {
- actual := testCase.testcci.GetAzureCluster()
- if actual != testCase.expected {
- t.Errorf("incorrect result: Actual: '%s', Expected: '%s", actual, testCase.expected)
- }
- })
- }
- }
- func TestGetAlibabaClusterFromCCI(t *testing.T) {
- alibabaCCIWithACKAliyunCom, clusterName1 := GenerateAlibabaMockCCIAndPID(4, 4, AlibabaMatchLabel1, ComputeCategory)
- awsCCI, _ := GenerateAWSMockCCIAndPID(1, 1, AWSMatchLabel1, ComputeCategory)
- alibabaCCINetworkCategory, clusterName1 := GenerateAlibabaMockCCIAndPID(4, 4, AlibabaMatchLabel1, NetworkCategory)
- testCases := map[string]struct {
- testcci *CloudCostItem
- expected string
- }{
- "cluster in label ack.aliyun.com": {
- testcci: alibabaCCIWithACKAliyunCom,
- expected: clusterName1,
- },
- "Not a Alibaba provider": {
- testcci: awsCCI,
- expected: "",
- },
- "Not a compute resource": {
- testcci: alibabaCCINetworkCategory,
- expected: "",
- },
- }
- for name, testCase := range testCases {
- t.Run(name, func(t *testing.T) {
- actual := testCase.testcci.GetAlibabaCluster()
- if actual != testCase.expected {
- t.Errorf("incorrect result: Actual: '%s', Expected: '%s", actual, testCase.expected)
- }
- })
- }
- }
|