Skip to content

Commit

Permalink
Add test cases for NewKubeletConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
laozc committed Feb 17, 2019
1 parent 39fcf1f commit 851ee5f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
90 changes: 90 additions & 0 deletions pkg/minikube/bootstrapper/kubeadm/kubeadm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,96 @@ import (
"k8s.io/minikube/pkg/util"
)

func TestGenerateKubeletConfig(t *testing.T) {
tests := []struct {
description string
cfg config.KubernetesConfig
expectedCfg string
shouldErr bool
}{
{
description: "docker runtime",
cfg: config.KubernetesConfig{
NodeIP: "192.168.1.100",
KubernetesVersion: "v1.1.0",
NodeName: "minikube",
ContainerRuntime: "docker",
},
expectedCfg: `
[Unit]
Wants=docker.socket
[Service]
ExecStart=
ExecStart=/usr/bin/kubelet --allow-privileged=true --authorization-mode=Webhook --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --cadvisor-port=0 --cgroup-driver=cgroupfs --client-ca-file=/var/lib/minikube/certs/ca.crt --cluster-dns=10.96.0.10 --cluster-domain=cluster.local --container-runtime=docker --hostname-override=minikube --kubeconfig=/etc/kubernetes/kubelet.conf --pod-manifest-path=/etc/kubernetes/manifests --require-kubeconfig=true
[Install]
`,
},
{
description: "cri runtime",
cfg: config.KubernetesConfig{
NodeIP: "192.168.1.100",
KubernetesVersion: "v1.1.0",
NodeName: "minikube",
ContainerRuntime: "cri-o",
},
expectedCfg: `
[Unit]
Wants=crio.service
[Service]
ExecStart=
ExecStart=/usr/bin/kubelet --allow-privileged=true --authorization-mode=Webhook --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --cadvisor-port=0 --cgroup-driver=cgroupfs --client-ca-file=/var/lib/minikube/certs/ca.crt --cluster-dns=10.96.0.10 --cluster-domain=cluster.local --container-runtime=remote --container-runtime-endpoint=/var/run/crio/crio.sock --hostname-override=minikube --image-service-endpoint=/var/run/crio/crio.sock --kubeconfig=/etc/kubernetes/kubelet.conf --pod-manifest-path=/etc/kubernetes/manifests --require-kubeconfig=true --runtime-request-timeout=15m
[Install]
`,
},
{
description: "docker runtime with custom image repository",
cfg: config.KubernetesConfig{
NodeIP: "192.168.1.100",
KubernetesVersion: "v1.1.0",
NodeName: "minikube",
ContainerRuntime: "docker",
ImageRepository: "docker-proxy-image.io/google_containers",
},
expectedCfg: `
[Unit]
Wants=docker.socket
[Service]
ExecStart=
ExecStart=/usr/bin/kubelet --allow-privileged=true --authorization-mode=Webhook --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --cadvisor-port=0 --cgroup-driver=cgroupfs --client-ca-file=/var/lib/minikube/certs/ca.crt --cluster-dns=10.96.0.10 --cluster-domain=cluster.local --container-runtime=docker --hostname-override=minikube --kubeconfig=/etc/kubernetes/kubelet.conf --pod-infra-container-image=docker-proxy-image.io/google_containers//pause-amd64:3.0 --pod-manifest-path=/etc/kubernetes/manifests --require-kubeconfig=true
[Install]
`,
},
}

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
runtime, err := cruntime.New(cruntime.Config{Type: test.cfg.ContainerRuntime})
if err != nil {
t.Fatalf("runtime: %v", err)
}

actualCfg, err := NewKubeletConfig(test.cfg, runtime)
if err != nil && !test.shouldErr {
t.Errorf("got unexpected error generating config: %v", err)
return
}
if err == nil && test.shouldErr {
t.Errorf("expected error but got none, config: %s", actualCfg)
return
}
if diff := cmp.Diff(test.expectedCfg, actualCfg); diff != "" {
t.Errorf("actual config does not match expected. (-want +got)\n%s", diff)
}
})
}
}

func TestGenerateConfig(t *testing.T) {
tests := []struct {
description string
Expand Down
9 changes: 7 additions & 2 deletions pkg/minikube/bootstrapper/kubeadm/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,13 @@ func ParseKubernetesVersion(version string) (semver.Version, error) {

func convertToFlags(opts map[string]string) string {
var flags []string
for k, v := range opts {
flags = append(flags, fmt.Sprintf("--%s=%s", k, v))
var keys []string
for k := range opts {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
flags = append(flags, fmt.Sprintf("--%s=%s", k, opts[k]))
}
return strings.Join(flags, " ")
}
Expand Down

0 comments on commit 851ee5f

Please sign in to comment.