|
|
@@ -48,6 +48,39 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
Cost: 2.0,
|
|
|
},
|
|
|
}, // 2 PVBytes, 2 PVCost
|
|
|
+ Properties: &kubecost.AllocationProperties{
|
|
|
+ Namespace: "test-namespace",
|
|
|
+ Controller: "test-controller-name",
|
|
|
+ ControllerKind: "test-controller-kind",
|
|
|
+ Pod: "test-pod",
|
|
|
+ Container: "test-container",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+ },
|
|
|
+ }
|
|
|
+ err := UpdateCSV(context.TODO(), storage, model, false, nil)
|
|
|
+ require.NoError(t, err)
|
|
|
+ // uploaded a single file with the data
|
|
|
+ assert.Len(t, model.ComputeAllocationCalls(), 1)
|
|
|
+ assert.Equal(t, time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), model.ComputeAllocationCalls()[0].Start)
|
|
|
+ assert.Equal(t, time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC), model.ComputeAllocationCalls()[0].End)
|
|
|
+ assert.Equal(t, `Date,Namespace,ControllerKind,ControllerName,Pod,Container,CPUCoreUsageAverage,CPUCoreRequestAverage,RAMBytesUsageAverage,RAMBytesRequestAverage,NetworkReceiveBytes,NetworkTransferBytes,GPUs,PVBytes,CPUCost,RAMCost,NetworkCost,PVCost,GPUCost,TotalCost
|
|
|
+2021-01-01,test-namespace,test-controller-kind,test-controller-name,test-pod,test-container,0.1,0.2,0.4,0.5,11,10,2,2,0.3,0.6,0.9,2,0.8,4.6000000000000005
|
|
|
+`, string(storage.Data))
|
|
|
+ })
|
|
|
+
|
|
|
+ t.Run("export labels", func(t *testing.T) {
|
|
|
+ storage := &filemanager.InMemoryFile{}
|
|
|
+ model := &AllocationModelMock{
|
|
|
+ DateRangeFunc: func() (time.Time, time.Time, error) {
|
|
|
+ return time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC), nil
|
|
|
+ },
|
|
|
+ ComputeAllocationFunc: func(start time.Time, end time.Time, resolution time.Duration) (*kubecost.AllocationSet, error) {
|
|
|
+ return &kubecost.AllocationSet{
|
|
|
+ Allocations: map[string]*kubecost.Allocation{
|
|
|
+ "test": {
|
|
|
Properties: &kubecost.AllocationProperties{
|
|
|
Namespace: "test-namespace",
|
|
|
Controller: "test-controller-name",
|
|
|
@@ -64,21 +97,19 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
}, nil
|
|
|
},
|
|
|
}
|
|
|
- err := UpdateCSV(context.TODO(), storage, model)
|
|
|
+ err := UpdateCSV(context.TODO(), storage, model, true, []string{"test-label1", "test-label2"})
|
|
|
require.NoError(t, err)
|
|
|
// uploaded a single file with the data
|
|
|
assert.Len(t, model.ComputeAllocationCalls(), 1)
|
|
|
- assert.Equal(t, time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), model.ComputeAllocationCalls()[0].Start)
|
|
|
- assert.Equal(t, time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC), model.ComputeAllocationCalls()[0].End)
|
|
|
- assert.Equal(t, `Date,Namespace,ControllerKind,ControllerName,Pod,Container,Labels,CPUCoreUsageAverage,CPUCoreRequestAverage,RAMBytesUsageAverage,RAMBytesRequestAverage,NetworkReceiveBytes,NetworkTransferBytes,GPUs,PVBytes,CPUCost,RAMCost,NetworkCost,PVCost,GPUCost,TotalCost
|
|
|
-2021-01-01,test-namespace,test-controller-kind,test-controller-name,test-pod,test-container,"{""test-label1"":""test-value1"",""test-label2"":""test-value2""}",0.1,0.2,0.4,0.5,11,10,2,2,0.3,0.6,0.9,2,0.8,4.6000000000000005
|
|
|
+ assert.Equal(t, `Date,Namespace,ControllerKind,ControllerName,Pod,Container,CPUCoreUsageAverage,CPUCoreRequestAverage,RAMBytesUsageAverage,RAMBytesRequestAverage,NetworkReceiveBytes,NetworkTransferBytes,GPUs,PVBytes,CPUCost,RAMCost,NetworkCost,PVCost,GPUCost,TotalCost,Labels,Label_test-label1,Label_test-label2
|
|
|
+2021-01-01,test-namespace,test-controller-kind,test-controller-name,test-pod,test-container,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"{""test-label1"":""test-value1"",""test-label2"":""test-value2""}",test-value1,test-value2
|
|
|
`, string(storage.Data))
|
|
|
})
|
|
|
|
|
|
t.Run("merge new data with previous data (with different CSV structure)", func(t *testing.T) {
|
|
|
storage := &filemanager.InMemoryFile{
|
|
|
- Data: []byte(`Date,Namespace,CPUCoreUsageAverage,CPUCoreRequestAverage,CPUCost,RAMBytesUsageAverage,RAMBytesRequestAverage,RAMCost
|
|
|
-2021-01-01,test-namespace,0.1,0.2,0.3,0.4,0.5,0.6
|
|
|
+ Data: []byte(`Date,Namespace,CPUCoreUsageAverage,CPUCoreRequestAverage,CPUCost,RAMBytesUsageAverage,RAMBytesRequestAverage,RAMCost,Label_app
|
|
|
+2021-01-01,test-namespace,0.1,0.2,0.3,0.4,0.5,0.6,app1
|
|
|
`),
|
|
|
}
|
|
|
model := &AllocationModelMock{
|
|
|
@@ -98,7 +129,7 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
}, nil
|
|
|
},
|
|
|
}
|
|
|
- err := UpdateCSV(context.TODO(), storage, model)
|
|
|
+ err := UpdateCSV(context.TODO(), storage, model, false, nil)
|
|
|
require.NoError(t, err)
|
|
|
// uploaded a single file with the data
|
|
|
assert.Len(t, model.ComputeAllocationCalls(), 1)
|
|
|
@@ -106,8 +137,8 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
// 2021-01-01 is already in the export file, so we only compute for 2021-01-02
|
|
|
assert.Equal(t, time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC), model.ComputeAllocationCalls()[0].Start)
|
|
|
assert.Equal(t, time.Date(2021, 1, 3, 0, 0, 0, 0, time.UTC), model.ComputeAllocationCalls()[0].End)
|
|
|
- assert.Equal(t, `Date,Namespace,CPUCoreUsageAverage,CPUCoreRequestAverage,CPUCost,RAMBytesUsageAverage,RAMBytesRequestAverage,RAMCost,ControllerKind,ControllerName,Pod,Container,Labels,NetworkReceiveBytes,NetworkTransferBytes,GPUs,PVBytes,NetworkCost,PVCost,GPUCost,TotalCost
|
|
|
-2021-01-01,test-namespace,0.1,0.2,0.3,0.4,0.5,0.6,,,,,,,,,,,,,
|
|
|
+ assert.Equal(t, `Date,Namespace,CPUCoreUsageAverage,CPUCoreRequestAverage,CPUCost,RAMBytesUsageAverage,RAMBytesRequestAverage,RAMCost,Label_app,ControllerKind,ControllerName,Pod,Container,NetworkReceiveBytes,NetworkTransferBytes,GPUs,PVBytes,NetworkCost,PVCost,GPUCost,TotalCost
|
|
|
+2021-01-01,test-namespace,0.1,0.2,0.3,0.4,0.5,0.6,app1,,,,,,,,,,,,
|
|
|
2021-01-02,test-namespace,0,0,1,0,0,0,,,,,,0,0,0,0,0,0,0,1
|
|
|
`, string(storage.Data))
|
|
|
})
|
|
|
@@ -124,7 +155,7 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
return time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), time.Date(2021, 1, 2, 0, 0, 0, 0, time.UTC), nil
|
|
|
},
|
|
|
}
|
|
|
- err := UpdateCSV(context.TODO(), storage, model)
|
|
|
+ err := UpdateCSV(context.TODO(), storage, model, false, nil)
|
|
|
require.Equal(t, err, errNoData)
|
|
|
assert.Equal(t, string(storage.Data), data)
|
|
|
assert.Len(t, model.ComputeAllocationCalls(), 0)
|
|
|
@@ -142,7 +173,7 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
},
|
|
|
}
|
|
|
storage := &filemanager.InMemoryFile{}
|
|
|
- err := UpdateCSV(context.TODO(), storage, model)
|
|
|
+ err := UpdateCSV(context.TODO(), storage, model, false, nil)
|
|
|
require.Equal(t, err, errNoData)
|
|
|
})
|
|
|
}
|