create_priorityclass_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package create
  14. import (
  15. "bytes"
  16. "io/ioutil"
  17. "net/http"
  18. "testing"
  19. "k8s.io/apimachinery/pkg/runtime/schema"
  20. "k8s.io/cli-runtime/pkg/genericclioptions"
  21. restclient "k8s.io/client-go/rest"
  22. "k8s.io/client-go/rest/fake"
  23. cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
  24. "k8s.io/kubernetes/pkg/kubectl/scheme"
  25. )
  26. func TestCreatePriorityClass(t *testing.T) {
  27. pcName := "my-pc"
  28. tf := cmdtesting.NewTestFactory()
  29. defer tf.Cleanup()
  30. ns := scheme.Codecs
  31. tf.Client = &fake.RESTClient{
  32. GroupVersion: schema.GroupVersion{Group: "scheduling.k8s.io", Version: "v1beta1"},
  33. NegotiatedSerializer: ns,
  34. Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
  35. return &http.Response{
  36. StatusCode: http.StatusOK,
  37. Body: ioutil.NopCloser(&bytes.Buffer{}),
  38. }, nil
  39. }),
  40. }
  41. tf.ClientConfigVal = &restclient.Config{}
  42. outputFormat := "name"
  43. ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams()
  44. cmd := NewCmdCreatePriorityClass(tf, ioStreams)
  45. cmd.Flags().Set("value", "1000")
  46. cmd.Flags().Set("global-default", "true")
  47. cmd.Flags().Set("description", "my priority")
  48. cmd.Flags().Set("dry-run", "true")
  49. cmd.Flags().Set("output", outputFormat)
  50. cmd.Flags().Set("preemption-policy", "Never")
  51. printFlags := genericclioptions.NewPrintFlags("created").WithTypeSetter(scheme.Scheme)
  52. printFlags.OutputFormat = &outputFormat
  53. options := &PriorityClassOpts{
  54. CreateSubcommandOptions: &CreateSubcommandOptions{
  55. PrintFlags: printFlags,
  56. Name: pcName,
  57. IOStreams: ioStreams,
  58. },
  59. }
  60. err := options.Complete(tf, cmd, []string{pcName})
  61. if err != nil {
  62. t.Fatalf("unexpected error: %v", err)
  63. }
  64. err = options.Run()
  65. if err != nil {
  66. t.Fatalf("unexpected error: %v", err)
  67. }
  68. expectedOutput := "priorityclass.scheduling.k8s.io/" + pcName + "\n"
  69. if buf.String() != expectedOutput {
  70. t.Errorf("expected output: %s, but got: %s", expectedOutput, buf.String())
  71. }
  72. }