Fix kubeadm unit tests relying on internet access

This commit is contained in:
Lucas Käldström 2018-06-19 20:34:45 +03:00
parent 988243cb83
commit e2ca0f7cce
No known key found for this signature in database
GPG Key ID: 3FA3783D77751514
3 changed files with 26 additions and 7 deletions

View File

@ -358,7 +358,7 @@ func NewCmdConfigImages(out io.Writer) *cobra.Command {
Short: "Interact with container images used by kubeadm.",
RunE: cmdutil.SubCmdRunE("images"),
}
cmd.AddCommand(NewCmdConfigImagesList(out))
cmd.AddCommand(NewCmdConfigImagesList(out, nil))
cmd.AddCommand(NewCmdConfigImagesPull())
return cmd
}
@ -416,12 +416,18 @@ func (ip *ImagesPull) PullAll() error {
}
// 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{}
kubeadmscheme.Scheme.Default(cfg)
var cfgPath, featureGatesString string
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{
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.",

View File

@ -34,11 +34,15 @@ import (
const (
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) {
var output bytes.Buffer
images := cmd.NewCmdConfigImagesList(&output)
mockK8sVersion := dummyKubernetesVersion
images := cmd.NewCmdConfigImagesList(&output, &mockK8sVersion)
images.Run(nil, nil)
actual := strings.Split(output.String(), "\n")
if len(actual) != defaultNumberOfImages {
@ -63,7 +67,7 @@ func TestImagesListRunWithCustomConfigPath(t *testing.T) {
configContents: []byte(dedent.Dedent(`
apiVersion: kubeadm.k8s.io/v1alpha2
kind: MasterConfiguration
kubernetesVersion: 1.10.1
kubernetesVersion: v1.10.1
`)),
},
{
@ -75,9 +79,9 @@ func TestImagesListRunWithCustomConfigPath(t *testing.T) {
configContents: []byte(dedent.Dedent(`
apiVersion: kubeadm.k8s.io/v1alpha2
kind: MasterConfiguration
kubernetesVersion: 1.11.0
kubernetesVersion: v1.11.0
featureGates:
CoreDNS: True
CoreDNS: True
`)),
},
}
@ -96,7 +100,9 @@ func TestImagesListRunWithCustomConfigPath(t *testing.T) {
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 {
t.Fatalf("Failed getting the kubeadm images command: %v", err)
}
@ -127,6 +133,9 @@ func TestConfigImagesListRunWithoutPath(t *testing.T) {
{
name: "empty config",
expectedImages: defaultNumberOfImages,
cfg: kubeadmapiv1alpha2.MasterConfiguration{
KubernetesVersion: dummyKubernetesVersion,
},
},
{
name: "external etcd configuration",
@ -136,6 +145,7 @@ func TestConfigImagesListRunWithoutPath(t *testing.T) {
Endpoints: []string{"https://some.etcd.com:2379"},
},
},
KubernetesVersion: dummyKubernetesVersion,
},
expectedImages: defaultNumberOfImages - 1,
},
@ -145,6 +155,7 @@ func TestConfigImagesListRunWithoutPath(t *testing.T) {
FeatureGates: map[string]bool{
features.CoreDNS: true,
},
KubernetesVersion: dummyKubernetesVersion,
},
expectedImages: defaultNumberOfImages,
},
@ -198,6 +209,7 @@ func TestMigrate(t *testing.T) {
cfg := []byte(dedent.Dedent(`
apiVersion: kubeadm.k8s.io/v1alpha2
kind: MasterConfiguration
kubernetesVersion: v1.10.0
`))
configFile, cleanup := tempConfig(t, cfg)
defer cleanup()

View File

@ -61,6 +61,7 @@ func SetupMasterConfigurationFile(t *testing.T, tmpdir string, cfg *kubeadmapi.M
bindPort: {{.API.BindPort}}
nodeRegistration:
name: {{.NodeRegistration.Name}}
kubernetesVersion: v1.10.0
`)))
f, err := os.Create(cfgPath)