1/*
2   Copyright The containerd Authors.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17package sandbox
18
19import (
20	"testing"
21
22	"github.com/containerd/containerd/pkg/cri/store/label"
23	assertlib "github.com/stretchr/testify/assert"
24	runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
25
26	"github.com/containerd/containerd/pkg/cri/store"
27)
28
29func TestSandboxStore(t *testing.T) {
30	sandboxes := map[string]Sandbox{
31		"1": NewSandbox(
32			Metadata{
33				ID:   "1",
34				Name: "Sandbox-1",
35				Config: &runtime.PodSandboxConfig{
36					Metadata: &runtime.PodSandboxMetadata{
37						Name:      "TestPod-1",
38						Uid:       "TestUid-1",
39						Namespace: "TestNamespace-1",
40						Attempt:   1,
41					},
42				},
43				NetNSPath: "TestNetNS-1",
44			},
45			Status{State: StateReady},
46		),
47		"2abcd": NewSandbox(
48			Metadata{
49				ID:   "2abcd",
50				Name: "Sandbox-2abcd",
51				Config: &runtime.PodSandboxConfig{
52					Metadata: &runtime.PodSandboxMetadata{
53						Name:      "TestPod-2abcd",
54						Uid:       "TestUid-2abcd",
55						Namespace: "TestNamespace-2abcd",
56						Attempt:   2,
57					},
58				},
59				NetNSPath: "TestNetNS-2",
60			},
61			Status{State: StateNotReady},
62		),
63		"4a333": NewSandbox(
64			Metadata{
65				ID:   "4a333",
66				Name: "Sandbox-4a333",
67				Config: &runtime.PodSandboxConfig{
68					Metadata: &runtime.PodSandboxMetadata{
69						Name:      "TestPod-4a333",
70						Uid:       "TestUid-4a333",
71						Namespace: "TestNamespace-4a333",
72						Attempt:   3,
73					},
74				},
75				NetNSPath: "TestNetNS-3",
76			},
77			Status{State: StateNotReady},
78		),
79		"4abcd": NewSandbox(
80			Metadata{
81				ID:   "4abcd",
82				Name: "Sandbox-4abcd",
83				Config: &runtime.PodSandboxConfig{
84					Metadata: &runtime.PodSandboxMetadata{
85						Name:      "TestPod-4abcd",
86						Uid:       "TestUid-4abcd",
87						Namespace: "TestNamespace-4abcd",
88						Attempt:   1,
89					},
90				},
91				NetNSPath: "TestNetNS-4abcd",
92			},
93			Status{State: StateReady},
94		),
95	}
96	unknown := NewSandbox(
97		Metadata{
98			ID:   "3defg",
99			Name: "Sandbox-3defg",
100			Config: &runtime.PodSandboxConfig{
101				Metadata: &runtime.PodSandboxMetadata{
102					Name:      "TestPod-3defg",
103					Uid:       "TestUid-3defg",
104					Namespace: "TestNamespace-3defg",
105					Attempt:   1,
106				},
107			},
108			NetNSPath: "TestNetNS-3defg",
109		},
110		Status{State: StateUnknown},
111	)
112	assert := assertlib.New(t)
113	s := NewStore(label.NewStore())
114
115	t.Logf("should be able to add sandbox")
116	for _, sb := range sandboxes {
117		assert.NoError(s.Add(sb))
118	}
119	assert.NoError(s.Add(unknown))
120
121	t.Logf("should be able to get sandbox")
122	genTruncIndex := func(normalName string) string { return normalName[:(len(normalName)+1)/2] }
123	for id, sb := range sandboxes {
124		got, err := s.Get(genTruncIndex(id))
125		assert.NoError(err)
126		assert.Equal(sb, got)
127	}
128
129	t.Logf("should be able to get sandbox in unknown state with Get")
130	got, err := s.Get(unknown.ID)
131	assert.NoError(err)
132	assert.Equal(unknown, got)
133
134	t.Logf("should be able to list sandboxes")
135	sbNum := len(sandboxes) + 1
136	sbs := s.List()
137	assert.Len(sbs, sbNum)
138
139	for testID, v := range sandboxes {
140		truncID := genTruncIndex(testID)
141
142		t.Logf("add should return already exists error for duplicated sandbox")
143		assert.Equal(store.ErrAlreadyExist, s.Add(v))
144
145		t.Logf("should be able to delete sandbox")
146		s.Delete(truncID)
147		sbNum--
148		sbs = s.List()
149		assert.Len(sbs, sbNum)
150
151		t.Logf("get should return not exist error after deletion")
152		sb, err := s.Get(truncID)
153		assert.Equal(Sandbox{}, sb)
154		assert.Equal(store.ErrNotExist, err)
155	}
156}
157