mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 14:07:14 +00:00
Fix kubeadm unit tests relying on internet access
This commit is contained in:
parent
988243cb83
commit
e2ca0f7cce
@ -358,7 +358,7 @@ func NewCmdConfigImages(out io.Writer) *cobra.Command {
|
|||||||
Short: "Interact with container images used by kubeadm.",
|
Short: "Interact with container images used by kubeadm.",
|
||||||
RunE: cmdutil.SubCmdRunE("images"),
|
RunE: cmdutil.SubCmdRunE("images"),
|
||||||
}
|
}
|
||||||
cmd.AddCommand(NewCmdConfigImagesList(out))
|
cmd.AddCommand(NewCmdConfigImagesList(out, nil))
|
||||||
cmd.AddCommand(NewCmdConfigImagesPull())
|
cmd.AddCommand(NewCmdConfigImagesPull())
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
@ -416,12 +416,18 @@ func (ip *ImagesPull) PullAll() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewCmdConfigImagesList returns the "kubeadm config images list" command
|
// NewCmdConfigImagesList returns the "kubeadm config images list" command
|
||||||
func NewCmdConfigImagesList(out io.Writer) *cobra.Command {
|
func NewCmdConfigImagesList(out io.Writer, mockK8sVersion *string) *cobra.Command {
|
||||||
cfg := &kubeadmapiv1alpha2.MasterConfiguration{}
|
cfg := &kubeadmapiv1alpha2.MasterConfiguration{}
|
||||||
kubeadmscheme.Scheme.Default(cfg)
|
kubeadmscheme.Scheme.Default(cfg)
|
||||||
var cfgPath, featureGatesString string
|
var cfgPath, featureGatesString string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
// This just sets the kubernetes version for unit testing so kubeadm won't try to
|
||||||
|
// lookup the latest release from the internet.
|
||||||
|
if mockK8sVersion != nil {
|
||||||
|
cfg.KubernetesVersion = *mockK8sVersion
|
||||||
|
}
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "Print a list of images kubeadm will use. The configuration file is used in case any images or image repositories are customized.",
|
Short: "Print a list of images kubeadm will use. The configuration file is used in case any images or image repositories are customized.",
|
||||||
|
@ -34,11 +34,15 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
defaultNumberOfImages = 8
|
defaultNumberOfImages = 8
|
||||||
|
// dummyKubernetesVersion is just used for unit testing, in order to not make
|
||||||
|
// kubeadm lookup dl.k8s.io to resolve what the latest stable release is
|
||||||
|
dummyKubernetesVersion = "v1.10.0"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewCmdConfigImagesList(t *testing.T) {
|
func TestNewCmdConfigImagesList(t *testing.T) {
|
||||||
var output bytes.Buffer
|
var output bytes.Buffer
|
||||||
images := cmd.NewCmdConfigImagesList(&output)
|
mockK8sVersion := dummyKubernetesVersion
|
||||||
|
images := cmd.NewCmdConfigImagesList(&output, &mockK8sVersion)
|
||||||
images.Run(nil, nil)
|
images.Run(nil, nil)
|
||||||
actual := strings.Split(output.String(), "\n")
|
actual := strings.Split(output.String(), "\n")
|
||||||
if len(actual) != defaultNumberOfImages {
|
if len(actual) != defaultNumberOfImages {
|
||||||
@ -63,7 +67,7 @@ func TestImagesListRunWithCustomConfigPath(t *testing.T) {
|
|||||||
configContents: []byte(dedent.Dedent(`
|
configContents: []byte(dedent.Dedent(`
|
||||||
apiVersion: kubeadm.k8s.io/v1alpha2
|
apiVersion: kubeadm.k8s.io/v1alpha2
|
||||||
kind: MasterConfiguration
|
kind: MasterConfiguration
|
||||||
kubernetesVersion: 1.10.1
|
kubernetesVersion: v1.10.1
|
||||||
`)),
|
`)),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -75,9 +79,9 @@ func TestImagesListRunWithCustomConfigPath(t *testing.T) {
|
|||||||
configContents: []byte(dedent.Dedent(`
|
configContents: []byte(dedent.Dedent(`
|
||||||
apiVersion: kubeadm.k8s.io/v1alpha2
|
apiVersion: kubeadm.k8s.io/v1alpha2
|
||||||
kind: MasterConfiguration
|
kind: MasterConfiguration
|
||||||
kubernetesVersion: 1.11.0
|
kubernetesVersion: v1.11.0
|
||||||
featureGates:
|
featureGates:
|
||||||
CoreDNS: True
|
CoreDNS: True
|
||||||
`)),
|
`)),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -96,7 +100,9 @@ func TestImagesListRunWithCustomConfigPath(t *testing.T) {
|
|||||||
t.Fatalf("Failed writing a config file: %v", err)
|
t.Fatalf("Failed writing a config file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
i, err := cmd.NewImagesList(configFilePath, &kubeadmapiv1alpha2.MasterConfiguration{})
|
i, err := cmd.NewImagesList(configFilePath, &kubeadmapiv1alpha2.MasterConfiguration{
|
||||||
|
KubernetesVersion: dummyKubernetesVersion,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed getting the kubeadm images command: %v", err)
|
t.Fatalf("Failed getting the kubeadm images command: %v", err)
|
||||||
}
|
}
|
||||||
@ -127,6 +133,9 @@ func TestConfigImagesListRunWithoutPath(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "empty config",
|
name: "empty config",
|
||||||
expectedImages: defaultNumberOfImages,
|
expectedImages: defaultNumberOfImages,
|
||||||
|
cfg: kubeadmapiv1alpha2.MasterConfiguration{
|
||||||
|
KubernetesVersion: dummyKubernetesVersion,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "external etcd configuration",
|
name: "external etcd configuration",
|
||||||
@ -136,6 +145,7 @@ func TestConfigImagesListRunWithoutPath(t *testing.T) {
|
|||||||
Endpoints: []string{"https://some.etcd.com:2379"},
|
Endpoints: []string{"https://some.etcd.com:2379"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
KubernetesVersion: dummyKubernetesVersion,
|
||||||
},
|
},
|
||||||
expectedImages: defaultNumberOfImages - 1,
|
expectedImages: defaultNumberOfImages - 1,
|
||||||
},
|
},
|
||||||
@ -145,6 +155,7 @@ func TestConfigImagesListRunWithoutPath(t *testing.T) {
|
|||||||
FeatureGates: map[string]bool{
|
FeatureGates: map[string]bool{
|
||||||
features.CoreDNS: true,
|
features.CoreDNS: true,
|
||||||
},
|
},
|
||||||
|
KubernetesVersion: dummyKubernetesVersion,
|
||||||
},
|
},
|
||||||
expectedImages: defaultNumberOfImages,
|
expectedImages: defaultNumberOfImages,
|
||||||
},
|
},
|
||||||
@ -198,6 +209,7 @@ func TestMigrate(t *testing.T) {
|
|||||||
cfg := []byte(dedent.Dedent(`
|
cfg := []byte(dedent.Dedent(`
|
||||||
apiVersion: kubeadm.k8s.io/v1alpha2
|
apiVersion: kubeadm.k8s.io/v1alpha2
|
||||||
kind: MasterConfiguration
|
kind: MasterConfiguration
|
||||||
|
kubernetesVersion: v1.10.0
|
||||||
`))
|
`))
|
||||||
configFile, cleanup := tempConfig(t, cfg)
|
configFile, cleanup := tempConfig(t, cfg)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
@ -61,6 +61,7 @@ func SetupMasterConfigurationFile(t *testing.T, tmpdir string, cfg *kubeadmapi.M
|
|||||||
bindPort: {{.API.BindPort}}
|
bindPort: {{.API.BindPort}}
|
||||||
nodeRegistration:
|
nodeRegistration:
|
||||||
name: {{.NodeRegistration.Name}}
|
name: {{.NodeRegistration.Name}}
|
||||||
|
kubernetesVersion: v1.10.0
|
||||||
`)))
|
`)))
|
||||||
|
|
||||||
f, err := os.Create(cfgPath)
|
f, err := os.Create(cfgPath)
|
||||||
|
Loading…
Reference in New Issue
Block a user