Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: change basic test to suite #437

Merged
merged 11 commits into from
Jul 5, 2024
74 changes: 40 additions & 34 deletions e2e/basic/basic_test.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
package basic

import (
"testing"
"context"

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

"github.com/celestiaorg/knuu/pkg/knuu"
"github.com/celestiaorg/knuu/pkg/instance"
)

func TestBasic(t *testing.T) {
t.Parallel()
const (
testImage = "alpine:latest"
)

func (ts *TestSuite) TestBasic() {
ts.T().Parallel()
// Setup

instance, err := knuu.NewInstance("alpine")
if err != nil {
t.Fatalf("Error creating instance '%v':", err)
}
err = instance.SetImage("docker.io/alpine:latest")
if err != nil {
t.Fatalf("Error setting image: %v", err)
}
err = instance.SetCommand("sleep", "infinity")
if err != nil {
t.Fatalf("Error setting command: %v", err)
}
err = instance.Commit()
if err != nil {
t.Fatalf("Error committing instance: %v", err)
}
ctx := context.Background()

t.Cleanup(func() {
require.NoError(t, knuu.BatchDestroy(instance))
target, err := ts.Knuu.NewInstance("alpine")
ts.Require().NoError(err)

ts.Require().NoError(target.SetImage(ctx, testImage))
ts.Require().NoError(target.SetCommand("sleep", "infinity"))
ts.Require().NoError(target.Commit())

ts.T().Cleanup(func() {
ts.T().Log("Tearing down Basic Test...")
err := instance.BatchDestroy(ctx, target)
if err != nil {
ts.T().Logf("error destroying instances: %v", err)
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
})

// Test logic
// Test Logic
ts.Require().NoError(target.Start(ctx))
ts.Require().NoError(target.WaitInstanceIsRunning(ctx))

err = instance.Start()
if err != nil {
t.Fatalf("Error starting instance: %v", err)
// Perform the test
type testCase struct {
name string
}
err = instance.WaitInstanceIsRunning()
if err != nil {
t.Fatalf("Error waiting for instance to be running: %v", err)

tt := []testCase{
{"Hello World"},
}
smuu marked this conversation as resolved.
Show resolved Hide resolved
wget, err := instance.ExecuteCommand("echo", "Hello World!")
if err != nil {
t.Fatalf("Error executing command '%v':", err)

for _, tc := range tt {
tc := tc
ts.Run(tc.name, func() {
output, err := target.ExecuteCommand(ctx, "echo", tc.name)
ts.Require().NoError(err)

assert.Contains(ts.T(), output, tc.name)
})
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
smuu marked this conversation as resolved.
Show resolved Hide resolved
}

assert.Contains(t, wget, "Hello World!")
}
34 changes: 22 additions & 12 deletions e2e/basic/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@ package basic

import (
"os"
"testing"

"github.com/sirupsen/logrus"

"github.com/celestiaorg/knuu/pkg/knuu"
)

func TestMain(m *testing.M) {
err := knuu.Initialize()
if err != nil {
logrus.Fatalf("error initializing knuu: %v", err)
func convertViaMap(b bool) int {
table := map[bool]int{
true: 1,
false: 0,
}
logrus.Infof("Scope: %s", knuu.Scope())
exitVal := m.Run()
os.Exit(exitVal)
return table[b]
}
smuu marked this conversation as resolved.
Show resolved Hide resolved

func (ts *TestSuite) TestMain() {
ts.T().Parallel()
// Setup

// Test Logic
ts.T().Log("Running test case: TestMain")

// Perform the test
exitVal := ts.Run("TestMain", func() {
ts.T().Logf("Scope: %s", ts.Knuu.Scope())
})

exitValue := convertViaMap(exitVal)

os.Exit(exitValue)
}
40 changes: 40 additions & 0 deletions e2e/basic/suite_setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package basic

import (
"context"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"

"github.com/celestiaorg/knuu/pkg/knuu"
)

type TestSuite struct {
suite.Suite
Knuu *knuu.Knuu
}
smuu marked this conversation as resolved.
Show resolved Hide resolved

func (s *TestSuite) SetupSuite() {
var (
err error
ctx = context.Background()
)
s.Knuu, err = knuu.New(ctx, knuu.Options{})
s.Require().NoError(err)
s.Knuu.HandleStopSignal(ctx)
}

func (s *TestSuite) TearDownSuite() {
s.T().Cleanup(func() {
logrus.Info("Tearing down test suite...")
err := s.Knuu.CleanUp(context.Background())
smuu marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
s.T().Logf("Error cleaning up test suite: %v", err)
}
})
smuu marked this conversation as resolved.
Show resolved Hide resolved
}

func TestRunSuite(t *testing.T) {
suite.Run(t, new(TestSuite))
}
Loading