diff options
| author | Shubham Singh <shubhammahar1306@gmail.com> | 2024-07-12 23:03:50 +0530 |
|---|---|---|
| committer | Shubham Singh <shubhammahar1306@gmail.com> | 2024-07-12 23:03:50 +0530 |
| commit | ff36ecfd74223ccf8b5ef901a893488f5bfe41a4 (patch) | |
| tree | 8087725cccf8ef8397a8d4ebc26400a411a7fa5b | |
| parent | Merge pull request #5695 from 1Shubham7/keadm-tests (diff) | |
| download | kubeedge-ff36ecfd74223ccf8b5ef901a893488f5bfe41a4.tar.gz | |
added tests for admission module
Signed-off-by: Shubham Singh <shubhammahar1306@gmail.com>
| -rw-r--r-- | cloud/cmd/admission/app/options/options_test.go | 128 | ||||
| -rw-r--r-- | cloud/cmd/admission/app/server_test.go | 65 |
2 files changed, 193 insertions, 0 deletions
diff --git a/cloud/cmd/admission/app/options/options_test.go b/cloud/cmd/admission/app/options/options_test.go new file mode 100644 index 000000000..084231cf7 --- /dev/null +++ b/cloud/cmd/admission/app/options/options_test.go @@ -0,0 +1,128 @@ +/* +Copyright 2024 The KubeEdge 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 options + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewAdmissionOptions(t *testing.T) { + assert := assert.New(t) + + opt := NewAdmissionOptions() + assert.NotNil(opt, "Expected NewAdmissionOptions to return a non-nil value") + assert.Equal(int32(0), opt.Port, "Expected Port to be 0 by default") + assert.False(opt.PrintVersion, "Expected PrintVersion to be false by default") + assert.Equal("", opt.Master) + assert.Equal("", opt.Kubeconfig) + assert.Equal("", opt.CertFile) + assert.Equal("", opt.KeyFile) + assert.Equal("", opt.CaCertFile) + assert.Equal("", opt.AdmissionServiceName) + assert.Equal("", opt.AdmissionServiceNamespace) + assert.Equal("", opt.SchedulerName) +} + +func TestAdmissionOptions_Flags(t *testing.T) { + assert := assert.New(t) + + opt := NewAdmissionOptions() + fss := opt.Flags() + fs := fss.FlagSet("admission") + + assert.NotNil(fs, "Expected Flags to return a non-nil FlagSet") + + flags := []struct { + name string + defaultVal string + usage string + }{ + { + name: "master", + defaultVal: "", + usage: "The address of the Kubernetes API server (overrides any value in kubeconfig)", + }, + { + name: "kubeconfig", + defaultVal: "", + usage: "Path to kubeconfig file with authorization and master location information.", + }, + { + name: "tls-cert-file", + defaultVal: "", + usage: "File containing the default x509 Certificate for HTTPS. (CA cert, if any, concatenated after server cert).", + }, + { + name: "tls-private-key-file", + defaultVal: "", + usage: "File containing the default x509 private key matching --tls-cert-file.", + }, + { + name: "ca-cert-file", + defaultVal: "", + usage: "File containing the x509 Certificate for HTTPS.", + }, + { + name: "port", + defaultVal: "443", + usage: "the port used by admission-controller-server.", + }, + { + name: "webhook-namespace", + defaultVal: "kubeedge", + usage: "The namespace of this webhook", + }, + { + name: "webhook-service-name", + defaultVal: "kubeedge-admission-service", + usage: "The name of this admission service", + }, + } + + for _, f := range flags { + flag := fs.Lookup(f.name) + assert.NotNil(flag, "Expected '%s' flag to be present in the FlagSet", f.name) + assert.Equal(f.name, flag.Name) + assert.Equal(f.defaultVal, flag.DefValue) + assert.Equal(f.usage, flag.Usage) + } + + err := fs.Parse([]string{ + "--master=http://localhost:8080", + "--kubeconfig=/path/to/kubeconfig", + "--tls-cert-file=/path/to/cert", + "--tls-private-key-file=/path/to/key", + "--ca-cert-file=/path/to/ca", + "--port=8443", + "--webhook-namespace=test-namespace", + "--webhook-service-name=test-service", + }) + assert.NoError(err) + + assert.Equal("http://localhost:8080", opt.Master) + assert.Equal("/path/to/kubeconfig", opt.Kubeconfig) + assert.Equal("/path/to/cert", opt.CertFile) + assert.Equal("/path/to/key", opt.KeyFile) + assert.Equal("/path/to/ca", opt.CaCertFile) + assert.Equal(int32(8443), opt.Port) + assert.Equal("test-namespace", opt.AdmissionServiceNamespace) + assert.Equal("test-service", opt.AdmissionServiceName) + assert.Equal(false, opt.PrintVersion, "Expected PrintVersion to be false by default") + assert.Equal("", opt.SchedulerName, "Expected SchedulerName to be an empty string by default") +} diff --git a/cloud/cmd/admission/app/server_test.go b/cloud/cmd/admission/app/server_test.go new file mode 100644 index 000000000..a3aab56b7 --- /dev/null +++ b/cloud/cmd/admission/app/server_test.go @@ -0,0 +1,65 @@ +/* +Copyright 2024 The KubeEdge 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 app + +import ( + "bytes" + "testing" + + "github.com/spf13/pflag" + "github.com/stretchr/testify/assert" + "k8s.io/component-base/cli/globalflag" + + "github.com/kubeedge/kubeedge/cloud/cmd/admission/app/options" + "github.com/kubeedge/kubeedge/pkg/version/verflag" +) + +func TestNewAdmissionCommand(t *testing.T) { + assert := assert.New(t) + + cmd := NewAdmissionCommand() + assert.NotNil(cmd) + assert.Equal("admission", cmd.Use) + assert.Equal(cmd.Long, `Admission leverage the feature of Dynamic Admission Control from kubernetes, start it +if want to admission control some kubeedge resources.`) + + fs := cmd.Flags() + assert.NotNil(fs, "Command should have flags") + namedFs := options.NewAdmissionOptions().Flags() + verflag.AddFlags(namedFs.FlagSet("global")) + globalflag.AddGlobalFlags(namedFs.FlagSet("global"), cmd.Name()) + + for _, f := range namedFs.FlagSets { + fs.AddFlagSet(f) + } + + for _, f := range namedFs.FlagSets { + f.VisitAll(func(flag *pflag.Flag) { + assert.NotNil(fs.Lookup(flag.Name), "Flag %s should be added to the command", flag.Name) + }) + } + + usage := &bytes.Buffer{} + cmd.SetOut(usage) + cmd.Usage() + assert.Contains(usage.String(), "Usage:\n admission") + + help := &bytes.Buffer{} + cmd.SetOut(help) + cmd.Help() + assert.Contains(help.String(), "Admission leverage the feature of Dynamic Admission Control from kubernetes") +} |
