...

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

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

     1  /*
     2  Copyright 2017 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  	"testing"
    24  
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	"k8s.io/cli-runtime/pkg/genericclioptions"
    27  	"k8s.io/cli-runtime/pkg/genericiooptions"
    28  	restclient "k8s.io/client-go/rest"
    29  	"k8s.io/client-go/rest/fake"
    30  	cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
    31  	"k8s.io/kubectl/pkg/scheme"
    32  )
    33  
    34  func TestCreatePriorityClass(t *testing.T) {
    35  	pcName := "my-pc"
    36  	tf := cmdtesting.NewTestFactory()
    37  	defer tf.Cleanup()
    38  
    39  	ns := scheme.Codecs.WithoutConversion()
    40  
    41  	tf.Client = &fake.RESTClient{
    42  		GroupVersion:         schema.GroupVersion{Group: "scheduling.k8s.io", Version: "v1beta1"},
    43  		NegotiatedSerializer: ns,
    44  		Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
    45  			return &http.Response{
    46  				StatusCode: http.StatusOK,
    47  				Body:       io.NopCloser(&bytes.Buffer{}),
    48  			}, nil
    49  		}),
    50  	}
    51  	tf.ClientConfigVal = &restclient.Config{}
    52  
    53  	outputFormat := "name"
    54  
    55  	ioStreams, _, buf, _ := genericiooptions.NewTestIOStreams()
    56  	cmd := NewCmdCreatePriorityClass(tf, ioStreams)
    57  	cmd.Flags().Set("value", "1000")
    58  	cmd.Flags().Set("global-default", "true")
    59  	cmd.Flags().Set("description", "my priority")
    60  	cmd.Flags().Set("dry-run", "client")
    61  	cmd.Flags().Set("output", outputFormat)
    62  	cmd.Flags().Set("preemption-policy", "Never")
    63  
    64  	printFlags := genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme)
    65  	printFlags.OutputFormat = &outputFormat
    66  
    67  	options := &PriorityClassOptions{
    68  		PrintFlags: printFlags,
    69  		Name:       pcName,
    70  		IOStreams:  ioStreams,
    71  	}
    72  	err := options.Complete(tf, cmd, []string{pcName})
    73  	if err != nil {
    74  		t.Fatalf("unexpected error: %v", err)
    75  	}
    76  
    77  	err = options.Run()
    78  	if err != nil {
    79  		t.Fatalf("unexpected error: %v", err)
    80  	}
    81  
    82  	expectedOutput := "priorityclass.scheduling.k8s.io/" + pcName + "\n"
    83  	if buf.String() != expectedOutput {
    84  		t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
    85  	}
    86  }
    87  

View as plain text