| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- package kubecost
- import (
- "testing"
- "time"
- "github.com/opencost/opencost/pkg/util/timeutil"
- )
- var ccaProperties1 = CloudCostAggregateProperties{
- Provider: "provider1",
- WorkGroupID: "workgroup1",
- BillingID: "billing1",
- Service: "service1",
- LabelValue: "labelValue1",
- }
- func TestCloudCostAggregatePropertiesIntersection(t *testing.T) {
- testCases := map[string]struct {
- baseCCAP CloudCostAggregateProperties
- intCCAP CloudCostAggregateProperties
- expectedCCAP CloudCostAggregateProperties
- }{
- "When properties match between both CloudCostAggregateProperties": {
- baseCCAP: CloudCostAggregateProperties{
- Provider: "CustomProvider",
- WorkGroupID: "WorkGroupID1",
- BillingID: "BillingID1",
- Service: "Service1",
- LabelValue: "Label1",
- },
- intCCAP: CloudCostAggregateProperties{
- Provider: "CustomProvider",
- WorkGroupID: "WorkGroupID1",
- BillingID: "BillingID1",
- Service: "Service1",
- LabelValue: "Label1",
- },
- expectedCCAP: CloudCostAggregateProperties{
- Provider: "CustomProvider",
- WorkGroupID: "WorkGroupID1",
- BillingID: "BillingID1",
- Service: "Service1",
- LabelValue: "Label1",
- },
- },
- "When one of the properties differ in the two CloudCostAggregateProperties": {
- baseCCAP: CloudCostAggregateProperties{
- Provider: "CustomProvider",
- WorkGroupID: "WorkGroupID1",
- BillingID: "BillingID1",
- Service: "Service1",
- LabelValue: "Label1",
- },
- intCCAP: CloudCostAggregateProperties{
- Provider: "CustomProvider",
- WorkGroupID: "WorkGroupID1",
- BillingID: "BillingID1",
- Service: "Service2",
- LabelValue: "Label1",
- },
- expectedCCAP: CloudCostAggregateProperties{
- Provider: "CustomProvider",
- WorkGroupID: "WorkGroupID1",
- BillingID: "BillingID1",
- Service: "",
- LabelValue: "Label1",
- },
- },
- "When two of the properties differ in the two CloudCostAggregateProperties": {
- baseCCAP: CloudCostAggregateProperties{
- Provider: "CustomProvider",
- WorkGroupID: "WorkGroupID1",
- BillingID: "BillingID1",
- Service: "Service1",
- LabelValue: "Label1",
- },
- intCCAP: CloudCostAggregateProperties{
- Provider: "CustomProvider",
- WorkGroupID: "WorkGroupID2",
- BillingID: "BillingID1",
- Service: "Service2",
- LabelValue: "Label1",
- },
- expectedCCAP: CloudCostAggregateProperties{
- Provider: "CustomProvider",
- WorkGroupID: "",
- BillingID: "BillingID1",
- Service: "",
- LabelValue: "Label1",
- },
- },
- }
- for name, tc := range testCases {
- t.Run(name, func(t *testing.T) {
- actualCCAP := tc.baseCCAP.Intersection(tc.intCCAP)
- if actualCCAP.Provider != tc.expectedCCAP.Provider {
- t.Errorf("Case %s: Provider properties dont match with expected CloudCostAggregateProperties: %v actual %v", name, tc.expectedCCAP, actualCCAP)
- }
- if actualCCAP.WorkGroupID != tc.expectedCCAP.WorkGroupID {
- t.Errorf("Case %s: WorkGroupID properties dont match with expected CloudCostAggregateProperties: %v actual %v", name, tc.expectedCCAP, actualCCAP)
- }
- if actualCCAP.BillingID != tc.expectedCCAP.BillingID {
- t.Errorf("Case %s: BillingID properties dont match with expected CloudCostAggregateProperties: %v actual %v", name, tc.expectedCCAP, actualCCAP)
- }
- if actualCCAP.Service != tc.expectedCCAP.Service {
- t.Errorf("Case %s: Service properties dont match with expected CloudCostAggregateProperties: %v actual %v", name, tc.expectedCCAP, actualCCAP)
- }
- if actualCCAP.LabelValue != tc.expectedCCAP.LabelValue {
- t.Errorf("Case %s: LabelValue properties dont match with expected CloudCostAggregateProperties: %v actual %v", name, tc.expectedCCAP, actualCCAP)
- }
- })
- }
- }
- // TestCloudCostAggregate_LoadCloudCostAggregate checks that loaded CloudCostAggregates end up in the correct set in the
- // correct proportions
- func TestCloudCostAggregate_LoadCloudCostAggregate(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)
- emtpyCASSR, _ := NewCloudCostAggregateSetRange(start, end, timeutil.Day, "integration", "label")
- testCases := map[string]struct {
- cca []*CloudCostAggregate
- windows []Window
- ccasr *CloudCostAggregateSetRange
- expected []*CloudCostAggregateSet
- }{
- "Load Single Day On Grid": {
- cca: []*CloudCostAggregate{
- {
- Properties: ccaProperties1,
- KubernetesPercent: 1,
- Cost: 100,
- NetCost: 80,
- },
- },
- windows: []Window{
- dayWindows[0],
- },
- ccasr: emtpyCASSR.Clone(),
- expected: []*CloudCostAggregateSet{
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[0],
- CloudCostAggregates: map[string]*CloudCostAggregate{
- ccaProperties1.Key(nil): {
- Properties: ccaProperties1,
- KubernetesPercent: 1,
- Cost: 100,
- NetCost: 80,
- },
- },
- },
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[1],
- CloudCostAggregates: map[string]*CloudCostAggregate{},
- },
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[2],
- CloudCostAggregates: map[string]*CloudCostAggregate{},
- },
- },
- },
- "Load Single Day Off Grid": {
- cca: []*CloudCostAggregate{
- {
- Properties: ccaProperties1,
- KubernetesPercent: 1,
- Cost: 100,
- NetCost: 80,
- },
- },
- windows: []Window{
- NewClosedWindow(start.Add(12*time.Hour), start.Add(36*time.Hour)),
- },
- ccasr: emtpyCASSR.Clone(),
- expected: []*CloudCostAggregateSet{
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[0],
- CloudCostAggregates: map[string]*CloudCostAggregate{
- ccaProperties1.Key(nil): {
- Properties: ccaProperties1,
- KubernetesPercent: 1,
- Cost: 50,
- NetCost: 40,
- },
- },
- },
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[1],
- CloudCostAggregates: map[string]*CloudCostAggregate{
- ccaProperties1.Key(nil): {
- Properties: ccaProperties1,
- KubernetesPercent: 1,
- Cost: 50,
- NetCost: 40,
- },
- },
- },
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[2],
- CloudCostAggregates: map[string]*CloudCostAggregate{},
- },
- },
- },
- "Load Single Day Off Grid Before Range Window": {
- cca: []*CloudCostAggregate{
- {
- Properties: ccaProperties1,
- KubernetesPercent: 1,
- Cost: 100,
- NetCost: 80,
- },
- },
- windows: []Window{
- NewClosedWindow(start.Add(-12*time.Hour), start.Add(12*time.Hour)),
- },
- ccasr: emtpyCASSR.Clone(),
- expected: []*CloudCostAggregateSet{
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[0],
- CloudCostAggregates: map[string]*CloudCostAggregate{
- ccaProperties1.Key(nil): {
- Properties: ccaProperties1,
- KubernetesPercent: 1,
- Cost: 50,
- NetCost: 40,
- },
- },
- },
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[1],
- CloudCostAggregates: map[string]*CloudCostAggregate{},
- },
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[2],
- CloudCostAggregates: map[string]*CloudCostAggregate{},
- },
- },
- },
- "Load Single Day Off Grid After Range Window": {
- cca: []*CloudCostAggregate{
- {
- Properties: ccaProperties1,
- KubernetesPercent: 1,
- Cost: 100,
- NetCost: 80,
- },
- },
- windows: []Window{
- NewClosedWindow(end.Add(-12*time.Hour), end.Add(12*time.Hour)),
- },
- ccasr: emtpyCASSR.Clone(),
- expected: []*CloudCostAggregateSet{
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[0],
- CloudCostAggregates: map[string]*CloudCostAggregate{},
- },
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[1],
- CloudCostAggregates: map[string]*CloudCostAggregate{},
- },
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[2],
- CloudCostAggregates: map[string]*CloudCostAggregate{
- ccaProperties1.Key(nil): {
- Properties: ccaProperties1,
- KubernetesPercent: 1,
- Cost: 50,
- NetCost: 40,
- },
- },
- },
- },
- },
- "Single Day Kubecost Percent": {
- cca: []*CloudCostAggregate{
- {
- Properties: ccaProperties1,
- KubernetesPercent: 1,
- Cost: 75,
- NetCost: 60,
- },
- {
- Properties: ccaProperties1,
- KubernetesPercent: 0,
- Cost: 25,
- NetCost: 20,
- },
- },
- windows: []Window{
- dayWindows[1],
- dayWindows[1],
- },
- ccasr: emtpyCASSR.Clone(),
- expected: []*CloudCostAggregateSet{
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[0],
- CloudCostAggregates: map[string]*CloudCostAggregate{},
- },
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[1],
- CloudCostAggregates: map[string]*CloudCostAggregate{
- ccaProperties1.Key(nil): {
- Properties: ccaProperties1,
- KubernetesPercent: 0.75,
- Cost: 100,
- NetCost: 80,
- },
- },
- },
- {
- Integration: "integration",
- LabelName: "label",
- Window: dayWindows[2],
- CloudCostAggregates: map[string]*CloudCostAggregate{},
- },
- },
- },
- }
- for name, tc := range testCases {
- t.Run(name, func(t *testing.T) {
- // load Cloud Cost Aggregates
- for i, cca := range tc.cca {
- tc.ccasr.LoadCloudCostAggregate(tc.windows[i], cca)
- }
- if len(tc.ccasr.CloudCostAggregateSets) != len(tc.expected) {
- t.Errorf("the CloudCostAggregateSetRanges did not have the expected length")
- }
- for i, ccas := range tc.ccasr.CloudCostAggregateSets {
- if !ccas.Equal(tc.expected[i]) {
- t.Errorf("CloudCostAggregateSet at index: %d did not match expected", i)
- }
- }
- })
- }
- }
|