Просмотр исходного кода

adding test cases for idle allocation

Signed-off-by: Alan Rodrigues <alanr5691@yahoo.com>
Alan Rodrigues 3 лет назад
Родитель
Сommit
fb2fc6894b
2 измененных файлов с 35 добавлено и 5 удалено
  1. 3 3
      pkg/kubecost/summaryallocation.go
  2. 32 2
      pkg/kubecost/summaryallocation_test.go

+ 3 - 3
pkg/kubecost/summaryallocation.go

@@ -170,7 +170,7 @@ func (sa *SummaryAllocation) Clone() *SummaryAllocation {
 // no usage or cost, then efficiency is zero. If there is no request, but there
 // no usage or cost, then efficiency is zero. If there is no request, but there
 // is usage or cost, then efficiency is 100%.
 // is usage or cost, then efficiency is 100%.
 func (sa *SummaryAllocation) CPUEfficiency() float64 {
 func (sa *SummaryAllocation) CPUEfficiency() float64 {
-	if sa == nil {
+	if sa == nil || sa.IsIdle() {
 		return 0.0
 		return 0.0
 	}
 	}
 
 
@@ -245,7 +245,7 @@ func (sa *SummaryAllocation) Minutes() float64 {
 // no usage or cost, then efficiency is zero. If there is no request, but there
 // no usage or cost, then efficiency is zero. If there is no request, but there
 // is usage or cost, then efficiency is 100%.
 // is usage or cost, then efficiency is 100%.
 func (sa *SummaryAllocation) RAMEfficiency() float64 {
 func (sa *SummaryAllocation) RAMEfficiency() float64 {
-	if sa == nil {
+	if sa == nil || sa.IsIdle() {
 		return 0.0
 		return 0.0
 	}
 	}
 
 
@@ -272,7 +272,7 @@ func (sa *SummaryAllocation) TotalCost() float64 {
 // TotalEfficiency is the cost-weighted average of CPU and RAM efficiency. If
 // TotalEfficiency is the cost-weighted average of CPU and RAM efficiency. If
 // there is no cost at all, then efficiency is zero.
 // there is no cost at all, then efficiency is zero.
 func (sa *SummaryAllocation) TotalEfficiency() float64 {
 func (sa *SummaryAllocation) TotalEfficiency() float64 {
-	if sa == nil {
+	if sa == nil || sa.IsIdle() {
 		return 0.0
 		return 0.0
 	}
 	}
 
 

+ 32 - 2
pkg/kubecost/summaryallocation_test.go

@@ -607,10 +607,10 @@ func TestSummaryAllocationSet_CPUEfficiency(t *testing.T) {
 
 
 func TestSummaryAllocationSet_TotalEfficiency(t *testing.T) {
 func TestSummaryAllocationSet_TotalEfficiency(t *testing.T) {
 	// Generating 6 sample summary allocations for testing
 	// Generating 6 sample summary allocations for testing
-	var sa1, sa2, sa3, sa4, sa5, sa6 *SummaryAllocation
+	var sa1, sa2, sa3, sa4, sa5, sa6, idlesa *SummaryAllocation
 
 
 	// Generating accumulated summary allocation sets for testing
 	// Generating accumulated summary allocation sets for testing
-	var sas1, sas2, sas3 *SummaryAllocationSet
+	var sas1, sas2, sas3, sas4 *SummaryAllocationSet
 
 
 	window, _ := ParseWindowUTC("7d")
 	window, _ := ParseWindowUTC("7d")
 
 
@@ -726,6 +726,20 @@ func TestSummaryAllocationSet_TotalEfficiency(t *testing.T) {
 		RAMCost:                1.0,
 		RAMCost:                1.0,
 	}
 	}
 
 
+	idlesa = &SummaryAllocation{
+		Name: IdleSuffix,
+		Properties: &AllocationProperties{
+			Cluster:   "cluster1",
+			Namespace: "namespace1",
+			Pod:       "pod1",
+			Container: "container7",
+		},
+		Start:   saStart,
+		End:     saEnd,
+		CPUCost: 1.0,
+		RAMCost: 1.0,
+	}
+
 	testcase1Map := map[string]*SummaryAllocation{
 	testcase1Map := map[string]*SummaryAllocation{
 		"cluster1/namespace1/pod1/container1": sa1,
 		"cluster1/namespace1/pod1/container1": sa1,
 		"cluster1/namespace1/pod1/container2": sa2,
 		"cluster1/namespace1/pod1/container2": sa2,
@@ -740,6 +754,12 @@ func TestSummaryAllocationSet_TotalEfficiency(t *testing.T) {
 		"cluster1/namespace1/pod1/container6": sa6,
 		"cluster1/namespace1/pod1/container6": sa6,
 	}
 	}
 
 
+	testcase4Map := map[string]*SummaryAllocation{
+		"cluster1/namespace1/pod1/container5": sa5,
+		"cluster1/namespace1/pod1/container6": sa6,
+		"cluster1/__idle__":                   idlesa,
+	}
+
 	sas1 = &SummaryAllocationSet{
 	sas1 = &SummaryAllocationSet{
 		SummaryAllocations: testcase1Map,
 		SummaryAllocations: testcase1Map,
 		Window:             window,
 		Window:             window,
@@ -755,6 +775,11 @@ func TestSummaryAllocationSet_TotalEfficiency(t *testing.T) {
 		Window:             window,
 		Window:             window,
 	}
 	}
 
 
+	sas4 = &SummaryAllocationSet{
+		SummaryAllocations: testcase4Map,
+		Window:             window,
+	}
+
 	cases := []struct {
 	cases := []struct {
 		name               string
 		name               string
 		testsas            *SummaryAllocationSet
 		testsas            *SummaryAllocationSet
@@ -775,6 +800,11 @@ func TestSummaryAllocationSet_TotalEfficiency(t *testing.T) {
 			testsas:            sas3,
 			testsas:            sas3,
 			expectedEfficiency: 0.30,
 			expectedEfficiency: 0.30,
 		},
 		},
+		{
+			name:               "Check TotalEfficiency with idle cost",
+			testsas:            sas4,
+			expectedEfficiency: 0.20,
+		},
 	}
 	}
 
 
 	for _, c := range cases {
 	for _, c := range cases {