rollingupdate_test.go 2.0 KB

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