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
72 changes: 36 additions & 36 deletions e2e/basic/basic_test.go
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
package basic

import (
"testing"
"context"
"strings"

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

"github.com/celestiaorg/knuu/pkg/knuu"
const (
testImage = "alpine:latest"
)

func TestBasic(t *testing.T) {
t.Parallel()
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()

target, err := ts.Knuu.NewInstance("alpine")
ts.Require().NoError(err)

t.Cleanup(func() {
require.NoError(t, knuu.BatchDestroy(instance))
ts.Require().NoError(target.SetImage(ctx, testImage))
ts.Require().NoError(target.SetCommand("sleep", "infinity"))
ts.Require().NoError(target.Commit())

ts.T().Cleanup(func() {
if err := target.Destroy(ctx); err != nil {
ts.T().Logf("error destroying instance: %v", err)
}
smuu marked this conversation as resolved.
Show resolved Hide resolved
})

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

err = instance.Start()
if err != nil {
t.Fatalf("Error starting instance: %v", err)
}
err = instance.WaitInstanceIsRunning()
if err != nil {
t.Fatalf("Error waiting for instance to be running: %v", err)
}
wget, err := instance.ExecuteCommand("echo", "Hello World!")
if err != nil {
t.Fatalf("Error executing command '%v':", err)
// Perform the test
tt := []struct {
name string
}{
{name: "Hello World"},
}

assert.Contains(t, wget, "Hello World!")
for _, tc := range tt {
tc := tc
ts.Run(tc.name, func() {
output, err := target.ExecuteCommand(ctx, "echo", tc.name)
ts.Require().NoError(err)

output = strings.TrimSpace(output)
assert.Contains(ts.T(), output, tc.name)
})
}
}
42 changes: 42 additions & 0 deletions e2e/basic/suite_setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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{
TestScope: "e2e-basic",
})
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