Skip to content

Commit

Permalink
chore(tests): rename test variables to kill sshd container more consi…
Browse files Browse the repository at this point in the history
…stently (testcontainers#2597)

* chore: rename variables

* chore: use helper terminate function

* revert: run reaper-off tests hourly

* Revert "revert: run reaper-off tests hourly"

This reverts commit 3981e9b.

* revert: run reaper-off job only

* fix: remove network after the container in tests

* chore: do not use helper

* fix: honor t.Cleanup execution order

* Revert "revert: run reaper-off job only"

This reverts commit 1af71c4.
  • Loading branch information
mdelapenya authored Jun 24, 2024
1 parent 23f2bcd commit d2056bb
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions port_forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ func TestExposeHostPorts(t *testing.T) {
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
freePorts := make([]int, tt.numberOfPorts)
for _, tc := range tests {
t.Run(tc.name, func(tt *testing.T) {
freePorts := make([]int, tc.numberOfPorts)
for i := range freePorts {
freePort, err := getFreePort()
if err != nil {
t.Fatal(err)
tt.Fatal(err)
}

freePorts[i] = freePort

// create an http server for each port
server, err := createHttpServer(freePort)
if err != nil {
t.Fatal(err)
tt.Fatal(err)
}
go func() {
_ = server.ListenAndServe()
}()
t.Cleanup(func() {
tt.Cleanup(func() {
server.Close()
})
}
Expand All @@ -83,14 +83,16 @@ func TestExposeHostPorts(t *testing.T) {
Started: true,
}

if tt.hasNetwork {
nw, err := network.New(context.Background())
var nw *testcontainers.DockerNetwork
if tc.hasNetwork {
var err error
nw, err = network.New(context.Background())
if err != nil {
t.Fatal(err)
tt.Fatal(err)
}
t.Cleanup(func() {
tt.Cleanup(func() {
if err := nw.Remove(context.Background()); err != nil {
t.Fatal(err)
tt.Fatal(err)
}
})

Expand All @@ -99,31 +101,31 @@ func TestExposeHostPorts(t *testing.T) {
}

ctx := context.Background()
if !tt.hasHostAccess {
if !tc.hasHostAccess {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
defer cancel()
}

c, err := testcontainers.GenericContainer(ctx, req)
if err != nil {
t.Fatal(err)
tt.Fatal(err)
}
t.Cleanup(func() {
tt.Cleanup(func() {
if err := c.Terminate(context.Background()); err != nil {
t.Fatal(err)
tt.Fatal(err)
}
})

if tt.hasHostAccess {
if tc.hasHostAccess {
// create a container that has host access, which will
// automatically forward the port to the container
assertContainerHasHostAccess(t, c, freePorts...)
assertContainerHasHostAccess(tt, c, freePorts...)
} else {
// force cancellation because of timeout
time.Sleep(11 * time.Second)

assertContainerHasNoHostAccess(t, c, freePorts...)
assertContainerHasNoHostAccess(tt, c, freePorts...)
}
})
}
Expand Down

0 comments on commit d2056bb

Please sign in to comment.