|
19 | 19 | package client |
20 | 20 |
|
21 | 21 | import ( |
| 22 | + "context" |
| 23 | + "strings" |
| 24 | + "sync" |
22 | 25 | "testing" |
23 | 26 |
|
| 27 | + "github.com/containerd/containerd/api/services/tasks/v1" |
| 28 | + "github.com/containerd/containerd/api/types/runc/options" |
24 | 29 | . "github.com/containerd/containerd/v2/client" |
25 | 30 | "github.com/containerd/containerd/v2/integration/images" |
26 | 31 | "github.com/containerd/containerd/v2/pkg/deprecation" |
| 32 | + "github.com/containerd/containerd/v2/pkg/oci" |
| 33 | + "github.com/containerd/containerd/v2/pkg/protobuf" |
| 34 | + "github.com/containerd/containerd/v2/plugins" |
| 35 | + "github.com/containerd/errdefs" |
| 36 | + "github.com/containerd/errdefs/pkg/errgrpc" |
27 | 37 | "github.com/containerd/platforms" |
| 38 | + "github.com/containerd/typeurl/v2" |
| 39 | + "github.com/google/go-cmp/cmp" |
| 40 | + "github.com/stretchr/testify/require" |
| 41 | + "google.golang.org/grpc" |
28 | 42 | ) |
29 | 43 |
|
30 | 44 | var ( |
@@ -63,3 +77,118 @@ func TestImagePullSchema1WithEmptyLayers(t *testing.T) { |
63 | 77 | t.Fatal(err) |
64 | 78 | } |
65 | 79 | } |
| 80 | + |
| 81 | +func TestNewTaskWithRuntimeOption(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + |
| 84 | + fakeTasks := &fakeTaskService{ |
| 85 | + TasksClient: tasks.NewTasksClient(nil), |
| 86 | + createRequests: map[string]*tasks.CreateTaskRequest{}, |
| 87 | + } |
| 88 | + |
| 89 | + cli, err := newClient(t, address, |
| 90 | + WithServices(WithTaskClient(fakeTasks)), |
| 91 | + ) |
| 92 | + require.NoError(t, err) |
| 93 | + defer cli.Close() |
| 94 | + |
| 95 | + var ( |
| 96 | + image Image |
| 97 | + ctx, cancel = testContext(t) |
| 98 | + ) |
| 99 | + defer cancel() |
| 100 | + |
| 101 | + image, err = cli.GetImage(ctx, testImage) |
| 102 | + require.NoError(t, err) |
| 103 | + |
| 104 | + for _, tc := range []struct { |
| 105 | + name string |
| 106 | + runtimeOption *options.Options |
| 107 | + taskOpts []NewTaskOpts |
| 108 | + expectedOptions *options.Options |
| 109 | + }{ |
| 110 | + { |
| 111 | + name: "should be empty options", |
| 112 | + runtimeOption: &options.Options{ |
| 113 | + BinaryName: "no-runc", |
| 114 | + }, |
| 115 | + expectedOptions: nil, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "should overwrite IOUid/ShimCgroup", |
| 119 | + runtimeOption: &options.Options{ |
| 120 | + BinaryName: "no-runc", |
| 121 | + ShimCgroup: "/abc", |
| 122 | + IoUid: 1000, |
| 123 | + SystemdCgroup: true, |
| 124 | + }, |
| 125 | + taskOpts: []NewTaskOpts{ |
| 126 | + WithUIDOwner(2000), |
| 127 | + WithGIDOwner(3000), |
| 128 | + WithShimCgroup("/def"), |
| 129 | + }, |
| 130 | + expectedOptions: &options.Options{ |
| 131 | + BinaryName: "no-runc", |
| 132 | + ShimCgroup: "/def", |
| 133 | + IoUid: 2000, |
| 134 | + IoGid: 3000, |
| 135 | + SystemdCgroup: true, |
| 136 | + }, |
| 137 | + }, |
| 138 | + } { |
| 139 | + t.Run(tc.name, func(t *testing.T) { |
| 140 | + id := strings.Replace(t.Name(), "/", "_", -1) |
| 141 | + |
| 142 | + container, err := cli.NewContainer( |
| 143 | + ctx, |
| 144 | + id, |
| 145 | + WithNewSnapshotView(id, image), |
| 146 | + WithNewSpec(oci.WithImageConfig(image), withExitStatus(7)), |
| 147 | + WithRuntime(plugins.RuntimeRuncV2, tc.runtimeOption), |
| 148 | + ) |
| 149 | + require.NoError(t, err) |
| 150 | + defer container.Delete(ctx, WithSnapshotCleanup) |
| 151 | + |
| 152 | + _, err = container.NewTask(ctx, empty(), tc.taskOpts...) |
| 153 | + require.NoError(t, err) |
| 154 | + |
| 155 | + fakeTasks.Lock() |
| 156 | + req := fakeTasks.createRequests[id] |
| 157 | + fakeTasks.Unlock() |
| 158 | + |
| 159 | + if tc.expectedOptions == nil { |
| 160 | + require.Nil(t, req.Options) |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + gotOptions := &options.Options{} |
| 165 | + require.NoError(t, typeurl.UnmarshalTo(req.Options, gotOptions)) |
| 166 | + require.True(t, cmp.Equal(tc.expectedOptions, gotOptions, protobuf.Compare)) |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +type fakeTaskService struct { |
| 172 | + sync.Mutex |
| 173 | + createRequests map[string]*tasks.CreateTaskRequest |
| 174 | + tasks.TasksClient |
| 175 | +} |
| 176 | + |
| 177 | +func (ts *fakeTaskService) Create(ctx context.Context, in *tasks.CreateTaskRequest, opts ...grpc.CallOption) (*tasks.CreateTaskResponse, error) { |
| 178 | + ts.Lock() |
| 179 | + defer ts.Unlock() |
| 180 | + |
| 181 | + ts.createRequests[in.ContainerID] = in |
| 182 | + return &tasks.CreateTaskResponse{ |
| 183 | + ContainerID: in.ContainerID, |
| 184 | + Pid: 1, |
| 185 | + }, nil |
| 186 | +} |
| 187 | + |
| 188 | +func (ts *fakeTaskService) Get(ctx context.Context, in *tasks.GetRequest, opts ...grpc.CallOption) (*tasks.GetResponse, error) { |
| 189 | + return nil, errgrpc.ToGRPC(errdefs.ErrNotFound) |
| 190 | +} |
| 191 | + |
| 192 | +func (ts *fakeTaskService) Delete(ctx context.Context, in *tasks.DeleteTaskRequest, opts ...grpc.CallOption) (*tasks.DeleteResponse, error) { |
| 193 | + return nil, errgrpc.ToGRPC(errdefs.ErrNotFound) |
| 194 | +} |
0 commit comments