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

chore: refactor e2e tests to have a common suite for all tests #554

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions e2e/basic/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

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

func (s *Suite) TestProbe() {
Expand All @@ -16,16 +18,16 @@ func (s *Suite) TestProbe() {
executor, err := s.Executor.NewInstance(ctx, namePrefix+"-executor")
s.Require().NoError(err)

web := s.createNginxInstanceWithVolume(ctx, namePrefix+"-web")
web := s.CreateNginxInstanceWithVolume(ctx, namePrefix+"-web")

err = web.Storage().AddFile(resourcesHTML+"/index.html", nginxHTMLPath+"/index.html", "0:0")
err = web.Storage().AddFile(resourcesHTML+"/index.html", e2e.NginxHTMLPath+"/index.html", "0:0")
s.Require().NoError(err)

livenessProbe := v1.Probe{
ProbeHandler: v1.ProbeHandler{
HTTPGet: &v1.HTTPGetAction{
Path: "/",
Port: intstr.IntOrString{Type: intstr.Int, IntVal: nginxPort},
Port: intstr.IntOrString{Type: intstr.Int, IntVal: e2e.NginxPort},
},
},
InitialDelaySeconds: 10,
Expand Down
5 changes: 3 additions & 2 deletions e2e/basic/reverse_proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/celestiaorg/knuu/e2e"
"github.com/celestiaorg/knuu/pkg/sidecars/netshaper"
)

Expand Down Expand Up @@ -47,7 +48,7 @@ func (s *Suite) TestAddHostWithReadyCheck() {
const namePrefix = "add-host-with-ready-check"
ctx := context.Background()

target := s.createNginxInstance(ctx, namePrefix+"-target")
target := s.CreateNginxInstance(ctx, namePrefix+"-target")
s.Require().NoError(target.Build().Commit(ctx))
s.Require().NoError(target.Execution().Start(ctx))

Expand Down Expand Up @@ -75,7 +76,7 @@ func (s *Suite) TestAddHostWithReadyCheck() {
return strings.Contains(string(bodyBytes), "Welcome to nginx!"), nil
}

host, err := target.Network().AddHostWithReadyCheck(ctx, nginxPort, checkFunc)
host, err := target.Network().AddHostWithReadyCheck(ctx, e2e.NginxPort, checkFunc)
s.Require().NoError(err, "error adding host with ready check")
s.Assert().NotEmpty(host, "host should not be empty")

Expand Down
70 changes: 2 additions & 68 deletions e2e/basic/suite_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ package basic

import (
"context"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
"k8s.io/apimachinery/pkg/api/resource"

"github.com/celestiaorg/knuu/e2e"
"github.com/celestiaorg/knuu/pkg/instance"
"github.com/celestiaorg/knuu/pkg/k8s"
"github.com/celestiaorg/knuu/pkg/knuu"
"github.com/celestiaorg/knuu/pkg/minio"
Expand All @@ -21,31 +17,14 @@ import (
const (
testTimeout = time.Minute * 15 // the same time that is used in the ci/cd pipeline

nginxImage = "docker.io/nginx:latest"
nginxVolumeOwner = 0
nginxPort = 80
nginxHTMLPath = "/usr/share/nginx/html"
nginxCommand = "nginx -g daemon off"

resourcesHTML = "../system/resources/html"

alpineImage = "alpine:latest"
alpineImage = "alpine:latest"
)

type Suite struct {
mojtaba-esk marked this conversation as resolved.
Show resolved Hide resolved
suite.Suite
Knuu *knuu.Knuu
Executor e2e.Executor

cleanupMu sync.Mutex
totalTests atomic.Int32
finishedTests int32
e2e.Suite
mojtaba-esk marked this conversation as resolved.
Show resolved Hide resolved
}

var (
nginxVolume = resource.MustParse("1Gi")
)

func TestRunSuite(t *testing.T) {
suite.Run(t, new(Suite))
}
Expand Down Expand Up @@ -75,48 +54,3 @@ func (s *Suite) SetupSuite() {

s.Executor.Kn = s.Knuu
}

// SetupTest is a test setup function that is called before each test is run.
func (s *Suite) SetupTest() {
s.totalTests.Add(1)
s.T().Parallel()
}

// TearDownTest is a test teardown function that is called after each test is run.
func (s *Suite) TearDownTest() {
s.cleanupMu.Lock()
defer s.cleanupMu.Unlock()
s.finishedTests++

// if I am the last test to finish, I need to clean up the suite
if s.finishedTests == s.totalTests.Load() {
s.cleanupSuite()
}
}

func (s *Suite) cleanupSuite() {
s.T().Logf("Cleaning up knuu...")
if err := s.Knuu.CleanUp(context.Background()); err != nil {
s.T().Logf("Error cleaning up test suite: %v", err)
}
s.T().Logf("Knuu is cleaned up")
}

func (s *Suite) createNginxInstance(ctx context.Context, name string) *instance.Instance {
ins, err := s.Knuu.NewInstance(name)
s.Require().NoError(err)

s.Require().NoError(ins.Build().SetImage(ctx, nginxImage))
s.Require().NoError(ins.Network().AddPortTCP(nginxPort))
return ins
}

func (s *Suite) createNginxInstanceWithVolume(ctx context.Context, name string) *instance.Instance {
ins := s.createNginxInstance(ctx, name)

err := ins.Build().ExecuteCommand("mkdir", "-p", nginxHTMLPath)
s.Require().NoError(err)

s.Require().NoError(ins.Storage().AddVolumeWithOwner(nginxHTMLPath, nginxVolume, nginxVolumeOwner))
return ins
}
36 changes: 2 additions & 34 deletions e2e/netshaper/suite_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ package netshaper

import (
"context"
"sync"
"sync/atomic"
"testing"

"github.com/stretchr/testify/suite"

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

type Suite struct {
suite.Suite
Knuu *knuu.Knuu

cleanupMu sync.Mutex
totalTests atomic.Int32
finishedTests int32
e2e.Suite
mojtaba-esk marked this conversation as resolved.
Show resolved Hide resolved
}

func TestRunSuite(t *testing.T) {
Expand All @@ -35,29 +29,3 @@ func (s *Suite) SetupSuite() {
s.T().Logf("Scope: %s", s.Knuu.Scope)
s.Knuu.HandleStopSignal(ctx)
}

// SetupTest is a test setup function that is called before each test is run.
func (s *Suite) SetupTest() {
s.totalTests.Add(1)
s.T().Parallel()
}

// TearDownTest is a test teardown function that is called after each test is run.
func (s *Suite) TearDownTest() {
s.cleanupMu.Lock()
defer s.cleanupMu.Unlock()
s.finishedTests++

// if I am the last test to finish, I need to clean up the suite
if s.finishedTests == s.totalTests.Load() {
s.cleanupSuite()
}
}

func (s *Suite) cleanupSuite() {
s.T().Logf("Cleaning up knuu...")
if err := s.Knuu.CleanUp(context.Background()); err != nil {
s.T().Logf("Error cleaning up test suite: %v", err)
}
s.T().Logf("Knuu is cleaned up")
}
95 changes: 95 additions & 0 deletions e2e/suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package e2e

import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"

"github.com/stretchr/testify/suite"
"k8s.io/apimachinery/pkg/api/resource"

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

const (
NginxHTMLPath = "/usr/share/nginx/html"
NginxPort = 80
NginxCommand = "nginx -g daemon off"

nginxImage = "docker.io/nginx:latest"
nginxVolumeOwner = 0
)

type Suite struct {
suite.Suite
Knuu *knuu.Knuu
Executor Executor

cleanupMu sync.Mutex
totalTests atomic.Int32
finishedTests int32
}

var (
nginxVolume = resource.MustParse("1Gi")
)

// SetupTest is a test setup function that is called before each test is run.
func (s *Suite) SetupTest() {
s.totalTests.Add(1)
s.T().Parallel()
}

// TearDownTest is a test teardown function that is called after each test is run.
func (s *Suite) TearDownTest() {
s.cleanupMu.Lock()
defer s.cleanupMu.Unlock()
s.finishedTests++

// if I am the last test to finish, I need to clean up the suite
if s.finishedTests == s.totalTests.Load() {
s.cleanupSuite()
}
}

func (s *Suite) cleanupSuite() {
s.T().Logf("Cleaning up knuu...")
if err := s.Knuu.CleanUp(context.Background()); err != nil {
s.T().Logf("Error cleaning up test suite: %v", err)
}
s.T().Logf("Knuu is cleaned up")
}

func (s *Suite) CreateNginxInstance(ctx context.Context, name string) *instance.Instance {
ins, err := s.Knuu.NewInstance(name)
s.Require().NoError(err)

s.Require().NoError(ins.Build().SetImage(ctx, nginxImage))
s.Require().NoError(ins.Network().AddPortTCP(NginxPort))
return ins
}

func (s *Suite) CreateNginxInstanceWithVolume(ctx context.Context, name string) *instance.Instance {
ins := s.CreateNginxInstance(ctx, name)

err := ins.Build().ExecuteCommand("mkdir", "-p", NginxHTMLPath)
s.Require().NoError(err)

s.Require().NoError(ins.Storage().AddVolumeWithOwner(NginxHTMLPath, nginxVolume, nginxVolumeOwner))
return ins
}

func (s *Suite) RetryOperation(operation func() error, maxRetries int) error {
var err error
for i := 0; i < maxRetries; i++ {
s.T().Logf("Retrying operation (%d/%d)...", i+1, maxRetries)
if err = operation(); err == nil {
return nil
}
time.Sleep(time.Second * time.Duration(i+1))
}
return fmt.Errorf("operation failed after %d retries: %w", maxRetries, err)
}
2 changes: 1 addition & 1 deletion e2e/system/build_from_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *Suite) TestBuildFromGitWithModifications() {
s.Require().NoError(err)

s.T().Log("Setting git repo")
err = s.retryOperation(func() error {
err = s.RetryOperation(func() error {
return target.Build().SetGitRepo(ctx, builder.GitContext{
Repo: gitRepo,
Branch: gitBranch,
Expand Down
7 changes: 4 additions & 3 deletions e2e/system/env_to_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"strings"

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

Expand Down Expand Up @@ -37,11 +38,11 @@ func (s *Suite) TestEnvToJSON() {
for i := 0; i < numberOfInstances; i++ {
name := fmt.Sprintf("%s-web%d", namePrefix, i+1)

ins := s.createNginxInstance(ctx, name)
ins := s.CreateNginxInstance(ctx, name)
s.Require().NoError(ins.Build().Commit(ctx))
s.Require().NoError(ins.Execution().Start(ctx))

_, err = ins.Execution().ExecuteCommand(ctx, "mkdir", "-p", nginxHTMLPath)
_, err = ins.Execution().ExecuteCommand(ctx, "mkdir", "-p", e2e.NginxHTMLPath)
s.Require().NoError(err)

s.T().Logf("Writing JSON to instance '%v': %v", name, jsonString)
Expand All @@ -53,7 +54,7 @@ func (s *Suite) TestEnvToJSON() {
s.Require().NoError(err)

// for testing it, we also add it as index.html to the nginx server
_, err = ins.Execution().ExecuteCommand(ctx, "echo", fmt.Sprintf("'%s'", jsonString), ">", nginxHTMLPath+"/index.html")
_, err = ins.Execution().ExecuteCommand(ctx, "echo", fmt.Sprintf("'%s'", jsonString), ">", e2e.NginxHTMLPath+"/index.html")
s.Require().NoError(err, "writing JSON to instance '%v': %v", name, err)

instances[i] = ins
Expand Down
6 changes: 4 additions & 2 deletions e2e/system/external_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"io"
"os"
"path/filepath"

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

func (s *Suite) TestExternalFile() {
Expand All @@ -15,7 +17,7 @@ func (s *Suite) TestExternalFile() {
executor, err := s.Executor.NewInstance(ctx, namePrefix+"-executor")
s.Require().NoError(err)

server := s.createNginxInstance(ctx, namePrefix+"-server")
server := s.CreateNginxInstance(ctx, namePrefix+"-server")

// copy resources/html/index.html to /tmp/index.html
srcFile, err := os.Open(resourcesHTML + "/index.html")
Expand All @@ -35,7 +37,7 @@ func (s *Suite) TestExternalFile() {
// Ensure that the copy is successful by syncing the written data to the disk
s.Require().NoError(dstFile.Sync())

err = server.Storage().AddFile(htmlTmpPath, nginxHTMLPath+"/index.html", "0:0")
err = server.Storage().AddFile(htmlTmpPath, e2e.NginxHTMLPath+"/index.html", "0:0")
s.Require().NoError(err)

s.Require().NoError(server.Build().Commit(ctx))
Expand Down
Loading
Loading