namespace_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2015 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 versioned
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/api/core/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. )
  20. func TestNamespaceGenerate(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. params map[string]interface{}
  24. expected *v1.Namespace
  25. expectErr bool
  26. }{
  27. {
  28. name: "test1",
  29. params: map[string]interface{}{
  30. "name": "foo",
  31. },
  32. expected: &v1.Namespace{
  33. ObjectMeta: metav1.ObjectMeta{
  34. Name: "foo",
  35. },
  36. },
  37. expectErr: false,
  38. },
  39. {
  40. name: "test2",
  41. params: map[string]interface{}{},
  42. expectErr: true,
  43. },
  44. {
  45. name: "test3",
  46. params: map[string]interface{}{
  47. "name": 1,
  48. },
  49. expectErr: true,
  50. },
  51. {
  52. name: "test4",
  53. params: map[string]interface{}{
  54. "name": "",
  55. },
  56. expectErr: true,
  57. },
  58. {
  59. name: "test5",
  60. params: map[string]interface{}{
  61. "name": nil,
  62. },
  63. expectErr: true,
  64. },
  65. {
  66. name: "test6",
  67. params: map[string]interface{}{
  68. "name_wrong_key": "some_value",
  69. },
  70. expectErr: true,
  71. },
  72. {
  73. name: "test7",
  74. params: map[string]interface{}{
  75. "NAME": "some_value",
  76. },
  77. expectErr: true,
  78. },
  79. }
  80. generator := NamespaceGeneratorV1{}
  81. for index, tt := range tests {
  82. t.Run(tt.name, func(t *testing.T) {
  83. obj, err := generator.Generate(tt.params)
  84. switch {
  85. case tt.expectErr && err != nil:
  86. return // loop, since there's no output to check
  87. case tt.expectErr && err == nil:
  88. t.Errorf("%v: expected error and didn't get one", index)
  89. return // loop, no expected output object
  90. case !tt.expectErr && err != nil:
  91. t.Errorf("%v: unexpected error %v", index, err)
  92. return // loop, no output object
  93. case !tt.expectErr && err == nil:
  94. // do nothing and drop through
  95. }
  96. if !reflect.DeepEqual(obj.(*v1.Namespace), tt.expected) {
  97. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", tt.expected, obj.(*v1.Namespace))
  98. }
  99. })
  100. }
  101. }