namespace_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 kubectl
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/kubernetes/pkg/api"
  18. )
  19. func TestNamespaceGenerate(t *testing.T) {
  20. tests := []struct {
  21. params map[string]interface{}
  22. expected *api.Namespace
  23. expectErr bool
  24. index int
  25. }{
  26. {
  27. params: map[string]interface{}{
  28. "name": "foo",
  29. },
  30. expected: &api.Namespace{
  31. ObjectMeta: api.ObjectMeta{
  32. Name: "foo",
  33. },
  34. },
  35. expectErr: false,
  36. },
  37. {
  38. params: map[string]interface{}{},
  39. expectErr: true,
  40. },
  41. {
  42. params: map[string]interface{}{
  43. "name": 1,
  44. },
  45. expectErr: true,
  46. },
  47. {
  48. params: map[string]interface{}{
  49. "name": nil,
  50. },
  51. expectErr: true,
  52. },
  53. {
  54. params: map[string]interface{}{
  55. "name_wrong_key": "some_value",
  56. },
  57. expectErr: true,
  58. },
  59. {
  60. params: map[string]interface{}{
  61. "NAME": "some_value",
  62. },
  63. expectErr: true,
  64. },
  65. }
  66. generator := NamespaceGeneratorV1{}
  67. for index, test := range tests {
  68. obj, err := generator.Generate(test.params)
  69. switch {
  70. case test.expectErr && err != nil:
  71. continue // loop, since there's no output to check
  72. case test.expectErr && err == nil:
  73. t.Errorf("%v: expected error and didn't get one", index)
  74. continue // loop, no expected output object
  75. case !test.expectErr && err != nil:
  76. t.Errorf("%v: unexpected error %v", index, err)
  77. continue // loop, no output object
  78. case !test.expectErr && err == nil:
  79. // do nothing and drop through
  80. }
  81. if !reflect.DeepEqual(obj.(*api.Namespace), test.expected) {
  82. t.Errorf("\nexpected:\n%#v\nsaw:\n%#v", test.expected, obj.(*api.Namespace))
  83. }
  84. }
  85. }