rollingupdate_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2014 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 rollingupdate
  14. import (
  15. "testing"
  16. "k8s.io/cli-runtime/pkg/genericclioptions"
  17. cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
  18. )
  19. func TestValidateArgs(t *testing.T) {
  20. f := cmdtesting.NewTestFactory()
  21. defer f.Cleanup()
  22. tests := []struct {
  23. testName string
  24. flags map[string]string
  25. filenames []string
  26. args []string
  27. expectErr bool
  28. }{
  29. {
  30. testName: "nothing",
  31. expectErr: true,
  32. },
  33. {
  34. testName: "no file, no image",
  35. flags: map[string]string{},
  36. args: []string{"foo"},
  37. expectErr: true,
  38. },
  39. {
  40. testName: "valid file example",
  41. filenames: []string{"bar.yaml"},
  42. args: []string{"foo"},
  43. },
  44. {
  45. testName: "missing second image name",
  46. flags: map[string]string{
  47. "image": "foo:v2",
  48. },
  49. args: []string{"foo"},
  50. },
  51. {
  52. testName: "valid image example",
  53. flags: map[string]string{
  54. "image": "foo:v2",
  55. },
  56. args: []string{"foo", "foo-v2"},
  57. },
  58. {
  59. testName: "both filename and image example",
  60. flags: map[string]string{
  61. "image": "foo:v2",
  62. },
  63. filenames: []string{"bar.yaml"},
  64. args: []string{"foo", "foo-v2"},
  65. expectErr: true,
  66. },
  67. }
  68. for _, test := range tests {
  69. cmd := NewCmdRollingUpdate(f, genericclioptions.NewTestIOStreamsDiscard())
  70. if test.flags != nil {
  71. for key, val := range test.flags {
  72. cmd.Flags().Set(key, val)
  73. }
  74. }
  75. err := validateArguments(cmd, test.filenames, test.args)
  76. if err != nil && !test.expectErr {
  77. t.Errorf("%s: unexpected error: %v", test.testName, err)
  78. }
  79. if err == nil && test.expectErr {
  80. t.Errorf("%s: unexpected non-error", test.testName)
  81. }
  82. }
  83. }