Merge pull request #119562 from my-git9/proxyut
kubeadm: increase ut for cmd/kubeadm/app/phases/addons/proxy
This commit is contained in:
		| @@ -17,12 +17,18 @@ limitations under the License. | |||||||
| package proxy | package proxy | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
|  | 	"bytes" | ||||||
|  | 	"context" | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"testing" | 	"testing" | ||||||
|  |  | ||||||
|  | 	"github.com/lithammer/dedent" | ||||||
|  |  | ||||||
| 	apps "k8s.io/api/apps/v1" | 	apps "k8s.io/api/apps/v1" | ||||||
|  | 	v1 "k8s.io/api/core/v1" | ||||||
| 	apierrors "k8s.io/apimachinery/pkg/api/errors" | 	apierrors "k8s.io/apimachinery/pkg/api/errors" | ||||||
|  | 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||||
| 	"k8s.io/apimachinery/pkg/runtime" | 	"k8s.io/apimachinery/pkg/runtime" | ||||||
| 	clientsetfake "k8s.io/client-go/kubernetes/fake" | 	clientsetfake "k8s.io/client-go/kubernetes/fake" | ||||||
| 	clientsetscheme "k8s.io/client-go/kubernetes/scheme" | 	clientsetscheme "k8s.io/client-go/kubernetes/scheme" | ||||||
| @@ -192,3 +198,119 @@ func TestDaemonSetsHaveSystemNodeCriticalPriorityClassName(t *testing.T) { | |||||||
| 		}) | 		}) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | func TestPrintOrCreateKubeProxyObjects(t *testing.T) { | ||||||
|  | 	tests := []struct { | ||||||
|  | 		name          string | ||||||
|  | 		printManifest bool | ||||||
|  | 		wantOut       string | ||||||
|  | 		wantErr       bool | ||||||
|  | 	}{ | ||||||
|  | 		{ | ||||||
|  | 			name:          "do not print manifest", | ||||||
|  | 			printManifest: false, | ||||||
|  | 			wantOut:       "[addons] Applied essential addon: kube-proxy\n", | ||||||
|  | 			wantErr:       false, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			name:          "print manifest", | ||||||
|  | 			printManifest: true, | ||||||
|  | 			wantOut: dedent.Dedent(`--- | ||||||
|  | apiVersion: v1 | ||||||
|  | kind: ServiceAccount | ||||||
|  | metadata: | ||||||
|  |   creationTimestamp: null | ||||||
|  |   name: kube-proxy | ||||||
|  |   namespace: kube-system | ||||||
|  | --- | ||||||
|  | apiVersion: rbac.authorization.k8s.io/v1 | ||||||
|  | kind: ClusterRoleBinding | ||||||
|  | metadata: | ||||||
|  |   creationTimestamp: null | ||||||
|  |   name: kubeadm:node-proxier | ||||||
|  | roleRef: | ||||||
|  |   apiGroup: rbac.authorization.k8s.io | ||||||
|  |   kind: ClusterRole | ||||||
|  |   name: system:node-proxier | ||||||
|  | subjects: | ||||||
|  | - kind: ServiceAccount | ||||||
|  |   name: kube-proxy | ||||||
|  |   namespace: kube-system | ||||||
|  | --- | ||||||
|  | apiVersion: rbac.authorization.k8s.io/v1 | ||||||
|  | kind: Role | ||||||
|  | metadata: | ||||||
|  |   creationTimestamp: null | ||||||
|  |   name: kube-proxy | ||||||
|  |   namespace: kube-system | ||||||
|  | rules: | ||||||
|  | - apiGroups: | ||||||
|  |   - "" | ||||||
|  |   resourceNames: | ||||||
|  |   - kube-proxy | ||||||
|  |   resources: | ||||||
|  |   - configmaps | ||||||
|  |   verbs: | ||||||
|  |   - get | ||||||
|  | --- | ||||||
|  | apiVersion: rbac.authorization.k8s.io/v1 | ||||||
|  | kind: RoleBinding | ||||||
|  | metadata: | ||||||
|  |   creationTimestamp: null | ||||||
|  |   name: kube-proxy | ||||||
|  |   namespace: kube-system | ||||||
|  | roleRef: | ||||||
|  |   apiGroup: rbac.authorization.k8s.io | ||||||
|  |   kind: Role | ||||||
|  |   name: kube-proxy | ||||||
|  | subjects: | ||||||
|  | - kind: Group | ||||||
|  |   name: system:bootstrappers:kubeadm:default-node-token | ||||||
|  | --- | ||||||
|  | foo | ||||||
|  | --- | ||||||
|  | bar | ||||||
|  | `), | ||||||
|  | 			wantErr: false, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 	for _, tt := range tests { | ||||||
|  | 		t.Run(tt.name, func(t *testing.T) { | ||||||
|  | 			out := &bytes.Buffer{} | ||||||
|  | 			client := newMockClientForTest(t) | ||||||
|  | 			cmByte := []byte{'\n', 'f', 'o', 'o', '\n'} | ||||||
|  | 			dsByte := []byte{'\n', 'b', 'a', 'r', '\n'} | ||||||
|  | 			if err := printOrCreateKubeProxyObjects(cmByte, dsByte, client, out, tt.printManifest); (err != nil) != tt.wantErr { | ||||||
|  | 				t.Fatalf("printOrCreateKubeProxyObjects() error = %v, wantErr %v", err, tt.wantErr) | ||||||
|  | 			} | ||||||
|  | 			if gotOut := out.String(); gotOut != tt.wantOut { | ||||||
|  | 				t.Fatalf("printOrCreateKubeProxyObjects() = %v, want %v", gotOut, tt.wantOut) | ||||||
|  | 			} | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func newMockClientForTest(t *testing.T) *clientsetfake.Clientset { | ||||||
|  | 	client := clientsetfake.NewSimpleClientset() | ||||||
|  | 	_, err := client.AppsV1().DaemonSets(metav1.NamespaceSystem).Create(context.TODO(), &apps.DaemonSet{ | ||||||
|  | 		TypeMeta: metav1.TypeMeta{ | ||||||
|  | 			Kind:       "DaemonSet", | ||||||
|  | 			APIVersion: "apps/v1", | ||||||
|  | 		}, | ||||||
|  | 		ObjectMeta: metav1.ObjectMeta{ | ||||||
|  | 			Name:      "kube-proxy", | ||||||
|  | 			Namespace: metav1.NamespaceSystem, | ||||||
|  | 			Labels: map[string]string{ | ||||||
|  | 				"k8s-app": "kube-proxy", | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		Spec: apps.DaemonSetSpec{ | ||||||
|  | 			Template: v1.PodTemplateSpec{}, | ||||||
|  | 		}, | ||||||
|  | 	}, metav1.CreateOptions{}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("error creating Daemonset: %v", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return client | ||||||
|  | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Kubernetes Prow Robot
					Kubernetes Prow Robot