@@ -2,13 +2,11 @@ package monitoring
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
meteringclient "kubesphere.io/kubesphere/pkg/simple/client/metering"
|
||||
"kubesphere.io/kubesphere/pkg/simple/client/monitoring"
|
||||
)
|
||||
|
||||
@@ -164,67 +162,6 @@ func TestGetAvgPointValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func saveTestConfig(t *testing.T, conf *MeterConfig) {
|
||||
content, err := yaml.Marshal(conf)
|
||||
if err != nil {
|
||||
t.Fatalf("error marshal config. %v", err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(meteringConfigName, content, 0640)
|
||||
if err != nil {
|
||||
t.Fatalf("error write configuration file, %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func testInvalidConfig(t *testing.T) {
|
||||
os.Mkdir(meteringConfigName, os.ModeDir)
|
||||
defer os.RemoveAll(meteringConfigName)
|
||||
_, err := LoadYaml()
|
||||
if err == nil {
|
||||
t.Error("LoadYaml should fail")
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func cleanTestConfig(t *testing.T) {
|
||||
|
||||
if _, err := os.Stat(meteringConfigName); os.IsNotExist(err) {
|
||||
t.Log("file not exists, skipping")
|
||||
return
|
||||
}
|
||||
|
||||
err := os.Remove(meteringConfigName)
|
||||
if err != nil {
|
||||
t.Fatalf("remove %s file failed", meteringConfigName)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestGetCurrencyUnit(t *testing.T) {
|
||||
if getCurrencyUnit() != "" {
|
||||
t.Fatal("currency unit should be empty")
|
||||
}
|
||||
|
||||
saveTestConfig(t, &MeterConfig{
|
||||
Billing: Billing{
|
||||
PriceInfo: PriceInfo{
|
||||
IngressNetworkTrafficPerMegabytesPerHour: 1,
|
||||
EgressNetworkTrafficPerMegabytesPerHour: 2,
|
||||
CpuPerCorePerHour: 3,
|
||||
MemPerGigabytesPerHour: 4,
|
||||
PvcPerGigabytesPerHour: 5,
|
||||
CurrencyUnit: "CNY",
|
||||
},
|
||||
},
|
||||
})
|
||||
defer cleanTestConfig(t)
|
||||
|
||||
if getCurrencyUnit() != "CNY" {
|
||||
t.Fatal("failed to get currency unit from config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateFloatFormat(t *testing.T) {
|
||||
format := generateFloatFormat(10)
|
||||
if format != "%.10f" {
|
||||
@@ -314,114 +251,49 @@ func TestSquashPoints(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadYaml(t *testing.T) {
|
||||
|
||||
testInvalidConfig(t)
|
||||
|
||||
// LoadYaml file not exist
|
||||
_, err := LoadYaml()
|
||||
if err == nil {
|
||||
t.Error("LoadYaml should return error.")
|
||||
return
|
||||
}
|
||||
|
||||
saveTestConfig(t, &MeterConfig{
|
||||
Billing: Billing{
|
||||
PriceInfo: PriceInfo{
|
||||
IngressNetworkTrafficPerMegabytesPerHour: 1,
|
||||
EgressNetworkTrafficPerMegabytesPerHour: 2,
|
||||
CpuPerCorePerHour: 3,
|
||||
MemPerGigabytesPerHour: 4,
|
||||
PvcPerGigabytesPerHour: 5,
|
||||
CurrencyUnit: "CNY",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
_, err = LoadYaml()
|
||||
if err != nil {
|
||||
t.Error("LoadYaml failed")
|
||||
return
|
||||
}
|
||||
cleanTestConfig(t)
|
||||
}
|
||||
|
||||
func TestGetFeeWithMeterName(t *testing.T) {
|
||||
saveTestConfig(t, &MeterConfig{
|
||||
Billing: Billing{
|
||||
PriceInfo: PriceInfo{
|
||||
IngressNetworkTrafficPerMegabytesPerHour: 1,
|
||||
EgressNetworkTrafficPerMegabytesPerHour: 2,
|
||||
CpuPerCorePerHour: 3,
|
||||
MemPerGigabytesPerHour: 4,
|
||||
PvcPerGigabytesPerHour: 5,
|
||||
CurrencyUnit: "CNY",
|
||||
},
|
||||
},
|
||||
})
|
||||
defer cleanTestConfig(t)
|
||||
|
||||
if getFeeWithMeterName("meter_cluster_cpu_usage", "1") != "3.000" {
|
||||
priceInfo := meteringclient.PriceInfo{
|
||||
IngressNetworkTrafficPerMegabytesPerHour: 1,
|
||||
EgressNetworkTrafficPerMegabytesPerHour: 2,
|
||||
CpuPerCorePerHour: 3,
|
||||
MemPerGigabytesPerHour: 4,
|
||||
PvcPerGigabytesPerHour: 5,
|
||||
CurrencyUnit: "CNY",
|
||||
}
|
||||
|
||||
if getFeeWithMeterName("meter_cluster_cpu_usage", "1", priceInfo) != "3.000" {
|
||||
t.Error("failed to get fee with meter_cluster_cpu_usage")
|
||||
return
|
||||
}
|
||||
if getFeeWithMeterName("meter_cluster_memory_usage", "0") != "0.000" {
|
||||
if getFeeWithMeterName("meter_cluster_memory_usage", "0", priceInfo) != "0.000" {
|
||||
t.Error("failed to get fee with meter_cluster_memory_usage")
|
||||
return
|
||||
}
|
||||
if getFeeWithMeterName("meter_cluster_net_bytes_transmitted", "0") != "0.000" {
|
||||
if getFeeWithMeterName("meter_cluster_net_bytes_transmitted", "0", priceInfo) != "0.000" {
|
||||
t.Error("failed to get fee with meter_cluster_net_bytes_transmitted")
|
||||
return
|
||||
}
|
||||
if getFeeWithMeterName("meter_cluster_net_bytes_received", "0") != "0.000" {
|
||||
if getFeeWithMeterName("meter_cluster_net_bytes_received", "0", priceInfo) != "0.000" {
|
||||
t.Error("failed to get fee with meter_cluster_net_bytes_received")
|
||||
return
|
||||
}
|
||||
if getFeeWithMeterName("meter_cluster_pvc_bytes_total", "0") != "0.000" {
|
||||
if getFeeWithMeterName("meter_cluster_pvc_bytes_total", "0", priceInfo) != "0.000" {
|
||||
t.Error("failed to get fee with meter_cluster_pvc_bytes_total")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetPriceInfo(t *testing.T) {
|
||||
meterConfig := MeterConfig{
|
||||
Billing: Billing{
|
||||
PriceInfo: PriceInfo{
|
||||
CpuPerCorePerHour: 1,
|
||||
MemPerGigabytesPerHour: 1,
|
||||
IngressNetworkTrafficPerMegabytesPerHour: 1,
|
||||
EgressNetworkTrafficPerMegabytesPerHour: 1,
|
||||
PvcPerGigabytesPerHour: 1,
|
||||
CurrencyUnit: "USD",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if meterConfig.GetPriceInfo().CurrencyUnit != meterConfig.Billing.PriceInfo.CurrencyUnit ||
|
||||
meterConfig.GetPriceInfo().CpuPerCorePerHour != meterConfig.Billing.PriceInfo.CpuPerCorePerHour ||
|
||||
meterConfig.GetPriceInfo().MemPerGigabytesPerHour != meterConfig.Billing.PriceInfo.MemPerGigabytesPerHour ||
|
||||
meterConfig.GetPriceInfo().IngressNetworkTrafficPerMegabytesPerHour != meterConfig.Billing.PriceInfo.IngressNetworkTrafficPerMegabytesPerHour ||
|
||||
meterConfig.GetPriceInfo().EgressNetworkTrafficPerMegabytesPerHour != meterConfig.Billing.PriceInfo.EgressNetworkTrafficPerMegabytesPerHour ||
|
||||
meterConfig.GetPriceInfo().PvcPerGigabytesPerHour != meterConfig.Billing.PriceInfo.PvcPerGigabytesPerHour {
|
||||
t.Error("failed to get price info")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateMetricStatData(t *testing.T) {
|
||||
|
||||
saveTestConfig(t, &MeterConfig{
|
||||
Billing: Billing{
|
||||
PriceInfo: PriceInfo{
|
||||
IngressNetworkTrafficPerMegabytesPerHour: 1,
|
||||
EgressNetworkTrafficPerMegabytesPerHour: 2,
|
||||
CpuPerCorePerHour: 3,
|
||||
MemPerGigabytesPerHour: 4,
|
||||
PvcPerGigabytesPerHour: 5,
|
||||
CurrencyUnit: "CNY",
|
||||
},
|
||||
},
|
||||
})
|
||||
defer cleanTestConfig(t)
|
||||
priceInfo := meteringclient.PriceInfo{
|
||||
IngressNetworkTrafficPerMegabytesPerHour: 1,
|
||||
EgressNetworkTrafficPerMegabytesPerHour: 2,
|
||||
CpuPerCorePerHour: 3,
|
||||
MemPerGigabytesPerHour: 4,
|
||||
PvcPerGigabytesPerHour: 5,
|
||||
CurrencyUnit: "CNY",
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
metric monitoring.Metric
|
||||
@@ -497,7 +369,7 @@ func TestUpdateMetricStatData(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
got := updateMetricStatData(test.metric, test.scalingMap)
|
||||
got := updateMetricStatData(test.metric, test.scalingMap, priceInfo)
|
||||
if diff := cmp.Diff(got, test.expected); diff != "" {
|
||||
t.Errorf("%T differ (-got, +want): %s", test.expected, diff)
|
||||
return
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/models/metering"
|
||||
@@ -191,3 +193,32 @@ func TestTransformMetricData(t *testing.T) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestGetAppNameFromLabels(t *testing.T) {
|
||||
var tOperator tenantOperator
|
||||
|
||||
tests := []struct {
|
||||
labels map[string]string
|
||||
expectedValue string
|
||||
}{
|
||||
{
|
||||
labels: make(map[string]string),
|
||||
expectedValue: "",
|
||||
},
|
||||
{
|
||||
labels: map[string]string{
|
||||
constants.ApplicationName: "app1",
|
||||
constants.ApplicationVersion: "v2",
|
||||
},
|
||||
expectedValue: "app1:v2",
|
||||
},
|
||||
}
|
||||
|
||||
for i, tt := range tests {
|
||||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
||||
if diff := cmp.Diff(tOperator.getAppNameFromLabels(tt.labels), tt.expectedValue); diff != "" {
|
||||
t.Errorf("%T differ (-got, +want): %s", tt.expectedValue, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user