mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-11-13 06:04:02 +00:00
173 lines
4.6 KiB
Go
173 lines
4.6 KiB
Go
/*
|
|
Copyright 2018 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 cmd_test
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/renstrom/dedent"
|
|
|
|
kubeadmapiext "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha1"
|
|
"k8s.io/kubernetes/cmd/kubeadm/app/cmd"
|
|
"k8s.io/kubernetes/cmd/kubeadm/app/features"
|
|
)
|
|
|
|
const (
|
|
defaultNumberOfImages = 8
|
|
)
|
|
|
|
func TestNewCmdConfigListImages(t *testing.T) {
|
|
var output bytes.Buffer
|
|
images := cmd.NewCmdConfigListImages(&output)
|
|
images.Run(nil, nil)
|
|
actual := strings.Split(output.String(), "\n")
|
|
if len(actual) != defaultNumberOfImages {
|
|
t.Fatalf("Expected %v but found %v images", defaultNumberOfImages, len(actual))
|
|
}
|
|
}
|
|
|
|
func TestListImagesRunWithCustomConfigPath(t *testing.T) {
|
|
testcases := []struct {
|
|
name string
|
|
expectedImageCount int
|
|
// each string provided here must appear in at least one image returned by Run
|
|
expectedImageSubstrings []string
|
|
configContents []byte
|
|
}{
|
|
{
|
|
name: "empty config contents",
|
|
expectedImageCount: defaultNumberOfImages,
|
|
configContents: []byte{},
|
|
},
|
|
{
|
|
name: "set k8s version",
|
|
expectedImageCount: defaultNumberOfImages,
|
|
expectedImageSubstrings: []string{
|
|
":v1.9.1",
|
|
},
|
|
configContents: []byte(dedent.Dedent(`
|
|
apiVersion: kubeadm.k8s.io/v1alpha1
|
|
kind: MasterConfiguration
|
|
kubernetesVersion: 1.9.1
|
|
`)),
|
|
},
|
|
{
|
|
name: "use coredns",
|
|
expectedImageCount: defaultNumberOfImages,
|
|
expectedImageSubstrings: []string{
|
|
"coredns",
|
|
},
|
|
configContents: []byte(dedent.Dedent(`
|
|
apiVersion: kubeadm.k8s.io/v1alpha1
|
|
kind: MasterConfiguration
|
|
featureGates:
|
|
CoreDNS: True
|
|
`)),
|
|
},
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
tmpDir, err := ioutil.TempDir("", "kubeadm-images-test")
|
|
if err != nil {
|
|
t.Fatalf("Unable to create temporary directory: %v", err)
|
|
}
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
configFilePath := filepath.Join(tmpDir, "test-config-file")
|
|
err = ioutil.WriteFile(configFilePath, tc.configContents, 0644)
|
|
if err != nil {
|
|
t.Fatalf("Failed writing a config file: %v", err)
|
|
}
|
|
|
|
i, err := cmd.NewListImages(configFilePath, &kubeadmapiext.MasterConfiguration{})
|
|
if err != nil {
|
|
t.Fatalf("Failed getting the kubeadm images command: %v", err)
|
|
}
|
|
var output bytes.Buffer
|
|
if i.Run(&output) != nil {
|
|
t.Fatalf("Error from running the images command: %v", err)
|
|
}
|
|
actual := strings.Split(output.String(), "\n")
|
|
if len(actual) != tc.expectedImageCount {
|
|
t.Fatalf("did not get the same number of images: actual: %v expected: %v. Actual value: %v", len(actual), tc.expectedImageCount, actual)
|
|
}
|
|
|
|
for _, substring := range tc.expectedImageSubstrings {
|
|
if !strings.Contains(output.String(), substring) {
|
|
t.Errorf("Expected to find %v but did not in this list of images: %v", substring, actual)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConfigListImagesRunWithoutPath(t *testing.T) {
|
|
testcases := []struct {
|
|
name string
|
|
cfg kubeadmapiext.MasterConfiguration
|
|
expectedImages int
|
|
}{
|
|
{
|
|
name: "empty config",
|
|
expectedImages: defaultNumberOfImages,
|
|
},
|
|
{
|
|
name: "external etcd configuration",
|
|
cfg: kubeadmapiext.MasterConfiguration{
|
|
Etcd: kubeadmapiext.Etcd{
|
|
Endpoints: []string{"hi"},
|
|
},
|
|
},
|
|
expectedImages: defaultNumberOfImages - 1,
|
|
},
|
|
{
|
|
name: "coredns enabled",
|
|
cfg: kubeadmapiext.MasterConfiguration{
|
|
FeatureGates: map[string]bool{
|
|
features.CoreDNS: true,
|
|
},
|
|
},
|
|
expectedImages: defaultNumberOfImages,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testcases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
i, err := cmd.NewListImages("", &tc.cfg)
|
|
if err != nil {
|
|
t.Fatalf("did not expect an error while creating the Images command: %v", err)
|
|
}
|
|
|
|
var output bytes.Buffer
|
|
if i.Run(&output) != nil {
|
|
t.Fatalf("did not expect an error running the Images command: %v", err)
|
|
}
|
|
|
|
actual := strings.Split(output.String(), "\n")
|
|
if len(actual) != tc.expectedImages {
|
|
t.Fatalf("expected %v images but got %v", tc.expectedImages, actual)
|
|
}
|
|
})
|
|
}
|
|
}
|