integration/internal/container: refactor CreateExpectingErr

This utility was only used for a single test, and it was very limited
in functionality as it only allowed for a certain error-string to be
matched.

Let's change it into a more generic function; a helper that allows a
container to be created from a `TestContainerConfig` (which can be
constructed using `NewTestConfig`) and that returns the response from
client.ContainerCreate(), so that any result from that can be tested,
leaving it up to the test to check the results.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-08-11 14:00:51 +02:00
parent 0899ba4a3f
commit 74feadacf8
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 10 additions and 6 deletions
integration
container
internal/container

View file

@ -40,7 +40,8 @@ func testCreateFailureWithCgroupNs(t *testing.T, daemonNsMode string, errStr str
d.StartWithBusybox(t)
defer d.Stop(t)
container.CreateExpectingErr(ctx, t, apiClient, errStr, containerOpts...)
_, err := container.CreateFromConfig(ctx, apiClient, container.NewTestConfig(containerOpts...))
assert.ErrorContains(t, err, errStr)
}
func TestCgroupNamespacesRun(t *testing.T) {

View file

@ -62,11 +62,14 @@ func Create(ctx context.Context, t *testing.T, apiClient client.APIClient, ops .
return c.ID
}
// CreateExpectingErr creates a container, expecting an error with the specified message
func CreateExpectingErr(ctx context.Context, t *testing.T, apiClient client.APIClient, errMsg string, ops ...func(*TestContainerConfig)) {
config := NewTestConfig(ops...)
_, err := apiClient.ContainerCreate(ctx, config.Config, config.HostConfig, config.NetworkingConfig, config.Platform, config.Name)
assert.ErrorContains(t, err, errMsg)
// CreateFromConfig creates a container from the given TestContainerConfig.
//
// Example use:
//
// ctr, err := container.CreateFromConfig(ctx, apiClient, container.NewTestConfig(container.WithAutoRemove))
// assert.Check(t, err)
func CreateFromConfig(ctx context.Context, apiClient client.APIClient, config *TestContainerConfig) (container.CreateResponse, error) {
return apiClient.ContainerCreate(ctx, config.Config, config.HostConfig, config.NetworkingConfig, config.Platform, config.Name)
}
// Run creates and start a container with the specified options