mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 11:50:44 +00:00
Update kubeadm test TestMarkControlPlane
- Use a dummy nodename instead of OS hostname - Inline toString() function - Use backticks to wrap expected patch - Remove redundant test name from error logs
This commit is contained in:
parent
7ba332a839
commit
d7f769be3e
@ -12,7 +12,6 @@ go_test(
|
|||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
deps = [
|
deps = [
|
||||||
"//cmd/kubeadm/app/constants:go_default_library",
|
"//cmd/kubeadm/app/constants:go_default_library",
|
||||||
"//cmd/kubeadm/app/util:go_default_library",
|
|
||||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||||
|
@ -19,17 +19,15 @@ package markcontrolplane
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
clientset "k8s.io/client-go/kubernetes"
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
|
||||||
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMarkControlPlane(t *testing.T) {
|
func TestMarkControlPlane(t *testing.T) {
|
||||||
@ -46,77 +44,74 @@ func TestMarkControlPlane(t *testing.T) {
|
|||||||
expectedPatch string
|
expectedPatch string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"control-plane label and taint missing",
|
name: "control-plane label and taint missing",
|
||||||
"",
|
existingLabel: "",
|
||||||
nil,
|
existingTaints: nil,
|
||||||
[]v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
newTaints: []v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
||||||
"{\"metadata\":{\"labels\":{\"node-role.kubernetes.io/master\":\"\"}},\"spec\":{\"taints\":[{\"effect\":\"NoSchedule\",\"key\":\"node-role.kubernetes.io/master\"}]}}",
|
expectedPatch: `{"metadata":{"labels":{"node-role.kubernetes.io/master":""}},"spec":{"taints":[{"effect":"NoSchedule","key":"node-role.kubernetes.io/master"}]}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"control-plane label and taint missing but taint not wanted",
|
name: "control-plane label and taint missing but taint not wanted",
|
||||||
"",
|
existingLabel: "",
|
||||||
nil,
|
existingTaints: nil,
|
||||||
nil,
|
newTaints: nil,
|
||||||
"{\"metadata\":{\"labels\":{\"node-role.kubernetes.io/master\":\"\"}}}",
|
expectedPatch: `{"metadata":{"labels":{"node-role.kubernetes.io/master":""}}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"control-plane label missing",
|
name: "control-plane label missing",
|
||||||
"",
|
existingLabel: "",
|
||||||
[]v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
existingTaints: []v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
||||||
[]v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
newTaints: []v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
||||||
"{\"metadata\":{\"labels\":{\"node-role.kubernetes.io/master\":\"\"}}}",
|
expectedPatch: `{"metadata":{"labels":{"node-role.kubernetes.io/master":""}}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"control-plane taint missing",
|
name: "control-plane taint missing",
|
||||||
kubeadmconstants.LabelNodeRoleMaster,
|
existingLabel: kubeadmconstants.LabelNodeRoleMaster,
|
||||||
nil,
|
existingTaints: nil,
|
||||||
[]v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
newTaints: []v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
||||||
"{\"spec\":{\"taints\":[{\"effect\":\"NoSchedule\",\"key\":\"node-role.kubernetes.io/master\"}]}}",
|
expectedPatch: `{"spec":{"taints":[{"effect":"NoSchedule","key":"node-role.kubernetes.io/master"}]}}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"nothing missing",
|
name: "nothing missing",
|
||||||
kubeadmconstants.LabelNodeRoleMaster,
|
existingLabel: kubeadmconstants.LabelNodeRoleMaster,
|
||||||
[]v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
existingTaints: []v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
||||||
[]v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
newTaints: []v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
||||||
"{}",
|
expectedPatch: `{}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"has taint and no new taints wanted",
|
name: "has taint and no new taints wanted",
|
||||||
kubeadmconstants.LabelNodeRoleMaster,
|
existingLabel: kubeadmconstants.LabelNodeRoleMaster,
|
||||||
[]v1.Taint{
|
existingTaints: []v1.Taint{
|
||||||
{
|
{
|
||||||
Key: "node.cloudprovider.kubernetes.io/uninitialized",
|
Key: "node.cloudprovider.kubernetes.io/uninitialized",
|
||||||
Effect: v1.TaintEffectNoSchedule,
|
Effect: v1.TaintEffectNoSchedule,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
nil,
|
newTaints: nil,
|
||||||
"{}",
|
expectedPatch: `{}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"has taint and should merge with wanted taint",
|
name: "has taint and should merge with wanted taint",
|
||||||
kubeadmconstants.LabelNodeRoleMaster,
|
existingLabel: kubeadmconstants.LabelNodeRoleMaster,
|
||||||
[]v1.Taint{
|
existingTaints: []v1.Taint{
|
||||||
{
|
{
|
||||||
Key: "node.cloudprovider.kubernetes.io/uninitialized",
|
Key: "node.cloudprovider.kubernetes.io/uninitialized",
|
||||||
Effect: v1.TaintEffectNoSchedule,
|
Effect: v1.TaintEffectNoSchedule,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
[]v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
newTaints: []v1.Taint{kubeadmconstants.ControlPlaneTaint},
|
||||||
"{\"spec\":{\"taints\":[{\"effect\":\"NoSchedule\",\"key\":\"node-role.kubernetes.io/master\"},{\"effect\":\"NoSchedule\",\"key\":\"node.cloudprovider.kubernetes.io/uninitialized\"}]}}",
|
expectedPatch: `{"spec":{"taints":[{"effect":"NoSchedule","key":"node-role.kubernetes.io/master"},{"effect":"NoSchedule","key":"node.cloudprovider.kubernetes.io/uninitialized"}]}}`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
hostname, err := kubeadmutil.GetHostname("")
|
nodename := "node01"
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("MarkControlPlane(%s): unexpected error: %v", tc.name, err)
|
|
||||||
}
|
|
||||||
controlPlaneNode := &v1.Node{
|
controlPlaneNode := &v1.Node{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: hostname,
|
Name: nodename,
|
||||||
Labels: map[string]string{
|
Labels: map[string]string{
|
||||||
v1.LabelHostname: hostname,
|
v1.LabelHostname: nodename,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -131,15 +126,15 @@ func TestMarkControlPlane(t *testing.T) {
|
|||||||
|
|
||||||
jsonNode, err := json.Marshal(controlPlaneNode)
|
jsonNode, err := json.Marshal(controlPlaneNode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("MarkControlPlane(%s): unexpected encoding error: %v", tc.name, err)
|
t.Fatalf("unexpected encoding error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var patchRequest string
|
var patchRequest string
|
||||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
if req.URL.Path != "/api/v1/nodes/"+hostname {
|
if req.URL.Path != "/api/v1/nodes/"+nodename {
|
||||||
t.Errorf("MarkControlPlane(%s): request for unexpected HTTP resource: %v", tc.name, req.URL.Path)
|
t.Errorf("request for unexpected HTTP resource: %v", req.URL.Path)
|
||||||
http.Error(w, "", http.StatusNotFound)
|
http.Error(w, "", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -147,9 +142,11 @@ func TestMarkControlPlane(t *testing.T) {
|
|||||||
switch req.Method {
|
switch req.Method {
|
||||||
case "GET":
|
case "GET":
|
||||||
case "PATCH":
|
case "PATCH":
|
||||||
patchRequest = toString(req.Body)
|
buf := new(bytes.Buffer)
|
||||||
|
buf.ReadFrom(req.Body)
|
||||||
|
patchRequest = buf.String()
|
||||||
default:
|
default:
|
||||||
t.Errorf("MarkControlPlane(%s): request for unexpected HTTP verb: %v", tc.name, req.Method)
|
t.Errorf("request for unexpected HTTP verb: %v", req.Method)
|
||||||
http.Error(w, "", http.StatusNotFound)
|
http.Error(w, "", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -161,22 +158,16 @@ func TestMarkControlPlane(t *testing.T) {
|
|||||||
|
|
||||||
cs, err := clientset.NewForConfig(&restclient.Config{Host: s.URL})
|
cs, err := clientset.NewForConfig(&restclient.Config{Host: s.URL})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("MarkControlPlane(%s): unexpected error building clientset: %v", tc.name, err)
|
t.Fatalf("unexpected error building clientset: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := MarkControlPlane(cs, hostname, tc.newTaints); err != nil {
|
if err := MarkControlPlane(cs, nodename, tc.newTaints); err != nil {
|
||||||
t.Errorf("MarkControlPlane(%s) returned unexpected error: %v", tc.name, err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if tc.expectedPatch != patchRequest {
|
if tc.expectedPatch != patchRequest {
|
||||||
t.Errorf("MarkControlPlane(%s) wanted patch %v, got %v", tc.name, tc.expectedPatch, patchRequest)
|
t.Errorf("unexpected error: wanted patch %v, got %v", tc.expectedPatch, patchRequest)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func toString(r io.Reader) string {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
buf.ReadFrom(r)
|
|
||||||
return buf.String()
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user