k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cmd/kubeadm/app/util/apiclient/init_dryrun_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package apiclient 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "testing" 23 24 rbac "k8s.io/api/rbac/v1" 25 "k8s.io/apimachinery/pkg/runtime/schema" 26 core "k8s.io/client-go/testing" 27 ) 28 29 func TestHandleGetAction(t *testing.T) { 30 controlPlaneName := "control-plane-foo" 31 serviceSubnet := "10.96.0.1/12" 32 idr := NewInitDryRunGetter(controlPlaneName, serviceSubnet) 33 34 var tests = []struct { 35 name string 36 action core.GetActionImpl 37 expectedHandled bool 38 expectedObjectJSON []byte 39 expectedErr bool 40 }{ 41 { 42 name: "get default services", 43 action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", "kubernetes"), 44 expectedHandled: true, 45 expectedObjectJSON: []byte(`{"metadata":{"name":"kubernetes","namespace":"default","creationTimestamp":null,"labels":{"component":"apiserver","provider":"kubernetes"}},"spec":{"ports":[{"name":"https","port":443,"targetPort":6443}],"clusterIP":"10.96.0.1"},"status":{"loadBalancer":{}}}`), 46 expectedErr: false, 47 }, 48 { 49 name: "get nodes", 50 action: core.NewRootGetAction(schema.GroupVersionResource{Version: "v1", Resource: "nodes"}, controlPlaneName), 51 expectedHandled: true, 52 expectedObjectJSON: []byte(`{"metadata":{"name":"control-plane-foo","creationTimestamp":null,"labels":{"kubernetes.io/hostname":"control-plane-foo"}},"spec":{},"status":{"daemonEndpoints":{"kubeletEndpoint":{"Port":0}},"nodeInfo":{"machineID":"","systemUUID":"","bootID":"","kernelVersion":"","osImage":"","containerRuntimeVersion":"","kubeletVersion":"","kubeProxyVersion":"","operatingSystem":"","architecture":""}}}`), 53 expectedErr: false, 54 }, 55 { 56 name: "get clusterrolebinings", 57 action: core.NewRootGetAction(schema.GroupVersionResource{Group: rbac.GroupName, Version: rbac.SchemeGroupVersion.Version, Resource: "clusterrolebindings"}, "system:node"), 58 expectedHandled: true, 59 expectedObjectJSON: []byte(``), 60 expectedErr: true, // we expect a NotFound error here 61 }, 62 { // an ask for a kubernetes service in the _kube-system_ ns should not be answered 63 name: "get kube-system services", 64 action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "kube-system", "kubernetes"), 65 expectedHandled: false, 66 expectedObjectJSON: []byte(``), 67 expectedErr: false, 68 }, 69 { // an ask for an other service than kubernetes should not be answered 70 name: "get default my-other-service", 71 action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", "my-other-service"), 72 expectedHandled: false, 73 expectedObjectJSON: []byte(``), 74 expectedErr: false, 75 }, 76 { // an ask for an other node than the control-plane should not be answered 77 name: "get other-node", 78 action: core.NewRootGetAction(schema.GroupVersionResource{Version: "v1", Resource: "nodes"}, "other-node"), 79 expectedHandled: false, 80 expectedObjectJSON: []byte(``), 81 expectedErr: false, 82 }, 83 } 84 for _, rt := range tests { 85 t.Run(rt.name, func(t *testing.T) { 86 handled, obj, actualErr := idr.HandleGetAction(rt.action) 87 objBytes := []byte(``) 88 if obj != nil { 89 var err error 90 objBytes, err = json.Marshal(obj) 91 if err != nil { 92 t.Fatalf("couldn't marshal returned object") 93 } 94 } 95 96 if handled != rt.expectedHandled { 97 t.Errorf( 98 "failed HandleGetAction:\n\texpected handled: %t\n\t actual: %t %v", 99 rt.expectedHandled, 100 handled, 101 rt.action, 102 ) 103 } 104 105 if !bytes.Equal(objBytes, rt.expectedObjectJSON) { 106 t.Errorf( 107 "failed HandleGetAction:\n\texpected object: %q\n\t actual: %q", 108 rt.expectedObjectJSON, 109 objBytes, 110 ) 111 } 112 113 if (actualErr != nil) != rt.expectedErr { 114 t.Errorf( 115 "failed HandleGetAction:\n\texpected error: %t\n\t actual: %t %v", 116 rt.expectedErr, 117 (actualErr != nil), 118 rt.action, 119 ) 120 } 121 }) 122 } 123 }