123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- /*
- Copyright 2017 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 set
- import (
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "testing"
- "github.com/stretchr/testify/assert"
- appsv1 "k8s.io/api/apps/v1"
- appsv1beta1 "k8s.io/api/apps/v1beta1"
- appsv1beta2 "k8s.io/api/apps/v1beta2"
- batchv1 "k8s.io/api/batch/v1"
- corev1 "k8s.io/api/core/v1"
- extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/runtime"
- "k8s.io/apimachinery/pkg/runtime/schema"
- "k8s.io/cli-runtime/pkg/genericclioptions"
- "k8s.io/cli-runtime/pkg/resource"
- restclient "k8s.io/client-go/rest"
- "k8s.io/client-go/rest/fake"
- cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
- "k8s.io/kubernetes/pkg/kubectl/scheme"
- )
- const (
- serviceAccount = "serviceaccount1"
- serviceAccountMissingErrString = "serviceaccount is required"
- resourceMissingErrString = `You must provide one or more resources by argument or filename.
- Example resource specifications include:
- '-f rsrc.yaml'
- '--filename=rsrc.json'
- '<resource> <name>'
- '<resource>'`
- )
- func TestSetServiceAccountLocal(t *testing.T) {
- inputs := []struct {
- yaml string
- apiGroup string
- }{
- {yaml: "../../../../test/fixtures/doc-yaml/user-guide/replication.yaml", apiGroup: ""},
- {yaml: "../../../../test/fixtures/doc-yaml/admin/daemon.yaml", apiGroup: "extensions"},
- {yaml: "../../../../test/fixtures/doc-yaml/user-guide/replicaset/redis-slave.yaml", apiGroup: "extensions"},
- {yaml: "../../../../test/fixtures/doc-yaml/user-guide/job.yaml", apiGroup: "batch"},
- {yaml: "../../../../test/fixtures/doc-yaml/user-guide/deployment.yaml", apiGroup: "extensions"},
- }
- for i, input := range inputs {
- t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.Client = &fake.RESTClient{
- GroupVersion: schema.GroupVersion{Version: "v1"},
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- t.Fatalf("unexpected request: %s %#v\n%#v", req.Method, req.URL, req)
- return nil, nil
- }),
- }
- outputFormat := "yaml"
- streams, _, buf, _ := genericclioptions.NewTestIOStreams()
- cmd := NewCmdServiceAccount(tf, streams)
- cmd.Flags().Set("output", outputFormat)
- cmd.Flags().Set("local", "true")
- saConfig := SetServiceAccountOptions{
- PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme),
- fileNameOptions: resource.FilenameOptions{
- Filenames: []string{input.yaml}},
- local: true,
- IOStreams: streams,
- }
- err := saConfig.Complete(tf, cmd, []string{serviceAccount})
- assert.NoError(t, err)
- err = saConfig.Run()
- assert.NoError(t, err)
- assert.Contains(t, buf.String(), "serviceAccountName: "+serviceAccount, fmt.Sprintf("serviceaccount not updated for %s", input.yaml))
- })
- }
- }
- func TestSetServiceAccountMultiLocal(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.Client = &fake.RESTClient{
- GroupVersion: schema.GroupVersion{Version: ""},
- NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- t.Fatalf("unexpected request: %s %#v\n%#v", req.Method, req.URL, req)
- return nil, nil
- }),
- }
- tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
- outputFormat := "name"
- streams, _, buf, _ := genericclioptions.NewTestIOStreams()
- cmd := NewCmdServiceAccount(tf, streams)
- cmd.Flags().Set("output", outputFormat)
- cmd.Flags().Set("local", "true")
- opts := SetServiceAccountOptions{
- PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme),
- fileNameOptions: resource.FilenameOptions{
- Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
- local: true,
- IOStreams: streams,
- }
- err := opts.Complete(tf, cmd, []string{serviceAccount})
- if err == nil {
- err = opts.Run()
- }
- if err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
- expectedOut := "replicationcontroller/first-rc\nreplicationcontroller/second-rc\n"
- if buf.String() != expectedOut {
- t.Errorf("expected out:\n%s\nbut got:\n%s", expectedOut, buf.String())
- }
- }
- func TestSetServiceAccountRemote(t *testing.T) {
- inputs := []struct {
- object runtime.Object
- groupVersion schema.GroupVersion
- path string
- args []string
- }{
- {
- object: &extensionsv1beta1.ReplicaSet{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: extensionsv1beta1.SchemeGroupVersion,
- path: "/namespaces/test/replicasets/nginx",
- args: []string{"replicaset", "nginx", serviceAccount},
- },
- {
- object: &appsv1beta2.ReplicaSet{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- Spec: appsv1beta2.ReplicaSetSpec{
- Template: corev1.PodTemplateSpec{
- Spec: corev1.PodSpec{
- Containers: []corev1.Container{
- {
- Name: "nginx",
- Image: "nginx",
- },
- },
- },
- },
- },
- },
- groupVersion: appsv1beta2.SchemeGroupVersion,
- path: "/namespaces/test/replicasets/nginx",
- args: []string{"replicaset", "nginx", serviceAccount},
- },
- {
- object: &appsv1.ReplicaSet{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- Spec: appsv1.ReplicaSetSpec{
- Template: corev1.PodTemplateSpec{
- Spec: corev1.PodSpec{
- Containers: []corev1.Container{
- {
- Name: "nginx",
- Image: "nginx",
- },
- },
- },
- },
- },
- },
- groupVersion: appsv1.SchemeGroupVersion,
- path: "/namespaces/test/replicasets/nginx",
- args: []string{"replicaset", "nginx", serviceAccount},
- },
- {
- object: &extensionsv1beta1.DaemonSet{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: extensionsv1beta1.SchemeGroupVersion,
- path: "/namespaces/test/daemonsets/nginx",
- args: []string{"daemonset", "nginx", serviceAccount},
- },
- {
- object: &appsv1beta2.DaemonSet{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: appsv1beta2.SchemeGroupVersion,
- path: "/namespaces/test/daemonsets/nginx",
- args: []string{"daemonset", "nginx", serviceAccount},
- },
- {
- object: &appsv1.DaemonSet{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: appsv1.SchemeGroupVersion,
- path: "/namespaces/test/daemonsets/nginx",
- args: []string{"daemonset", "nginx", serviceAccount},
- },
- {
- object: &extensionsv1beta1.Deployment{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: extensionsv1beta1.SchemeGroupVersion,
- path: "/namespaces/test/deployments/nginx",
- args: []string{"deployment", "nginx", serviceAccount},
- },
- {
- object: &appsv1beta1.Deployment{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: appsv1beta1.SchemeGroupVersion,
- path: "/namespaces/test/deployments/nginx",
- args: []string{"deployment", "nginx", serviceAccount},
- },
- {
- object: &appsv1beta2.Deployment{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: appsv1beta2.SchemeGroupVersion,
- path: "/namespaces/test/deployments/nginx",
- args: []string{"deployment", "nginx", serviceAccount},
- },
- {
- object: &appsv1.Deployment{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- Spec: appsv1.DeploymentSpec{
- Template: corev1.PodTemplateSpec{
- Spec: corev1.PodSpec{
- Containers: []corev1.Container{
- {
- Name: "nginx",
- Image: "nginx",
- },
- },
- },
- },
- },
- },
- groupVersion: appsv1.SchemeGroupVersion,
- path: "/namespaces/test/deployments/nginx",
- args: []string{"deployment", "nginx", serviceAccount},
- },
- {
- object: &appsv1beta1.StatefulSet{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: appsv1beta1.SchemeGroupVersion,
- path: "/namespaces/test/statefulsets/nginx",
- args: []string{"statefulset", "nginx", serviceAccount},
- },
- {
- object: &appsv1beta2.StatefulSet{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: appsv1beta2.SchemeGroupVersion,
- path: "/namespaces/test/statefulsets/nginx",
- args: []string{"statefulset", "nginx", serviceAccount},
- },
- {
- object: &appsv1.StatefulSet{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- Spec: appsv1.StatefulSetSpec{
- Template: corev1.PodTemplateSpec{
- Spec: corev1.PodSpec{
- Containers: []corev1.Container{
- {
- Name: "nginx",
- Image: "nginx",
- },
- },
- },
- },
- },
- },
- groupVersion: appsv1.SchemeGroupVersion,
- path: "/namespaces/test/statefulsets/nginx",
- args: []string{"statefulset", "nginx", serviceAccount},
- },
- {
- object: &batchv1.Job{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: batchv1.SchemeGroupVersion,
- path: "/namespaces/test/jobs/nginx",
- args: []string{"job", "nginx", serviceAccount},
- },
- {
- object: &corev1.ReplicationController{
- ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
- },
- groupVersion: corev1.SchemeGroupVersion,
- path: "/namespaces/test/replicationcontrollers/nginx",
- args: []string{"replicationcontroller", "nginx", serviceAccount},
- },
- }
- for i, input := range inputs {
- t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.Client = &fake.RESTClient{
- GroupVersion: input.groupVersion,
- NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- switch p, m := req.URL.Path, req.Method; {
- case p == input.path && m == http.MethodGet:
- return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: objBody(input.object)}, nil
- case p == input.path && m == http.MethodPatch:
- stream, err := req.GetBody()
- if err != nil {
- return nil, err
- }
- bytes, err := ioutil.ReadAll(stream)
- if err != nil {
- return nil, err
- }
- assert.Contains(t, string(bytes), `"serviceAccountName":`+`"`+serviceAccount+`"`, fmt.Sprintf("serviceaccount not updated for %#v", input.object))
- return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: objBody(input.object)}, nil
- default:
- t.Errorf("%s: unexpected request: %s %#v\n%#v", "serviceaccount", req.Method, req.URL, req)
- return nil, fmt.Errorf("unexpected request")
- }
- }),
- }
- outputFormat := "yaml"
- streams := genericclioptions.NewTestIOStreamsDiscard()
- cmd := NewCmdServiceAccount(tf, streams)
- cmd.Flags().Set("output", outputFormat)
- saConfig := SetServiceAccountOptions{
- PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme),
- local: false,
- IOStreams: streams,
- }
- err := saConfig.Complete(tf, cmd, input.args)
- assert.NoError(t, err)
- err = saConfig.Run()
- assert.NoError(t, err)
- })
- }
- }
- func TestServiceAccountValidation(t *testing.T) {
- inputs := []struct {
- name string
- args []string
- errorString string
- }{
- {name: "test service account missing", args: []string{}, errorString: serviceAccountMissingErrString},
- {name: "test service account resource missing", args: []string{serviceAccount}, errorString: resourceMissingErrString},
- }
- for _, input := range inputs {
- t.Run(input.name, func(t *testing.T) {
- tf := cmdtesting.NewTestFactory().WithNamespace("test")
- defer tf.Cleanup()
- tf.Client = &fake.RESTClient{
- GroupVersion: schema.GroupVersion{Version: "v1"},
- Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
- t.Fatalf("unexpected request: %s %#v\n%#v", req.Method, req.URL, req)
- return nil, nil
- }),
- }
- outputFormat := ""
- streams := genericclioptions.NewTestIOStreamsDiscard()
- cmd := NewCmdServiceAccount(tf, streams)
- saConfig := &SetServiceAccountOptions{
- PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme),
- IOStreams: streams,
- }
- err := saConfig.Complete(tf, cmd, input.args)
- assert.EqualError(t, err, input.errorString)
- })
- }
- }
- func objBody(obj runtime.Object) io.ReadCloser {
- return cmdtesting.BytesBody([]byte(runtime.EncodeOrDie(scheme.DefaultJSONEncoder(), obj)))
- }
|