Skip to content

Commit

Permalink
chore: use container.State() function in tests (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Oct 6, 2022
1 parent 38fbdc6 commit 91df4c4
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions docker_test.go
Expand Up @@ -457,11 +457,7 @@ func TestContainerTerminationResetsState(t *testing.T) {

func TestContainerStopWithReaper(t *testing.T) {
ctx := context.Background()
client, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
t.Fatal(err)
}
client.NegotiateAPIVersion(ctx)

nginxA, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Expand All @@ -476,38 +472,34 @@ func TestContainerStopWithReaper(t *testing.T) {
require.NoError(t, err)
terminateContainerOnEnd(t, ctx, nginxA)

containerID := nginxA.GetContainerID()
resp, err := client.ContainerInspect(ctx, containerID)
state, err := nginxA.State(ctx)
if err != nil {
t.Fatal(err)
}
if resp.State.Running != true {
if state.Running != true {
t.Fatal("The container shoud be in running state")
}
stopTimeout := 10 * time.Second
err = nginxA.Stop(ctx, &stopTimeout)
if err != nil {
t.Fatal(err)
}
resp, err = client.ContainerInspect(ctx, containerID)

state, err = nginxA.State(ctx)
if err != nil {
t.Fatal(err)
}
if resp.State.Running != false {
if state.Running != false {
t.Fatal("The container shoud not be running")
}
if resp.State.Status != "exited" {
if state.Status != "exited" {
t.Fatal("The container shoud be in exited state")
}
}

func TestContainerTerminationWithReaper(t *testing.T) {
ctx := context.Background()
client, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
t.Fatal(err)
}
client.NegotiateAPIVersion(ctx)

nginxA, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Expand All @@ -521,31 +513,27 @@ func TestContainerTerminationWithReaper(t *testing.T) {
if err != nil {
t.Fatal(err)
}
containerID := nginxA.GetContainerID()
resp, err := client.ContainerInspect(ctx, containerID)

state, err := nginxA.State(ctx)
if err != nil {
t.Fatal(err)
}
if resp.State.Running != true {
if state.Running != true {
t.Fatal("The container shoud be in running state")
}
err = nginxA.Terminate(ctx)
if err != nil {
t.Fatal(err)
}
_, err = client.ContainerInspect(ctx, containerID)
_, err = nginxA.State(ctx)
if err == nil {
t.Fatal("expected error from container inspect.")
}
}

func TestContainerTerminationWithoutReaper(t *testing.T) {
ctx := context.Background()
client, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
t.Fatal(err)
}
client.NegotiateAPIVersion(ctx)

nginxA, err := GenericContainer(ctx, GenericContainerRequest{
ProviderType: providerType,
ContainerRequest: ContainerRequest{
Expand All @@ -560,19 +548,20 @@ func TestContainerTerminationWithoutReaper(t *testing.T) {
if err != nil {
t.Fatal(err)
}
containerID := nginxA.GetContainerID()
resp, err := client.ContainerInspect(ctx, containerID)

state, err := nginxA.State(ctx)
if err != nil {
t.Fatal(err)
}
if resp.State.Running != true {
if state.Running != true {
t.Fatal("The container shoud be in running state")
}
err = nginxA.Terminate(ctx)
if err != nil {
t.Fatal(err)
}
_, err = client.ContainerInspect(ctx, containerID)

_, err = nginxA.State(ctx)
if err == nil {
t.Fatal("expected error from container inspect.")
}
Expand Down

0 comments on commit 91df4c4

Please sign in to comment.