podman

Форк
0
/
common_test.go 
249 строк · 6.0 Кб
1
//go:build !remote
2

3
package libpod
4

5
import (
6
	"net"
7
	"reflect"
8
	"strings"
9
	"testing"
10
	"time"
11

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"
19
)
20

21
func getTestContainer(id, name string, manager lock.Manager) (*Container, error) {
22
	ctr := &Container{
23
		config: &ContainerConfig{
24
			ID:   id,
25
			Name: name,
26
			ContainerRootFSConfig: ContainerRootFSConfig{
27
				RootfsImageID:   id,
28
				RootfsImageName: "testimg",
29
				StaticDir:       "/does/not/exist/",
30
				Mounts:          []string{"/does/not/exist"},
31
			},
32
			ContainerMiscConfig: ContainerMiscConfig{
33
				LogPath:     "/does/not/exist/",
34
				Stdin:       true,
35
				Labels:      map[string]string{"a": "b", "c": "d"},
36
				StopSignal:  0,
37
				StopTimeout: 0,
38
				CreatedTime: time.Now(),
39
			},
40
			ContainerSecurityConfig: ContainerSecurityConfig{
41
				Privileged: true,
42
			},
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{
47
					{
48
						HostPort:      80,
49
						ContainerPort: 90,
50
						Protocol:      "tcp",
51
						HostIP:        "192.168.3.3",
52
						Range:         1,
53
					},
54
					{
55
						HostPort:      100,
56
						ContainerPort: 110,
57
						Protocol:      "udp",
58
						HostIP:        "192.168.4.4",
59
						Range:         1,
60
					},
61
				},
62
			},
63
		},
64
		state: &ContainerState{
65
			State:      define.ContainerStateRunning,
66
			ConfigPath: "/does/not/exist/specs/" + id,
67
			RunDir:     "/does/not/exist/tmp/",
68
			Mounted:    true,
69
			Mountpoint: "/does/not/exist/tmp/" + id,
70
			PID:        1234,
71
			ExecSessions: map[string]*ExecSession{
72
				"abcd": {
73
					Id:  "1",
74
					PID: 9876,
75
				},
76
				"ef01": {
77
					Id:  "5",
78
					PID: 46765,
79
				},
80
			},
81
			BindMounts: map[string]string{
82
				"/1/2/3":          "/4/5/6",
83
				"/test/file.test": "/test2/file2.test",
84
			},
85
		},
86
		runtime: &Runtime{
87
			config: &config.Config{
88
				Engine: config.EngineConfig{
89
					VolumePath: "/does/not/exist/tmp/volumes",
90
				},
91
			},
92
		},
93
		valid: true,
94
	}
95

96
	g, err := generate.New("linux")
97
	if err != nil {
98
		return nil, err
99
	}
100
	ctr.config.Spec = g.Config
101

102
	ctr.config.Labels["test"] = "testing"
103

104
	// Allocate a containerLock for the container
105
	containerLock, err := manager.AllocateLock()
106
	if err != nil {
107
		return nil, err
108
	}
109
	ctr.lock = containerLock
110
	ctr.config.LockID = containerLock.ID()
111

112
	return ctr, nil
113
}
114

115
func getTestPod(id, name string, manager lock.Manager) (*Pod, error) {
116
	pod := &Pod{
117
		config: &PodConfig{
118
			ID:           id,
119
			Name:         name,
120
			Labels:       map[string]string{"a": "b", "c": "d"},
121
			CgroupParent: "/hello/world/cgroup/parent",
122
		},
123
		state: &podState{
124
			CgroupPath: "/path/to/cgroups/hello/",
125
		},
126
		valid: true,
127
	}
128

129
	// Allocate a podLock for the pod
130
	podLock, err := manager.AllocateLock()
131
	if err != nil {
132
		return nil, err
133
	}
134
	pod.lock = podLock
135
	pod.config.LockID = podLock.ID()
136

137
	return pod, nil
138
}
139

140
func getTestCtrN(n string, manager lock.Manager) (*Container, error) {
141
	return getTestContainer(strings.Repeat(n, 32), "test"+n, manager)
142
}
143

144
func getTestCtr1(manager lock.Manager) (*Container, error) {
145
	return getTestCtrN("1", manager)
146
}
147

148
func getTestCtr2(manager lock.Manager) (*Container, error) {
149
	return getTestCtrN("2", manager)
150
}
151

152
func getTestPodN(n string, manager lock.Manager) (*Pod, error) {
153
	return getTestPod(strings.Repeat(n, 32), "test"+n, manager)
154
}
155

156
func getTestPod1(manager lock.Manager) (*Pod, error) {
157
	return getTestPodN("1", manager)
158
}
159

160
func getTestPod2(manager lock.Manager) (*Pod, error) {
161
	return getTestPodN("2", manager)
162
}
163

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 {
170
		return
171
	}
172
	require.NotNil(t, a)
173
	require.NotNil(t, b)
174

175
	require.NotNil(t, a.config)
176
	require.NotNil(t, b.config)
177
	require.NotNil(t, a.state)
178
	require.NotNil(t, b.state)
179

180
	aConfig := new(ContainerConfig)
181
	bConfig := new(ContainerConfig)
182
	aState := new(ContainerState)
183
	bState := new(ContainerState)
184

185
	blankState := new(ContainerState)
186

187
	assert.Equal(t, a.valid, b.valid)
188

189
	assert.Equal(t, a.lock.ID(), b.lock.ID())
190

191
	aConfigJSON, err := json.Marshal(a.config)
192
	assert.NoError(t, err)
193
	err = json.Unmarshal(aConfigJSON, aConfig)
194
	assert.NoError(t, err)
195

196
	bConfigJSON, err := json.Marshal(b.config)
197
	assert.NoError(t, err)
198
	err = json.Unmarshal(bConfigJSON, bConfig)
199
	assert.NoError(t, err)
200

201
	assert.EqualValues(t, aConfig, bConfig)
202

203
	aStateJSON, err := json.Marshal(a.state)
204
	assert.NoError(t, err)
205
	err = json.Unmarshal(aStateJSON, aState)
206
	assert.NoError(t, err)
207

208
	bStateJSON, err := json.Marshal(b.state)
209
	assert.NoError(t, err)
210
	err = json.Unmarshal(bStateJSON, bState)
211
	assert.NoError(t, err)
212

213
	if allowedEmpty {
214
		assert.True(t, reflect.DeepEqual(aState, bState) || reflect.DeepEqual(aState, blankState))
215
	} else {
216
		assert.EqualValues(t, aState, bState)
217
	}
218
}
219

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 {
225
		return
226
	}
227

228
	blankState := new(podState)
229

230
	require.NotNil(t, a)
231
	require.NotNil(t, b)
232

233
	require.NotNil(t, a.config)
234
	require.NotNil(t, b.config)
235
	require.NotNil(t, a.state)
236
	require.NotNil(t, b.state)
237

238
	assert.Equal(t, a.valid, b.valid)
239

240
	assert.Equal(t, a.lock.ID(), b.lock.ID())
241

242
	assert.EqualValues(t, a.config, b.config)
243

244
	if allowedEmpty {
245
		assert.True(t, reflect.DeepEqual(a.state, b.state) || reflect.DeepEqual(a.state, blankState))
246
	} else {
247
		assert.EqualValues(t, a.state, b.state)
248
	}
249
}
250

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.