|
|
@@ -164,11 +164,24 @@ func TestGetPVCCostCoefficients(t *testing.T) {
|
|
|
pod4Key := newPodKey("cluster1", "namespace1", "pod4")
|
|
|
ummountedPodKey := newPodKey("cluster1", kubecost.UnmountedSuffix, kubecost.UnmountedSuffix)
|
|
|
|
|
|
+ zeroDuration, _ := time.ParseDuration("0m0s")
|
|
|
+ fiveMinOffset, _ := time.ParseDuration("5m")
|
|
|
+ // Reflects the pvc that is offset by duration, the actual case that happens in allocation workflow.
|
|
|
+ // before core-370, the offset was causing a unmounted shared pvc coefficient map for the duration of the offset.
|
|
|
+ pvcWithDurationOffset := &pvc{
|
|
|
+ Bytes: 0,
|
|
|
+ Name: "pvc1",
|
|
|
+ Cluster: "cluster1",
|
|
|
+ Namespace: "namespace1",
|
|
|
+ Start: time.Date(2021, 2, 19, 8, 0, 0, 0, time.UTC).Add(-fiveMinOffset),
|
|
|
+ End: time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC),
|
|
|
+ }
|
|
|
cases := []struct {
|
|
|
name string
|
|
|
pvc *pvc
|
|
|
pvcIntervalMap map[podKey]kubecost.Window
|
|
|
intervals []IntervalPoint
|
|
|
+ resolution time.Duration
|
|
|
expected map[podKey][]CoefficientComponent
|
|
|
}{
|
|
|
{
|
|
|
@@ -184,6 +197,7 @@ func TestGetPVCCostCoefficients(t *testing.T) {
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", pod3Key),
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", pod1Key),
|
|
|
},
|
|
|
+ resolution: zeroDuration,
|
|
|
expected: map[podKey][]CoefficientComponent{
|
|
|
pod1Key: {
|
|
|
{0.5, 0.25},
|
|
|
@@ -212,6 +226,7 @@ func TestGetPVCCostCoefficients(t *testing.T) {
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 8, 30, 0, 0, time.UTC), "end", pod1Key),
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", pod2Key),
|
|
|
},
|
|
|
+ resolution: zeroDuration,
|
|
|
expected: map[podKey][]CoefficientComponent{
|
|
|
pod1Key: {
|
|
|
{1.0, 0.5},
|
|
|
@@ -230,6 +245,7 @@ func TestGetPVCCostCoefficients(t *testing.T) {
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", pod1Key),
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", pod2Key),
|
|
|
},
|
|
|
+ resolution: zeroDuration,
|
|
|
expected: map[podKey][]CoefficientComponent{
|
|
|
pod1Key: {
|
|
|
{0.5, 0.5},
|
|
|
@@ -249,6 +265,7 @@ func TestGetPVCCostCoefficients(t *testing.T) {
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 8, 0, 0, 0, time.UTC), "start", pod1Key),
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", pod1Key),
|
|
|
},
|
|
|
+ resolution: zeroDuration,
|
|
|
expected: map[podKey][]CoefficientComponent{
|
|
|
pod1Key: {
|
|
|
{1.0, 1.0},
|
|
|
@@ -264,6 +281,7 @@ func TestGetPVCCostCoefficients(t *testing.T) {
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 8, 45, 0, 0, time.UTC), "start", pod2Key),
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", pod2Key),
|
|
|
},
|
|
|
+ resolution: zeroDuration,
|
|
|
expected: map[podKey][]CoefficientComponent{
|
|
|
pod1Key: {
|
|
|
{1.0, 0.25},
|
|
|
@@ -283,6 +301,7 @@ func TestGetPVCCostCoefficients(t *testing.T) {
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 8, 15, 0, 0, time.UTC), "start", pod1Key),
|
|
|
NewIntervalPoint(time.Date(2021, 2, 19, 8, 45, 0, 0, time.UTC), "end", pod1Key),
|
|
|
},
|
|
|
+ resolution: zeroDuration,
|
|
|
expected: map[podKey][]CoefficientComponent{
|
|
|
pod1Key: {
|
|
|
{1.0, 0.5},
|
|
|
@@ -293,11 +312,31 @@ func TestGetPVCCostCoefficients(t *testing.T) {
|
|
|
},
|
|
|
},
|
|
|
},
|
|
|
+ // Test case to ensure the offset doesnt cause any unmounted
|
|
|
+ {
|
|
|
+ name: "pvcMap with duration offset not causing any unmounted entry in sharedPV Coefficient",
|
|
|
+ pvc: pvcWithDurationOffset,
|
|
|
+ intervals: []IntervalPoint{
|
|
|
+ NewIntervalPoint(time.Date(2021, 2, 19, 8, 0, 0, 0, time.UTC), "start", pod1Key),
|
|
|
+ NewIntervalPoint(time.Date(2021, 2, 19, 8, 30, 0, 0, time.UTC), "start", pod2Key),
|
|
|
+ NewIntervalPoint(time.Date(2021, 2, 19, 8, 30, 0, 0, time.UTC), "end", pod1Key),
|
|
|
+ NewIntervalPoint(time.Date(2021, 2, 19, 9, 0, 0, 0, time.UTC), "end", pod2Key),
|
|
|
+ },
|
|
|
+ resolution: fiveMinOffset,
|
|
|
+ expected: map[podKey][]CoefficientComponent{
|
|
|
+ pod1Key: {
|
|
|
+ {1.0, 0.5},
|
|
|
+ },
|
|
|
+ pod2Key: {
|
|
|
+ {1.0, 0.5},
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
}
|
|
|
|
|
|
for _, testCase := range cases {
|
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
|
- result := getPVCCostCoefficients(testCase.intervals, testCase.pvc)
|
|
|
+ result := getPVCCostCoefficients(testCase.intervals, testCase.pvc, testCase.resolution)
|
|
|
|
|
|
if !reflect.DeepEqual(result, testCase.expected) {
|
|
|
t.Errorf("getPVCCostCoefficients test failed: %s: Got %+v but expected %+v", testCase.name, result, testCase.expected)
|