12
"github.com/containers/common/libnetwork/types"
13
"github.com/containers/common/pkg/config"
14
"github.com/containers/podman/v5/libpod/define"
15
"github.com/containers/podman/v5/libpod/lock"
16
"github.com/opencontainers/runtime-tools/generate"
17
"github.com/stretchr/testify/assert"
18
"github.com/stretchr/testify/require"
21
func getTestContainer(id, name string, manager lock.Manager) (*Container, error) {
23
config: &ContainerConfig{
26
ContainerRootFSConfig: ContainerRootFSConfig{
28
RootfsImageName: "testimg",
29
StaticDir: "/does/not/exist/",
30
Mounts: []string{"/does/not/exist"},
32
ContainerMiscConfig: ContainerMiscConfig{
33
LogPath: "/does/not/exist/",
35
Labels: map[string]string{"a": "b", "c": "d"},
38
CreatedTime: time.Now(),
40
ContainerSecurityConfig: ContainerSecurityConfig{
43
ContainerNetworkConfig: ContainerNetworkConfig{
44
DNSServer: []net.IP{net.ParseIP("192.168.1.1"), net.ParseIP("192.168.2.2")},
45
DNSSearch: []string{"example.com", "example.example.com"},
46
PortMappings: []types.PortMapping{
51
HostIP: "192.168.3.3",
58
HostIP: "192.168.4.4",
64
state: &ContainerState{
65
State: define.ContainerStateRunning,
66
ConfigPath: "/does/not/exist/specs/" + id,
67
RunDir: "/does/not/exist/tmp/",
69
Mountpoint: "/does/not/exist/tmp/" + id,
71
ExecSessions: map[string]*ExecSession{
81
BindMounts: map[string]string{
83
"/test/file.test": "/test2/file2.test",
87
config: &config.Config{
88
Engine: config.EngineConfig{
89
VolumePath: "/does/not/exist/tmp/volumes",
96
g, err := generate.New("linux")
100
ctr.config.Spec = g.Config
102
ctr.config.Labels["test"] = "testing"
104
// Allocate a containerLock for the container
105
containerLock, err := manager.AllocateLock()
109
ctr.lock = containerLock
110
ctr.config.LockID = containerLock.ID()
115
func getTestPod(id, name string, manager lock.Manager) (*Pod, error) {
120
Labels: map[string]string{"a": "b", "c": "d"},
121
CgroupParent: "/hello/world/cgroup/parent",
124
CgroupPath: "/path/to/cgroups/hello/",
129
// Allocate a podLock for the pod
130
podLock, err := manager.AllocateLock()
135
pod.config.LockID = podLock.ID()
140
func getTestCtrN(n string, manager lock.Manager) (*Container, error) {
141
return getTestContainer(strings.Repeat(n, 32), "test"+n, manager)
144
func getTestCtr1(manager lock.Manager) (*Container, error) {
145
return getTestCtrN("1", manager)
148
func getTestCtr2(manager lock.Manager) (*Container, error) {
149
return getTestCtrN("2", manager)
152
func getTestPodN(n string, manager lock.Manager) (*Pod, error) {
153
return getTestPod(strings.Repeat(n, 32), "test"+n, manager)
156
func getTestPod1(manager lock.Manager) (*Pod, error) {
157
return getTestPodN("1", manager)
160
func getTestPod2(manager lock.Manager) (*Pod, error) {
161
return getTestPodN("2", manager)
164
// This horrible hack tests if containers are equal in a way that should handle
165
// empty arrays being dropped to nil pointers in the spec JSON
166
// For some operations (container retrieval from the database) state is allowed
167
// to be empty. This is controlled by the allowedEmpty bool.
168
func testContainersEqual(t *testing.T, a, b *Container, allowedEmpty bool) {
169
if a == nil && b == nil {
175
require.NotNil(t, a.config)
176
require.NotNil(t, b.config)
177
require.NotNil(t, a.state)
178
require.NotNil(t, b.state)
180
aConfig := new(ContainerConfig)
181
bConfig := new(ContainerConfig)
182
aState := new(ContainerState)
183
bState := new(ContainerState)
185
blankState := new(ContainerState)
187
assert.Equal(t, a.valid, b.valid)
189
assert.Equal(t, a.lock.ID(), b.lock.ID())
191
aConfigJSON, err := json.Marshal(a.config)
192
assert.NoError(t, err)
193
err = json.Unmarshal(aConfigJSON, aConfig)
194
assert.NoError(t, err)
196
bConfigJSON, err := json.Marshal(b.config)
197
assert.NoError(t, err)
198
err = json.Unmarshal(bConfigJSON, bConfig)
199
assert.NoError(t, err)
201
assert.EqualValues(t, aConfig, bConfig)
203
aStateJSON, err := json.Marshal(a.state)
204
assert.NoError(t, err)
205
err = json.Unmarshal(aStateJSON, aState)
206
assert.NoError(t, err)
208
bStateJSON, err := json.Marshal(b.state)
209
assert.NoError(t, err)
210
err = json.Unmarshal(bStateJSON, bState)
211
assert.NoError(t, err)
214
assert.True(t, reflect.DeepEqual(aState, bState) || reflect.DeepEqual(aState, blankState))
216
assert.EqualValues(t, aState, bState)
220
// Test if pods are equal.
221
// For some operations (pod retrieval from the database) state is allowed to be
222
// empty. This is controlled by the allowedEmpty bool.
223
func testPodsEqual(t *testing.T, a, b *Pod, allowedEmpty bool) {
224
if a == nil && b == nil {
228
blankState := new(podState)
233
require.NotNil(t, a.config)
234
require.NotNil(t, b.config)
235
require.NotNil(t, a.state)
236
require.NotNil(t, b.state)
238
assert.Equal(t, a.valid, b.valid)
240
assert.Equal(t, a.lock.ID(), b.lock.ID())
242
assert.EqualValues(t, a.config, b.config)
245
assert.True(t, reflect.DeepEqual(a.state, b.state) || reflect.DeepEqual(a.state, blankState))
247
assert.EqualValues(t, a.state, b.state)