Skip to content

Commit

Permalink
chore!: remove Executor from knuu (#508)
Browse files Browse the repository at this point in the history
* chore: rename TestScope field

* chore: bump k8s version to fix the DOS Vulnerability

* chore: bumped go version to fix it

* chore: deprecate Executor

* fix: rename the deprecated NewExecutor

* chore: refactor e2e system tests to new knuu obj (#520)

* chore: refactor system e2e tests to new knuu obj

* fix: added missing commit before start

* fix: added missing commit before start

* fix: added missing commit before start

* chore: code cleanup

* chore: some more cleanup
  • Loading branch information
mojtaba-esk authored Jul 24, 2024
1 parent 920bf78 commit 15389c6
Show file tree
Hide file tree
Showing 23 changed files with 601 additions and 925 deletions.
43 changes: 0 additions & 43 deletions e2e/assert_cleanups.go

This file was deleted.

55 changes: 0 additions & 55 deletions e2e/assert_create.go

This file was deleted.

24 changes: 20 additions & 4 deletions e2e/basic/probe_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package basic

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

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

func TestProbe(t *testing.T) {
t.Parallel()
// Setup

executor, err := knuu.NewExecutor()
// Ideally this has to be defined in the test suit setup
exe := e2e.Executor{
Kn: knuu.GetKnuuObj(),
}

ctx := context.Background()
executor, err := exe.NewInstance(ctx, "prob-executor")
if err != nil {
t.Fatalf("Error creating executor: %v", err)
}
Expand Down Expand Up @@ -60,7 +67,16 @@ func TestProbe(t *testing.T) {
}

t.Cleanup(func() {
require.NoError(t, knuu.BatchDestroy(executor.Instance, web))
// after refactor, we can use instance.BatchDestroy for simplicity
err := executor.Destroy(ctx)
if err != nil {
t.Logf("Error destroying instance: %v", err)
}

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

// Test logic
Expand All @@ -79,7 +95,7 @@ func TestProbe(t *testing.T) {
t.Fatalf("Error waiting for instance to be running: %v", err)
}

wget, err := executor.ExecuteCommand("wget", "-q", "-O", "-", webIP)
wget, err := executor.ExecuteCommand(ctx, "wget", "-q", "-O", "-", webIP)
if err != nil {
t.Fatalf("Error executing command '%v':", err)
}
Expand Down
35 changes: 17 additions & 18 deletions pkg/instance/executor.go → e2e/executor.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,58 @@
package instance
package e2e

import (
"context"

"k8s.io/apimachinery/pkg/api/resource"

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

const (
executorDefaultImage = "docker.io/nicolaka/netshoot:latest"
executorName = "executor"
sleepCommand = "sleep"
infinityArg = "infinity"
)

type Executor struct {
Kn *knuu.Knuu
}

var (
executorMemoryLimit = resource.MustParse("100Mi")
executorCpuLimit = resource.MustParse("100m")
)

type Executor struct {
*Instance
}

func NewExecutor(ctx context.Context, sysDeps system.SystemDependencies) (*Executor, error) {
i, err := New(executorName, sysDeps)
func (e *Executor) NewInstance(ctx context.Context, name string) (*instance.Instance, error) {
i, err := e.Kn.NewInstance(name)
if err != nil {
return nil, ErrCreatingInstance.Wrap(err)
return nil, err
}

if err := i.SetImage(ctx, executorDefaultImage); err != nil {
return nil, ErrSettingImage.Wrap(err)
return nil, err
}

if err := i.Commit(); err != nil {
return nil, ErrCommittingInstance.Wrap(err)
return nil, err
}

if err := i.SetArgs(sleepCommand, infinityArg); err != nil {
return nil, ErrSettingArgs.Wrap(err)
return nil, err
}

if err := i.SetMemory(executorMemoryLimit, executorMemoryLimit); err != nil {
return nil, ErrSettingMemory.Wrap(err)
return nil, err
}

if err := i.SetCPU(executorCpuLimit); err != nil {
return nil, ErrSettingCPU.Wrap(err)
return nil, err
}
i.instanceType = ExecutorInstance

if err := i.Start(ctx); err != nil {
return nil, ErrStartingInstance.Wrap(err)
return nil, err
}

return &Executor{Instance: i}, nil
return i, nil
}
101 changes: 36 additions & 65 deletions e2e/system/build_from_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,21 @@ package system
import (
"context"
"strings"
"testing"

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

"github.com/celestiaorg/knuu/pkg/builder"
"github.com/celestiaorg/knuu/pkg/k8s"
"github.com/celestiaorg/knuu/pkg/knuu"
"github.com/celestiaorg/knuu/pkg/minio"
)

func TestBuildFromGit(t *testing.T) {
t.Parallel()
func (s *Suite) TestBuildFromGit() {
const namePrefix = "build-from-git"
s.T().Parallel()

// Setup
ctx := context.Background()

// The default image builder is kaniko here
kn, err := knuu.New(ctx, knuu.Options{})
require.NoError(t, err, "Error creating knuu")

target, err := kn.NewInstance("git-builder")
require.NoError(t, err, "Error creating instance")
target, err := s.Knuu.NewInstance(namePrefix)
s.Require().NoError(err)

t.Log("Building the image")
s.T().Log("Building the image")

// This is a blocking call which builds the image from git repo
err = target.SetGitRepo(ctx, builder.GitContext{
Expand All @@ -37,84 +26,66 @@ func TestBuildFromGit(t *testing.T) {
Username: "",
Password: "",
})
require.NoError(t, err, "Error setting git repo")
s.Require().NoError(err)

t.Log("Image built")
s.T().Log("Image built")

t.Cleanup(func() {
if err := kn.CleanUp(ctx); err != nil {
t.Logf("Error cleaning up knuu: %v", err)
s.T().Cleanup(func() {
if err := target.Destroy(ctx); err != nil {
s.T().Logf("Error cleaning up knuu: %v", err)
}
})

require.NoError(t, target.Commit())

t.Logf("Starting instance")
s.Require().NoError(target.Commit())

assert.NoError(t, target.Start(ctx))
s.T().Logf("Starting instance")
s.Require().NoError(target.Start(ctx))

t.Logf("Instance started")
s.T().Logf("Instance started")

// The file is created by the dockerfile in the repo,
// so to make sure it is built correctly, we check the file
data, err := target.GetFileBytes(ctx, "/test.txt")
require.NoError(t, err, "Error getting file bytes")
s.Require().NoError(err)

data = []byte(strings.TrimSpace(string(data)))
assert.Equal(t, []byte("Hello, World!"), data, "File bytes do not match")
s.Assert().Equal([]byte("Hello, World!"), data, "File bytes do not match")
}
func TestBuildFromGitWithModifications(t *testing.T) {
t.Parallel()
func (s *Suite) TestBuildFromGitWithModifications() {
const namePrefix = "build-from-git-with-modifications"
s.T().Parallel()

// Setup
ctx := context.Background()

logger := logrus.New()

k8sClient, err := k8s.NewClient(ctx, knuu.DefaultScope(), logger)
require.NoError(t, err, "Error creating k8s client")

// Since we are modifying the git repo,
// we need to setup minio to allow the builder to push the changes
minioClient, err := minio.New(ctx, k8sClient, logger)
require.NoError(t, err, "Error creating minio client")

// The default image builder is kaniko here
kn, err := knuu.New(ctx, knuu.Options{
K8sClient: k8sClient,
MinioClient: minioClient,
})
require.NoError(t, err, "Error creating knuu")

sampleInstance, err := kn.NewInstance("git-builder")
require.NoError(t, err, "Error creating instance")
target, err := s.Knuu.NewInstance(namePrefix)
s.Require().NoError(err)

ctx := context.Background()
// This is a blocking call which builds the image from git repo
err = sampleInstance.SetGitRepo(ctx, builder.GitContext{
err = target.SetGitRepo(ctx, builder.GitContext{
Repo: "https://github.com/celestiaorg/knuu.git",
Branch: "test/build-from-git", // This branch has a Dockerfile and is protected as to not be deleted
Username: "",
Password: "",
})
require.NoError(t, err, "Error setting git repo")
s.Require().NoError(err)

require.NoError(t, sampleInstance.SetCommand("sleep", "infinity"), "Error setting command")
s.Require().NoError(target.SetCommand("sleep", "infinity"))

err = sampleInstance.AddFileBytes([]byte("Hello, world!"), "/home/hello.txt", "root:root")
require.NoError(t, err, "Error adding file")
err = target.AddFileBytes([]byte("Hello, world!"), "/home/hello.txt", "root:root")
s.Require().NoError(err, "Error adding file")

require.NoError(t, sampleInstance.Commit(), "Error committing instance")
s.Require().NoError(target.Commit())

t.Cleanup(func() {
if err := kn.CleanUp(ctx); err != nil {
t.Logf("Error cleaning up knuu: %v", err)
s.T().Cleanup(func() {
if err := target.Destroy(ctx); err != nil {
s.T().Logf("Error cleaning up knuu: %v", err)
}
})

require.NoError(t, sampleInstance.Start(ctx), "Error starting instance")
s.Require().NoError(target.Start(ctx))

data, err := sampleInstance.GetFileBytes(ctx, "/home/hello.txt")
require.NoError(t, err, "Error getting file bytes")
data, err := target.GetFileBytes(ctx, "/home/hello.txt")
s.Require().NoError(err, "Error getting file bytes")

require.Equal(t, []byte("Hello, world!"), data, "File bytes do not match")
s.Assert().Equal([]byte("Hello, world!"), data, "File bytes do not match")
}
Loading

0 comments on commit 15389c6

Please sign in to comment.