mirror_client_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 pod
  14. import (
  15. "context"
  16. "errors"
  17. "testing"
  18. "github.com/stretchr/testify/assert"
  19. "github.com/stretchr/testify/require"
  20. v1 "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/apimachinery/pkg/types"
  23. "k8s.io/client-go/kubernetes/fake"
  24. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  25. kubetypes "k8s.io/kubernetes/pkg/kubelet/types"
  26. "k8s.io/utils/pointer"
  27. )
  28. func TestParsePodFullName(t *testing.T) {
  29. type nameTuple struct {
  30. Name string
  31. Namespace string
  32. }
  33. successfulCases := map[string]nameTuple{
  34. "bar_foo": {Name: "bar", Namespace: "foo"},
  35. "bar.org_foo.com": {Name: "bar.org", Namespace: "foo.com"},
  36. "bar-bar_foo": {Name: "bar-bar", Namespace: "foo"},
  37. }
  38. failedCases := []string{"barfoo", "bar_foo_foo", "", "bar_", "_foo"}
  39. for podFullName, expected := range successfulCases {
  40. name, namespace, err := kubecontainer.ParsePodFullName(podFullName)
  41. if err != nil {
  42. t.Errorf("unexpected error when parsing the full name: %v", err)
  43. continue
  44. }
  45. if name != expected.Name || namespace != expected.Namespace {
  46. t.Errorf("expected name %q, namespace %q; got name %q, namespace %q",
  47. expected.Name, expected.Namespace, name, namespace)
  48. }
  49. }
  50. for _, podFullName := range failedCases {
  51. _, _, err := kubecontainer.ParsePodFullName(podFullName)
  52. if err == nil {
  53. t.Errorf("expected error when parsing the full name, got none")
  54. }
  55. }
  56. }
  57. func TestCreateMirrorPod(t *testing.T) {
  58. const (
  59. testNodeName = "test-node-name"
  60. testNodeUID = types.UID("test-node-uid-1234")
  61. testPodName = "test-pod-name"
  62. testPodNS = "test-pod-ns"
  63. testPodHash = "123456789"
  64. )
  65. testcases := []struct {
  66. desc string
  67. node *v1.Node
  68. nodeErr error
  69. expectSuccess bool
  70. }{{
  71. desc: "cannot get node",
  72. nodeErr: errors.New("expected: cannot get node"),
  73. expectSuccess: false,
  74. }, {
  75. desc: "node missing UID",
  76. node: &v1.Node{
  77. ObjectMeta: metav1.ObjectMeta{
  78. Name: testNodeName,
  79. },
  80. },
  81. expectSuccess: false,
  82. }, {
  83. desc: "successfully fetched node",
  84. node: &v1.Node{
  85. ObjectMeta: metav1.ObjectMeta{
  86. Name: testNodeName,
  87. UID: testNodeUID,
  88. },
  89. },
  90. expectSuccess: true,
  91. }}
  92. for _, test := range testcases {
  93. t.Run(test.desc, func(t *testing.T) {
  94. clientset := fake.NewSimpleClientset()
  95. nodeGetter := &fakeNodeGetter{
  96. t: t,
  97. expectNodeName: testNodeName,
  98. node: test.node,
  99. err: test.nodeErr,
  100. }
  101. mc := NewBasicMirrorClient(clientset, testNodeName, nodeGetter)
  102. pod := &v1.Pod{
  103. ObjectMeta: metav1.ObjectMeta{
  104. Name: testPodName,
  105. Namespace: testPodNS,
  106. Annotations: map[string]string{
  107. kubetypes.ConfigHashAnnotationKey: testPodHash,
  108. },
  109. },
  110. }
  111. err := mc.CreateMirrorPod(pod)
  112. if !test.expectSuccess {
  113. assert.Error(t, err)
  114. return
  115. }
  116. createdPod, err := clientset.CoreV1().Pods(testPodNS).Get(context.TODO(), testPodName, metav1.GetOptions{})
  117. require.NoError(t, err)
  118. // Validate created pod
  119. assert.Equal(t, testPodHash, createdPod.Annotations[kubetypes.ConfigMirrorAnnotationKey])
  120. assert.Len(t, createdPod.OwnerReferences, 1)
  121. expectedOwnerRef := metav1.OwnerReference{
  122. APIVersion: "v1",
  123. Kind: "Node",
  124. Name: testNodeName,
  125. UID: testNodeUID,
  126. Controller: pointer.BoolPtr(true),
  127. }
  128. assert.Equal(t, expectedOwnerRef, createdPod.OwnerReferences[0])
  129. })
  130. }
  131. }
  132. type fakeNodeGetter struct {
  133. t *testing.T
  134. expectNodeName string
  135. node *v1.Node
  136. err error
  137. }
  138. func (f *fakeNodeGetter) Get(nodeName string) (*v1.Node, error) {
  139. require.Equal(f.t, f.expectNodeName, nodeName)
  140. return f.node, f.err
  141. }