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

Add UnmarshalJSON for Node + unit tests

Kaelan Patel 4 лет назад
Родитель
Сommit
b86f451537
2 измененных файлов с 129 добавлено и 40 удалено
  1. 32 25
      pkg/kubecost/asset.go
  2. 97 15
      pkg/kubecost/asset_unmarshal_test.go

+ 32 - 25
pkg/kubecost/asset.go

@@ -2021,15 +2021,13 @@ func (n *Node) MarshalJSON() ([]byte, error) {
 }
 
 func (n *Node) UnmarshalJSON(b []byte) error {
-	//f := reflect.New(reflect.TypeOf((*Node)(nil)).Elem()).Interface().(interface{ UnmarshalBinary([]byte) error })
-	//var f map[string]*json.RawMessage
+
 	var f interface{}
-	//fmt.Println(fmt.Sprintf("%T", f))
+
 	err := json.Unmarshal(b, &f)
 	if err != nil {
 		return err
 	}
-	//fmt.Println(f)
 
 	fmap := f.(map[string]interface{})
 
@@ -2048,11 +2046,17 @@ func (n *Node) UnmarshalJSON(b []byte) error {
 	if err != nil {
 		return err
 	}
-	end, err := time.Parse(time.RFC3339, fmap["start"].(string))
+	end, err := time.Parse(time.RFC3339, fmap["end"].(string))
 	if err != nil {
 		return err
 	}
 
+	fcpuBreakdown := fmap["cpuBreakdown"].(map[string]interface{})
+	framBreakdown := fmap["ramBreakdown"].(map[string]interface{})
+
+	cpuBreakdown := toBreakdown(fcpuBreakdown)
+	ramBreakdown := toBreakdown(framBreakdown)
+
 	n.properties = &properties
 	n.labels = labels
 	n.start = start
@@ -2061,6 +2065,8 @@ func (n *Node) UnmarshalJSON(b []byte) error {
 		start: &start,
 		end:   &end,
 	}
+	n.CPUBreakdown = &cpuBreakdown
+	n.RAMBreakdown = &ramBreakdown
 
 	if adjustment, err := getTypedVal(fmap["adjustment"]); err == nil {
 		n.adjustment = adjustment.(float64)
@@ -2096,29 +2102,9 @@ func (n *Node) UnmarshalJSON(b []byte) error {
 		n.Preemptible = Preemptible.(float64)
 	}
 
-	fmt.Println(n)
-
 	return nil
 }
 
-// labels       AssetLabels
-// 	start        time.Time
-// 	end          time.Time
-// 	window       Window
-// 	adjustment   float64
-// 	NodeType     string
-// 	CPUCoreHours float64
-// 	RAMByteHours float64
-// 	GPUHours     float64
-// 	CPUBreakdown *Breakdown
-// 	RAMBreakdown *Breakdown
-// 	CPUCost      float64
-// 	GPUCost      float64
-// 	GPUCount     float64
-// 	RAMCost      float64
-// 	Discount     float64
-// 	Preemptible  float64
-
 // String implements fmt.Stringer
 func (n *Node) String() string {
 	return toString(n)
@@ -3210,6 +3196,27 @@ func toAssetProp(fproperties map[string]interface{}) AssetProperties {
 
 }
 
+// Creates an Breakdown directly from map[string]interface{}
+func toBreakdown(fproperties map[string]interface{}) Breakdown {
+	var breakdown Breakdown
+
+	if idle, v := fproperties["idle"].(float64); v {
+		breakdown.Idle = idle
+	}
+	if other, v := fproperties["other"].(float64); v {
+		breakdown.Other = other
+	}
+	if system, v := fproperties["system"].(float64); v {
+		breakdown.System = system
+	}
+	if user, v := fproperties["user"].(float64); v {
+		breakdown.User = user
+	}
+
+	return breakdown
+
+}
+
 func getTypedVal(itf interface{}) (interface{}, error) {
 	switch itf := itf.(type) {
 	case float64:

+ 97 - 15
pkg/kubecost/asset_unmarshal_test.go

@@ -2,29 +2,111 @@ package kubecost
 
 import (
 	"encoding/json"
-	"fmt"
 	"testing"
 	"time"
 )
 
+var s = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
+var e = start1.Add(day)
+var unmarshalWindow = NewWindow(&s, &e)
+
 func TestNode_Unmarshal(t *testing.T) {
 
-	var s time.Time
-	var e time.Time
-	unmarshalWindow := NewWindow(&s, &e)
+	hours := unmarshalWindow.Duration().Hours()
 
 	node1 := NewNode("node1", "cluster1", "provider1", *unmarshalWindow.start, *unmarshalWindow.end, unmarshalWindow)
+	node1.CPUCoreHours = 1.0 * hours
+	node1.RAMByteHours = 2.0 * gb * hours
+	node1.GPUHours = 0.0 * hours
+	node1.GPUCost = 0.0
+	node1.CPUCost = 8.0
+	node1.RAMCost = 4.0
+	node1.Discount = 0.3
+	node1.CPUBreakdown = &Breakdown{
+		Idle:   0.6,
+		System: 0.2,
+		User:   0.2,
+		Other:  0.0,
+	}
+	node1.RAMBreakdown = &Breakdown{
+		Idle:   0.6,
+		System: 0.2,
+		User:   0.2,
+		Other:  0.0,
+	}
+	node1.SetAdjustment(1.6)
 
-	node1.CPUCost = 1000
-
-	fmt.Println(node1)
 	bytes, _ := json.Marshal(node1)
-	//fmt.Println(string(bytes))
-	var node2 Node
-	//fmt.Println(node2)
-	err := json.Unmarshal(bytes, &node2)
-	fmt.Println("error:", err)
-	fmt.Println(node2.CPUCost)
-	//bytes2, _ := json.Marshal(&node2)
-	//fmt.Println(string(bytes2))
+
+	var thisnode Node
+	node2 := &thisnode
+
+	err := json.Unmarshal(bytes, node2)
+
+	// Check if unmarshal was successful
+	if err != nil {
+		t.Fatalf("Node Unmarshal: unexpected error: %s", err)
+	}
+
+	// Check if all fields in initial Node equal those in Node from unmarshal
+	if !node1.properties.Equal(node2.properties) {
+		t.Fatalf("Node Unmarshal: properties mutated in unmarshal")
+	}
+	if !node1.labels.Equal(node2.labels) {
+		t.Fatalf("Node Unmarshal: labels mutated in unmarshal")
+	}
+	if !node1.window.Equal(node2.window) {
+		t.Fatalf("Node Unmarshal: window mutated in unmarshal")
+	}
+	if !node1.CPUBreakdown.Equal(node2.CPUBreakdown) {
+		t.Fatalf("Node Unmarshal: CPUBreakdown mutated in unmarshal")
+	}
+	if !node1.RAMBreakdown.Equal(node2.RAMBreakdown) {
+		t.Fatalf("Node Unmarshal: RAMBreakdown mutated in unmarshal")
+	}
+	if !node1.start.Equal(node2.start) {
+		t.Fatalf("Node Unmarshal: start mutated in unmarshal")
+	}
+	if !node1.end.Equal(node2.end) {
+		t.Fatalf("Node Unmarshal: end mutated in unmarshal")
+	}
+	if node1.adjustment != node2.adjustment {
+		t.Fatalf("Node Unmarshal: adjustment mutated in unmarshal")
+	}
+	if node1.NodeType != node2.NodeType {
+		t.Fatalf("Node Unmarshal: NodeType mutated in unmarshal")
+	}
+	if node1.CPUCoreHours != node2.CPUCoreHours {
+		t.Fatalf("Node Unmarshal: CPUCoreHours mutated in unmarshal")
+	}
+	if node1.RAMByteHours != node2.RAMByteHours {
+		t.Fatalf("Node Unmarshal: RAMByteHours mutated in unmarshal")
+	}
+	if node1.GPUHours != node2.GPUHours {
+		t.Fatalf("Node Unmarshal: GPUHours mutated in unmarshal")
+	}
+	if node1.CPUCost != node2.CPUCost {
+		t.Fatalf("Node Unmarshal: CPUCost mutated in unmarshal")
+	}
+	if node1.GPUCost != node2.GPUCost {
+		t.Fatalf("Node Unmarshal: GPUCost mutated in unmarshal")
+	}
+	if node1.GPUCount != node2.GPUCount {
+		t.Fatalf("Node Unmarshal: GPUCount mutated in unmarshal")
+	}
+	if node1.RAMCost != node2.RAMCost {
+		t.Fatalf("Node Unmarshal: RAMCost mutated in unmarshal")
+	}
+	if node1.Discount != node2.Discount {
+		t.Fatalf("Node Unmarshal: Discount mutated in unmarshal")
+	}
+	if node1.Preemptible != node2.Preemptible {
+		t.Fatalf("Node Unmarshal: Preemptible mutated in unmarshal")
+	}
+
+	// As a final check, make sure the above checks out
+	if !node1.Equal(node2) {
+		t.Fatalf("Node Unmarshal: Node mutated in unmarshal")
+	}
+
 }