docker_sandbox_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Copyright 2016 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 dockershim
  14. import (
  15. "errors"
  16. "fmt"
  17. "math/rand"
  18. "net"
  19. "testing"
  20. "time"
  21. "github.com/stretchr/testify/assert"
  22. "github.com/stretchr/testify/require"
  23. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  24. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  25. "k8s.io/kubernetes/pkg/kubelet/dockershim/libdocker"
  26. "k8s.io/kubernetes/pkg/kubelet/dockershim/network"
  27. "k8s.io/kubernetes/pkg/kubelet/types"
  28. )
  29. // A helper to create a basic config.
  30. func makeSandboxConfig(name, namespace, uid string, attempt uint32) *runtimeapi.PodSandboxConfig {
  31. return makeSandboxConfigWithLabelsAndAnnotations(name, namespace, uid, attempt, map[string]string{}, map[string]string{})
  32. }
  33. func makeSandboxConfigWithLabelsAndAnnotations(name, namespace, uid string, attempt uint32, labels, annotations map[string]string) *runtimeapi.PodSandboxConfig {
  34. return &runtimeapi.PodSandboxConfig{
  35. Metadata: &runtimeapi.PodSandboxMetadata{
  36. Name: name,
  37. Namespace: namespace,
  38. Uid: uid,
  39. Attempt: attempt,
  40. },
  41. Labels: labels,
  42. Annotations: annotations,
  43. }
  44. }
  45. // TestListSandboxes creates several sandboxes and then list them to check
  46. // whether the correct metadatas, states, and labels are returned.
  47. func TestListSandboxes(t *testing.T) {
  48. ds, _, fakeClock := newTestDockerService()
  49. name, namespace := "foo", "bar"
  50. configs := []*runtimeapi.PodSandboxConfig{}
  51. for i := 0; i < 3; i++ {
  52. c := makeSandboxConfigWithLabelsAndAnnotations(fmt.Sprintf("%s%d", name, i),
  53. fmt.Sprintf("%s%d", namespace, i), fmt.Sprintf("%d", i), 0,
  54. map[string]string{"label": fmt.Sprintf("foo%d", i)},
  55. map[string]string{"annotation": fmt.Sprintf("bar%d", i)},
  56. )
  57. configs = append(configs, c)
  58. }
  59. expected := []*runtimeapi.PodSandbox{}
  60. state := runtimeapi.PodSandboxState_SANDBOX_READY
  61. var createdAt int64 = fakeClock.Now().UnixNano()
  62. for i := range configs {
  63. runResp, err := ds.RunPodSandbox(getTestCTX(), &runtimeapi.RunPodSandboxRequest{Config: configs[i]})
  64. require.NoError(t, err)
  65. // Prepend to the expected list because ListPodSandbox returns
  66. // the most recent sandbox first.
  67. expected = append([]*runtimeapi.PodSandbox{{
  68. Metadata: configs[i].Metadata,
  69. Id: runResp.PodSandboxId,
  70. State: state,
  71. CreatedAt: createdAt,
  72. Labels: configs[i].Labels,
  73. Annotations: configs[i].Annotations,
  74. }}, expected...)
  75. }
  76. listResp, err := ds.ListPodSandbox(getTestCTX(), &runtimeapi.ListPodSandboxRequest{})
  77. require.NoError(t, err)
  78. assert.Len(t, listResp.Items, len(expected))
  79. assert.Equal(t, expected, listResp.Items)
  80. }
  81. // TestSandboxStatus tests the basic lifecycle operations and verify that
  82. // the status returned reflects the operations performed.
  83. func TestSandboxStatus(t *testing.T) {
  84. ds, fDocker, fClock := newTestDockerService()
  85. labels := map[string]string{"label": "foobar1"}
  86. annotations := map[string]string{"annotation": "abc"}
  87. config := makeSandboxConfigWithLabelsAndAnnotations("foo", "bar", "1", 0, labels, annotations)
  88. r := rand.New(rand.NewSource(0)).Uint32()
  89. podIP := fmt.Sprintf("10.%d.%d.%d", byte(r>>16), byte(r>>8), byte(r))
  90. state := runtimeapi.PodSandboxState_SANDBOX_READY
  91. ct := int64(0)
  92. expected := &runtimeapi.PodSandboxStatus{
  93. State: state,
  94. CreatedAt: ct,
  95. Metadata: config.Metadata,
  96. Network: &runtimeapi.PodSandboxNetworkStatus{Ip: podIP, AdditionalIps: []*runtimeapi.PodIP{}},
  97. Linux: &runtimeapi.LinuxPodSandboxStatus{
  98. Namespaces: &runtimeapi.Namespace{
  99. Options: &runtimeapi.NamespaceOption{
  100. Pid: runtimeapi.NamespaceMode_CONTAINER,
  101. },
  102. },
  103. },
  104. Labels: labels,
  105. Annotations: annotations,
  106. }
  107. // Create the sandbox.
  108. fClock.SetTime(time.Now())
  109. expected.CreatedAt = fClock.Now().UnixNano()
  110. runResp, err := ds.RunPodSandbox(getTestCTX(), &runtimeapi.RunPodSandboxRequest{Config: config})
  111. require.NoError(t, err)
  112. id := runResp.PodSandboxId
  113. // Check internal labels
  114. c, err := fDocker.InspectContainer(id)
  115. assert.NoError(t, err)
  116. assert.Equal(t, c.Config.Labels[containerTypeLabelKey], containerTypeLabelSandbox)
  117. assert.Equal(t, c.Config.Labels[types.KubernetesContainerNameLabel], sandboxContainerName)
  118. expected.Id = id // ID is only known after the creation.
  119. statusResp, err := ds.PodSandboxStatus(getTestCTX(), &runtimeapi.PodSandboxStatusRequest{PodSandboxId: id})
  120. require.NoError(t, err)
  121. assert.Equal(t, expected, statusResp.Status)
  122. // Stop the sandbox.
  123. expected.State = runtimeapi.PodSandboxState_SANDBOX_NOTREADY
  124. _, err = ds.StopPodSandbox(getTestCTX(), &runtimeapi.StopPodSandboxRequest{PodSandboxId: id})
  125. require.NoError(t, err)
  126. // IP not valid after sandbox stop
  127. expected.Network.Ip = ""
  128. expected.Network.AdditionalIps = []*runtimeapi.PodIP{}
  129. statusResp, err = ds.PodSandboxStatus(getTestCTX(), &runtimeapi.PodSandboxStatusRequest{PodSandboxId: id})
  130. require.NoError(t, err)
  131. assert.Equal(t, expected, statusResp.Status)
  132. // Remove the container.
  133. _, err = ds.RemovePodSandbox(getTestCTX(), &runtimeapi.RemovePodSandboxRequest{PodSandboxId: id})
  134. require.NoError(t, err)
  135. statusResp, err = ds.PodSandboxStatus(getTestCTX(), &runtimeapi.PodSandboxStatusRequest{PodSandboxId: id})
  136. assert.Error(t, err, fmt.Sprintf("status of sandbox: %+v", statusResp))
  137. }
  138. // TestSandboxStatusAfterRestart tests that retrieving sandbox status returns
  139. // an IP address even if RunPodSandbox() was not yet called for this pod, as
  140. // would happen on kubelet restart
  141. func TestSandboxStatusAfterRestart(t *testing.T) {
  142. ds, _, fClock := newTestDockerService()
  143. config := makeSandboxConfig("foo", "bar", "1", 0)
  144. r := rand.New(rand.NewSource(0)).Uint32()
  145. podIP := fmt.Sprintf("10.%d.%d.%d", byte(r>>16), byte(r>>8), byte(r))
  146. state := runtimeapi.PodSandboxState_SANDBOX_READY
  147. ct := int64(0)
  148. expected := &runtimeapi.PodSandboxStatus{
  149. State: state,
  150. CreatedAt: ct,
  151. Metadata: config.Metadata,
  152. Network: &runtimeapi.PodSandboxNetworkStatus{Ip: podIP, AdditionalIps: []*runtimeapi.PodIP{}},
  153. Linux: &runtimeapi.LinuxPodSandboxStatus{
  154. Namespaces: &runtimeapi.Namespace{
  155. Options: &runtimeapi.NamespaceOption{
  156. Pid: runtimeapi.NamespaceMode_CONTAINER,
  157. },
  158. },
  159. },
  160. Labels: map[string]string{},
  161. Annotations: map[string]string{},
  162. }
  163. // Create the sandbox.
  164. fClock.SetTime(time.Now())
  165. expected.CreatedAt = fClock.Now().UnixNano()
  166. createConfig, err := ds.makeSandboxDockerConfig(config, defaultSandboxImage)
  167. assert.NoError(t, err)
  168. createResp, err := ds.client.CreateContainer(*createConfig)
  169. assert.NoError(t, err)
  170. err = ds.client.StartContainer(createResp.ID)
  171. assert.NoError(t, err)
  172. // Check status without RunPodSandbox() having set up networking
  173. expected.Id = createResp.ID // ID is only known after the creation.
  174. statusResp, err := ds.PodSandboxStatus(getTestCTX(), &runtimeapi.PodSandboxStatusRequest{PodSandboxId: createResp.ID})
  175. require.NoError(t, err)
  176. assert.Equal(t, expected, statusResp.Status)
  177. }
  178. // TestNetworkPluginInvocation checks that the right SetUpPod and TearDownPod
  179. // calls are made when we run/stop a sandbox.
  180. func TestNetworkPluginInvocation(t *testing.T) {
  181. ds, _, _ := newTestDockerService()
  182. mockPlugin := newTestNetworkPlugin(t)
  183. ds.network = network.NewPluginManager(mockPlugin)
  184. defer mockPlugin.Finish()
  185. name := "foo0"
  186. ns := "bar0"
  187. c := makeSandboxConfigWithLabelsAndAnnotations(
  188. name, ns, "0", 0,
  189. map[string]string{"label": name},
  190. map[string]string{"annotation": ns},
  191. )
  192. cID := kubecontainer.ContainerID{Type: runtimeName, ID: libdocker.GetFakeContainerID(fmt.Sprintf("/%v", makeSandboxName(c)))}
  193. mockPlugin.EXPECT().Name().Return("mockNetworkPlugin").AnyTimes()
  194. setup := mockPlugin.EXPECT().SetUpPod(ns, name, cID)
  195. mockPlugin.EXPECT().TearDownPod(ns, name, cID).After(setup)
  196. _, err := ds.RunPodSandbox(getTestCTX(), &runtimeapi.RunPodSandboxRequest{Config: c})
  197. require.NoError(t, err)
  198. _, err = ds.StopPodSandbox(getTestCTX(), &runtimeapi.StopPodSandboxRequest{PodSandboxId: cID.ID})
  199. require.NoError(t, err)
  200. }
  201. // TestHostNetworkPluginInvocation checks that *no* SetUp/TearDown calls happen
  202. // for host network sandboxes.
  203. func TestHostNetworkPluginInvocation(t *testing.T) {
  204. ds, _, _ := newTestDockerService()
  205. mockPlugin := newTestNetworkPlugin(t)
  206. ds.network = network.NewPluginManager(mockPlugin)
  207. defer mockPlugin.Finish()
  208. name := "foo0"
  209. ns := "bar0"
  210. c := makeSandboxConfigWithLabelsAndAnnotations(
  211. name, ns, "0", 0,
  212. map[string]string{"label": name},
  213. map[string]string{"annotation": ns},
  214. )
  215. c.Linux = &runtimeapi.LinuxPodSandboxConfig{
  216. SecurityContext: &runtimeapi.LinuxSandboxSecurityContext{
  217. NamespaceOptions: &runtimeapi.NamespaceOption{
  218. Network: runtimeapi.NamespaceMode_NODE,
  219. },
  220. },
  221. }
  222. cID := kubecontainer.ContainerID{Type: runtimeName, ID: libdocker.GetFakeContainerID(fmt.Sprintf("/%v", makeSandboxName(c)))}
  223. // No calls to network plugin are expected
  224. _, err := ds.RunPodSandbox(getTestCTX(), &runtimeapi.RunPodSandboxRequest{Config: c})
  225. require.NoError(t, err)
  226. _, err = ds.StopPodSandbox(getTestCTX(), &runtimeapi.StopPodSandboxRequest{PodSandboxId: cID.ID})
  227. require.NoError(t, err)
  228. }
  229. // TestSetUpPodFailure checks that the sandbox should be not ready when it
  230. // hits a SetUpPod failure.
  231. func TestSetUpPodFailure(t *testing.T) {
  232. ds, _, _ := newTestDockerService()
  233. mockPlugin := newTestNetworkPlugin(t)
  234. ds.network = network.NewPluginManager(mockPlugin)
  235. defer mockPlugin.Finish()
  236. name := "foo0"
  237. ns := "bar0"
  238. c := makeSandboxConfigWithLabelsAndAnnotations(
  239. name, ns, "0", 0,
  240. map[string]string{"label": name},
  241. map[string]string{"annotation": ns},
  242. )
  243. cID := kubecontainer.ContainerID{Type: runtimeName, ID: libdocker.GetFakeContainerID(fmt.Sprintf("/%v", makeSandboxName(c)))}
  244. mockPlugin.EXPECT().Name().Return("mockNetworkPlugin").AnyTimes()
  245. mockPlugin.EXPECT().SetUpPod(ns, name, cID).Return(errors.New("setup pod error")).AnyTimes()
  246. // If SetUpPod() fails, we expect TearDownPod() to immediately follow
  247. mockPlugin.EXPECT().TearDownPod(ns, name, cID)
  248. // Assume network plugin doesn't return error, dockershim should still be able to return not ready correctly.
  249. mockPlugin.EXPECT().GetPodNetworkStatus(ns, name, cID).Return(&network.PodNetworkStatus{IP: net.IP("127.0.0.01")}, nil).AnyTimes()
  250. t.Logf("RunPodSandbox should return error")
  251. _, err := ds.RunPodSandbox(getTestCTX(), &runtimeapi.RunPodSandboxRequest{Config: c})
  252. assert.Error(t, err)
  253. t.Logf("PodSandboxStatus should be not ready")
  254. statusResp, err := ds.PodSandboxStatus(getTestCTX(), &runtimeapi.PodSandboxStatusRequest{PodSandboxId: cID.ID})
  255. require.NoError(t, err)
  256. assert.Equal(t, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, statusResp.Status.State)
  257. t.Logf("ListPodSandbox should also show not ready")
  258. listResp, err := ds.ListPodSandbox(getTestCTX(), &runtimeapi.ListPodSandboxRequest{})
  259. require.NoError(t, err)
  260. var sandbox *runtimeapi.PodSandbox
  261. for _, s := range listResp.Items {
  262. if s.Id == cID.ID {
  263. sandbox = s
  264. break
  265. }
  266. }
  267. assert.NotNil(t, sandbox)
  268. assert.Equal(t, runtimeapi.PodSandboxState_SANDBOX_NOTREADY, sandbox.State)
  269. }