Skip to content

Commit

Permalink
chore: refactor batch destroy (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk authored May 16, 2024
1 parent bf34125 commit 2dcd7ac
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 392 deletions.
51 changes: 21 additions & 30 deletions e2e/basic/assert_cleanups.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,35 @@ import (

// assertCleanupInstance is a helper function that cleans up a single instance.
func assertCleanupInstance(t *testing.T, instance *knuu.Instance) error {
if instance != nil {
err := instance.Destroy()
if err != nil {
t.Fatalf("Error destroying instance: %v", err)
}
if instance == nil {
t.Fatal("Instance is nil")
}

if err := instance.Destroy(); err != nil {
t.Fatalf("Error destroying instance: %v", err)
}
return nil
}

// assertCleanupInstances is a helper function that cleans up a list of instances.
func assertCleanupInstances(t *testing.T, executor *knuu.Executor, instances []*knuu.Instance) error {
if os.Getenv("KNUU_SKIP_CLEANUP") != "true" {
err := executor.Destroy()
if err != nil {
t.Fatalf("Error destroying executor: %v", err)
}

for _, instance := range instances {
if instance != nil {
err := instance.Destroy()
if err != nil {
t.Fatalf("Error destroying instance: %v", err)
}
}
}
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return nil
}
return nil
}

// BatchDestroy destroys a list of instances.
func BatchDestroy(instances ...*knuu.Instance) error {
for _, instance := range instances {
if instance != nil {
err := instance.Destroy()
if err != nil {
return err
}
}
if executor == nil {
t.Fatal("Executor is nil")
}

if err := executor.Destroy(); err != nil {
t.Fatalf("Error destroying executor: %v", err)
}

err := knuu.BatchDestroy(instances...)
if err != nil {
t.Fatalf("Error destroying instances: %v", err)
}

return nil
}
13 changes: 2 additions & 11 deletions e2e/basic/basic_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package basic

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/celestiaorg/knuu/pkg/knuu"
)
Expand All @@ -31,16 +31,7 @@ func TestBasic(t *testing.T) {
}

t.Cleanup(func() {
// Cleanup
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

err = instance.Destroy()
if err != nil {
t.Fatalf("Error destroying instance: %v", err)
}
require.NoError(t, knuu.BatchDestroy(instance))
})

// Test logic
Expand Down
33 changes: 4 additions & 29 deletions e2e/basic/bittwister_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"math"
"os"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -50,13 +49,7 @@ func TestBittwister_Bandwidth(t *testing.T) {
require.NoError(t, err, "Error cloning instance")

t.Cleanup(func() {
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

require.NoError(t, iperfServer.Destroy(), "Error destroying iperf-server instance")
require.NoError(t, iperfClient.Destroy(), "Error destroying iperf-client instance")
require.NoError(t, knuu.BatchDestroy(iperfServer, iperfClient))
})

// Prepare iperf client & server
Expand Down Expand Up @@ -190,13 +183,7 @@ func TestBittwister_Packetloss(t *testing.T) {
require.NoError(t, err, "Error cloning instance")

t.Cleanup(func() {
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

require.NoError(t, executor.Destroy(), "Error destroying executor instance")
require.NoError(t, target.Destroy(), "Error destroying target instance")
require.NoError(t, knuu.BatchDestroy(executor, target))
})

// Prepare ping executor & target
Expand Down Expand Up @@ -326,13 +313,7 @@ func TestBittwister_Latency(t *testing.T) {
require.NoError(t, err, "Error cloning instance")

t.Cleanup(func() {
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

require.NoError(t, executor.Destroy(), "Error destroying executor instance")
require.NoError(t, target.Destroy(), "Error destroying target instance")
require.NoError(t, knuu.BatchDestroy(executor, target))
})

// Prepare ping executor & target
Expand Down Expand Up @@ -479,13 +460,7 @@ func TestBittwister_Jitter(t *testing.T) {
require.NoError(t, err, "Error cloning instance")

t.Cleanup(func() {
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

require.NoError(t, executor.Destroy(), "Error destroying executor instance")
require.NoError(t, target.Destroy(), "Error destroying target instance")
require.NoError(t, knuu.BatchDestroy(executor, target))
})

// Prepare ping executor & target
Expand Down
7 changes: 1 addition & 6 deletions e2e/basic/build_from_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ func TestBuildFromGit(t *testing.T) {
require.NoError(t, instance.Commit(), "Error committing instance")

t.Cleanup(func() {
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

require.NoError(t, instance.Destroy(), "Error destroying instance")
require.NoError(t, knuu.BatchDestroy(instance))
})

// Test logic
Expand Down
19 changes: 3 additions & 16 deletions e2e/basic/env_to_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/celestiaorg/knuu/pkg/knuu"
)
Expand Down Expand Up @@ -81,22 +82,8 @@ func TestEnvToJSON(t *testing.T) {
}

t.Cleanup(func() {
// Cleanup
if os.Getenv("KNUU_SKIP_CLEANUP") != "true" {
err := executor.Destroy()
if err != nil {
t.Fatalf("Error destroying executor: %v", err)
}

for _, instance := range instances {
if instance != nil {
err := instance.Destroy()
if err != nil {
t.Fatalf("Error destroying instance: %v", err)
}
}
}
}
all := append(instances, executor.Instance)
require.NoError(t, knuu.BatchDestroy(all...))
})

// Test logic
Expand Down
22 changes: 5 additions & 17 deletions e2e/basic/external_file_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package basic

import (
"github.com/celestiaorg/knuu/pkg/knuu"
"github.com/stretchr/testify/assert"
"io"
"os"
"testing"

"github.com/celestiaorg/knuu/pkg/knuu"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestExternalFile(t *testing.T) {
Expand Down Expand Up @@ -66,21 +68,7 @@ func TestExternalFile(t *testing.T) {
}

t.Cleanup(func() {
// Cleanup
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

err = executor.Destroy()
if err != nil {
t.Fatalf("Error destroying executor: %v", err)
}

err = web.Destroy()
if err != nil {
t.Fatalf("Error destroying instance: %v", err)
}
require.NoError(t, knuu.BatchDestroy(executor.Instance, web))
})

// Test logic
Expand Down
32 changes: 3 additions & 29 deletions e2e/basic/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,7 @@ func TestFile(t *testing.T) {
}

t.Cleanup(func() {
// Cleanup
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

err = executor.Destroy()
if err != nil {
t.Fatalf("Error destroying executor: %v", err)
}

err = web.Destroy()
if err != nil {
t.Fatalf("Error destroying instance: %v", err)
}
require.NoError(t, knuu.BatchDestroy(executor.Instance, web))
})

// Test logic
Expand Down Expand Up @@ -101,13 +87,7 @@ func TestDownloadFileFromRunningInstance(t *testing.T) {
require.NoError(t, target.Start(), "Error starting instance")

t.Cleanup(func() {
// Cleanup
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

require.NoError(t, target.Destroy(), "Error destroying instance")
require.NoError(t, knuu.BatchDestroy(target))
})

// Test logic
Expand Down Expand Up @@ -137,13 +117,7 @@ func TestMinio(t *testing.T) {
require.NoError(t, target.Start(), "Error starting instance")

t.Cleanup(func() {
// Cleanup
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

require.NoError(t, target.Destroy(), "Error destroying instance")
require.NoError(t, knuu.BatchDestroy(target))
})

fileContent := "Hello World!"
Expand Down
20 changes: 0 additions & 20 deletions e2e/basic/file_test_image_cached_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package basic

import (
"fmt"
"os"
"sync"
"testing"

Expand Down Expand Up @@ -69,25 +68,6 @@ func TestFileCached(t *testing.T) {
}
})

t.Cleanup(func() {
// Cleanup
if os.Getenv("KNUU_SKIP_CLEANUP") != "true" {
err := executor.Destroy()
if err != nil {
t.Fatalf("Error destroying executor: %v", err)
}

for _, instance := range instances {
if instance != nil {
err := instance.Destroy()
if err != nil {
t.Fatalf("Error destroying instance: %v", err)
}
}
}
}
})

// Test logic
for _, instance := range instances {
err = instance.StartAsync()
Expand Down
18 changes: 2 additions & 16 deletions e2e/basic/folder_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package basic

import (
"os"
"testing"

"github.com/celestiaorg/knuu/pkg/knuu"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFolder(t *testing.T) {
Expand Down Expand Up @@ -40,21 +40,7 @@ func TestFolder(t *testing.T) {
}

t.Cleanup(func() {
// Cleanup
if os.Getenv("KNUU_SKIP_CLEANUP") == "true" {
t.Log("Skipping cleanup")
return
}

err = executor.Destroy()
if err != nil {
t.Fatalf("Error destroying executor: %v", err)
}

err = web.Destroy()
if err != nil {
t.Fatalf("Error destroying instance: %v", err)
}
require.NoError(t, knuu.BatchDestroy(executor.Instance, web))
})

// Test logic
Expand Down
Loading

0 comments on commit 2dcd7ac

Please sign in to comment.