From 95b9e1019b0e84dfec3bbade1781a438a78ed66b Mon Sep 17 00:00:00 2001 From: mohamedasifs123 <142201466+mohamedasifs123@users.noreply.github.com> Date: Sat, 1 Jun 2024 10:26:02 +0000 Subject: [PATCH 1/3] addi ng tests for components package Signed-off-by: mohamedasifs123 <142201466+mohamedasifs123@users.noreply.github.com> --- go.mod | 2 + .../dashboard_metrics_scraper_test.go | 128 ++++++++++++ pkg/kwokctl/components/dashboard_test.go | 190 ++++++++++++++++++ pkg/kwokctl/components/jaeger_test.go | 123 ++++++++++++ pkg/kwokctl/components/kubectl_proxy_test.go | 126 ++++++++++++ pkg/kwokctl/components/metrics_server_test.go | 130 ++++++++++++ pkg/kwokctl/components/prometheus_test.go | 136 +++++++++++++ 7 files changed, 835 insertions(+) create mode 100644 pkg/kwokctl/components/dashboard_metrics_scraper_test.go create mode 100644 pkg/kwokctl/components/dashboard_test.go create mode 100644 pkg/kwokctl/components/jaeger_test.go create mode 100644 pkg/kwokctl/components/kubectl_proxy_test.go create mode 100644 pkg/kwokctl/components/metrics_server_test.go create mode 100644 pkg/kwokctl/components/prometheus_test.go diff --git a/go.mod b/go.mod index 4269cd12f..ca7b3375a 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/prometheus/client_model v0.5.0 github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.5 + github.com/stretchr/testify v1.8.4 github.com/wzshiming/cmux v0.3.3 github.com/wzshiming/ctc v1.2.3 github.com/wzshiming/easycel v0.5.0 @@ -102,6 +103,7 @@ require ( github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.1.0-rc3 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/prometheus/common v0.45.0 // indirect github.com/prometheus/procfs v0.12.0 // indirect github.com/rogpeppe/go-internal v1.10.1-0.20230524175051-ec119421bb97 // indirect diff --git a/pkg/kwokctl/components/dashboard_metrics_scraper_test.go b/pkg/kwokctl/components/dashboard_metrics_scraper_test.go new file mode 100644 index 000000000..d09155b3d --- /dev/null +++ b/pkg/kwokctl/components/dashboard_metrics_scraper_test.go @@ -0,0 +1,128 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package components + +import ( + "fmt" + "reflect" + "testing" + + "sigs.k8s.io/kwok/pkg/apis/internalversion" +) + +func TestBuildDashboardMetricsScraperComponent(t *testing.T) { + type args struct { + conf BuildDashboardMetricsScraperComponentConfig + } + tests := []struct { + name string + args args + want internalversion.Component + wantComponent string + + wantErr bool + }{{ + name: "as", + args: args{ + conf: BuildDashboardMetricsScraperComponentConfig{ + Runtime: "native", + KubeconfigPath: "", + Image: "test-image", + Workdir: "/workdir", + }, + }, + wantComponent: `{dashboard-metrics-scraper [metrics-server] test-image [] root [--db-file=/metrics.db --kubeconfig=/root/.kube/config] /workdir [] [] [{ true /root/.kube/config } { true /etc/kubernetes/pki/ca.crt } { true /etc/kubernetes/pki/admin.crt } { true /etc/kubernetes/pki/admin.key }] }`, + }, + { + name: "ss", + args: args{ + conf: BuildDashboardMetricsScraperComponentConfig{ + Runtime: "containerd", + Binary: "binary", + Image: "dashboard-image", + Workdir: "/workdir", + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + KubeconfigPath: "/path/to/kubeconfig", + }, + }, + wantComponent: "{dashboard-metrics-scraper [metrics-server] dashboard-image [] root [--db-file=/metrics.db --kubeconfig=/root/.kube/config] /workdir [] [] [{ true /path/to/kubeconfig /root/.kube/config } { true /path/to/ca.crt /etc/kubernetes/pki/ca.crt } { true /path/to/admin.crt /etc/kubernetes/pki/admin.crt } { true /path/to/admin.key /etc/kubernetes/pki/admin.key }] }"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotComponent, err := BuildDashboardMetricsScraperComponent(tt.args.conf) + if (err != nil) != tt.wantErr { + t.Errorf("BuildDashboardMetricsScraperComponent() error = %v, wantErr %v", err, tt.wantErr) + return + } + a := fmt.Sprintf("%v", gotComponent) + if !reflect.DeepEqual(a, tt.wantComponent) { + t.Errorf("BuildDashboardMetricsScraperComponent() = %v, want %v", gotComponent, a) + } + }) + } +} + +// Handles empty or nil config paths gracefully +func TestBuildDashboardMetricsScraperComponent_EmptyConfigPaths(t *testing.T) { + conf := BuildDashboardMetricsScraperComponentConfig{ + Runtime: "native", + KubeconfigPath: "", + Image: "test-image", + Workdir: "/workdir", + } + + component, err := BuildDashboardMetricsScraperComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedArgs := []string{ + "--db-file=/metrics.db", + "--kubeconfig=/root/.kube/config", + } + + if !reflect.DeepEqual(component.Args, expectedArgs) { + t.Errorf("expected args %v, got %v", expectedArgs, component.Args) + } + +} + +// Creates component with correct default arguments for native runtime +func TestBuildDashboardMetricsScraperComponent_NativeRuntime(t *testing.T) { + conf := BuildDashboardMetricsScraperComponentConfig{ + Runtime: "nnative", + KubeconfigPath: "/path/to/kubeconfig", + Image: "test-image", + Workdir: "/workdir", + } + + component, err := BuildDashboardMetricsScraperComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedArgs := []string{ + "--db-file=/metrics.db", + "--kubeconfig=/root/.kube/config", + } + + if !reflect.DeepEqual(component.Args, expectedArgs) { + t.Errorf("expected args %v, got %v", expectedArgs, component.Args) + } +} diff --git a/pkg/kwokctl/components/dashboard_test.go b/pkg/kwokctl/components/dashboard_test.go new file mode 100644 index 000000000..5cf162fcf --- /dev/null +++ b/pkg/kwokctl/components/dashboard_test.go @@ -0,0 +1,190 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package components + +import ( + "reflect" + "testing" + + "sigs.k8s.io/kwok/pkg/apis/internalversion" + "sigs.k8s.io/kwok/pkg/consts" +) + +func TestBuildDashboardComponent(t *testing.T) { + tests := []struct { + name string + config BuildDashboardComponentConfig + want internalversion.Component + }{ + { + name: "default config with metrics enabled", + config: BuildDashboardComponentConfig{ + Runtime: "container", + ProjectName: "kwok", + Image: "dashboard-image", + Workdir: "/workdir", + BindAddress: "0.0.0.0", + Port: 8080, + Banner: "Welcome", + EnableMetrics: true, + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + KubeconfigPath: "/path/to/kubeconfig", + }, + want: internalversion.Component{ + Name: consts.ComponentDashboard, + Image: "dashboard-image", + Links: []string{ + consts.ComponentKubeApiserver, + }, + WorkDir: "/workdir", + Ports: []internalversion.Port{ + { + Name: "http", + Port: 8080, + HostPort: 8080, + Protocol: internalversion.ProtocolTCP, + }, + }, + Volumes: []internalversion.Volume{ + { + HostPath: "/path/to/kubeconfig", + MountPath: "/root/.kube/config", + ReadOnly: true, + }, + { + HostPath: "/path/to/ca.crt", + MountPath: "/etc/kubernetes/pki/ca.crt", + ReadOnly: true, + }, + { + HostPath: "/path/to/admin.crt", + MountPath: "/etc/kubernetes/pki/admin.crt", + ReadOnly: true, + }, + { + HostPath: "/path/to/admin.key", + MountPath: "/etc/kubernetes/pki/admin.key", + ReadOnly: true, + }, + }, + Args: []string{ + "--insecure-bind-address=0.0.0.0", + "--bind-address=127.0.0.1", + "--port=0", + "--enable-insecure-login", + "--enable-skip-login", + "--disable-settings-authorizer", + "--sidecar-host=127.0.0.1:8000", + "--system-banner=Welcome", + "--kubeconfig=/root/.kube/config", + "--insecure-port=8080", + }, + User: "", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := BuildDashboardComponent(tt.config) + if err != nil { + t.Fatalf("BuildDashboardComponent() error = %v", err) + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("BuildDashboardComponent() = %v, want %v", got, tt.want) + } + }) + } +} + +// Generates correct dashboardArgs with default configuration +func TestBuildDashboardComponent_DefaultConfiguration(t *testing.T) { + conf := BuildDashboardComponentConfig{ + BindAddress: "0.0.0.0", + EnableMetrics: false, + Runtime: "native", + Banner: "", + KubeconfigPath: "/path/to/kubeconfig", + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + Port: 8080, + Image: "dashboard-image", + Workdir: "/workdir", + } + + component, err := BuildDashboardComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedArgs := []string{ + "--insecure-bind-address=0.0.0.0", + "--bind-address=127.0.0.1", + "--port=0", + "--enable-insecure-login", + "--enable-skip-login", + "--disable-settings-authorizer", + "--metrics-provider=none", + "--kubeconfig=/root/.kube/config", + "--insecure-port=8080", + } + + if !reflect.DeepEqual(component.Args, expectedArgs) { + t.Errorf("expected args %v, got %v", expectedArgs, component.Args) + } +} + +// Handles empty BindAddress +func TestBuildDashboardComponent_EmptyBindAddress(t *testing.T) { + conf := BuildDashboardComponentConfig{ + BindAddress: "", + EnableMetrics: false, + Runtime: "native", + Banner: "", + KubeconfigPath: "/path/to/kubeconfig", + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + Port: 8080, + Image: "dashboard-image", + Workdir: "/workdir", + } + + component, err := BuildDashboardComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedArgs := []string{ + "--insecure-bind-address=", + "--bind-address=127.0.0.1", + "--port=0", + "--enable-insecure-login", + "--enable-skip-login", + "--disable-settings-authorizer", + "--metrics-provider=none", + "--kubeconfig=/root/.kube/config", + "--insecure-port=8080", + } + + if !reflect.DeepEqual(component.Args, expectedArgs) { + t.Errorf("expected args %v, got %v", expectedArgs, component.Args) + } +} diff --git a/pkg/kwokctl/components/jaeger_test.go b/pkg/kwokctl/components/jaeger_test.go new file mode 100644 index 000000000..4ecc6a7c6 --- /dev/null +++ b/pkg/kwokctl/components/jaeger_test.go @@ -0,0 +1,123 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package components + +import ( + "reflect" + "testing" + + "github.com/blang/semver/v4" + + "sigs.k8s.io/kwok/pkg/apis/internalversion" + "sigs.k8s.io/kwok/pkg/consts" + "sigs.k8s.io/kwok/pkg/log" +) + +// Returns correct component with default configuration +func TestBuildJaegerComponentWithDefaultConfig(t *testing.T) { + conf := BuildJaegerComponentConfig{ + Runtime: "default", + Port: 16686, + BindAddress: "0.0.0.0", + Verbosity: log.LevelInfo, + Version: semver.MustParse("1.0.0"), + Binary: "/usr/bin/jaeger", + Image: "jaegertracing/all-in-one:1.0.0", + Workdir: "/var/lib/jaeger", + } + + component, err := BuildJaegerComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedArgs := []string{ + "--collector.otlp.enabled=true", + "--query.http-server.host-port=0.0.0.0:16686", + } + + if !reflect.DeepEqual(component.Args, expectedArgs) { + t.Errorf("expected args %v, got %v", expectedArgs, component.Args) + } + + if component.Name != consts.ComponentJaeger { + t.Errorf("expected component name %s, got %s", consts.ComponentJaeger, component.Name) + } + + if component.Version != "1.0.0" { + t.Errorf("expected version %s, got %s", "1.0.0", component.Version) + } +} + +// Correctly sets ports when runtime mode is not native +func TestBuildJaegerComponentNonNativeRuntimePorts(t *testing.T) { + conf := BuildJaegerComponentConfig{ + Runtime: "non-native", + Port: 16686, + BindAddress: "0.0.0.0", + Verbosity: log.LevelInfo, + Version: semver.MustParse("1.0.0"), + Binary: "/usr/bin/jaeger", + Image: "jaegertracing/all-in-one:1.0.0", + Workdir: "/var/lib/jaeger", + } + + component, err := BuildJaegerComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedPorts := []internalversion.Port{ + { + HostPort: 16686, + Port: 16686, + }, + } + + if !reflect.DeepEqual(component.Ports, expectedPorts) { + t.Errorf("expected ports %v, got %v", expectedPorts, component.Ports) + } +} + +// Correctly sets ports when runtime mode is native +func TestBuildJaegerComponentNativeRuntimePorts(t *testing.T) { + conf := BuildJaegerComponentConfig{ + Runtime: "native", + Port: 16686, + BindAddress: "0.0.0.0", + OtlpGrpcPort: 14250, + Verbosity: log.LevelInfo, + Version: semver.MustParse("1.0.0"), + Binary: "/usr/bin/jaeger", + Image: "jaegertracing/all-in-one:1.0.0", + Workdir: "/var/lib/jaeger", + } + + component, err := BuildJaegerComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedArgs := []string{ + "--collector.otlp.enabled=true", + "--query.http-server.host-port=0.0.0.0:16686", + } + + if !reflect.DeepEqual(component.Args, expectedArgs) { + t.Errorf("expected args %v, got %v", expectedArgs, component.Args) + } +} diff --git a/pkg/kwokctl/components/kubectl_proxy_test.go b/pkg/kwokctl/components/kubectl_proxy_test.go new file mode 100644 index 000000000..edc3fa4e9 --- /dev/null +++ b/pkg/kwokctl/components/kubectl_proxy_test.go @@ -0,0 +1,126 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package components + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestBuildKwokControllerComponent_ManageNodesWithAnnotationSelectorEmpty(t *testing.T) { + conf := BuildKwokControllerComponentConfig{ + ManageNodesWithAnnotationSelector: "", + Runtime: "native", + KubeconfigPath: "/path/to/kubeconfig", + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + ConfigPath: "/path/to/config", + NodeIP: "127.0.0.1", + NodeName: "test-node", + BindAddress: "0.0.0.0", + NodeLeaseDurationSeconds: 40, + EnableCRDs: []string{}, + Binary: "kwok", + Image: "kwok-image", + Workdir: "/workdir", + } + + component := BuildKwokControllerComponent(conf) + + assert.Contains(t, component.Args, "--manage-all-nodes=true") + assert.NotContains(t, component.Args, "--manage-nodes-with-annotation-selector=") +} + +func TestBuildKwokControllerComponent_EmptyPort(t *testing.T) { + conf := BuildKwokControllerComponentConfig{ + ManageNodesWithAnnotationSelector: "key=value", + Runtime: "native", + KubeconfigPath: "/path/to/kubeconfig", + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + ConfigPath: "/path/to/config", + NodeIP: "127.0.0.1", + NodeName: "test-node", + BindAddress: "0.0.0.0", + NodeLeaseDurationSeconds: 40, + EnableCRDs: []string{}, + Binary: "kwok", + Image: "kwok-image", + Workdir: "/workdir", + } + + component := BuildKwokControllerComponent(conf) + + assert.NotContains(t, component.Args, "--node-port=") + assert.NotContains(t, component.Args, "--server-address=0.0.0.0:") +} + +// Sets metricsHost correctly for native runtime mode +func TestBuildKwokControllerComponent_MetricsHostForNativeRuntime(t *testing.T) { + conf := BuildKwokControllerComponentConfig{ + ManageNodesWithAnnotationSelector: "", + Runtime: "native", + KubeconfigPath: "/path/to/kubeconfig", + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + ConfigPath: "/path/to/config", + NodeIP: "127.0.0.1", + NodeName: "test-node", + BindAddress: "0.0.0.0", + NodeLeaseDurationSeconds: 40, + EnableCRDs: []string{}, + Binary: "kwok", + Image: "kwok-image", + Workdir: "/workdir", + Port: 8080, + } + + component := BuildKwokControllerComponent(conf) + + assert.Contains(t, component.Args, "--node-port=10247") + assert.Contains(t, component.Args, "--node-lease-duration-seconds=40") + assert.Contains(t, component.Args, "--node-ip=127.0.0.1") +} + +// Appends enable-crds argument when EnableCRDs is not empty +func TestBuildKwokControllerComponent_EnableCRDsNotEmpty(t *testing.T) { + conf := BuildKwokControllerComponentConfig{ + ManageNodesWithAnnotationSelector: "", + Runtime: "native", + KubeconfigPath: "/path/to/kubeconfig", + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + ConfigPath: "/path/to/config", + NodeIP: "127.0.0.1", + NodeName: "test-node", + BindAddress: "0.0.0.0", + NodeLeaseDurationSeconds: 40, + EnableCRDs: []string{"crd1", "crd2"}, + Binary: "kwok", + Image: "kwok-image", + Workdir: "/workdir", + } + + component := BuildKwokControllerComponent(conf) + + assert.Contains(t, component.Args, "--enable-crds=crd1,crd2") +} diff --git a/pkg/kwokctl/components/metrics_server_test.go b/pkg/kwokctl/components/metrics_server_test.go new file mode 100644 index 000000000..2647ab15b --- /dev/null +++ b/pkg/kwokctl/components/metrics_server_test.go @@ -0,0 +1,130 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package components + +import ( + "reflect" + "testing" + + "github.com/blang/semver/v4" + + "sigs.k8s.io/kwok/pkg/apis/internalversion" + "sigs.k8s.io/kwok/pkg/log" +) + +func TestBuildMetricsServerComponent(t *testing.T) { + type args struct { + conf BuildMetricsServerComponentConfig + } + tests := []struct { + name string + args args + wantComponent internalversion.Component + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotComponent, err := BuildMetricsServerComponent(tt.args.conf) + if (err != nil) != tt.wantErr { + t.Errorf("BuildMetricsServerComponent() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(gotComponent, tt.wantComponent) { + t.Errorf("BuildMetricsServerComponent() = %v, want %v", gotComponent, tt.wantComponent) + } + }) + } +} + +// Handles empty or zero conf.Port correctly +func TestBuildMetricsServerComponent_HandlesEmptyOrZeroPort(t *testing.T) { + conf := BuildMetricsServerComponentConfig{ + Runtime: "container", + Port: 0, + BindAddress: "127.0.0.1", + KubeconfigPath: "/path/to/kubeconfig", + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + Verbosity: log.LevelInfo, + Version: semver.MustParse("1.0.0"), + Binary: "/usr/local/bin/metrics-server", + Image: "metrics-server:latest", + Workdir: "/var/lib/metrics-server", + } + + component, err := BuildMetricsServerComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + if len(component.Ports) != 0 { + t.Errorf("expected no ports to be set, got %v", component.Ports) + } +} + +// Appends correct volumes for non-native runtime modes +func TestBuildMetricsServerComponent_AppendsCorrectVolumesForNonNativeRuntimeModes(t *testing.T) { + conf := BuildMetricsServerComponentConfig{ + Runtime: "container", + Port: 8080, + BindAddress: "127.0.0.1", + KubeconfigPath: "/path/to/kubeconfig", + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + Verbosity: log.LevelInfo, + Version: semver.MustParse("1.0.0"), + Binary: "/usr/local/bin/metrics-server", + Image: "metrics-server:latest", + Workdir: "/var/lib/metrics-server", + } + + component, err := BuildMetricsServerComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedVolumes := []internalversion.Volume{ + { + HostPath: conf.KubeconfigPath, + MountPath: "/root/.kube/config", + ReadOnly: true, + }, + { + HostPath: conf.CaCertPath, + MountPath: "/etc/kubernetes/pki/ca.crt", + ReadOnly: true, + }, + { + HostPath: conf.AdminCertPath, + MountPath: "/etc/kubernetes/pki/admin.crt", + ReadOnly: true, + }, + { + HostPath: conf.AdminKeyPath, + MountPath: "/etc/kubernetes/pki/admin.key", + ReadOnly: true, + }, + } + + if !reflect.DeepEqual(component.Volumes, expectedVolumes) { + t.Errorf("expected volumes %v, got %v", expectedVolumes, component.Volumes) + } +} diff --git a/pkg/kwokctl/components/prometheus_test.go b/pkg/kwokctl/components/prometheus_test.go new file mode 100644 index 000000000..0443774af --- /dev/null +++ b/pkg/kwokctl/components/prometheus_test.go @@ -0,0 +1,136 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package components + +import ( + "reflect" + "testing" + + "sigs.k8s.io/kwok/pkg/apis/internalversion" + "sigs.k8s.io/kwok/pkg/log" + "sigs.k8s.io/kwok/pkg/utils/format" + "sigs.k8s.io/kwok/pkg/utils/net" +) + +// Correct volumes and ports are set when runtime mode is not native +func TestBuildPrometheusComponent_NonNativeRuntime(t *testing.T) { + conf := BuildPrometheusComponentConfig{ + Runtime: "docker", + ConfigPath: "/path/to/config", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + Port: 9090, + BindAddress: "0.0.0.0", + Verbosity: log.LevelInfo, + + //Version: "", + Binary: "/usr/local/bin/prometheus", + Image: "prom/prometheus:v2.26.0", + Workdir: "/prometheus", + } + + component, err := BuildPrometheusComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + if len(component.Volumes) != 3 { + t.Errorf("expected 3 volumes, got %d", len(component.Volumes)) + } + + if len(component.Ports) != 1 { + t.Errorf("expected 1 port, got %d", len(component.Ports)) + } + + expectedArgs := []string{ + "--config.file=/etc/prometheus/prometheus.yaml", + "--web.listen-address=0.0.0.0:9090", + } + for _, arg := range expectedArgs { + if !contains(component.Args, arg) { + t.Errorf("expected argument %s not found in component args", arg) + } + } +} + +func contains(slice []string, item string) bool { + for _, s := range slice { + if s == item { + return true + } + } + return false +} + +// Metric component is correctly configured +func TestBuildPrometheusComponent_MetricConfiguration(t *testing.T) { + conf := BuildPrometheusComponentConfig{ + Runtime: "docker", + ConfigPath: "/path/to/config", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + Port: 9090, + BindAddress: "0.0.0.0", + Verbosity: log.LevelInfo, + Binary: "/usr/local/bin/prometheus", + Image: "prom/prometheus:v2.26.0", + Workdir: "/prometheus", + } + + component, err := BuildPrometheusComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedMetric := &internalversion.ComponentMetric{ + Scheme: "http", + Host: net.LocalAddress + ":" + format.String(conf.Port), + Path: "/metrics", + } + + if !reflect.DeepEqual(component.Metric, expectedMetric) { + t.Errorf("expected metric configuration does not match") + } +} + +// Correct arguments are set when runtime mode is native +func TestBuildPrometheusComponent_NativeRuntime(t *testing.T) { + conf := BuildPrometheusComponentConfig{ + Runtime: "native", + ConfigPath: "/path/to/config", + Port: 9090, + BindAddress: "0.0.0.0", + Verbosity: log.LevelInfo, + Binary: "/usr/local/bin/prometheus", + Image: "prom/prometheus:v2.26.0", + Workdir: "/prometheus", + } + + component, err := BuildPrometheusComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedArgs := []string{ + "--web.listen-address=0.0.0.0:9090", + } + for _, arg := range expectedArgs { + if !contains(component.Args, arg) { + t.Errorf("expected argument %s not found in component args", arg) + } + } +} From d5cfb5c5a7ca316b0449e889a159d15f18cc4c7c Mon Sep 17 00:00:00 2001 From: mohamedasifs123 <142201466+mohamedasifs123@users.noreply.github.com> Date: Sun, 2 Jun 2024 07:16:05 +0000 Subject: [PATCH 2/3] fix errors Signed-off-by: mohamedasifs123 <142201466+mohamedasifs123@users.noreply.github.com> --- pkg/kwokctl/components/dashboard_metrics_scraper_test.go | 1 - pkg/kwokctl/components/dashboard_test.go | 2 +- pkg/kwokctl/components/jaeger_test.go | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/kwokctl/components/dashboard_metrics_scraper_test.go b/pkg/kwokctl/components/dashboard_metrics_scraper_test.go index d09155b3d..62127bd03 100644 --- a/pkg/kwokctl/components/dashboard_metrics_scraper_test.go +++ b/pkg/kwokctl/components/dashboard_metrics_scraper_test.go @@ -100,7 +100,6 @@ func TestBuildDashboardMetricsScraperComponent_EmptyConfigPaths(t *testing.T) { if !reflect.DeepEqual(component.Args, expectedArgs) { t.Errorf("expected args %v, got %v", expectedArgs, component.Args) } - } // Creates component with correct default arguments for native runtime diff --git a/pkg/kwokctl/components/dashboard_test.go b/pkg/kwokctl/components/dashboard_test.go index 5cf162fcf..4cf54adf8 100644 --- a/pkg/kwokctl/components/dashboard_test.go +++ b/pkg/kwokctl/components/dashboard_test.go @@ -1,5 +1,5 @@ /* -Copyright 2023 The Kubernetes Authors. +Copyright 2024 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/pkg/kwokctl/components/jaeger_test.go b/pkg/kwokctl/components/jaeger_test.go index 4ecc6a7c6..2d5fe1c6f 100644 --- a/pkg/kwokctl/components/jaeger_test.go +++ b/pkg/kwokctl/components/jaeger_test.go @@ -1,5 +1,5 @@ /* -Copyright 2023 The Kubernetes Authors. +Copyright 2024 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. From f9fda71c2291441cecb4d54d28be310aec8524cf Mon Sep 17 00:00:00 2001 From: mohamedasifs123 <142201466+mohamedasifs123@users.noreply.github.com> Date: Mon, 3 Jun 2024 09:23:34 +0000 Subject: [PATCH 3/3] fixing structure Signed-off-by: mohamedasifs123 <142201466+mohamedasifs123@users.noreply.github.com> --- .../dashboard_metrics_scraper_test.go | 140 ++++++++++++------ 1 file changed, 92 insertions(+), 48 deletions(-) diff --git a/pkg/kwokctl/components/dashboard_metrics_scraper_test.go b/pkg/kwokctl/components/dashboard_metrics_scraper_test.go index 62127bd03..836b9640b 100644 --- a/pkg/kwokctl/components/dashboard_metrics_scraper_test.go +++ b/pkg/kwokctl/components/dashboard_metrics_scraper_test.go @@ -24,57 +24,101 @@ import ( "sigs.k8s.io/kwok/pkg/apis/internalversion" ) -func TestBuildDashboardMetricsScraperComponent(t *testing.T) { - type args struct { - conf BuildDashboardMetricsScraperComponentConfig - } - tests := []struct { - name string - args args - want internalversion.Component - wantComponent string - - wantErr bool - }{{ - name: "as", - args: args{ - conf: BuildDashboardMetricsScraperComponentConfig{ - Runtime: "native", - KubeconfigPath: "", - Image: "test-image", - Workdir: "/workdir", +// Without paths +func TestBuildDashboardMetricsScraperWithoutPaths(t *testing.T) { + conf := BuildDashboardMetricsScraperComponentConfig{ + Runtime: "native", + KubeconfigPath: "", + Image: "test-image", + Workdir: "/workdir", + } + component, err := BuildDashboardMetricsScraperComponent(conf) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + fmt.Printf("%+v\n", component) + + expectedArgs := internalversion.Component{ + Name: "dashboard-metrics-scraper", + Links: []string{"metrics-server"}, + Image: "test-image", + User: "root", + Args: []string{ + "--db-file=/metrics.db", + "--kubeconfig=/root/.kube/config", + }, + WorkDir: "/workdir", + Volumes: []internalversion.Volume{{ReadOnly: true, + MountPath: "/root/.kube/config"}, + + {ReadOnly: true, MountPath: "/etc/kubernetes/pki/ca.crt"}, + {ReadOnly: true, + MountPath: "/etc/kubernetes/pki/admin.crt", }, + {ReadOnly: true, MountPath: "/etc/kubernetes/pki/admin.key"}, + }, + } + + if !reflect.DeepEqual(component, expectedArgs) { + t.Errorf("expected args %v, got %v", expectedArgs, component) + } +} + +// With paths +func TestBuildDashboardMetricsScraperWithPaths(t *testing.T) { + conf := BuildDashboardMetricsScraperComponentConfig{ + Runtime: "containerd", + Binary: "binary", + Image: "dashboard-image", + Workdir: "/workdir", + CaCertPath: "/path/to/ca.crt", + AdminCertPath: "/path/to/admin.crt", + AdminKeyPath: "/path/to/admin.key", + KubeconfigPath: "/path/to/kubeconfig", + } + component, err := BuildDashboardMetricsScraperComponent(conf) + fmt.Printf("%+v\n", component) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + + expectedArgs := internalversion.Component{ + Name: "dashboard-metrics-scraper", + Links: []string{"metrics-server"}, + Image: "dashboard-image", + User: "root", + Args: []string{ + "--db-file=/metrics.db", + "--kubeconfig=/root/.kube/config", }, - wantComponent: `{dashboard-metrics-scraper [metrics-server] test-image [] root [--db-file=/metrics.db --kubeconfig=/root/.kube/config] /workdir [] [] [{ true /root/.kube/config } { true /etc/kubernetes/pki/ca.crt } { true /etc/kubernetes/pki/admin.crt } { true /etc/kubernetes/pki/admin.key }] }`, - }, - { - name: "ss", - args: args{ - conf: BuildDashboardMetricsScraperComponentConfig{ - Runtime: "containerd", - Binary: "binary", - Image: "dashboard-image", - Workdir: "/workdir", - CaCertPath: "/path/to/ca.crt", - AdminCertPath: "/path/to/admin.crt", - AdminKeyPath: "/path/to/admin.key", - KubeconfigPath: "/path/to/kubeconfig", - }, + WorkDir: "/workdir", + Volumes: []internalversion.Volume{ + { + ReadOnly: true, + HostPath: "/path/to/kubeconfig", + MountPath: "/root/.kube/config", + }, + + { + ReadOnly: true, + HostPath: "/path/to/ca.crt", + MountPath: "/etc/kubernetes/pki/ca.crt", + }, + { + ReadOnly: true, + HostPath: "/path/to/admin.crt", + MountPath: "/etc/kubernetes/pki/admin.crt", }, - wantComponent: "{dashboard-metrics-scraper [metrics-server] dashboard-image [] root [--db-file=/metrics.db --kubeconfig=/root/.kube/config] /workdir [] [] [{ true /path/to/kubeconfig /root/.kube/config } { true /path/to/ca.crt /etc/kubernetes/pki/ca.crt } { true /path/to/admin.crt /etc/kubernetes/pki/admin.crt } { true /path/to/admin.key /etc/kubernetes/pki/admin.key }] }"}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - gotComponent, err := BuildDashboardMetricsScraperComponent(tt.args.conf) - if (err != nil) != tt.wantErr { - t.Errorf("BuildDashboardMetricsScraperComponent() error = %v, wantErr %v", err, tt.wantErr) - return - } - a := fmt.Sprintf("%v", gotComponent) - if !reflect.DeepEqual(a, tt.wantComponent) { - t.Errorf("BuildDashboardMetricsScraperComponent() = %v, want %v", gotComponent, a) - } - }) + { + ReadOnly: true, + HostPath: "/path/to/admin.key", + MountPath: "/etc/kubernetes/pki/admin.key", + }, + }, + } + + if !reflect.DeepEqual(component, expectedArgs) { + t.Errorf("expected args %v, got %v", expectedArgs, component) } }