|
|
@@ -4,9 +4,11 @@ import (
|
|
|
"fmt"
|
|
|
"math"
|
|
|
"reflect"
|
|
|
+ "strings"
|
|
|
"testing"
|
|
|
"time"
|
|
|
|
|
|
+ "github.com/davecgh/go-spew/spew"
|
|
|
"github.com/opencost/opencost/pkg/util"
|
|
|
"github.com/opencost/opencost/pkg/util/json"
|
|
|
)
|
|
|
@@ -2767,3 +2769,82 @@ func TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate(t *testin
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func Test_AggregateByService_UnmountedLBs(t *testing.T) {
|
|
|
+ end := time.Now().UTC().Truncate(day)
|
|
|
+ start := end.Add(-day)
|
|
|
+
|
|
|
+ normalProps := &AllocationProperties{
|
|
|
+ Cluster: "cluster-one",
|
|
|
+ Container: "nginx-plus-nginx-ingress",
|
|
|
+ Controller: "nginx-plus-nginx-ingress",
|
|
|
+ ControllerKind: "deployment",
|
|
|
+ Namespace: "nginx-plus",
|
|
|
+ Pod: "nginx-plus-nginx-ingress-123a4b5678-ab12c",
|
|
|
+ ProviderID: "test",
|
|
|
+ Node: "testnode",
|
|
|
+ Services: []string{
|
|
|
+ "nginx-plus-nginx-ingress",
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ problematicProps := &AllocationProperties{
|
|
|
+ Cluster: "cluster-one",
|
|
|
+ Container: UnmountedSuffix,
|
|
|
+ Namespace: UnmountedSuffix,
|
|
|
+ Pod: UnmountedSuffix,
|
|
|
+ ProviderID: "test",
|
|
|
+ Node: "testnode",
|
|
|
+ Services: []string{
|
|
|
+ "nginx-plus-nginx-ingress",
|
|
|
+ "ingress-nginx-controller",
|
|
|
+ "pacman",
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ idle := NewMockUnitAllocation(fmt.Sprintf("cluster-one/%s", IdleSuffix), start, day, &AllocationProperties{
|
|
|
+ Cluster: "cluster-one",
|
|
|
+ })
|
|
|
+ // this allocation is the main point of the test; an unmounted LB that has services
|
|
|
+ problematicAllocation := NewMockUnitAllocation("cluster-one//__unmounted__/__unmounted__/__unmounted__", start, day, problematicProps)
|
|
|
+
|
|
|
+ two := NewMockUnitAllocation("cluster-one//nginx-plus/nginx-plus-nginx-ingress-123a4b5678-ab12c/nginx-plus-nginx-ingress", start, day, normalProps)
|
|
|
+ three := NewMockUnitAllocation("cluster-one//nginx-plus/nginx-plus-nginx-ingress-123a4b5678-ab12c/nginx-plus-nginx-ingress", start, day, normalProps)
|
|
|
+ four := NewMockUnitAllocation("cluster-one//nginx-plus/nginx-plus-nginx-ingress-123a4b5678-ab12c/nginx-plus-nginx-ingress", start, day, normalProps)
|
|
|
+
|
|
|
+ problematicAllocation.ExternalCost = 2.35
|
|
|
+ two.ExternalCost = 1.35
|
|
|
+ three.ExternalCost = 2.60
|
|
|
+ four.ExternalCost = 4.30
|
|
|
+ set := NewAllocationSet(start, start.Add(day), problematicAllocation, two, three, four)
|
|
|
+
|
|
|
+ set.Insert(idle)
|
|
|
+
|
|
|
+ set.AggregateBy([]string{AllocationServiceProp}, &AllocationAggregationOptions{
|
|
|
+ Filter: AllocationFilterCondition{Field: FilterServices, Op: FilterContains, Value: "nginx-plus-nginx-ingress"},
|
|
|
+ })
|
|
|
+
|
|
|
+ for _, alloc := range set.Allocations {
|
|
|
+ if !strings.Contains(UnmountedSuffix, alloc.Name) {
|
|
|
+ props := alloc.Properties
|
|
|
+ if props.Cluster == UnmountedSuffix {
|
|
|
+ t.Error("cluster unmounted")
|
|
|
+ }
|
|
|
+ if props.Container == UnmountedSuffix {
|
|
|
+ t.Error("container unmounted")
|
|
|
+ }
|
|
|
+ if props.Namespace == UnmountedSuffix {
|
|
|
+ t.Error("namespace unmounted")
|
|
|
+ }
|
|
|
+ if props.Pod == UnmountedSuffix {
|
|
|
+ t.Error("pod unmounted")
|
|
|
+ }
|
|
|
+ if props.Controller == UnmountedSuffix {
|
|
|
+ t.Error("controller unmounted")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ spew.Config.DisableMethods = true
|
|
|
+ t.Logf("%s", spew.Sdump(set.Allocations))
|
|
|
+}
|