Selaa lähdekoodia

fix .gitignore, delete .iml file from IntelliJ
fix PR commends from Niko

Signed-off-by: saweber <saweber@gmail.com>

saweber 3 vuotta sitten
vanhempi
sitoutus
abf8286bb0
4 muutettua tiedostoa jossa 24 lisäystä ja 67 poistoa
  1. 4 1
      .gitignore
  2. 0 10
      opencost.iml
  3. 7 7
      pkg/kubecost/allocation.go
  4. 13 49
      pkg/kubecost/allocation_test.go

+ 4 - 1
.gitignore

@@ -1,5 +1,8 @@
+# Jetbrains project files
 .idea
+*.iml
+
 ui/.cache
 ui/dist
 ui/node_modules/
-cmd/costmodel/costmodel
+cmd/costmodel/costmodel

+ 0 - 10
opencost.iml

@@ -1,10 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<module type="WEB_MODULE" version="4">
-  <component name="Go" enabled="true" />
-  <component name="NewModuleRootManager" inherit-compiler-output="true">
-    <exclude-output />
-    <content url="file://$MODULE_DIR$" />
-    <orderEntry type="inheritedJdk" />
-    <orderEntry type="sourceFolder" forTests="false" />
-  </component>
-</module>

+ 7 - 7
pkg/kubecost/allocation.go

@@ -1914,7 +1914,7 @@ func (as *AllocationSet) UTCOffset() time.Duration {
 	return time.Duration(zone) * time.Second
 }
 
-func (as *AllocationSet) accumulate(that *AllocationSet) (*AllocationSet, error) {
+func (as *AllocationSet) Accumulate(that *AllocationSet) (*AllocationSet, error) {
 	if as.IsEmpty() {
 		return that.Clone(), nil
 	}
@@ -1985,7 +1985,7 @@ func (asr *AllocationSetRange) Accumulate() (*AllocationSet, error) {
 	var err error
 
 	for _, as := range asr.Allocations {
-		allocSet, err = allocSet.accumulate(as)
+		allocSet, err = allocSet.Accumulate(as)
 		if err != nil {
 			return nil, err
 		}
@@ -1995,10 +1995,10 @@ func (asr *AllocationSetRange) Accumulate() (*AllocationSet, error) {
 }
 
 // NewAccumulation clones the first available AllocationSet to use as the data structure to
-// accumulate the remaining data. This leaves the original AllocationSetRange intact.
+// Accumulate the remaining data. This leaves the original AllocationSetRange intact.
 func (asr *AllocationSetRange) NewAccumulation() (*AllocationSet, error) {
 	// NOTE: Adding this API for consistency across SummaryAllocation and Assets, but this
-	// NOTE: implementation is almost identical to regular Accumulate(). The accumulate() method
+	// NOTE: implementation is almost identical to regular Accumulate(). The Accumulate() method
 	// NOTE: for Allocation returns Clone() of the input, which is required for AccumulateBy
 	// NOTE: support (unit tests are great for verifying this information).
 	var allocSet *AllocationSet
@@ -2015,7 +2015,7 @@ func (asr *AllocationSetRange) NewAccumulation() (*AllocationSet, error) {
 			allocSetCopy = as.Clone()
 		}
 
-		allocSet, err = allocSet.accumulate(allocSetCopy)
+		allocSet, err = allocSet.Accumulate(allocSetCopy)
 		if err != nil {
 			return nil, err
 		}
@@ -2025,7 +2025,7 @@ func (asr *AllocationSetRange) NewAccumulation() (*AllocationSet, error) {
 }
 
 // AccumulateBy sums AllocationSets based on the resolution given. The resolution given is subject to the scale used for the AllocationSets.
-// Resolutions not evenly divisible by the AllocationSetRange window durations accumulate sets until a sum greater than or equal to the resolution is met,
+// Resolutions not evenly divisible by the AllocationSetRange window durations Accumulate sets until a sum greater than or equal to the resolution is met,
 // at which point AccumulateBy will start summing from 0 until the requested resolution is met again.
 // If the requested resolution is smaller than the window of an AllocationSet then the resolution will default to the duration of a set.
 // Resolutions larger than the duration of the entire AllocationSetRange will default to the duration of the range.
@@ -2035,7 +2035,7 @@ func (asr *AllocationSetRange) AccumulateBy(resolution time.Duration) (*Allocati
 	var err error
 
 	for i, as := range asr.Allocations {
-		allocSet, err = allocSet.accumulate(as)
+		allocSet, err = allocSet.Accumulate(as)
 		if err != nil {
 			return nil, err
 		}

+ 13 - 49
pkg/kubecost/allocation_test.go

@@ -2670,6 +2670,12 @@ func TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate(t *testin
 		start = start.AddDate(0, 0, 1)
 	}
 
+	var originalAllocationSets []*AllocationSet
+
+	for _, as := range allocationSets {
+		originalAllocationSets = append(originalAllocationSets, as.Clone())
+	}
+
 	asr := NewAllocationSetRange()
 	for _, as := range allocationSets {
 		asr.Append(as.Clone())
@@ -2682,22 +2688,10 @@ func TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate(t *testin
 
 	var got *AllocationSet
 
-	for i := 0; i < len(allocationSets)-1; i++ {
-		got, err = allocationSets[i].Clone().accumulate(allocationSets[i+1])
+	for i := 0; i < len(allocationSets); i++ {
+		got, err = got.Accumulate(allocationSets[i])
 		if err != nil {
-			t.Errorf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: allocationSets[%d].accumulate returned an error\n", i-1)
-		}
-	}
-
-	got, err = allocationSets[0].Clone().accumulate(allocationSets[1].Clone())
-	if err != nil {
-		t.Errorf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: allocationSets[0].accumulate(1) returned an error\n")
-	}
-
-	for i := 2; i < len(allocationSets); i++ {
-		got, err = got.accumulate(allocationSets[i].Clone())
-		if err != nil {
-			t.Errorf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: got.accumulate(allocationSets[%d]) returned an error\n", i-1)
+			t.Errorf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: got.Accumulate(allocationSets[%d]) returned an error\n", i)
 		}
 	}
 
@@ -2723,10 +2717,6 @@ func TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate(t *testin
 		t.Fatalf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: length of got.IdleKeys does not match length of expected.IdleKeys\n")
 	}
 
-	if got.FromSource != expected.FromSource {
-		t.Fatalf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: FromSource: got:%s, expected:%s\n", got.FromSource, expected.FromSource)
-	}
-
 	if !got.Window.Start().UTC().Equal(expected.Window.Start().UTC()) {
 		t.Fatalf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: Window.start: got:%s, expected:%s\n", got.Window.Start(), expected.Window.Start())
 	}
@@ -2734,37 +2724,11 @@ func TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate(t *testin
 		t.Fatalf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: Window.end: got:%s, expected:%s\n", got.Window.End(), expected.Window.End())
 	}
 
-	if got.Warnings != nil {
-		t.Fatalf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: got.Warnings is not nil")
-	}
-	if expected.Warnings != nil {
-		t.Fatalf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: expected.Warnings is not nil")
-	}
-
-	if got.Errors != nil {
-		t.Fatalf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: got.Errors is not nil")
-	}
-
-	if expected.Errors != nil {
-		t.Fatalf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: expected.Errors is not nil")
-	}
-
-	// Since the mock allocation sets have distinct ranges - we can compare the allocations in the allocation set back to the generated mocks to ensure no unexpected mutations
-	for key, allocation := range got.Allocations {
-		sum, err := allocationSets[0].Allocations[key].Add(allocationSets[1].Allocations[key])
-		if err != nil {
-			t.Errorf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: adding source allocations 0 and 1 errored for key: %s\n", key)
-		}
-
-		for i := 2; i < len(allocationSets); i++ {
-			sum, err = sum.Add(allocationSets[i].Allocations[key])
-			if err != nil {
-				t.Errorf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: adding source allocation %d errored for key:%s\n", i, key)
+	for i := range allocationSets {
+		for key, allocation := range allocationSets[i].Allocations {
+			if !allocation.Equal(originalAllocationSets[i].Allocations[key]) {
+				t.Fatalf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: allocationSet has been mutated in Accumulate; allocationSet: %d, allocation: %s\n", i, key)
 			}
 		}
-
-		if !allocation.Equal(sum) {
-			t.Fatalf("TestAllocationSet_Accumulate_Equals_AllocationSetRange_Accumulate: got: %v for allocation, expected: %v\n", allocation, sum)
-		}
 	}
 }