util_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright 2018 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 serviceaccount
  14. import (
  15. "testing"
  16. "k8s.io/api/core/v1"
  17. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  18. )
  19. func TestIsServiceAccountToken(t *testing.T) {
  20. secretIns := &v1.Secret{
  21. ObjectMeta: metav1.ObjectMeta{
  22. Name: "token-secret-1",
  23. Namespace: "default",
  24. UID: "23456",
  25. ResourceVersion: "1",
  26. Annotations: map[string]string{
  27. v1.ServiceAccountNameKey: "default",
  28. v1.ServiceAccountUIDKey: "12345",
  29. },
  30. },
  31. Type: v1.SecretTypeServiceAccountToken,
  32. Data: map[string][]byte{
  33. "token": []byte("ABC"),
  34. "ca.crt": []byte("CA Data"),
  35. "namespace": []byte("default"),
  36. },
  37. }
  38. secretTypeMistmatch := &v1.Secret{
  39. ObjectMeta: metav1.ObjectMeta{
  40. Name: "token-secret-2",
  41. Namespace: "default",
  42. UID: "23456",
  43. ResourceVersion: "1",
  44. Annotations: map[string]string{
  45. v1.ServiceAccountNameKey: "default",
  46. v1.ServiceAccountUIDKey: "12345",
  47. },
  48. },
  49. Type: v1.SecretTypeOpaque,
  50. }
  51. saIns := &v1.ServiceAccount{
  52. ObjectMeta: metav1.ObjectMeta{
  53. Name: "default",
  54. UID: "12345",
  55. Namespace: "default",
  56. ResourceVersion: "1",
  57. },
  58. }
  59. saInsNameNotEqual := &v1.ServiceAccount{
  60. ObjectMeta: metav1.ObjectMeta{
  61. Name: "non-default",
  62. UID: "12345",
  63. Namespace: "default",
  64. ResourceVersion: "1",
  65. },
  66. }
  67. saInsUIDNotEqual := &v1.ServiceAccount{
  68. ObjectMeta: metav1.ObjectMeta{
  69. Name: "default",
  70. UID: "67890",
  71. Namespace: "default",
  72. ResourceVersion: "1",
  73. },
  74. }
  75. tests := map[string]struct {
  76. secret *v1.Secret
  77. sa *v1.ServiceAccount
  78. expect bool
  79. }{
  80. "correct service account": {
  81. secret: secretIns,
  82. sa: saIns,
  83. expect: true,
  84. },
  85. "service account name not equal": {
  86. secret: secretIns,
  87. sa: saInsNameNotEqual,
  88. expect: false,
  89. },
  90. "service account uid not equal": {
  91. secret: secretIns,
  92. sa: saInsUIDNotEqual,
  93. expect: false,
  94. },
  95. "service account type not equal": {
  96. secret: secretTypeMistmatch,
  97. sa: saIns,
  98. expect: false,
  99. },
  100. }
  101. for k, v := range tests {
  102. actual := IsServiceAccountToken(v.secret, v.sa)
  103. if actual != v.expect {
  104. t.Errorf("%s failed, expected %t but received %t", k, v.expect, actual)
  105. }
  106. }
  107. }