Skip to content

Commit

Permalink
Merge pull request kubeedge#5695 from 1Shubham7/keadm-tests
Browse files Browse the repository at this point in the history
Added tests for `keadm beta` and `cloud` packages
  • Loading branch information
kubeedge-bot authored Jul 8, 2024
2 parents 46b7d0c + c3e7bb0 commit ebfb298
Show file tree
Hide file tree
Showing 8 changed files with 714 additions and 0 deletions.
37 changes: 37 additions & 0 deletions keadm/cmd/keadm/app/cmd/beta/beta_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
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 beta

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewBeta(t *testing.T) {
assert := assert.New(t)
cmd := NewBeta()

assert.NotNil(cmd)
assert.Equal(cmd.Use, "beta")
assert.Equal(cmd.Short, "keadm beta command")
assert.Equal(cmd.Long, `keadm beta command provides some subcommands that are still in testing, but have complete functions and can be used in advance, but now it contains nothing`)

flags := cmd.Flags()
assert.NotNil(flags)
assert.Equal("", flags.FlagUsages())
}
16 changes: 16 additions & 0 deletions keadm/cmd/keadm/app/cmd/cloud/gettoken.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
Copyright 2022 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 cloud

import (
Expand Down
95 changes: 95 additions & 0 deletions keadm/cmd/keadm/app/cmd/cloud/gettoken_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
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 cloud

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"

"github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/common"
)

func TestNewGetToken(t *testing.T) {
assert := assert.New(t)

cmd := NewGettoken()

assert.NotNil(cmd)
assert.Equal(cmd.Use, "gettoken")
assert.Equal(cmd.Short, "To get the token for edge nodes to join the cluster")
assert.Equal(cmd.Long, gettokenLongDescription)
assert.Equal(cmd.Example, gettokenExample)

assert.NotNil(cmd.RunE)

flag := cmd.Flags().Lookup(common.FlagNameKubeConfig)
assert.NotNil(flag)
assert.Equal(common.DefaultKubeConfig, flag.DefValue)
assert.Equal(common.FlagNameKubeConfig, flag.Name)
}

func TestAddGettokenFlags(t *testing.T) {
assert := assert.New(t)

cmd := &cobra.Command{}
gettokenOptions := newGettokenOptions()

addGettokenFlags(cmd, gettokenOptions)

flag := cmd.Flags().Lookup(common.FlagNameKubeConfig)
assert.NotNil(flag)
assert.Equal(common.DefaultKubeConfig, flag.DefValue)
assert.Equal(common.FlagNameKubeConfig, flag.Name)
}

func TestNewGettokenOptions(t *testing.T) {
assert := assert.New(t)

opts := newGettokenOptions()

assert.NotNil(opts)
assert.Equal(common.DefaultKubeConfig, opts.Kubeconfig)
}

func TestShowToken(t *testing.T) {
cases := []struct {
data []byte
wantErr bool
}{
{
data: []byte("valid token"),
wantErr: false,
},
{
data: []byte(""),
wantErr: false,
},
}

assert := assert.New(t)

for _, test := range cases {
t.Run("Testing showToken()", func(t *testing.T) {
err := showToken(test.data)
if !test.wantErr {
assert.NoError(err)
}
})
}
}
187 changes: 187 additions & 0 deletions keadm/cmd/keadm/app/cmd/cloud/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
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 cloud

import (
"fmt"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"

types "github.com/kubeedge/kubeedge/keadm/cmd/keadm/app/cmd/common"
)

func TestNewCloudInit(t *testing.T) {
assert := assert.New(t)
cmd := NewCloudInit()

assert.Equal(cmd.Use, "init")
assert.Equal(cmd.Short, "Bootstraps cloud component. Checks and install (if required) the pre-requisites.")
assert.Equal(cmd.Long, cloudInitLongDescription)
assert.Equal(cmd.Example, fmt.Sprintf(cloudInitExample, types.DefaultKubeEdgeVersion))

assert.NotNil(cmd.RunE)

expectedFlags := []struct {
name string
defaultValue string
}{
{
types.FlagNameKubeEdgeVersion,
"",
},
{
types.FlagNameAdvertiseAddress,
"",
},
{
types.FlagNameKubeConfig,
types.DefaultKubeConfig,
},
{
types.FlagNameManifests,
"",
},
{
types.FlagNameFiles,
"",
},
{
types.FlagNameDryRun,
"false",
},
{
types.FlagNameExternalHelmRoot,
"",
},
{
types.FlagNameImageRepository,
"",
},
{
types.FlagNameSet,
"[]",
},
{
types.FlagNameProfile,
"",
},
{
types.FlagNameForce,
"false",
},
}

for _, flag := range expectedFlags {
assert.Equal(flag.defaultValue, cmd.Flag(flag.name).DefValue)
assert.Equal(flag.name, cmd.Flag(flag.name).Name)
}
}

func TestNewInitOptions(t *testing.T) {
assert := assert.New(t)

opts := newInitOptions()
assert.Equal(opts.KubeConfig, types.DefaultKubeConfig)
}

func TestAddInitOtherFlags(t *testing.T) {
assert := assert.New(t)
cmd := &cobra.Command{}
opts := newInitOptions()

addInitOtherFlags(cmd, opts)

expectedFlags := []struct {
name string
defaultValue string
}{
{
types.FlagNameKubeEdgeVersion,
"",
},
{
types.FlagNameAdvertiseAddress,
"",
},
{
types.FlagNameKubeConfig,
types.DefaultKubeConfig,
},
{
types.FlagNameManifests,
"",
},
{
types.FlagNameFiles,
"",
},
{
types.FlagNameDryRun,
"false",
},
{
types.FlagNameExternalHelmRoot,
"",
},
{
types.FlagNameImageRepository,
"",
},
}

for _, flag := range expectedFlags {
assert.Equal(flag.defaultValue, cmd.Flag(flag.name).DefValue)
assert.Equal(flag.name, cmd.Flag(flag.name).Name)
}
}

func TestAddHelmValueOptionsFlags(t *testing.T) {
assert := assert.New(t)
cmd := &cobra.Command{}
opts := newInitOptions()

addHelmValueOptionsFlags(cmd, opts)

assert.Equal("", cmd.Flag(types.FlagNameProfile).DefValue)
assert.Equal(types.FlagNameProfile, cmd.Flag(types.FlagNameProfile).Name)

assert.Equal("[]", cmd.Flag(types.FlagNameSet).DefValue)
assert.Equal(types.FlagNameSet, cmd.Flag(types.FlagNameSet).Name)
}

func TestAddForceOptionsFlags(t *testing.T) {
assert := assert.New(t)
cmd := &cobra.Command{}
opts := newInitOptions()

addForceOptionsFlags(cmd, opts)

assert.Equal("false", cmd.Flag(types.FlagNameForce).Value.String())
assert.Equal(types.FlagNameForce, cmd.Flag(types.FlagNameForce).Name)
}

func TestAddInit2ToolsList(t *testing.T) {
assert := assert.New(t)
toolList := make(map[string]types.ToolsInstaller)
opts := newInitOptions()

err := AddInit2ToolsList(toolList, opts)
assert.Nil(err)
assert.NotNil(toolList["helm"])
}
Loading

0 comments on commit ebfb298

Please sign in to comment.