2017-05-22 06:53:41 +00:00
|
|
|
/*
|
|
|
|
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 (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2017-11-10 18:36:12 +00:00
|
|
|
appsv1 "k8s.io/api/apps/v1"
|
|
|
|
appsv1beta1 "k8s.io/api/apps/v1beta1"
|
|
|
|
appsv1beta2 "k8s.io/api/apps/v1beta2"
|
|
|
|
batchv1 "k8s.io/api/batch/v1"
|
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
|
2017-05-22 06:53:41 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2017-10-04 22:42:54 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2018-05-14 14:51:51 +00:00
|
|
|
"k8s.io/apimachinery/pkg/runtime/serializer"
|
2017-11-10 18:36:12 +00:00
|
|
|
restclient "k8s.io/client-go/rest"
|
2017-05-22 06:53:41 +00:00
|
|
|
"k8s.io/client-go/rest/fake"
|
|
|
|
"k8s.io/kubernetes/pkg/api/testapi"
|
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2018-04-25 12:32:08 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
|
2018-05-10 12:20:27 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
|
2017-11-10 18:36:12 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/scheme"
|
2017-05-22 06:53:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const serviceAccount = "serviceaccount1"
|
|
|
|
const serviceAccountMissingErrString = "serviceaccount is required"
|
|
|
|
const 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>'`
|
|
|
|
|
2017-11-10 18:36:12 +00:00
|
|
|
func TestSetServiceAccountLocal(t *testing.T) {
|
2017-05-22 06:53:41 +00:00
|
|
|
inputs := []struct {
|
|
|
|
yaml string
|
|
|
|
apiGroup string
|
|
|
|
}{
|
2017-11-10 18:36:12 +00:00
|
|
|
{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"},
|
2017-05-22 06:53:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 04:01:51 +00:00
|
|
|
for i, input := range inputs {
|
|
|
|
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
|
2018-02-22 14:52:10 +00:00
|
|
|
tf := cmdtesting.NewTestFactory()
|
2018-03-08 22:23:55 +00:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
2017-11-14 04:01:51 +00:00
|
|
|
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
|
|
|
|
}),
|
|
|
|
}
|
2018-04-04 20:23:33 +00:00
|
|
|
|
|
|
|
outputFormat := "yaml"
|
|
|
|
|
2017-11-14 04:01:51 +00:00
|
|
|
tf.Namespace = "test"
|
2018-04-25 12:32:08 +00:00
|
|
|
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
|
|
|
cmd := NewCmdServiceAccount(tf, streams)
|
2018-04-04 20:23:33 +00:00
|
|
|
cmd.Flags().Set("output", outputFormat)
|
2017-11-14 04:01:51 +00:00
|
|
|
cmd.Flags().Set("local", "true")
|
|
|
|
testapi.Default = testapi.Groups[input.apiGroup]
|
2018-04-19 12:32:15 +00:00
|
|
|
saConfig := SetServiceAccountOptions{
|
2018-05-02 19:15:47 +00:00
|
|
|
PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme),
|
2018-04-04 20:23:33 +00:00
|
|
|
fileNameOptions: resource.FilenameOptions{
|
|
|
|
Filenames: []string{input.yaml}},
|
2018-04-25 12:32:08 +00:00
|
|
|
local: true,
|
|
|
|
IOStreams: streams,
|
|
|
|
}
|
2018-02-22 14:52:10 +00:00
|
|
|
err := saConfig.Complete(tf, cmd, []string{serviceAccount})
|
2017-11-14 04:01:51 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = saConfig.Run()
|
|
|
|
assert.NoError(t, err)
|
2018-04-25 12:32:08 +00:00
|
|
|
assert.Contains(t, buf.String(), "serviceAccountName: "+serviceAccount, fmt.Sprintf("serviceaccount not updated for %s", input.yaml))
|
2017-11-14 04:01:51 +00:00
|
|
|
})
|
2017-05-22 06:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 18:36:12 +00:00
|
|
|
func TestSetServiceAccountMultiLocal(t *testing.T) {
|
|
|
|
testapi.Default = testapi.Groups[""]
|
2018-02-22 14:52:10 +00:00
|
|
|
tf := cmdtesting.NewTestFactory()
|
2018-03-08 22:23:55 +00:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
2017-11-10 18:36:12 +00:00
|
|
|
tf.Client = &fake.RESTClient{
|
|
|
|
GroupVersion: schema.GroupVersion{Version: ""},
|
2018-05-14 14:51:51 +00:00
|
|
|
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
|
2017-11-10 18:36:12 +00:00
|
|
|
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.Namespace = "test"
|
2018-02-22 14:52:10 +00:00
|
|
|
tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
|
2017-11-10 18:36:12 +00:00
|
|
|
|
2018-04-04 20:23:33 +00:00
|
|
|
outputFormat := "name"
|
|
|
|
|
2018-04-25 12:32:08 +00:00
|
|
|
streams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
|
|
|
cmd := NewCmdServiceAccount(tf, streams)
|
2018-04-04 20:23:33 +00:00
|
|
|
cmd.Flags().Set("output", outputFormat)
|
2017-11-10 18:36:12 +00:00
|
|
|
cmd.Flags().Set("local", "true")
|
2018-04-19 12:32:15 +00:00
|
|
|
opts := SetServiceAccountOptions{
|
2018-05-02 19:15:47 +00:00
|
|
|
PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme),
|
2018-04-04 20:23:33 +00:00
|
|
|
fileNameOptions: resource.FilenameOptions{
|
|
|
|
Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}},
|
2018-04-25 12:32:08 +00:00
|
|
|
local: true,
|
|
|
|
IOStreams: streams,
|
|
|
|
}
|
2017-11-10 18:36:12 +00:00
|
|
|
|
2018-02-22 14:52:10 +00:00
|
|
|
err := opts.Complete(tf, cmd, []string{serviceAccount})
|
2017-11-10 18:36:12 +00:00
|
|
|
if err == nil {
|
|
|
|
err = opts.Run()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
2018-02-05 16:21:00 +00:00
|
|
|
expectedOut := "replicationcontroller/first-rc\nreplicationcontroller/second-rc\n"
|
2017-11-10 18:36:12 +00:00
|
|
|
if buf.String() != expectedOut {
|
|
|
|
t.Errorf("expected out:\n%s\nbut got:\n%s", expectedOut, buf.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetServiceAccountRemote(t *testing.T) {
|
2017-05-22 06:53:41 +00:00
|
|
|
inputs := []struct {
|
2017-11-10 18:36:12 +00:00
|
|
|
object runtime.Object
|
|
|
|
apiPrefix, apiGroup, apiVersion string
|
|
|
|
testAPIGroup string
|
|
|
|
args []string
|
2017-05-22 06:53:41 +00:00
|
|
|
}{
|
|
|
|
{
|
2017-11-10 18:36:12 +00:00
|
|
|
object: &extensionsv1beta1.ReplicaSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
|
|
|
testAPIGroup: "extensions",
|
|
|
|
apiPrefix: "/apis", apiGroup: "extensions", apiVersion: "v1beta1",
|
|
|
|
args: []string{"replicaset", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: &appsv1beta2.ReplicaSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
Spec: appsv1beta2.ReplicaSetSpec{
|
|
|
|
Template: v1.PodTemplateSpec{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: "nginx",
|
|
|
|
Image: "nginx",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
testAPIGroup: "extensions",
|
|
|
|
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta2",
|
|
|
|
args: []string{"replicaset", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: &appsv1.ReplicaSet{
|
2017-05-22 06:53:41 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
2017-11-10 18:36:12 +00:00
|
|
|
Spec: appsv1.ReplicaSetSpec{
|
|
|
|
Template: v1.PodTemplateSpec{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: "nginx",
|
|
|
|
Image: "nginx",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-05-22 06:53:41 +00:00
|
|
|
},
|
2017-11-10 18:36:12 +00:00
|
|
|
testAPIGroup: "extensions",
|
|
|
|
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1",
|
2017-05-22 06:53:41 +00:00
|
|
|
args: []string{"replicaset", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
2017-11-10 18:36:12 +00:00
|
|
|
object: &extensionsv1beta1.DaemonSet{
|
2017-05-22 06:53:41 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
2017-11-10 18:36:12 +00:00
|
|
|
testAPIGroup: "extensions",
|
|
|
|
apiPrefix: "/apis", apiGroup: "extensions", apiVersion: "v1beta1",
|
2017-05-22 06:53:41 +00:00
|
|
|
args: []string{"daemonset", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
2017-11-10 18:36:12 +00:00
|
|
|
object: &appsv1beta2.DaemonSet{
|
2017-05-22 06:53:41 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
2017-11-10 18:36:12 +00:00
|
|
|
testAPIGroup: "extensions",
|
|
|
|
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta2",
|
|
|
|
args: []string{"daemonset", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: &appsv1.DaemonSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
|
|
|
testAPIGroup: "extensions",
|
|
|
|
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1",
|
|
|
|
args: []string{"daemonset", "nginx", serviceAccount},
|
|
|
|
},
|
2017-05-22 06:53:41 +00:00
|
|
|
{
|
2017-11-10 18:36:12 +00:00
|
|
|
object: &extensionsv1beta1.Deployment{
|
2017-05-22 06:53:41 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
2017-11-10 18:36:12 +00:00
|
|
|
testAPIGroup: "extensions",
|
|
|
|
apiPrefix: "/apis", apiGroup: "extensions", apiVersion: "v1beta1",
|
2017-05-22 06:53:41 +00:00
|
|
|
args: []string{"deployment", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
2017-11-10 18:36:12 +00:00
|
|
|
object: &appsv1beta1.Deployment{
|
2017-05-22 06:53:41 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
2017-11-10 18:36:12 +00:00
|
|
|
testAPIGroup: "extensions",
|
|
|
|
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta1",
|
|
|
|
args: []string{"deployment", "nginx", serviceAccount},
|
2017-05-22 06:53:41 +00:00
|
|
|
},
|
|
|
|
{
|
2017-11-10 18:36:12 +00:00
|
|
|
object: &appsv1beta2.Deployment{
|
2017-05-22 06:53:41 +00:00
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
2017-11-10 18:36:12 +00:00
|
|
|
testAPIGroup: "extensions",
|
|
|
|
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta2",
|
|
|
|
args: []string{"deployment", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: &appsv1.Deployment{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
Spec: appsv1.DeploymentSpec{
|
|
|
|
Template: v1.PodTemplateSpec{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: "nginx",
|
|
|
|
Image: "nginx",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
testAPIGroup: "extensions",
|
|
|
|
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1",
|
|
|
|
args: []string{"deployment", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: &appsv1beta1.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
|
|
|
testAPIGroup: "apps",
|
|
|
|
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta1",
|
|
|
|
args: []string{"statefulset", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: &appsv1beta2.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
|
|
|
testAPIGroup: "apps",
|
|
|
|
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1beta2",
|
2017-05-22 06:53:41 +00:00
|
|
|
args: []string{"statefulset", "nginx", serviceAccount},
|
|
|
|
},
|
2017-11-10 18:36:12 +00:00
|
|
|
{
|
|
|
|
object: &appsv1.StatefulSet{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
Spec: appsv1.StatefulSetSpec{
|
|
|
|
Template: v1.PodTemplateSpec{
|
|
|
|
Spec: v1.PodSpec{
|
|
|
|
Containers: []v1.Container{
|
|
|
|
{
|
|
|
|
Name: "nginx",
|
|
|
|
Image: "nginx",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
testAPIGroup: "apps",
|
|
|
|
apiPrefix: "/apis", apiGroup: "apps", apiVersion: "v1",
|
|
|
|
args: []string{"statefulset", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: &batchv1.Job{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
|
|
|
testAPIGroup: "batch",
|
|
|
|
apiPrefix: "/apis", apiGroup: "batch", apiVersion: "v1",
|
|
|
|
args: []string{"job", "nginx", serviceAccount},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object: &v1.ReplicationController{
|
|
|
|
ObjectMeta: metav1.ObjectMeta{Name: "nginx"},
|
|
|
|
},
|
|
|
|
testAPIGroup: "",
|
|
|
|
apiPrefix: "/api", apiGroup: "", apiVersion: "v1",
|
|
|
|
args: []string{"replicationcontroller", "nginx", serviceAccount},
|
|
|
|
},
|
2017-05-22 06:53:41 +00:00
|
|
|
}
|
|
|
|
for _, input := range inputs {
|
2018-03-08 22:23:55 +00:00
|
|
|
t.Run(input.apiPrefix, func(t *testing.T) {
|
|
|
|
groupVersion := schema.GroupVersion{Group: input.apiGroup, Version: input.apiVersion}
|
|
|
|
testapi.Default = testapi.Groups[input.testAPIGroup]
|
|
|
|
tf := cmdtesting.NewTestFactory()
|
|
|
|
defer tf.Cleanup()
|
|
|
|
|
|
|
|
tf.Namespace = "test"
|
|
|
|
tf.Client = &fake.RESTClient{
|
|
|
|
GroupVersion: groupVersion,
|
2018-05-14 14:51:51 +00:00
|
|
|
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
|
2018-03-08 22:23:55 +00:00
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
resourcePath := testapi.Default.ResourcePath(input.args[0]+"s", tf.Namespace, input.args[1])
|
|
|
|
switch p, m := req.URL.Path, req.Method; {
|
|
|
|
case p == resourcePath && m == http.MethodGet:
|
2018-05-14 14:51:51 +00:00
|
|
|
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(input.object)}, nil
|
2018-03-08 22:23:55 +00:00
|
|
|
case p == resourcePath && 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))
|
2018-05-14 14:51:51 +00:00
|
|
|
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(input.object)}, nil
|
2018-03-08 22:23:55 +00:00
|
|
|
default:
|
|
|
|
t.Errorf("%s: unexpected request: %s %#v\n%#v", "serviceaccount", req.Method, req.URL, req)
|
|
|
|
return nil, fmt.Errorf("unexpected request")
|
2017-05-22 06:53:41 +00:00
|
|
|
}
|
2018-03-08 22:23:55 +00:00
|
|
|
}),
|
|
|
|
VersionedAPIPath: path.Join(input.apiPrefix, testapi.Default.GroupVersion().String()),
|
|
|
|
}
|
2018-04-04 20:23:33 +00:00
|
|
|
|
|
|
|
outputFormat := "yaml"
|
|
|
|
|
2018-04-25 12:32:08 +00:00
|
|
|
streams := genericclioptions.NewTestIOStreamsDiscard()
|
|
|
|
cmd := NewCmdServiceAccount(tf, streams)
|
2018-04-04 20:23:33 +00:00
|
|
|
cmd.Flags().Set("output", outputFormat)
|
2018-04-19 12:32:15 +00:00
|
|
|
saConfig := SetServiceAccountOptions{
|
2018-05-02 19:15:47 +00:00
|
|
|
PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme),
|
2018-04-04 20:23:33 +00:00
|
|
|
|
2018-04-25 12:32:08 +00:00
|
|
|
local: false,
|
|
|
|
IOStreams: streams,
|
|
|
|
}
|
2018-03-08 22:23:55 +00:00
|
|
|
err := saConfig.Complete(tf, cmd, input.args)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = saConfig.Run()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
2017-05-22 06:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceAccountValidation(t *testing.T) {
|
|
|
|
inputs := []struct {
|
2018-03-08 22:23:55 +00:00
|
|
|
name string
|
2017-05-22 06:53:41 +00:00
|
|
|
args []string
|
|
|
|
errorString string
|
|
|
|
}{
|
2018-03-08 22:23:55 +00:00
|
|
|
{name: "test service account missing", args: []string{}, errorString: serviceAccountMissingErrString},
|
|
|
|
{name: "test service account resource missing", args: []string{serviceAccount}, errorString: resourceMissingErrString},
|
2017-05-22 06:53:41 +00:00
|
|
|
}
|
|
|
|
for _, input := range inputs {
|
2018-03-08 22:23:55 +00:00
|
|
|
t.Run(input.name, func(t *testing.T) {
|
|
|
|
tf := cmdtesting.NewTestFactory()
|
|
|
|
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
|
|
|
|
}),
|
|
|
|
}
|
2018-04-04 20:23:33 +00:00
|
|
|
|
|
|
|
outputFormat := ""
|
|
|
|
|
2018-03-08 22:23:55 +00:00
|
|
|
tf.Namespace = "test"
|
2018-04-25 12:32:08 +00:00
|
|
|
streams := genericclioptions.NewTestIOStreamsDiscard()
|
|
|
|
cmd := NewCmdServiceAccount(tf, streams)
|
2017-05-22 06:53:41 +00:00
|
|
|
|
2018-04-19 12:32:15 +00:00
|
|
|
saConfig := &SetServiceAccountOptions{
|
2018-05-02 19:15:47 +00:00
|
|
|
PrintFlags: genericclioptions.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme),
|
2018-05-16 18:19:21 +00:00
|
|
|
IOStreams: streams,
|
2018-04-04 20:23:33 +00:00
|
|
|
}
|
2018-03-08 22:23:55 +00:00
|
|
|
err := saConfig.Complete(tf, cmd, input.args)
|
|
|
|
assert.EqualError(t, err, input.errorString)
|
|
|
|
})
|
2017-05-22 06:53:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:51:51 +00:00
|
|
|
func objBody(obj runtime.Object) io.ReadCloser {
|
|
|
|
return bytesBody([]byte(runtime.EncodeOrDie(scheme.DefaultJSONEncoder(), obj)))
|
2017-05-22 06:53:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultHeader() http.Header {
|
|
|
|
header := http.Header{}
|
|
|
|
header.Set("Content-Type", runtime.ContentTypeJSON)
|
|
|
|
return header
|
|
|
|
}
|
|
|
|
|
|
|
|
func bytesBody(bodyBytes []byte) io.ReadCloser {
|
|
|
|
return ioutil.NopCloser(bytes.NewReader(bodyBytes))
|
|
|
|
}
|