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

Updated test cases to ignore order inside slice and added comments to Diff type and DiffAsset function

nickcurie 3 лет назад
Родитель
Сommit
52a520abc9
2 измененных файлов с 6 добавлено и 36 удалено
  1. 5 20
      pkg/kubecost/asset.go
  2. 1 16
      pkg/kubecost/diff_test.go

+ 5 - 20
pkg/kubecost/asset.go

@@ -2846,11 +2846,16 @@ const (
     DiffRemoved = "removed"
 )
 
+// Diff stores an object and a string that denotes whether that object was
+// added or removed from a set of those objects
 type Diff[T any] struct {
 	Entity T
 	Kind DiffKind
 }
 
+// DiffAsset takes two AssetSets and returns a slice of Diffs by checking
+// the keys of each AssetSet. If a key is not found, a Diff is generated
+// and added to the slice.
 func DiffAsset(before, after *AssetSet) []Diff[Asset]{
 	changedItems := []Diff[Asset]{}
 
@@ -2871,26 +2876,6 @@ func DiffAsset(before, after *AssetSet) []Diff[Asset]{
 	return changedItems
 }
 
-// func DiffAllocation(before, after *AllocationSet) []Diff[Allocation]{
-// 	changedItems := []Diff[Allocation]{}
-
-// 	for allocationKey1, allocation1 := range before.allocations {
-// 		if _, ok := after.allocations[allocationKey1]; !ok {
-// 			d := Diff[Allocation]{allocation1, removedState}
-// 			changedItems = append(changedItems, d)
-// 		}
-// 	}
-	
-// 	for allocationKey2, allocation2 := range after.allocations {
-// 		if _, ok := before.allocations[allocationKey2]; !ok {
-// 			d := Diff[Allocation]{allocation2, addedState}
-// 			changedItems = append(changedItems, d)
-// 		}
-// 	}
-
-// 	return changedItems
-// }
-
 // AssetSetRange is a thread-safe slice of AssetSets. It is meant to
 // be used such that the AssetSets held are consecutive and coherent with
 // respect to using the same aggregation properties, UTC offset, and

+ 1 - 16
pkg/kubecost/diff_test.go

@@ -93,7 +93,7 @@ func TestDiff(t *testing.T) {
 			trans := cmp.Transformer("Sort", func(in []Diff[Asset]) []Diff[Asset] {
 				out := append([]Diff[Asset](nil), in...) // Copy input to avoid mutating it
 				sort.Slice(out, func(i, j int) bool {
-					return out[i].Kind < out[j].Kind
+					return out[i].Entity.Properties().Name < out[i].Entity.Properties().Name
 				})
 				return out
 			})
@@ -105,20 +105,5 @@ func TestDiff(t *testing.T) {
 			
 		})
 	}
-	// as1 := NewAssetSet(start, end, 
-	// 	NewNode("node1", "cluster1", "123abc", start, end, window1), 
-	// 	NewNode("node2", "cluster1", "123abc", start, end, window1)
-	// 	NewNode("node3", "cluster1", "123abc", start, end, window1))
-	// as2 := NewAssetSet(start, end, 
-	// 	NewNode("node2", "cluster1", "123abc", start, end, window1),
-	// 	NewNode("node4", "cluster1", "123abc", start, end, window1))
-
-	// t.Logf("testing")
-
-	// result := DiffAsset(as1, as2)
-
-	// for i := range result {
-	// 	t.Logf("%+v", result[i])
-	// }
 
 }