Bladeren bron

Initial simple test for buildNodeMap

Michael Dresser 5 jaren geleden
bovenliggende
commit
b0d2bab20b
2 gewijzigde bestanden met toevoegingen van 79 en 0 verwijderingen
  1. 1 0
      go.mod
  2. 78 0
      pkg/costmodel/costmodel_test.go

+ 1 - 0
go.mod

@@ -8,6 +8,7 @@ require (
 	github.com/Azure/azure-sdk-for-go v24.1.0+incompatible
 	github.com/Azure/go-autorest v11.3.2+incompatible
 	github.com/aws/aws-sdk-go v1.28.9
+	github.com/davecgh/go-spew v1.1.1
 	github.com/dimchansky/utfbom v1.1.0 // indirect
 	github.com/getsentry/sentry-go v0.6.1
 	github.com/google/martian v2.1.0+incompatible

+ 78 - 0
pkg/costmodel/costmodel_test.go

@@ -3,6 +3,8 @@ package costmodel
 import (
 	"reflect"
 	"testing"
+
+	"github.com/davecgh/go-spew/spew"
 )
 
 func TestMergeTypeMaps(t *testing.T) {
@@ -122,3 +124,79 @@ func TestMergeTypeMaps(t *testing.T) {
 		}
 	}
 }
+
+func TestBuildNodeMap(t *testing.T) {
+	cases := []struct {
+		name                 string
+		cpuCostMap           map[NodeIdentifier]float64
+		ramCostMap           map[NodeIdentifier]float64
+		gpuCostMap           map[NodeIdentifier]float64
+		cpuCoresMap          map[nodeIdentifierNoProviderID]float64
+		ramBytesMap          map[nodeIdentifierNoProviderID]float64
+		ramUserPctMap        map[nodeIdentifierNoProviderID]float64
+		ramSystemPctMap      map[nodeIdentifierNoProviderID]float64
+		cpuBreakdownMap      map[nodeIdentifierNoProviderID]*ClusterCostsBreakdown
+		activeDataMap        map[NodeIdentifier]activeData
+		preemptibleMap       map[NodeIdentifier]bool
+		clusterAndNameToType map[nodeIdentifierNoProviderID]string
+		expected             map[NodeIdentifier]*Node
+	}{
+		{
+			name:     "empty",
+			expected: map[NodeIdentifier]*Node{},
+		},
+		{
+			name: "just cpu cost",
+			cpuCostMap: map[NodeIdentifier]float64{
+				NodeIdentifier{
+					Cluster:    "cluster1",
+					Name:       "node1",
+					ProviderID: "prov_node1",
+				}: 0.048,
+			},
+			clusterAndNameToType: map[nodeIdentifierNoProviderID]string{
+				nodeIdentifierNoProviderID{
+					Cluster: "cluster1",
+					Name:    "node1",
+				}: "type1",
+			},
+			expected: map[NodeIdentifier]*Node{
+				NodeIdentifier{
+					Cluster:    "cluster1",
+					Name:       "node1",
+					ProviderID: "prov_node1",
+				}: &Node{
+					Cluster:      "cluster1",
+					Name:         "node1",
+					ProviderID:   "prov_node1",
+					NodeType:     "type1",
+					CPUCost:      0.048,
+					CPUBreakdown: &ClusterCostsBreakdown{},
+					RAMBreakdown: &ClusterCostsBreakdown{},
+				},
+			},
+		},
+	}
+
+	for _, testCase := range cases {
+
+		result := buildNodeMap(
+			testCase.cpuCostMap, testCase.ramCostMap, testCase.gpuCostMap,
+			testCase.cpuCoresMap, testCase.ramBytesMap, testCase.ramUserPctMap,
+			testCase.ramSystemPctMap,
+			testCase.cpuBreakdownMap,
+			testCase.activeDataMap,
+			testCase.preemptibleMap,
+			testCase.clusterAndNameToType,
+		)
+
+		if !reflect.DeepEqual(result, testCase.expected) {
+			t.Errorf("buildNodeMap case %s failed. Got %+v but expected %+v", testCase.name, result, testCase.expected)
+
+			// Use spew because we have to follow pointers to figure out
+			// what isn't matching up
+			t.Logf("Got: %s", spew.Sdump(result))
+			t.Logf("Expected: %s", spew.Sdump(testCase.expected))
+		}
+	}
+}