Skip to content

Commit

Permalink
fix: build from git + minio & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Jun 25, 2024
1 parent fef3306 commit 6793a8b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
63 changes: 63 additions & 0 deletions e2e/system/build_from_git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package system

import (
"context"
"strings"
"testing"

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

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

func TestBuildFromGit(t *testing.T) {
Expand All @@ -22,6 +26,65 @@ func TestBuildFromGit(t *testing.T) {
kn, err := knuu.New(ctx, knuu.Options{TestScope: e2e.DefaultTestScope()})
require.NoError(t, err, "Error creating knuu")

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

t.Log("Building the image")

// This is a blocking call which builds the image from git repo
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")

t.Log("Image built")

t.Cleanup(func() {
if err := target.Destroy(ctx); err != nil {
t.Logf("Error destroying instance: %v", err)
}
})

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

t.Logf("Starting instance")

assert.NoError(t, target.Start(ctx))

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")

data = []byte(strings.TrimSpace(string(data)))
assert.Equal(t, []byte("Hello, World!"), data, "File bytes do not match")
}
func TestBuildFromGitWithModifications(t *testing.T) {
t.Parallel()

// Setup
ctx := context.Background()

k8sClient, err := k8s.NewClient(ctx, e2e.DefaultTestScope())
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)
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")

Expand Down
14 changes: 5 additions & 9 deletions e2e/system/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ package system
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)
}
logrus.Infof("Scope: %s", knuu.Scope())
// err := knuu.Initialize()
// if err != nil {
// logrus.Fatalf("error initializing knuu: %v", err)
// }
// logrus.Infof("Scope: %s", knuu.Scope())
exitVal := m.Run()
os.Exit(exitVal)
}
12 changes: 6 additions & 6 deletions pkg/knuu/knuu.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,6 @@ func setDefaults(ctx context.Context, k *Knuu) error {
k.timeout = defaultTimeout
}

if k.ImageBuilder == nil {
k.ImageBuilder = &kaniko.Kaniko{
SystemDependencies: k.SystemDependencies,
}
}

if k.K8sClient == nil {
var err error
k.K8sClient, err = k8s.NewClient(ctx, k.TestScope)
Expand All @@ -216,6 +210,12 @@ func setDefaults(ctx context.Context, k *Knuu) error {
}
}

if k.ImageBuilder == nil {
k.ImageBuilder = &kaniko.Kaniko{
SystemDependencies: k.SystemDependencies,
}
}

return nil
}

Expand Down

0 comments on commit 6793a8b

Please sign in to comment.