1/*
2Copyright 2018 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package rollout
18
19import (
20	"bytes"
21	"io/ioutil"
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/genericclioptions"
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
36var rolloutPauseGroupVersionEncoder = schema.GroupVersion{Group: "apps", Version: "v1"}
37var rolloutPauseGroupVersionDecoder = schema.GroupVersion{Group: "apps", Version: "v1"}
38
39func TestRolloutPause(t *testing.T) {
40	deploymentName := "deployment/nginx-deployment"
41	ns := scheme.Codecs.WithoutConversion()
42	tf := cmdtesting.NewTestFactory().WithNamespace("test")
43
44	info, _ := runtime.SerializerInfoForMediaType(ns.SupportedMediaTypes(), runtime.ContentTypeJSON)
45	encoder := ns.EncoderForVersion(info.Serializer, rolloutPauseGroupVersionEncoder)
46	tf.Client = &RolloutPauseRESTClient{
47		RESTClient: &fake.RESTClient{
48			GroupVersion:         rolloutPauseGroupVersionEncoder,
49			NegotiatedSerializer: ns,
50			Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
51				switch p, m := req.URL.Path, req.Method; {
52				case p == "/namespaces/test/deployments/nginx-deployment" && (m == "GET" || m == "PATCH"):
53					responseDeployment := &appsv1.Deployment{}
54					responseDeployment.Name = deploymentName
55					body := ioutil.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(encoder, responseDeployment))))
56					return &http.Response{StatusCode: http.StatusOK, Header: cmdtesting.DefaultHeader(), Body: body}, nil
57				default:
58					t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
59					return nil, nil
60				}
61			}),
62		},
63	}
64
65	streams, _, buf, _ := genericclioptions.NewTestIOStreams()
66	cmd := NewCmdRolloutPause(tf, streams)
67
68	cmd.Run(cmd, []string{deploymentName})
69	expectedOutput := "deployment.apps/" + deploymentName + " paused\n"
70	if buf.String() != expectedOutput {
71		t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
72	}
73}
74
75type RolloutPauseRESTClient struct {
76	*fake.RESTClient
77}
78
79func (c *RolloutPauseRESTClient) Get() *restclient.Request {
80	return c.RESTClient.Verb("GET")
81}
82
83func (c *RolloutPauseRESTClient) Patch(pt types.PatchType) *restclient.Request {
84	return c.RESTClient.Verb("PATCH")
85}
86