...

Source file src/k8s.io/kubectl/pkg/cmd/rollout/rollout_pause_test.go

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

     1  /*
     2  Copyright 2018 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 rollout
    18  
    19  import (
    20  	"bytes"
    21  	"io"
    22  	"net/http"
    23  	"testing"
    24  
    25  	appsv1 "k8s.io/api/apps/v1"
    26  	"k8s.io/apimachinery/pkg/runtime"
    27  	"k8s.io/apimachinery/pkg/runtime/schema"
    28  	"k8s.io/apimachinery/pkg/types"
    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  	"k8s.io/kubectl/pkg/scheme"
    34  )
    35  
    36  var rolloutPauseGroupVersionEncoder = schema.GroupVersion{Group: "apps", Version: "v1"}
    37  
    38  func TestRolloutPause(t *testing.T) {
    39  	deploymentName := "deployment/nginx-deployment"
    40  	ns := scheme.Codecs.WithoutConversion()
    41  	tf := cmdtesting.NewTestFactory().WithNamespace("test")
    42  
    43  	info, _ := runtime.SerializerInfoForMediaType(ns.SupportedMediaTypes(), runtime.ContentTypeJSON)
    44  	encoder := ns.EncoderForVersion(info.Serializer, rolloutPauseGroupVersionEncoder)
    45  	tf.Client = &RolloutPauseRESTClient{
    46  		RESTClient: &fake.RESTClient{
    47  			GroupVersion:         rolloutPauseGroupVersionEncoder,
    48  			NegotiatedSerializer: ns,
    49  			Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    50  				switch p, m := req.URL.Path, req.Method; {
    51  				case p == "/namespaces/test/deployments/nginx-deployment" && (m == "GET" || m == "PATCH"):
    52  					responseDeployment := &appsv1.Deployment{}
    53  					responseDeployment.Name = deploymentName
    54  					body := io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment))))
    55  					return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil
    56  				default:
    57  					t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
    58  					return nil, nil
    59  				}
    60  			}),
    61  		},
    62  	}
    63  
    64  	streams, _, buf, _ := genericiooptions.NewTestIOStreams()
    65  	cmd := NewCmdRolloutPause(tf, streams)
    66  
    67  	cmd.Run(cmd, []string{deploymentName})
    68  	expectedOutput := "deployment.apps/" + deploymentName + " paused\n"
    69  	if buf.String() != expectedOutput {
    70  		t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
    71  	}
    72  }
    73  
    74  type RolloutPauseRESTClient struct {
    75  	*fake.RESTClient
    76  }
    77  
    78  func (c *RolloutPauseRESTClient) Get() *restclient.Request {
    79  	return c.RESTClient.Verb("GET")
    80  }
    81  
    82  func (c *RolloutPauseRESTClient) Patch(pt types.PatchType) *restclient.Request {
    83  	return c.RESTClient.Verb("PATCH")
    84  }
    85  

View as plain text