mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-02 16:29:21 +00:00
Merge pull request #35231 from apprenda/kubeadm-unit-tests-pkg-util
Automatic merge from submit-queue Kubeadm added unit tests for pkg app/util Added unit tests for kubeadm/app/util package testing functionality of tokens.go, error.go, and kubeconfig.go. This PR is part of the ongoing effort to add tests (#35025) /cc @pires @jbeda
This commit is contained in:
commit
defb44a4d6
@ -23,9 +23,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var GlobalEnvParams = SetEnvParams()
|
||||||
|
|
||||||
// TODO(phase2) use componentconfig
|
// TODO(phase2) use componentconfig
|
||||||
// we need some params for testing etc, let's keep these hidden for now
|
// we need some params for testing etc, let's keep these hidden for now
|
||||||
func GetEnvParams() map[string]string {
|
func SetEnvParams() *EnvParams {
|
||||||
|
|
||||||
envParams := map[string]string{
|
envParams := map[string]string{
|
||||||
// TODO(phase1+): Mode prefix and host_pki_path to another place as constants, and use them everywhere
|
// TODO(phase1+): Mode prefix and host_pki_path to another place as constants, and use them everywhere
|
||||||
@ -45,5 +47,13 @@ func GetEnvParams() map[string]string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return envParams
|
return &EnvParams{
|
||||||
|
KubernetesDir: envParams["kubernetes_dir"],
|
||||||
|
HostPKIPath: envParams["host_pki_path"],
|
||||||
|
HostEtcdPath: envParams["host_etcd_path"],
|
||||||
|
HyperkubeImage: envParams["hyperkube_image"],
|
||||||
|
DiscoveryImage: envParams["discovery_image"],
|
||||||
|
EtcdImage: envParams["etcd_image"],
|
||||||
|
ComponentLoglevel: envParams["component_loglevel"],
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,16 @@ package kubeadm
|
|||||||
|
|
||||||
import "k8s.io/kubernetes/pkg/api/unversioned"
|
import "k8s.io/kubernetes/pkg/api/unversioned"
|
||||||
|
|
||||||
|
type EnvParams struct {
|
||||||
|
KubernetesDir string
|
||||||
|
HostPKIPath string
|
||||||
|
HostEtcdPath string
|
||||||
|
HyperkubeImage string
|
||||||
|
DiscoveryImage string
|
||||||
|
EtcdImage string
|
||||||
|
ComponentLoglevel string
|
||||||
|
}
|
||||||
|
|
||||||
type MasterConfiguration struct {
|
type MasterConfiguration struct {
|
||||||
unversioned.TypeMeta
|
unversioned.TypeMeta
|
||||||
|
|
||||||
|
@ -31,13 +31,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func createKubeProxyPodSpec(cfg *kubeadmapi.MasterConfiguration) api.PodSpec {
|
func createKubeProxyPodSpec(cfg *kubeadmapi.MasterConfiguration) api.PodSpec {
|
||||||
envParams := kubeadmapi.GetEnvParams()
|
|
||||||
privilegedTrue := true
|
privilegedTrue := true
|
||||||
return api.PodSpec{
|
return api.PodSpec{
|
||||||
SecurityContext: &api.PodSecurityContext{HostNetwork: true},
|
SecurityContext: &api.PodSecurityContext{HostNetwork: true},
|
||||||
Containers: []api.Container{{
|
Containers: []api.Container{{
|
||||||
Name: kubeProxy,
|
Name: kubeProxy,
|
||||||
Image: images.GetCoreImage(images.KubeProxyImage, cfg, envParams["hyperkube_image"]),
|
Image: images.GetCoreImage(images.KubeProxyImage, cfg, kubeadmapi.GlobalEnvParams.HyperkubeImage),
|
||||||
Command: append(getProxyCommand(cfg), "--kubeconfig=/run/kubeconfig"),
|
Command: append(getProxyCommand(cfg), "--kubeconfig=/run/kubeconfig"),
|
||||||
SecurityContext: &api.SecurityContext{Privileged: &privilegedTrue},
|
SecurityContext: &api.SecurityContext{Privileged: &privilegedTrue},
|
||||||
VolumeMounts: []api.VolumeMount{
|
VolumeMounts: []api.VolumeMount{
|
||||||
@ -67,7 +66,7 @@ func createKubeProxyPodSpec(cfg *kubeadmapi.MasterConfiguration) api.PodSpec {
|
|||||||
{
|
{
|
||||||
Name: "kubeconfig",
|
Name: "kubeconfig",
|
||||||
VolumeSource: api.VolumeSource{
|
VolumeSource: api.VolumeSource{
|
||||||
HostPath: &api.HostPathVolumeSource{Path: path.Join(envParams["kubernetes_dir"], "kubelet.conf")},
|
HostPath: &api.HostPathVolumeSource{Path: path.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, "kubelet.conf")},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -61,7 +61,6 @@ func encodeKubeDiscoverySecretData(cfg *kubeadmapi.MasterConfiguration, caCert *
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newKubeDiscoveryPodSpec(cfg *kubeadmapi.MasterConfiguration) api.PodSpec {
|
func newKubeDiscoveryPodSpec(cfg *kubeadmapi.MasterConfiguration) api.PodSpec {
|
||||||
envParams := kubeadmapi.GetEnvParams()
|
|
||||||
return api.PodSpec{
|
return api.PodSpec{
|
||||||
// We have to use host network namespace, as `HostPort`/`HostIP` are Docker's
|
// We have to use host network namespace, as `HostPort`/`HostIP` are Docker's
|
||||||
// buisness and CNI support isn't quite there yet (except for kubenet)
|
// buisness and CNI support isn't quite there yet (except for kubenet)
|
||||||
@ -70,7 +69,7 @@ func newKubeDiscoveryPodSpec(cfg *kubeadmapi.MasterConfiguration) api.PodSpec {
|
|||||||
SecurityContext: &api.PodSecurityContext{HostNetwork: true},
|
SecurityContext: &api.PodSecurityContext{HostNetwork: true},
|
||||||
Containers: []api.Container{{
|
Containers: []api.Container{{
|
||||||
Name: kubeDiscoveryName,
|
Name: kubeDiscoveryName,
|
||||||
Image: envParams["discovery_image"],
|
Image: kubeadmapi.GlobalEnvParams.DiscoveryImage,
|
||||||
Command: []string{"/usr/local/bin/kube-discovery"},
|
Command: []string{"/usr/local/bin/kube-discovery"},
|
||||||
VolumeMounts: []api.VolumeMount{{
|
VolumeMounts: []api.VolumeMount{{
|
||||||
Name: kubeDiscoverySecretName,
|
Name: kubeDiscoverySecretName,
|
||||||
|
@ -54,12 +54,11 @@ const (
|
|||||||
// WriteStaticPodManifests builds manifest objects based on user provided configuration and then dumps it to disk
|
// WriteStaticPodManifests builds manifest objects based on user provided configuration and then dumps it to disk
|
||||||
// where kubelet will pick and schedule them.
|
// where kubelet will pick and schedule them.
|
||||||
func WriteStaticPodManifests(cfg *kubeadmapi.MasterConfiguration) error {
|
func WriteStaticPodManifests(cfg *kubeadmapi.MasterConfiguration) error {
|
||||||
envParams := kubeadmapi.GetEnvParams()
|
|
||||||
// Prepare static pod specs
|
// Prepare static pod specs
|
||||||
staticPodSpecs := map[string]api.Pod{
|
staticPodSpecs := map[string]api.Pod{
|
||||||
kubeAPIServer: componentPod(api.Container{
|
kubeAPIServer: componentPod(api.Container{
|
||||||
Name: kubeAPIServer,
|
Name: kubeAPIServer,
|
||||||
Image: images.GetCoreImage(images.KubeAPIServerImage, cfg, envParams["hyperkube_image"]),
|
Image: images.GetCoreImage(images.KubeAPIServerImage, cfg, kubeadmapi.GlobalEnvParams.HyperkubeImage),
|
||||||
Command: getAPIServerCommand(cfg),
|
Command: getAPIServerCommand(cfg),
|
||||||
VolumeMounts: []api.VolumeMount{certsVolumeMount(), k8sVolumeMount()},
|
VolumeMounts: []api.VolumeMount{certsVolumeMount(), k8sVolumeMount()},
|
||||||
LivenessProbe: componentProbe(8080, "/healthz"),
|
LivenessProbe: componentProbe(8080, "/healthz"),
|
||||||
@ -67,7 +66,7 @@ func WriteStaticPodManifests(cfg *kubeadmapi.MasterConfiguration) error {
|
|||||||
}, certsVolume(cfg), k8sVolume(cfg)),
|
}, certsVolume(cfg), k8sVolume(cfg)),
|
||||||
kubeControllerManager: componentPod(api.Container{
|
kubeControllerManager: componentPod(api.Container{
|
||||||
Name: kubeControllerManager,
|
Name: kubeControllerManager,
|
||||||
Image: images.GetCoreImage(images.KubeControllerManagerImage, cfg, envParams["hyperkube_image"]),
|
Image: images.GetCoreImage(images.KubeControllerManagerImage, cfg, kubeadmapi.GlobalEnvParams.HyperkubeImage),
|
||||||
Command: getControllerManagerCommand(cfg),
|
Command: getControllerManagerCommand(cfg),
|
||||||
VolumeMounts: []api.VolumeMount{certsVolumeMount(), k8sVolumeMount()},
|
VolumeMounts: []api.VolumeMount{certsVolumeMount(), k8sVolumeMount()},
|
||||||
LivenessProbe: componentProbe(10252, "/healthz"),
|
LivenessProbe: componentProbe(10252, "/healthz"),
|
||||||
@ -75,7 +74,7 @@ func WriteStaticPodManifests(cfg *kubeadmapi.MasterConfiguration) error {
|
|||||||
}, certsVolume(cfg), k8sVolume(cfg)),
|
}, certsVolume(cfg), k8sVolume(cfg)),
|
||||||
kubeScheduler: componentPod(api.Container{
|
kubeScheduler: componentPod(api.Container{
|
||||||
Name: kubeScheduler,
|
Name: kubeScheduler,
|
||||||
Image: images.GetCoreImage(images.KubeSchedulerImage, cfg, envParams["hyperkube_image"]),
|
Image: images.GetCoreImage(images.KubeSchedulerImage, cfg, kubeadmapi.GlobalEnvParams.HyperkubeImage),
|
||||||
Command: getSchedulerCommand(cfg),
|
Command: getSchedulerCommand(cfg),
|
||||||
LivenessProbe: componentProbe(10251, "/healthz"),
|
LivenessProbe: componentProbe(10251, "/healthz"),
|
||||||
Resources: componentResources("100m"),
|
Resources: componentResources("100m"),
|
||||||
@ -93,7 +92,7 @@ func WriteStaticPodManifests(cfg *kubeadmapi.MasterConfiguration) error {
|
|||||||
"--data-dir=/var/etcd/data",
|
"--data-dir=/var/etcd/data",
|
||||||
},
|
},
|
||||||
VolumeMounts: []api.VolumeMount{certsVolumeMount(), etcdVolumeMount(), k8sVolumeMount()},
|
VolumeMounts: []api.VolumeMount{certsVolumeMount(), etcdVolumeMount(), k8sVolumeMount()},
|
||||||
Image: images.GetCoreImage(images.KubeEtcdImage, cfg, envParams["etcd_image"]),
|
Image: images.GetCoreImage(images.KubeEtcdImage, cfg, kubeadmapi.GlobalEnvParams.EtcdImage),
|
||||||
LivenessProbe: componentProbe(2379, "/health"),
|
LivenessProbe: componentProbe(2379, "/health"),
|
||||||
Resources: componentResources("200m"),
|
Resources: componentResources("200m"),
|
||||||
SecurityContext: &api.SecurityContext{
|
SecurityContext: &api.SecurityContext{
|
||||||
@ -108,7 +107,7 @@ func WriteStaticPodManifests(cfg *kubeadmapi.MasterConfiguration) error {
|
|||||||
}, certsVolume(cfg), etcdVolume(cfg), k8sVolume(cfg))
|
}, certsVolume(cfg), etcdVolume(cfg), k8sVolume(cfg))
|
||||||
}
|
}
|
||||||
|
|
||||||
manifestsPath := path.Join(envParams["kubernetes_dir"], "manifests")
|
manifestsPath := path.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, "manifests")
|
||||||
if err := os.MkdirAll(manifestsPath, 0700); err != nil {
|
if err := os.MkdirAll(manifestsPath, 0700); err != nil {
|
||||||
return fmt.Errorf("<master/manifests> failed to create directory %q [%v]", manifestsPath, err)
|
return fmt.Errorf("<master/manifests> failed to create directory %q [%v]", manifestsPath, err)
|
||||||
}
|
}
|
||||||
@ -127,11 +126,10 @@ func WriteStaticPodManifests(cfg *kubeadmapi.MasterConfiguration) error {
|
|||||||
|
|
||||||
// etcdVolume exposes a path on the host in order to guarantee data survival during reboot.
|
// etcdVolume exposes a path on the host in order to guarantee data survival during reboot.
|
||||||
func etcdVolume(cfg *kubeadmapi.MasterConfiguration) api.Volume {
|
func etcdVolume(cfg *kubeadmapi.MasterConfiguration) api.Volume {
|
||||||
envParams := kubeadmapi.GetEnvParams()
|
|
||||||
return api.Volume{
|
return api.Volume{
|
||||||
Name: "etcd",
|
Name: "etcd",
|
||||||
VolumeSource: api.VolumeSource{
|
VolumeSource: api.VolumeSource{
|
||||||
HostPath: &api.HostPathVolumeSource{Path: envParams["host_etcd_path"]},
|
HostPath: &api.HostPathVolumeSource{Path: kubeadmapi.GlobalEnvParams.HostEtcdPath},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -162,11 +160,10 @@ func certsVolumeMount() api.VolumeMount {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func k8sVolume(cfg *kubeadmapi.MasterConfiguration) api.Volume {
|
func k8sVolume(cfg *kubeadmapi.MasterConfiguration) api.Volume {
|
||||||
envParams := kubeadmapi.GetEnvParams()
|
|
||||||
return api.Volume{
|
return api.Volume{
|
||||||
Name: "pki",
|
Name: "pki",
|
||||||
VolumeSource: api.VolumeSource{
|
VolumeSource: api.VolumeSource{
|
||||||
HostPath: &api.HostPathVolumeSource{Path: envParams["kubernetes_dir"]},
|
HostPath: &api.HostPathVolumeSource{Path: kubeadmapi.GlobalEnvParams.KubernetesDir},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -222,13 +219,12 @@ func componentPod(container api.Container, volumes ...api.Volume) api.Pod {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getComponentBaseCommand(component string) (command []string) {
|
func getComponentBaseCommand(component string) (command []string) {
|
||||||
envParams := kubeadmapi.GetEnvParams()
|
if kubeadmapi.GlobalEnvParams.HyperkubeImage != "" {
|
||||||
if envParams["hyperkube_image"] != "" {
|
|
||||||
command = []string{"/hyperkube", component}
|
command = []string{"/hyperkube", component}
|
||||||
} else {
|
} else {
|
||||||
command = []string{"kube-" + component}
|
command = []string{"kube-" + component}
|
||||||
}
|
}
|
||||||
command = append(command, envParams["component_loglevel"])
|
command = append(command, kubeadmapi.GlobalEnvParams.ComponentLoglevel)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,7 +158,7 @@ func CreatePKIAssets(cfg *kubeadmapi.MasterConfiguration) (*rsa.PrivateKey, *x50
|
|||||||
}
|
}
|
||||||
altNames.DNSNames = append(altNames.DNSNames, cfg.API.ExternalDNSNames...)
|
altNames.DNSNames = append(altNames.DNSNames, cfg.API.ExternalDNSNames...)
|
||||||
|
|
||||||
pkiPath := path.Join(kubeadmapi.GetEnvParams()["host_pki_path"])
|
pkiPath := path.Join(kubeadmapi.GlobalEnvParams.HostPKIPath)
|
||||||
|
|
||||||
caKey, caCert, err := newCertificateAuthority()
|
caKey, caCert, err := newCertificateAuthority()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -48,12 +48,12 @@ func generateTokenIfNeeded(s *kubeadmapi.Secrets) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CreateTokenAuthFile(s *kubeadmapi.Secrets) error {
|
func CreateTokenAuthFile(s *kubeadmapi.Secrets) error {
|
||||||
tokenAuthFilePath := path.Join(kubeadmapi.GetEnvParams()["host_pki_path"], "tokens.csv")
|
tokenAuthFilePath := path.Join(kubeadmapi.GlobalEnvParams.HostPKIPath, "tokens.csv")
|
||||||
if err := generateTokenIfNeeded(s); err != nil {
|
if err := generateTokenIfNeeded(s); err != nil {
|
||||||
return fmt.Errorf("<master/tokens> failed to generate token(s) [%v]", err)
|
return fmt.Errorf("<master/tokens> failed to generate token(s) [%v]", err)
|
||||||
}
|
}
|
||||||
if err := os.MkdirAll(kubeadmapi.GetEnvParams()["host_pki_path"], 0700); err != nil {
|
if err := os.MkdirAll(kubeadmapi.GlobalEnvParams.HostPKIPath, 0700); err != nil {
|
||||||
return fmt.Errorf("<master/tokens> failed to create directory %q [%v]", kubeadmapi.GetEnvParams()["host_pki_path"], err)
|
return fmt.Errorf("<master/tokens> failed to create directory %q [%v]", kubeadmapi.GlobalEnvParams.HostPKIPath, err)
|
||||||
}
|
}
|
||||||
serialized := []byte(fmt.Sprintf("%s,kubeadm-node-csr,%s,system:kubelet-bootstrap\n", s.BearerToken, uuid.NewUUID()))
|
serialized := []byte(fmt.Sprintf("%s,kubeadm-node-csr,%s,system:kubelet-bootstrap\n", s.BearerToken, uuid.NewUUID()))
|
||||||
// DumpReaderToFile create a file with mode 0600
|
// DumpReaderToFile create a file with mode 0600
|
||||||
|
@ -30,8 +30,16 @@ go_library(
|
|||||||
|
|
||||||
go_test(
|
go_test(
|
||||||
name = "go_default_test",
|
name = "go_default_test",
|
||||||
srcs = ["tokens_test.go"],
|
srcs = [
|
||||||
|
"error_test.go",
|
||||||
|
"kubeconfig_test.go",
|
||||||
|
"tokens_test.go",
|
||||||
|
],
|
||||||
library = "go_default_library",
|
library = "go_default_library",
|
||||||
tags = ["automanaged"],
|
tags = ["automanaged"],
|
||||||
deps = ["//cmd/kubeadm/app/apis/kubeadm:go_default_library"],
|
deps = [
|
||||||
|
"//cmd/kubeadm/app/apis/kubeadm:go_default_library",
|
||||||
|
"//cmd/kubeadm/app/preflight:go_default_library",
|
||||||
|
"//pkg/client/unversioned/clientcmd/api:go_default_library",
|
||||||
|
],
|
||||||
)
|
)
|
||||||
|
52
cmd/kubeadm/app/util/error_test.go
Normal file
52
cmd/kubeadm/app/util/error_test.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 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 util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/kubernetes/cmd/kubeadm/app/preflight"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCheckErr(t *testing.T) {
|
||||||
|
var codeReturned int
|
||||||
|
errHandle := func(err string, code int) {
|
||||||
|
codeReturned = code
|
||||||
|
}
|
||||||
|
|
||||||
|
var tokenTest = []struct {
|
||||||
|
e error
|
||||||
|
expected int
|
||||||
|
}{
|
||||||
|
{nil, 0},
|
||||||
|
{fmt.Errorf(""), DefaultErrorExitCode},
|
||||||
|
{&preflight.PreFlightError{}, PreFlight},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rt := range tokenTest {
|
||||||
|
codeReturned = 0
|
||||||
|
checkErr("", rt.e, errHandle)
|
||||||
|
if codeReturned != rt.expected {
|
||||||
|
t.Errorf(
|
||||||
|
"failed checkErr:\n\texpected: %d\n\t actual: %d",
|
||||||
|
rt.expected,
|
||||||
|
codeReturned,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -76,12 +76,11 @@ func MakeClientConfigWithToken(config *clientcmdapi.Config, clusterName string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func WriteKubeconfigIfNotExists(name string, kubeconfig *clientcmdapi.Config) error {
|
func WriteKubeconfigIfNotExists(name string, kubeconfig *clientcmdapi.Config) error {
|
||||||
envParams := kubeadmapi.GetEnvParams()
|
if err := os.MkdirAll(kubeadmapi.GlobalEnvParams.KubernetesDir, 0700); err != nil {
|
||||||
if err := os.MkdirAll(envParams["kubernetes_dir"], 0700); err != nil {
|
return fmt.Errorf("<util/kubeconfig> failed to create directory %q [%v]", kubeadmapi.GlobalEnvParams.KubernetesDir, err)
|
||||||
return fmt.Errorf("<util/kubeconfig> failed to create directory %q [%v]", envParams["kubernetes_dir"], err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filename := path.Join(envParams["kubernetes_dir"], fmt.Sprintf("%s.conf", name))
|
filename := path.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, fmt.Sprintf("%s.conf", name))
|
||||||
// Create and open the file, only if it does not already exist.
|
// Create and open the file, only if it does not already exist.
|
||||||
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600)
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_EXCL, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
199
cmd/kubeadm/app/util/kubeconfig_test.go
Normal file
199
cmd/kubeadm/app/util/kubeconfig_test.go
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2016 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 util
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||||
|
clientcmdapi "k8s.io/kubernetes/pkg/client/unversioned/clientcmd/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
configOut1 = `apiVersion: v1
|
||||||
|
clusters:
|
||||||
|
- cluster:
|
||||||
|
server: ""
|
||||||
|
name: ""
|
||||||
|
contexts: []
|
||||||
|
current-context: ""
|
||||||
|
kind: Config
|
||||||
|
preferences: {}
|
||||||
|
users: []
|
||||||
|
`
|
||||||
|
configOut2 = `apiVersion: v1
|
||||||
|
clusters:
|
||||||
|
- cluster:
|
||||||
|
server: ""
|
||||||
|
name: kubernetes
|
||||||
|
contexts: []
|
||||||
|
current-context: ""
|
||||||
|
kind: Config
|
||||||
|
preferences: {}
|
||||||
|
users: []
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
type configClient struct {
|
||||||
|
c string
|
||||||
|
s string
|
||||||
|
ca []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type configClientWithCerts struct {
|
||||||
|
c *clientcmdapi.Config
|
||||||
|
clusterName string
|
||||||
|
userName string
|
||||||
|
clientKey []byte
|
||||||
|
clientCert []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type configClientWithToken struct {
|
||||||
|
c *clientcmdapi.Config
|
||||||
|
clusterName string
|
||||||
|
userName string
|
||||||
|
token string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateBasicClientConfig(t *testing.T) {
|
||||||
|
var createBasicTest = []struct {
|
||||||
|
cc configClient
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{configClient{}, ""},
|
||||||
|
{configClient{c: "kubernetes"}, ""},
|
||||||
|
}
|
||||||
|
for _, rt := range createBasicTest {
|
||||||
|
c := CreateBasicClientConfig(rt.cc.c, rt.cc.s, rt.cc.ca)
|
||||||
|
if c.Kind != rt.expected {
|
||||||
|
t.Errorf(
|
||||||
|
"failed CreateBasicClientConfig:\n\texpected: %s\n\t actual: %s",
|
||||||
|
c.Kind,
|
||||||
|
rt.expected,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMakeClientConfigWithCerts(t *testing.T) {
|
||||||
|
var createBasicTest = []struct {
|
||||||
|
cc configClient
|
||||||
|
ccWithCerts configClientWithCerts
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{configClient{}, configClientWithCerts{}, ""},
|
||||||
|
{configClient{c: "kubernetes"}, configClientWithCerts{}, ""},
|
||||||
|
}
|
||||||
|
for _, rt := range createBasicTest {
|
||||||
|
c := CreateBasicClientConfig(rt.cc.c, rt.cc.s, rt.cc.ca)
|
||||||
|
rt.ccWithCerts.c = c
|
||||||
|
cwc := MakeClientConfigWithCerts(
|
||||||
|
rt.ccWithCerts.c,
|
||||||
|
rt.ccWithCerts.clusterName,
|
||||||
|
rt.ccWithCerts.userName,
|
||||||
|
rt.ccWithCerts.clientKey,
|
||||||
|
rt.ccWithCerts.clientCert,
|
||||||
|
)
|
||||||
|
if cwc.Kind != rt.expected {
|
||||||
|
t.Errorf(
|
||||||
|
"failed MakeClientConfigWithCerts:\n\texpected: %s\n\t actual: %s",
|
||||||
|
c.Kind,
|
||||||
|
rt.expected,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMakeClientConfigWithToken(t *testing.T) {
|
||||||
|
var createBasicTest = []struct {
|
||||||
|
cc configClient
|
||||||
|
ccWithToken configClientWithToken
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{configClient{}, configClientWithToken{}, ""},
|
||||||
|
{configClient{c: "kubernetes"}, configClientWithToken{}, ""},
|
||||||
|
}
|
||||||
|
for _, rt := range createBasicTest {
|
||||||
|
c := CreateBasicClientConfig(rt.cc.c, rt.cc.s, rt.cc.ca)
|
||||||
|
rt.ccWithToken.c = c
|
||||||
|
cwc := MakeClientConfigWithToken(
|
||||||
|
rt.ccWithToken.c,
|
||||||
|
rt.ccWithToken.clusterName,
|
||||||
|
rt.ccWithToken.userName,
|
||||||
|
rt.ccWithToken.token,
|
||||||
|
)
|
||||||
|
if cwc.Kind != rt.expected {
|
||||||
|
t.Errorf(
|
||||||
|
"failed MakeClientConfigWithCerts:\n\texpected: %s\n\t actual: %s",
|
||||||
|
c.Kind,
|
||||||
|
rt.expected,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteKubeconfigIfNotExists(t *testing.T) {
|
||||||
|
tmpdir, err := ioutil.TempDir("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Couldn't create tmpdir")
|
||||||
|
}
|
||||||
|
defer os.Remove(tmpdir)
|
||||||
|
|
||||||
|
// set up tmp GlobalEnvParams values for testing
|
||||||
|
oldEnv := kubeadmapi.GlobalEnvParams
|
||||||
|
kubeadmapi.GlobalEnvParams = kubeadmapi.SetEnvParams()
|
||||||
|
kubeadmapi.GlobalEnvParams.KubernetesDir = fmt.Sprintf("%s/etc/kubernetes", tmpdir)
|
||||||
|
kubeadmapi.GlobalEnvParams.HostPKIPath = fmt.Sprintf("%s/etc/kubernetes/pki", tmpdir)
|
||||||
|
kubeadmapi.GlobalEnvParams.HostEtcdPath = fmt.Sprintf("%s/var/lib/etcd", tmpdir)
|
||||||
|
kubeadmapi.GlobalEnvParams.DiscoveryImage = fmt.Sprintf("%s/var/lib/etcd", tmpdir)
|
||||||
|
defer func() { kubeadmapi.GlobalEnvParams = oldEnv }()
|
||||||
|
|
||||||
|
var writeConfig = []struct {
|
||||||
|
name string
|
||||||
|
cc configClient
|
||||||
|
expected error
|
||||||
|
file []byte
|
||||||
|
}{
|
||||||
|
{"test1", configClient{}, nil, []byte(configOut1)},
|
||||||
|
{"test2", configClient{c: "kubernetes"}, nil, []byte(configOut2)},
|
||||||
|
}
|
||||||
|
for _, rt := range writeConfig {
|
||||||
|
c := CreateBasicClientConfig(rt.cc.c, rt.cc.s, rt.cc.ca)
|
||||||
|
err := WriteKubeconfigIfNotExists(rt.name, c)
|
||||||
|
if err != rt.expected {
|
||||||
|
t.Errorf(
|
||||||
|
"failed WriteKubeconfigIfNotExists with an error:\n\texpected: %s\n\t actual: %s",
|
||||||
|
err,
|
||||||
|
rt.expected,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
configPath := filepath.Join(kubeadmapi.GlobalEnvParams.KubernetesDir, fmt.Sprintf("%s.conf", rt.name))
|
||||||
|
newFile, err := ioutil.ReadFile(configPath)
|
||||||
|
if !bytes.Equal(newFile, rt.file) {
|
||||||
|
t.Errorf(
|
||||||
|
"failed WriteKubeconfigIfNotExists config write:\n\texpected: %s\n\t actual: %s",
|
||||||
|
newFile,
|
||||||
|
rt.file,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -18,6 +18,7 @@ package util
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
|
||||||
@ -83,3 +84,88 @@ func newSecretsWithToken(token string) *kubeadmapi.Secrets {
|
|||||||
s.GivenToken = token
|
s.GivenToken = token
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGenerateToken(t *testing.T) {
|
||||||
|
var genTest = []struct {
|
||||||
|
s kubeadmapi.Secrets
|
||||||
|
l int
|
||||||
|
n int
|
||||||
|
}{
|
||||||
|
{kubeadmapi.Secrets{}, 2, 6},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rt := range genTest {
|
||||||
|
GenerateToken(&rt.s)
|
||||||
|
givenToken := strings.Split(strings.ToLower(rt.s.GivenToken), ".")
|
||||||
|
if len(givenToken) != rt.l {
|
||||||
|
t.Errorf(
|
||||||
|
"failed GenerateToken num parts:\n\texpected: %d\n\t actual: %d",
|
||||||
|
rt.l,
|
||||||
|
len(givenToken),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if len(givenToken[0]) != rt.n {
|
||||||
|
t.Errorf(
|
||||||
|
"failed GenerateToken first part length:\n\texpected: %d\n\t actual: %d",
|
||||||
|
rt.l,
|
||||||
|
len(givenToken),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUseGivenTokenIfValid(t *testing.T) {
|
||||||
|
var tokenTest = []struct {
|
||||||
|
s kubeadmapi.Secrets
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{kubeadmapi.Secrets{GivenToken: ""}, false}, // GivenToken == ""
|
||||||
|
{kubeadmapi.Secrets{GivenToken: "noperiod"}, false}, // not 2-part '.' format
|
||||||
|
{kubeadmapi.Secrets{GivenToken: "abcd.a"}, false}, // len(tokenID) != 6
|
||||||
|
{kubeadmapi.Secrets{GivenToken: "abcdef.a"}, true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rt := range tokenTest {
|
||||||
|
actual, _ := UseGivenTokenIfValid(&rt.s)
|
||||||
|
if actual != rt.expected {
|
||||||
|
t.Errorf(
|
||||||
|
"failed UseGivenTokenIfValid:\n\texpected: %t\n\t actual: %t\n\t token:%s",
|
||||||
|
rt.expected,
|
||||||
|
actual,
|
||||||
|
rt.s.GivenToken,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRandBytes(t *testing.T) {
|
||||||
|
var randTest = []struct {
|
||||||
|
r int
|
||||||
|
l int
|
||||||
|
expected error
|
||||||
|
}{
|
||||||
|
{0, 0, nil},
|
||||||
|
{1, 1, nil},
|
||||||
|
{2, 2, nil},
|
||||||
|
{3, 3, nil},
|
||||||
|
{100, 100, nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rt := range randTest {
|
||||||
|
actual, _, err := RandBytes(rt.r)
|
||||||
|
if err != rt.expected {
|
||||||
|
t.Errorf(
|
||||||
|
"failed RandBytes:\n\texpected: %s\n\t actual: %s",
|
||||||
|
rt.expected,
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if len(actual) != rt.l {
|
||||||
|
t.Errorf(
|
||||||
|
"failed RandBytes:\n\texpected: %d\n\t actual: %d\n",
|
||||||
|
rt.l,
|
||||||
|
len(actual),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user