...

Source file src/k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient/dryrunclient_test.go

Documentation: k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient

     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  	"io"
    22  	"testing"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	rbac "k8s.io/api/rbac/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/apimachinery/pkg/runtime/schema"
    28  	pkgversion "k8s.io/apimachinery/pkg/version"
    29  	fakediscovery "k8s.io/client-go/discovery/fake"
    30  	core "k8s.io/client-go/testing"
    31  )
    32  
    33  func TestLogDryRunAction(t *testing.T) {
    34  	var tests = []struct {
    35  		name          string
    36  		action        core.Action
    37  		expectedBytes []byte
    38  		buf           *bytes.Buffer
    39  	}{
    40  		{
    41  			name:   "action GET on services",
    42  			action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", "kubernetes"),
    43  			expectedBytes: []byte(`[dryrun] Would perform action GET on resource "services" in API group "core/v1"
    44  [dryrun] Resource name: "kubernetes"
    45  `),
    46  		},
    47  		{
    48  			name:   "action GET on clusterrolebindings",
    49  			action: core.NewRootGetAction(schema.GroupVersionResource{Group: rbac.GroupName, Version: rbac.SchemeGroupVersion.Version, Resource: "clusterrolebindings"}, "system:node"),
    50  			expectedBytes: []byte(`[dryrun] Would perform action GET on resource "clusterrolebindings" in API group "rbac.authorization.k8s.io/v1"
    51  [dryrun] Resource name: "system:node"
    52  `),
    53  		},
    54  		{
    55  			name:   "action LIST on services",
    56  			action: core.NewListAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, schema.GroupVersionKind{Version: "v1", Kind: "Service"}, "default", metav1.ListOptions{}),
    57  			expectedBytes: []byte(`[dryrun] Would perform action LIST on resource "services" in API group "core/v1"
    58  `),
    59  		},
    60  		{
    61  			name: "action CREATE on services",
    62  			action: core.NewCreateAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", &v1.Service{
    63  				ObjectMeta: metav1.ObjectMeta{
    64  					Name: "foo",
    65  				},
    66  				Spec: v1.ServiceSpec{
    67  					ClusterIP: "1.1.1.1",
    68  				},
    69  			}),
    70  			expectedBytes: []byte(`[dryrun] Would perform action CREATE on resource "services" in API group "core/v1"
    71  	apiVersion: v1
    72  	kind: Service
    73  	metadata:
    74  	  creationTimestamp: null
    75  	  name: foo
    76  	spec:
    77  	  clusterIP: 1.1.1.1
    78  	status:
    79  	  loadBalancer: {}
    80  `),
    81  		},
    82  		{
    83  			name:   "action PATCH on nodes",
    84  			action: core.NewPatchAction(schema.GroupVersionResource{Version: "v1", Resource: "nodes"}, "default", "my-node", "application/strategic-merge-patch+json", []byte(`{"spec":{"taints":[{"key": "foo", "value": "bar"}]}}`)),
    85  			expectedBytes: []byte(`[dryrun] Would perform action PATCH on resource "nodes" in API group "core/v1"
    86  [dryrun] Resource name: "my-node"
    87  [dryrun] Attached patch:
    88  	{"spec":{"taints":[{"key": "foo", "value": "bar"}]}}
    89  `),
    90  		},
    91  		{
    92  			name:   "action DELETE on pods",
    93  			action: core.NewDeleteAction(schema.GroupVersionResource{Version: "v1", Resource: "pods"}, "default", "my-pod"),
    94  			expectedBytes: []byte(`[dryrun] Would perform action DELETE on resource "pods" in API group "core/v1"
    95  [dryrun] Resource name: "my-pod"
    96  `),
    97  		},
    98  	}
    99  	for _, rt := range tests {
   100  		t.Run(rt.name, func(t *testing.T) {
   101  			rt.buf = bytes.NewBufferString("")
   102  			logDryRunAction(rt.action, rt.buf, DefaultMarshalFunc)
   103  			actualBytes := rt.buf.Bytes()
   104  
   105  			if !bytes.Equal(actualBytes, rt.expectedBytes) {
   106  				t.Errorf(
   107  					"failed LogDryRunAction:\n\texpected bytes: %q\n\t  actual: %q",
   108  					rt.expectedBytes,
   109  					actualBytes,
   110  				)
   111  			}
   112  		})
   113  	}
   114  }
   115  
   116  func TestDiscoveryServerVersion(t *testing.T) {
   117  	dryRunGetter := &InitDryRunGetter{
   118  		controlPlaneName: "controlPlane",
   119  		serviceSubnet:    "serviceSubnet",
   120  	}
   121  	c := NewDryRunClient(dryRunGetter, io.Discard)
   122  	fakeclientDiscovery, ok := c.Discovery().(*fakediscovery.FakeDiscovery)
   123  	if !ok {
   124  		t.Fatal("could not obtain FakeDiscovery from dry run client")
   125  	}
   126  	const gitVersion = "foo"
   127  	fakeclientDiscovery.FakedServerVersion = &pkgversion.Info{GitVersion: gitVersion}
   128  	ver, err := c.Discovery().ServerVersion()
   129  	if err != nil {
   130  		t.Fatalf("Get ServerVersion failed.: %v", err)
   131  	}
   132  	if ver.GitVersion != gitVersion {
   133  		t.Fatalf("GitVersion did not match, expected %s, got %s", gitVersion, ver.GitVersion)
   134  	}
   135  }
   136  

View as plain text