strategy_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 secret
  14. import (
  15. "reflect"
  16. "testing"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  20. apitesting "k8s.io/kubernetes/pkg/api/testing"
  21. api "k8s.io/kubernetes/pkg/apis/core"
  22. // ensure types are installed
  23. _ "k8s.io/kubernetes/pkg/apis/core/install"
  24. )
  25. func TestExportSecret(t *testing.T) {
  26. tests := []struct {
  27. objIn runtime.Object
  28. objOut runtime.Object
  29. exact bool
  30. expectErr bool
  31. }{
  32. {
  33. objIn: &api.Secret{
  34. ObjectMeta: metav1.ObjectMeta{
  35. Name: "foo",
  36. Namespace: "bar",
  37. },
  38. Data: map[string][]byte{
  39. "foo": []byte("bar"),
  40. },
  41. },
  42. objOut: &api.Secret{
  43. ObjectMeta: metav1.ObjectMeta{
  44. Name: "foo",
  45. Namespace: "bar",
  46. },
  47. Data: map[string][]byte{
  48. "foo": []byte("bar"),
  49. },
  50. },
  51. exact: true,
  52. },
  53. {
  54. objIn: &api.Secret{
  55. ObjectMeta: metav1.ObjectMeta{
  56. Name: "foo",
  57. Namespace: "bar",
  58. },
  59. Type: api.SecretTypeServiceAccountToken,
  60. },
  61. expectErr: true,
  62. },
  63. {
  64. objIn: &api.Secret{
  65. ObjectMeta: metav1.ObjectMeta{
  66. Name: "foo",
  67. Namespace: "bar",
  68. Annotations: map[string]string{
  69. api.ServiceAccountUIDKey: "true",
  70. },
  71. },
  72. },
  73. expectErr: true,
  74. },
  75. {
  76. objIn: &api.Pod{},
  77. expectErr: true,
  78. },
  79. }
  80. for _, test := range tests {
  81. err := Strategy.Export(genericapirequest.NewContext(), test.objIn, test.exact)
  82. if err != nil {
  83. if !test.expectErr {
  84. t.Errorf("unexpected error: %v", err)
  85. }
  86. continue
  87. }
  88. if test.expectErr {
  89. t.Error("unexpected non-error")
  90. continue
  91. }
  92. if !reflect.DeepEqual(test.objIn, test.objOut) {
  93. t.Errorf("expected:\n%v\nsaw:\n%v\n", test.objOut, test.objIn)
  94. }
  95. }
  96. }
  97. func TestSelectableFieldLabelConversions(t *testing.T) {
  98. apitesting.TestSelectableFieldLabelConversionsOfKind(t,
  99. "v1",
  100. "Secret",
  101. SelectableFields(&api.Secret{}),
  102. nil,
  103. )
  104. }