File: bundle_linux_test.go

package info (click to toggle)
containerd 2.1.4~ds2-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 21,772 kB
  • sloc: sh: 1,885; makefile: 591
file content (143 lines) | stat: -rw-r--r-- 3,563 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
   Copyright The containerd Authors.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package v2

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"syscall"
	"testing"

	"github.com/containerd/containerd/v2/pkg/namespaces"
	"github.com/containerd/containerd/v2/pkg/oci"
	"github.com/containerd/containerd/v2/pkg/testutil"
	"github.com/containerd/typeurl/v2"
	"github.com/opencontainers/runtime-spec/specs-go"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNewBundle(t *testing.T) {
	testutil.RequiresRoot(t)
	tests := []struct {
		userns bool
	}{{
		userns: false,
	}, {
		userns: true,
	}}
	const usernsGID = 4200

	for i, tc := range tests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			dir := t.TempDir()
			work := filepath.Join(dir, "work")
			state := filepath.Join(dir, "state")
			id := fmt.Sprintf("new-bundle-%d", i)
			spec := oci.Spec{}
			if tc.userns {
				spec.Linux = &specs.Linux{
					GIDMappings: []specs.LinuxIDMapping{{ContainerID: 0, HostID: usernsGID}},
				}
			}
			specAny, err := typeurl.MarshalAny(&spec)
			require.NoError(t, err, "failed to marshal spec")

			ctx := namespaces.WithNamespace(context.TODO(), namespaces.Default)
			b, err := NewBundle(ctx, work, state, id, specAny)
			require.NoError(t, err, "NewBundle should succeed")
			require.NotNil(t, b, "bundle should not be nil")

			fi, err := os.Stat(b.Path)
			assert.NoError(t, err, "should be able to stat bundle path")
			if tc.userns {
				assert.Equal(t, os.ModeDir|0710, fi.Mode(), "bundle path should be a directory with perm 0710")
			} else {
				assert.Equal(t, os.ModeDir|0700, fi.Mode(), "bundle path should be a directory with perm 0700")
			}
			stat, ok := fi.Sys().(*syscall.Stat_t)
			require.True(t, ok, "should assert to *syscall.Stat_t")
			expectedGID := uint32(0)
			if tc.userns {
				expectedGID = usernsGID
			}
			assert.Equal(t, expectedGID, stat.Gid, "gid should match")

		})
	}
}

func TestRemappedGID(t *testing.T) {
	tests := []struct {
		spec oci.Spec
		gid  uint32
	}{{
		// empty spec
		spec: oci.Spec{},
		gid:  0,
	}, {
		// empty Linux section
		spec: oci.Spec{
			Linux: &specs.Linux{},
		},
		gid: 0,
	}, {
		// empty ID mappings
		spec: oci.Spec{
			Linux: &specs.Linux{
				GIDMappings: make([]specs.LinuxIDMapping, 0),
			},
		},
		gid: 0,
	}, {
		// valid ID mapping
		spec: oci.Spec{
			Linux: &specs.Linux{
				GIDMappings: []specs.LinuxIDMapping{{
					ContainerID: 0,
					HostID:      1000,
				}},
			},
		},
		gid: 1000,
	}, {
		// missing ID mapping
		spec: oci.Spec{
			Linux: &specs.Linux{
				GIDMappings: []specs.LinuxIDMapping{{
					ContainerID: 100,
					HostID:      1000,
				}},
			},
		},
		gid: 0,
	}}

	for i, tc := range tests {
		t.Run(strconv.Itoa(i), func(t *testing.T) {
			s, err := json.Marshal(tc.spec)
			require.NoError(t, err, "failed to marshal spec")
			gid, err := remappedGID(s)
			assert.NoError(t, err, "should unmarshal successfully")
			assert.Equal(t, tc.gid, gid, "expected GID to match")
		})
	}
}