From ab86f606a8ea037bad8fdbfeb9fd7c92dcdd6050 Mon Sep 17 00:00:00 2001 From: Rao Yunkun Date: Mon, 19 Apr 2021 17:36:40 +0800 Subject: [PATCH] Update UT. Signed-off-by: Rao Yunkun --- pkg/apiserver/config/config_test.go | 66 +++++-- pkg/kapis/monitoring/v1alpha3/helper_test.go | 19 +- pkg/models/monitoring/utils_test.go | 176 +++---------------- pkg/models/tenant/metering_test.go | 31 ++++ tools/cmd/doc-gen/main.go | 2 +- 5 files changed, 129 insertions(+), 165 deletions(-) diff --git a/pkg/apiserver/config/config_test.go b/pkg/apiserver/config/config_test.go index 5330e3b2c..d0834cf28 100644 --- a/pkg/apiserver/config/config_test.go +++ b/pkg/apiserver/config/config_test.go @@ -176,7 +176,7 @@ func newTestConfig() (*Config, error) { Endpoint: "http://edge-watcher.kubeedge.svc/api/", }, MeteringOptions: &metering.Options{ - Enable: false, + RetentionDay: "7d", }, } 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) { file := fmt.Sprintf("%s.yaml", defaultConfigurationName) if _, err := os.Stat(file); os.IsNotExist(err) { @@ -229,7 +222,58 @@ func TestGet(t *testing.T) { if diff := cmp.Diff(conf, conf2); 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") + } } diff --git a/pkg/kapis/monitoring/v1alpha3/helper_test.go b/pkg/kapis/monitoring/v1alpha3/helper_test.go index 9199b49bc..3d2ed7c4c 100644 --- a/pkg/kapis/monitoring/v1alpha3/helper_test.go +++ b/pkg/kapis/monitoring/v1alpha3/helper_test.go @@ -331,7 +331,7 @@ func TestParseRequestParams(t *testing.T) { 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) 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") + } +} diff --git a/pkg/models/monitoring/utils_test.go b/pkg/models/monitoring/utils_test.go index ad794ee6a..52dcd9708 100644 --- a/pkg/models/monitoring/utils_test.go +++ b/pkg/models/monitoring/utils_test.go @@ -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 diff --git a/pkg/models/tenant/metering_test.go b/pkg/models/tenant/metering_test.go index 434377705..89252395d 100644 --- a/pkg/models/tenant/metering_test.go +++ b/pkg/models/tenant/metering_test.go @@ -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) + } + }) + } +} diff --git a/tools/cmd/doc-gen/main.go b/tools/cmd/doc-gen/main.go index 3d4f2234c..40c7ae70b 100644 --- a/tools/cmd/doc-gen/main.go +++ b/tools/cmd/doc-gen/main.go @@ -131,7 +131,7 @@ func generateSwaggerJson() []byte { urlruntime.Must(operationsv1alpha2.AddToContainer(container, clientsets.Kubernetes())) urlruntime.Must(resourcesv1alpha2.AddToContainer(container, clientsets.Kubernetes(), informerFactory, "")) 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(metricsv1alpha2.AddToContainer(container)) urlruntime.Must(networkv1alpha2.AddToContainer(container, ""))