...

Source file src/k8s.io/kubectl/pkg/cmd/create/create_deployment_test.go

Documentation: k8s.io/kubectl/pkg/cmd/create

     1  /*
     2  Copyright 2016 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 create
    18  
    19  import (
    20  	"bytes"
    21  	"io"
    22  	"net/http"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  
    28  	"k8s.io/cli-runtime/pkg/genericclioptions"
    29  	"k8s.io/cli-runtime/pkg/genericiooptions"
    30  	restclient "k8s.io/client-go/rest"
    31  	"k8s.io/client-go/rest/fake"
    32  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    33  	cmdutil "k8s.io/kubectl/pkg/cmd/util"
    34  	"k8s.io/kubectl/pkg/scheme"
    35  )
    36  
    37  func TestCreateDeployment(t *testing.T) {
    38  	depName := "jonny-dep"
    39  	tf := cmdtesting.NewTestFactory().WithNamespace("test")
    40  	defer tf.Cleanup()
    41  
    42  	ns := scheme.Codecs.WithoutConversion()
    43  	fakeDiscovery := "{\"kind\":\"APIResourceList\",\"apiVersion\":\"v1\",\"groupVersion\":\"apps/v1\",\"resources\":[{\"name\":\"deployments\",\"singularName\":\"\",\"namespaced\":true,\"kind\":\"Deployment\",\"verbs\":[\"create\",\"delete\",\"deletecollection\",\"get\",\"list\",\"patch\",\"update\",\"watch\"],\"shortNames\":[\"deploy\"],\"categories\":[\"all\"]}]}"
    44  	tf.Client = &fake.RESTClient{
    45  		NegotiatedSerializer: ns,
    46  		Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    47  			return &http.Response{
    48  				StatusCode: http.StatusOK,
    49  				Body:       io.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))),
    50  			}, nil
    51  		}),
    52  	}
    53  	tf.ClientConfigVal = &restclient.Config{}
    54  
    55  	ioStreams, _, buf, _ := genericiooptions.NewTestIOStreams()
    56  	cmd := NewCmdCreateDeployment(tf, ioStreams)
    57  	cmd.Flags().Set("dry-run", "client")
    58  	cmd.Flags().Set("output", "name")
    59  	cmd.Flags().Set("image", "hollywood/jonny.depp:v2")
    60  	cmd.Run(cmd, []string{depName})
    61  	expectedOutput := "deployment.apps/" + depName + "\n"
    62  	if buf.String() != expectedOutput {
    63  		t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
    64  	}
    65  }
    66  
    67  func TestCreateDeploymentWithPort(t *testing.T) {
    68  	depName := "jonny-dep"
    69  	port := "5701"
    70  	tf := cmdtesting.NewTestFactory().WithNamespace("test")
    71  	defer tf.Cleanup()
    72  
    73  	ns := scheme.Codecs.WithoutConversion()
    74  	fakeDiscovery := "{\"kind\":\"APIResourceList\",\"apiVersion\":\"v1\",\"groupVersion\":\"apps/v1\",\"resources\":[{\"name\":\"deployments\",\"singularName\":\"\",\"namespaced\":true,\"kind\":\"Deployment\",\"verbs\":[\"create\",\"delete\",\"deletecollection\",\"get\",\"list\",\"patch\",\"update\",\"watch\"],\"shortNames\":[\"deploy\"],\"categories\":[\"all\"]}]}"
    75  	tf.Client = &fake.RESTClient{
    76  		NegotiatedSerializer: ns,
    77  		Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    78  			return &http.Response{
    79  				StatusCode: http.StatusOK,
    80  				Body:       io.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))),
    81  			}, nil
    82  		}),
    83  	}
    84  	tf.ClientConfigVal = &restclient.Config{}
    85  
    86  	ioStreams, _, buf, _ := genericiooptions.NewTestIOStreams()
    87  	cmd := NewCmdCreateDeployment(tf, ioStreams)
    88  	cmd.Flags().Set("dry-run", "client")
    89  	cmd.Flags().Set("output", "yaml")
    90  	cmd.Flags().Set("port", port)
    91  	cmd.Flags().Set("image", "hollywood/jonny.depp:v2")
    92  	cmd.Run(cmd, []string{depName})
    93  	if !strings.Contains(buf.String(), port) {
    94  		t.Errorf("unexpected output: %s\nexpected to contain: %s", buf.String(), port)
    95  	}
    96  }
    97  
    98  func TestCreateDeploymentWithReplicas(t *testing.T) {
    99  	depName := "jonny-dep"
   100  	replicas := "3"
   101  	tf := cmdtesting.NewTestFactory().WithNamespace("test")
   102  	defer tf.Cleanup()
   103  
   104  	ns := scheme.Codecs.WithoutConversion()
   105  	fakeDiscovery := "{\"kind\":\"APIResourceList\",\"apiVersion\":\"v1\",\"groupVersion\":\"apps/v1\",\"resources\":[{\"name\":\"deployments\",\"singularName\":\"\",\"namespaced\":true,\"kind\":\"Deployment\",\"verbs\":[\"create\",\"delete\",\"deletecollection\",\"get\",\"list\",\"patch\",\"update\",\"watch\"],\"shortNames\":[\"deploy\"],\"categories\":[\"all\"]}]}"
   106  	tf.Client = &fake.RESTClient{
   107  		NegotiatedSerializer: ns,
   108  		Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
   109  			return &http.Response{
   110  				StatusCode: http.StatusOK,
   111  				Body:       io.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))),
   112  			}, nil
   113  		}),
   114  	}
   115  	tf.ClientConfigVal = &restclient.Config{}
   116  
   117  	ioStreams, _, buf, _ := genericiooptions.NewTestIOStreams()
   118  	cmd := NewCmdCreateDeployment(tf, ioStreams)
   119  	cmd.Flags().Set("dry-run", "client")
   120  	cmd.Flags().Set("output", "jsonpath={.spec.replicas}")
   121  	cmd.Flags().Set("replicas", replicas)
   122  	cmd.Flags().Set("image", "hollywood/jonny.depp:v2")
   123  	cmd.Run(cmd, []string{depName})
   124  	if buf.String() != replicas {
   125  		t.Errorf("expected output: %s, but got: %s", replicas, buf.String())
   126  	}
   127  }
   128  
   129  func TestCreateDeploymentNoImage(t *testing.T) {
   130  	depName := "jonny-dep"
   131  	tf := cmdtesting.NewTestFactory().WithNamespace("test")
   132  	defer tf.Cleanup()
   133  
   134  	ns := scheme.Codecs.WithoutConversion()
   135  	fakeDiscovery := "{\"kind\":\"APIResourceList\",\"apiVersion\":\"v1\",\"groupVersion\":\"apps/v1\",\"resources\":[{\"name\":\"deployments\",\"singularName\":\"\",\"namespaced\":true,\"kind\":\"Deployment\",\"verbs\":[\"create\",\"delete\",\"deletecollection\",\"get\",\"list\",\"patch\",\"update\",\"watch\"],\"shortNames\":[\"deploy\"],\"categories\":[\"all\"]}]}"
   136  	tf.Client = &fake.RESTClient{
   137  		NegotiatedSerializer: ns,
   138  		Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
   139  			return &http.Response{
   140  				StatusCode: http.StatusOK,
   141  				Body:       io.NopCloser(bytes.NewBuffer([]byte(fakeDiscovery))),
   142  			}, nil
   143  		}),
   144  	}
   145  	tf.ClientConfigVal = &restclient.Config{}
   146  
   147  	ioStreams := genericiooptions.NewTestIOStreamsDiscard()
   148  	cmd := NewCmdCreateDeployment(tf, ioStreams)
   149  	cmd.Flags().Set("output", "name")
   150  	options := &CreateDeploymentOptions{
   151  		PrintFlags:     genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme),
   152  		DryRunStrategy: cmdutil.DryRunClient,
   153  		IOStreams:      ioStreams,
   154  	}
   155  
   156  	err := options.Complete(tf, cmd, []string{depName})
   157  	if err != nil {
   158  		t.Fatalf("unexpected error: %v", err)
   159  	}
   160  
   161  	err = options.Run()
   162  	assert.Error(t, err, "at least one image must be specified")
   163  }
   164  

View as plain text