@@ -176,7 +176,7 @@ func newTestConfig() (*Config, error) {
|
|||||||
Endpoint: "http://edge-watcher.kubeedge.svc/api/",
|
Endpoint: "http://edge-watcher.kubeedge.svc/api/",
|
||||||
},
|
},
|
||||||
MeteringOptions: &metering.Options{
|
MeteringOptions: &metering.Options{
|
||||||
Enable: false,
|
RetentionDay: "7d",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
return conf, nil
|
return conf, nil
|
||||||
@@ -193,13 +193,6 @@ func saveTestConfig(t *testing.T, conf *Config) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMeteringConfig(t *testing.T, conf *Config) {
|
|
||||||
conf.ToMap()
|
|
||||||
if conf.MeteringOptions != nil {
|
|
||||||
t.Fatalf("setting metering options failed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func cleanTestConfig(t *testing.T) {
|
func cleanTestConfig(t *testing.T) {
|
||||||
file := fmt.Sprintf("%s.yaml", defaultConfigurationName)
|
file := fmt.Sprintf("%s.yaml", defaultConfigurationName)
|
||||||
if _, err := os.Stat(file); os.IsNotExist(err) {
|
if _, err := os.Stat(file); os.IsNotExist(err) {
|
||||||
@@ -229,7 +222,58 @@ func TestGet(t *testing.T) {
|
|||||||
if diff := cmp.Diff(conf, conf2); diff != "" {
|
if diff := cmp.Diff(conf, conf2); diff != "" {
|
||||||
t.Fatal(diff)
|
t.Fatal(diff)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
testMeteringConfig(t, conf)
|
|
||||||
|
func TestStripEmptyOptions(t *testing.T) {
|
||||||
|
var config Config
|
||||||
|
|
||||||
|
config.RedisOptions = &cache.Options{Host: ""}
|
||||||
|
config.DevopsOptions = &jenkins.Options{Host: ""}
|
||||||
|
config.MonitoringOptions = &prometheus.Options{Endpoint: ""}
|
||||||
|
config.SonarQubeOptions = &sonarqube.Options{Host: ""}
|
||||||
|
config.LdapOptions = &ldap.Options{Host: ""}
|
||||||
|
config.NetworkOptions = &network.Options{
|
||||||
|
EnableNetworkPolicy: false,
|
||||||
|
WeaveScopeHost: "",
|
||||||
|
IPPoolType: networkv1alpha1.IPPoolTypeNone,
|
||||||
|
}
|
||||||
|
config.ServiceMeshOptions = &servicemesh.Options{
|
||||||
|
IstioPilotHost: "",
|
||||||
|
ServicemeshPrometheusHost: "",
|
||||||
|
JaegerQueryHost: "",
|
||||||
|
}
|
||||||
|
config.S3Options = &s3.Options{
|
||||||
|
Endpoint: "",
|
||||||
|
}
|
||||||
|
config.AlertingOptions = &alerting.Options{
|
||||||
|
Endpoint: "",
|
||||||
|
PrometheusEndpoint: "",
|
||||||
|
ThanosRulerEndpoint: "",
|
||||||
|
}
|
||||||
|
config.LoggingOptions = &logging.Options{Host: ""}
|
||||||
|
config.NotificationOptions = ¬ification.Options{Endpoint: ""}
|
||||||
|
config.MultiClusterOptions = &multicluster.Options{Enable: false}
|
||||||
|
config.EventsOptions = &events.Options{Host: ""}
|
||||||
|
config.AuditingOptions = &auditing.Options{Host: ""}
|
||||||
|
config.KubeEdgeOptions = &kubeedge.Options{Endpoint: ""}
|
||||||
|
|
||||||
|
config.stripEmptyOptions()
|
||||||
|
|
||||||
|
if config.RedisOptions != nil ||
|
||||||
|
config.DevopsOptions != nil ||
|
||||||
|
config.MonitoringOptions != nil ||
|
||||||
|
config.SonarQubeOptions != nil ||
|
||||||
|
config.LdapOptions != nil ||
|
||||||
|
config.NetworkOptions != nil ||
|
||||||
|
config.ServiceMeshOptions != nil ||
|
||||||
|
config.S3Options != nil ||
|
||||||
|
config.AlertingOptions != nil ||
|
||||||
|
config.LoggingOptions != nil ||
|
||||||
|
config.NotificationOptions != nil ||
|
||||||
|
config.MultiClusterOptions != nil ||
|
||||||
|
config.EventsOptions != nil ||
|
||||||
|
config.AuditingOptions != nil ||
|
||||||
|
config.KubeEdgeOptions != nil {
|
||||||
|
t.Fatal("config stripEmptyOptions failed")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -331,7 +331,7 @@ func TestParseRequestParams(t *testing.T) {
|
|||||||
|
|
||||||
fakeInformerFactory.KubeSphereSharedInformerFactory()
|
fakeInformerFactory.KubeSphereSharedInformerFactory()
|
||||||
|
|
||||||
handler := NewHandler(client, nil, nil, fakeInformerFactory, ksClient, nil)
|
handler := NewHandler(client, nil, nil, fakeInformerFactory, ksClient, nil, nil)
|
||||||
|
|
||||||
result, err := handler.makeQueryOptions(tt.params, tt.lvl)
|
result, err := handler.makeQueryOptions(tt.params, tt.lvl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -419,3 +419,20 @@ func TestExportMetrics(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetMetricPosMap(t *testing.T) {
|
||||||
|
metrics := []monitoring.Metric{
|
||||||
|
{
|
||||||
|
MetricName: "a",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
MetricName: "b",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
metricMap := getMetricPosMap(metrics)
|
||||||
|
if metricMap["a"] != 0 ||
|
||||||
|
metricMap["b"] != 1 {
|
||||||
|
t.Fatal("getMetricPosMap failed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,13 +2,11 @@ package monitoring
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"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"
|
"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) {
|
func TestGenerateFloatFormat(t *testing.T) {
|
||||||
format := generateFloatFormat(10)
|
format := generateFloatFormat(10)
|
||||||
if format != "%.10f" {
|
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) {
|
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")
|
t.Error("failed to get fee with meter_cluster_cpu_usage")
|
||||||
return
|
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")
|
t.Error("failed to get fee with meter_cluster_memory_usage")
|
||||||
return
|
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")
|
t.Error("failed to get fee with meter_cluster_net_bytes_transmitted")
|
||||||
return
|
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")
|
t.Error("failed to get fee with meter_cluster_net_bytes_received")
|
||||||
return
|
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")
|
t.Error("failed to get fee with meter_cluster_pvc_bytes_total")
|
||||||
return
|
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) {
|
func TestUpdateMetricStatData(t *testing.T) {
|
||||||
|
|
||||||
saveTestConfig(t, &MeterConfig{
|
priceInfo := meteringclient.PriceInfo{
|
||||||
Billing: Billing{
|
IngressNetworkTrafficPerMegabytesPerHour: 1,
|
||||||
PriceInfo: PriceInfo{
|
EgressNetworkTrafficPerMegabytesPerHour: 2,
|
||||||
IngressNetworkTrafficPerMegabytesPerHour: 1,
|
CpuPerCorePerHour: 3,
|
||||||
EgressNetworkTrafficPerMegabytesPerHour: 2,
|
MemPerGigabytesPerHour: 4,
|
||||||
CpuPerCorePerHour: 3,
|
PvcPerGigabytesPerHour: 5,
|
||||||
MemPerGigabytesPerHour: 4,
|
CurrencyUnit: "CNY",
|
||||||
PvcPerGigabytesPerHour: 5,
|
}
|
||||||
CurrencyUnit: "CNY",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
defer cleanTestConfig(t)
|
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
metric monitoring.Metric
|
metric monitoring.Metric
|
||||||
@@ -497,7 +369,7 @@ func TestUpdateMetricStatData(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
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 != "" {
|
if diff := cmp.Diff(got, test.expected); diff != "" {
|
||||||
t.Errorf("%T differ (-got, +want): %s", test.expected, diff)
|
t.Errorf("%T differ (-got, +want): %s", test.expected, diff)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"kubesphere.io/kubesphere/pkg/constants"
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
|
|
||||||
"kubesphere.io/kubesphere/pkg/models/metering"
|
"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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -131,7 +131,7 @@ func generateSwaggerJson() []byte {
|
|||||||
urlruntime.Must(operationsv1alpha2.AddToContainer(container, clientsets.Kubernetes()))
|
urlruntime.Must(operationsv1alpha2.AddToContainer(container, clientsets.Kubernetes()))
|
||||||
urlruntime.Must(resourcesv1alpha2.AddToContainer(container, clientsets.Kubernetes(), informerFactory, ""))
|
urlruntime.Must(resourcesv1alpha2.AddToContainer(container, clientsets.Kubernetes(), informerFactory, ""))
|
||||||
urlruntime.Must(resourcesv1alpha3.AddToContainer(container, informerFactory, nil))
|
urlruntime.Must(resourcesv1alpha3.AddToContainer(container, informerFactory, nil))
|
||||||
urlruntime.Must(tenantv1alpha2.AddToContainer(container, informerFactory, nil, nil, nil, nil, nil, nil, nil, nil, nil))
|
urlruntime.Must(tenantv1alpha2.AddToContainer(container, informerFactory, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil))
|
||||||
urlruntime.Must(terminalv1alpha2.AddToContainer(container, clientsets.Kubernetes(), nil))
|
urlruntime.Must(terminalv1alpha2.AddToContainer(container, clientsets.Kubernetes(), nil))
|
||||||
urlruntime.Must(metricsv1alpha2.AddToContainer(container))
|
urlruntime.Must(metricsv1alpha2.AddToContainer(container))
|
||||||
urlruntime.Must(networkv1alpha2.AddToContainer(container, ""))
|
urlruntime.Must(networkv1alpha2.AddToContainer(container, ""))
|
||||||
|
|||||||
Reference in New Issue
Block a user