|
@@ -60,7 +60,7 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
}, nil
|
|
}, nil
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|
|
|
- err := UpdateCSV(context.TODO(), storage, model)
|
|
|
|
|
|
|
+ err := UpdateCSV(context.TODO(), storage, model, false, nil)
|
|
|
require.NoError(t, err)
|
|
require.NoError(t, err)
|
|
|
// uploaded a single file with the data
|
|
// uploaded a single file with the data
|
|
|
assert.Len(t, model.ComputeAllocationCalls(), 1)
|
|
assert.Len(t, model.ComputeAllocationCalls(), 1)
|
|
@@ -71,10 +71,45 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
`, string(storage.Data))
|
|
`, 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",
|
|
|
|
|
+ ControllerKind: "test-controller-kind",
|
|
|
|
|
+ Pod: "test-pod",
|
|
|
|
|
+ Container: "test-container",
|
|
|
|
|
+ Labels: map[string]string{
|
|
|
|
|
+ "test-label1": "test-value1",
|
|
|
|
|
+ "test-label2": "test-value2",
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ },
|
|
|
|
|
+ }, nil
|
|
|
|
|
+ },
|
|
|
|
|
+ }
|
|
|
|
|
+ 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, `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) {
|
|
t.Run("merge new data with previous data (with different CSV structure)", func(t *testing.T) {
|
|
|
storage := &filemanager.InMemoryFile{
|
|
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{
|
|
model := &AllocationModelMock{
|
|
@@ -94,7 +129,7 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
}, nil
|
|
}, nil
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|
|
|
- err := UpdateCSV(context.TODO(), storage, model)
|
|
|
|
|
|
|
+ err := UpdateCSV(context.TODO(), storage, model, false, nil)
|
|
|
require.NoError(t, err)
|
|
require.NoError(t, err)
|
|
|
// uploaded a single file with the data
|
|
// uploaded a single file with the data
|
|
|
assert.Len(t, model.ComputeAllocationCalls(), 1)
|
|
assert.Len(t, model.ComputeAllocationCalls(), 1)
|
|
@@ -102,9 +137,9 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
// 2021-01-01 is already in the export file, so we only compute for 2021-01-02
|
|
// 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, 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, 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,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,,,,,,,,,,,,
|
|
|
|
|
-2021-01-02,test-namespace,0,0,1,0,0,0,,,,,0,0,0,0,0,0,0,1
|
|
|
|
|
|
|
+ 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))
|
|
`, string(storage.Data))
|
|
|
})
|
|
})
|
|
|
|
|
|
|
@@ -120,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
|
|
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)
|
|
require.Equal(t, err, errNoData)
|
|
|
assert.Equal(t, string(storage.Data), data)
|
|
assert.Equal(t, string(storage.Data), data)
|
|
|
assert.Len(t, model.ComputeAllocationCalls(), 0)
|
|
assert.Len(t, model.ComputeAllocationCalls(), 0)
|
|
@@ -138,7 +173,7 @@ func Test_UpdateCSV(t *testing.T) {
|
|
|
},
|
|
},
|
|
|
}
|
|
}
|
|
|
storage := &filemanager.InMemoryFile{}
|
|
storage := &filemanager.InMemoryFile{}
|
|
|
- err := UpdateCSV(context.TODO(), storage, model)
|
|
|
|
|
|
|
+ err := UpdateCSV(context.TODO(), storage, model, false, nil)
|
|
|
require.Equal(t, err, errNoData)
|
|
require.Equal(t, err, errNoData)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|