default_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright 2017 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 nodeidentifier
  14. import (
  15. "testing"
  16. "k8s.io/apiserver/pkg/authentication/user"
  17. )
  18. func TestDefaultNodeIdentifier_NodeIdentity(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. user user.Info
  22. expectNodeName string
  23. expectIsNode bool
  24. }{
  25. {
  26. name: "nil user",
  27. user: nil,
  28. expectNodeName: "",
  29. expectIsNode: false,
  30. },
  31. {
  32. name: "node username without group",
  33. user: &user.DefaultInfo{Name: "system:node:foo"},
  34. expectNodeName: "",
  35. expectIsNode: false,
  36. },
  37. {
  38. name: "node group without username",
  39. user: &user.DefaultInfo{Name: "foo", Groups: []string{"system:nodes"}},
  40. expectNodeName: "",
  41. expectIsNode: false,
  42. },
  43. {
  44. name: "node group and username",
  45. user: &user.DefaultInfo{Name: "system:node:foo", Groups: []string{"system:nodes"}},
  46. expectNodeName: "foo",
  47. expectIsNode: true,
  48. },
  49. }
  50. for _, tt := range tests {
  51. t.Run(tt.name, func(t *testing.T) {
  52. nodeName, isNode := NewDefaultNodeIdentifier().NodeIdentity(tt.user)
  53. if nodeName != tt.expectNodeName {
  54. t.Errorf("DefaultNodeIdentifier.NodeIdentity() got = %v, want %v", nodeName, tt.expectNodeName)
  55. }
  56. if isNode != tt.expectIsNode {
  57. t.Errorf("DefaultNodeIdentifier.NodeIdentity() got1 = %v, want %v", isNode, tt.expectIsNode)
  58. }
  59. })
  60. }
  61. }