2017-10-31 09:16:25 +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.
|
|
|
|
*/
|
|
|
|
|
2018-04-10 14:21:50 +00:00
|
|
|
package create
|
2017-10-31 09:16:25 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
2018-08-21 10:46:39 +00:00
|
|
|
"k8s.io/cli-runtime/pkg/genericclioptions"
|
2017-10-31 09:16:25 +00:00
|
|
|
restclient "k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/rest/fake"
|
|
|
|
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
|
2018-08-02 17:29:16 +00:00
|
|
|
"k8s.io/kubernetes/pkg/kubectl/scheme"
|
2017-10-31 09:16:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreatePriorityClass(t *testing.T) {
|
|
|
|
pcName := "my-pc"
|
2018-02-22 14:52:10 +00:00
|
|
|
tf := cmdtesting.NewTestFactory()
|
2018-03-08 22:23:55 +00:00
|
|
|
defer tf.Cleanup()
|
|
|
|
|
2018-08-02 17:29:16 +00:00
|
|
|
ns := scheme.Codecs
|
2018-02-21 17:10:38 +00:00
|
|
|
|
2017-10-31 09:16:25 +00:00
|
|
|
tf.Client = &fake.RESTClient{
|
2018-05-11 16:33:08 +00:00
|
|
|
GroupVersion: schema.GroupVersion{Group: "scheduling.k8s.io", Version: "v1beta1"},
|
2017-10-31 09:16:25 +00:00
|
|
|
NegotiatedSerializer: ns,
|
|
|
|
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
|
|
|
|
return &http.Response{
|
|
|
|
StatusCode: http.StatusOK,
|
|
|
|
Body: ioutil.NopCloser(&bytes.Buffer{}),
|
|
|
|
}, nil
|
|
|
|
}),
|
|
|
|
}
|
2018-02-22 14:52:10 +00:00
|
|
|
tf.ClientConfigVal = &restclient.Config{}
|
2017-10-31 09:16:25 +00:00
|
|
|
|
2018-04-05 22:39:17 +00:00
|
|
|
outputFormat := "name"
|
|
|
|
|
2018-04-19 21:43:28 +00:00
|
|
|
ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
|
|
|
|
cmd := NewCmdCreatePriorityClass(tf, ioStreams)
|
2017-10-31 09:16:25 +00:00
|
|
|
cmd.Flags().Set("value", "1000")
|
|
|
|
cmd.Flags().Set("global-default", "true")
|
|
|
|
cmd.Flags().Set("description", "my priority")
|
|
|
|
cmd.Flags().Set("dry-run", "true")
|
2018-04-05 22:39:17 +00:00
|
|
|
cmd.Flags().Set("output", outputFormat)
|
|
|
|
|
2018-08-02 17:29:16 +00:00
|
|
|
printFlags := genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme)
|
2018-04-05 22:39:17 +00:00
|
|
|
printFlags.OutputFormat = &outputFormat
|
|
|
|
|
|
|
|
options := &PriorityClassOpts{
|
|
|
|
CreateSubcommandOptions: &CreateSubcommandOptions{
|
|
|
|
PrintFlags: printFlags,
|
|
|
|
Name: pcName,
|
2018-04-19 21:43:28 +00:00
|
|
|
IOStreams: ioStreams,
|
2018-04-05 22:39:17 +00:00
|
|
|
},
|
|
|
|
}
|
2018-05-07 21:47:29 +00:00
|
|
|
err := options.Complete(tf, cmd, []string{pcName})
|
2018-04-05 22:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-07 21:47:29 +00:00
|
|
|
err = options.Run()
|
2018-04-05 22:39:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-02-21 01:14:21 +00:00
|
|
|
expectedOutput := "priorityclass.scheduling.k8s.io/" + pcName + "\n"
|
2017-10-31 09:16:25 +00:00
|
|
|
if buf.String() != expectedOutput {
|
|
|
|
t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
|
|
|
|
}
|
|
|
|
}
|