AjayTripathy 6 yıl önce
ebeveyn
işleme
b87fd48477
2 değiştirilmiş dosya ile 183 ekleme ve 0 silme
  1. 107 0
      test/aggregation_test.go
  2. 76 0
      test/remote_cluster_test.go

+ 107 - 0
test/aggregation_test.go

@@ -0,0 +1,107 @@
+package costmodel_test
+
+import (
+	"log"
+	"testing"
+
+	"gotest.tools/assert"
+
+	"github.com/kubecost/cost-model/cloud"
+	costModel "github.com/kubecost/cost-model/costmodel"
+)
+
+func TestAggregation(t *testing.T) {
+	cd1 := &costModel.CostData{
+		Namespace: "test1",
+		NodeName:  "testnode",
+		NodeData: &cloud.Node{
+			VCPUCost: "1.0",
+			RAMCost:  "1.0",
+		},
+		RAMAllocation: []*costModel.Vector{&costModel.Vector{
+			Timestamp: 10,
+			Value:     1073741824,
+		}},
+		CPUAllocation: []*costModel.Vector{&costModel.Vector{
+			Timestamp: 10,
+			Value:     1.0,
+		}},
+		GPUReq: []*costModel.Vector{&costModel.Vector{}},
+		PVCData: []*costModel.PersistentVolumeClaimData{
+			&costModel.PersistentVolumeClaimData{
+				Namespace:  "test1",
+				VolumeName: "foo",
+				Volume: &cloud.PV{
+					Cost: "1.0",
+					Size: "1073741824",
+				},
+				Values: []*costModel.Vector{&costModel.Vector{
+					Timestamp: 10,
+					Value:     1073741824,
+				}},
+			},
+			&costModel.PersistentVolumeClaimData{
+				Namespace:  "test1",
+				VolumeName: "bar",
+				Volume: &cloud.PV{
+					Cost: "1.0",
+					Size: "1073741824",
+				},
+				Values: []*costModel.Vector{&costModel.Vector{
+					Timestamp: 10,
+					Value:     1073741824,
+				}},
+			},
+		},
+	}
+	cd2 := &costModel.CostData{
+		Namespace: "test1",
+		NodeName:  "testnode",
+		NodeData: &cloud.Node{
+			VCPUCost: "1.0",
+			RAMCost:  "1.0",
+		},
+		RAMAllocation: []*costModel.Vector{&costModel.Vector{
+			Timestamp: 10,
+			Value:     1073741824,
+		}},
+		CPUAllocation: []*costModel.Vector{&costModel.Vector{
+			Timestamp: 10,
+			Value:     1.0,
+		}},
+		GPUReq: []*costModel.Vector{&costModel.Vector{}},
+		PVCData: []*costModel.PersistentVolumeClaimData{
+			&costModel.PersistentVolumeClaimData{
+				Namespace:  "test1",
+				VolumeName: "foo",
+				Volume: &cloud.PV{
+					Cost: "1.0",
+					Size: "1073741824",
+				},
+				Values: []*costModel.Vector{&costModel.Vector{
+					Timestamp: 10,
+					Value:     1073741824,
+				}},
+			},
+			&costModel.PersistentVolumeClaimData{
+				Namespace:  "test1",
+				VolumeName: "bar",
+				Volume: &cloud.PV{
+					Cost: "1.0",
+					Size: "1073741824",
+				},
+				Values: []*costModel.Vector{&costModel.Vector{
+					Timestamp: 10,
+					Value:     1073741824,
+				}},
+			},
+		},
+	}
+
+	costData := make(map[string]*costModel.CostData)
+	costData["test1,foo,nginx,testnode"] = cd1
+	costData["test1,bar,nginx,testnode"] = cd2
+	agg := costModel.AggregateCostModel(costData, 0.0, 1.0, nil, "namespace", "")
+	log.Printf("agg: %+v", agg["test1"])
+	assert.Equal(t, agg["test1"].TotalCost, 8.0)
+}

+ 76 - 0
test/remote_cluster_test.go

@@ -0,0 +1,76 @@
+package costmodel_test
+
+import (
+	"log"
+	"net"
+	"net/http"
+	"os"
+	"testing"
+	"time"
+
+	"github.com/kubecost/cost-model/cloud"
+	costModel "github.com/kubecost/cost-model/costmodel"
+	"gotest.tools/assert"
+
+	prometheusClient "github.com/prometheus/client_golang/api"
+
+	_ "k8s.io/client-go/plugin/pkg/client/auth"
+)
+
+func TestClusterConvergence(t *testing.T) {
+	rclient, err := getKubernetesClient()
+	if err != nil {
+		panic(err)
+	}
+	var LongTimeoutRoundTripper http.RoundTripper = &http.Transport{ // may be necessary for long prometheus queries. TODO: make this configurable
+		Proxy: http.ProxyFromEnvironment,
+		DialContext: (&net.Dialer{
+			Timeout:   120 * time.Second,
+			KeepAlive: 120 * time.Second,
+		}).DialContext,
+		TLSHandshakeTimeout: 10 * time.Second,
+	}
+
+	pc := prometheusClient.Config{
+		Address:      address,
+		RoundTripper: LongTimeoutRoundTripper,
+	}
+	promCli, err := prometheusClient.NewClient(pc)
+	if err != nil {
+		panic(err)
+	}
+	cm := costModel.NewCostModel(rclient)
+
+	provider := &cloud.CustomProvider{
+		Clientset: rclient,
+	}
+	loc, _ := time.LoadLocation("UTC")
+	endTime := time.Now().In(loc)
+	d, _ := time.ParseDuration("24h")
+	startTime := endTime.Add(-1 * d)
+	layout := "2006-01-02T15:04:05.000Z"
+	startStr := startTime.Format(layout)
+	endStr := endTime.Format(layout)
+	log.Printf("Starting at %s \n", startStr)
+	log.Printf("Ending at %s \n", endStr)
+	provider.DownloadPricingData()
+
+	data, err := cm.ComputeCostDataRange(promCli, rclient, provider, startStr, endStr, "1h", "", false)
+	if err != nil {
+		panic(err)
+	}
+
+	os.Setenv("SQL_ADDRESS", "ab5cfc235d64e11e9b8280265f54018f-778641917.us-east-2.elb.amazonaws.com")
+	os.Setenv("REMOTE_WRITE_PASSWORD", "savemoney123")
+
+	data2, err := cm.ComputeCostDataRange(promCli, rclient, provider, startStr, endStr, "1h", "", true)
+	if err != nil {
+		panic(err)
+	}
+
+	agg := costModel.AggregateCostModel(data, 0.0, 1.0, nil, "namespace", "")
+	agg2 := costModel.AggregateCostModel(data2, 0.0, 1.0, nil, "namespace", "")
+
+	assert.Equal(t, agg["kubecost"].TotalCost, agg2["kubecost"].TotalCost)
+
+}