Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding tests for components package #1129

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package is currently not expected to be imported

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay , I will refactor the code without using this package.

github.com/wzshiming/cmux v0.3.3
github.com/wzshiming/ctc v1.2.3
github.com/wzshiming/easycel v0.5.0
Expand Down Expand Up @@ -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
Expand Down
127 changes: 127 additions & 0 deletions pkg/kwokctl/components/dashboard_metrics_scraper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
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 }] <nil> <nil> }`,
},
{
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 }] <nil> <nil> }"},
}
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)
}
mohamedasifs123 marked this conversation as resolved.
Show resolved Hide resolved
})
}
}

// 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)
}
}
190 changes: 190 additions & 0 deletions pkg/kwokctl/components/dashboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
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/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)
}
}
Loading
Loading