generate_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 kubectl
  14. import (
  15. "reflect"
  16. "testing"
  17. "github.com/spf13/cobra"
  18. )
  19. type TestStruct struct {
  20. val int
  21. }
  22. func TestIsZero(t *testing.T) {
  23. tests := []struct {
  24. val interface{}
  25. expectZero bool
  26. }{
  27. {"", true},
  28. {nil, true},
  29. {0, true},
  30. {TestStruct{}, true},
  31. {"foo", false},
  32. {1, false},
  33. {TestStruct{val: 2}, false},
  34. }
  35. for _, test := range tests {
  36. output := IsZero(test.val)
  37. if output != test.expectZero {
  38. t.Errorf("expected: %v, saw %v", test.expectZero, output)
  39. }
  40. }
  41. }
  42. func TestValidateParams(t *testing.T) {
  43. tests := []struct {
  44. paramSpec []GeneratorParam
  45. params map[string]interface{}
  46. valid bool
  47. }{
  48. {
  49. paramSpec: []GeneratorParam{},
  50. params: map[string]interface{}{},
  51. valid: true,
  52. },
  53. {
  54. paramSpec: []GeneratorParam{
  55. {Name: "foo"},
  56. },
  57. params: map[string]interface{}{},
  58. valid: true,
  59. },
  60. {
  61. paramSpec: []GeneratorParam{
  62. {Name: "foo", Required: true},
  63. },
  64. params: map[string]interface{}{
  65. "foo": "bar",
  66. },
  67. valid: true,
  68. },
  69. {
  70. paramSpec: []GeneratorParam{
  71. {Name: "foo", Required: true},
  72. },
  73. params: map[string]interface{}{
  74. "baz": "blah",
  75. "foo": "bar",
  76. },
  77. valid: true,
  78. },
  79. {
  80. paramSpec: []GeneratorParam{
  81. {Name: "foo", Required: true},
  82. {Name: "baz", Required: true},
  83. },
  84. params: map[string]interface{}{
  85. "baz": "blah",
  86. "foo": "bar",
  87. },
  88. valid: true,
  89. },
  90. {
  91. paramSpec: []GeneratorParam{
  92. {Name: "foo", Required: true},
  93. {Name: "baz", Required: true},
  94. },
  95. params: map[string]interface{}{
  96. "foo": "bar",
  97. },
  98. valid: false,
  99. },
  100. }
  101. for _, test := range tests {
  102. err := ValidateParams(test.paramSpec, test.params)
  103. if test.valid && err != nil {
  104. t.Errorf("unexpected error: %v", err)
  105. }
  106. if !test.valid && err == nil {
  107. t.Errorf("unexpected non-error")
  108. }
  109. }
  110. }
  111. func TestMakeParams(t *testing.T) {
  112. cmd := &cobra.Command{}
  113. cmd.Flags().String("foo", "bar", "")
  114. cmd.Flags().String("baz", "", "")
  115. cmd.Flags().Set("baz", "blah")
  116. paramSpec := []GeneratorParam{
  117. {Name: "foo", Required: true},
  118. {Name: "baz", Required: true},
  119. }
  120. expected := map[string]interface{}{
  121. "foo": "bar",
  122. "baz": "blah",
  123. }
  124. params := MakeParams(cmd, paramSpec)
  125. if !reflect.DeepEqual(params, expected) {
  126. t.Errorf("\nexpected:\n%v\nsaw:\n%v", expected, params)
  127. }
  128. }