generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 204
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
mohamedasifs123
wants to merge
3
commits into
kubernetes-sigs:main
from
mohamedasifs123:component_test
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
pkg/kwokctl/components/dashboard_metrics_scraper_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.