Skip to content

Commit

Permalink
builder tests
Browse files Browse the repository at this point in the history
Signed-off-by: DaveF <fridrich.david19@gmail.com>
  • Loading branch information
gauron99 committed Mar 18, 2024
1 parent bb57089 commit f3cf731
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
37 changes: 37 additions & 0 deletions pkg/builders/buildpacks/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package buildpacks
import (
"context"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"testing"

pack "github.com/buildpacks/pack/pkg/client"
Expand Down Expand Up @@ -216,6 +218,41 @@ func TestBuild_Errors(t *testing.T) {
}
}

// TestBuild_WithoutHome ensures that test fails with HOME not being defined
func TestBuild_WithoutHome(t *testing.T) {
// home is empty, should fail for pack builder bcs it writes into .pack
os.Setenv("HOME", "")

b := NewBuilder()
err := b.Build(context.Background(), fn.Function{Runtime: "go"}, nil)
if err == nil || !strings.Contains(err.Error(), "$HOME is not defined") {
// if type is implemented this could test that type
t.Error("expected an error about HOME not being defined")
}
}

// TestBuild_UnwritableDotConfig ensures that test fails with HOME not being defined
func TestBuild_UnwritableDotConfig(t *testing.T) {
tempdir := t.TempDir()
f := fn.Function{Root: tempdir, Name: "myfunc", Runtime: "go", Build: fn.BuildSpec{Image: "example.com/alena"}}
t.Setenv("HOME", tempdir)
err := os.Mkdir(path.Join(tempdir, ".config"), 0000)
if err != nil {
t.Error(err)
}

imp := &mockImpl{}
imp.BuildFn = func(ctx context.Context, opts pack.BuildOptions) error {
return nil
}

b := NewBuilder(WithImpl(imp))
err = b.Build(context.Background(), f, nil)
if err != nil {
t.Error(err)
}
}

type mockImpl struct {
BuildFn func(context.Context, pack.BuildOptions) error
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/builders/s2i/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,32 @@ func TestBuildFail(t *testing.T) {
}
}

// TestBuild_WithoutHome ensures that test passes even with HOME not being defined
func TestBuild_WithoutHome(t *testing.T) {
// home is empty, should return success because s2i doesnt need to access
// home, contrary to the pack builder (which writes into .pack)
os.Setenv("HOME", "")

cli := mockDocker{
build: func(ctx context.Context, context io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
return types.ImageBuildResponse{
Body: io.NopCloser(strings.NewReader(`{"body": {"message": "all ok"}}`)),
OSType: "linux",
}, nil
},
}
impl := &mockImpl{
BuildFn: func(config *api.Config) (*api.Result, error) {
return &api.Result{Success: true}, nil
},
}
b := s2i.NewBuilder(s2i.WithImpl(impl), s2i.WithDockerClient(cli))
err := b.Build(context.Background(), fn.Function{Runtime: "node"}, nil)
if err != nil {
t.Error(err)
}
}

// mockImpl is a mock implementation of an S2I builder.
type mockImpl struct {
BuildFn func(*api.Config) (*api.Result, error)
Expand Down

0 comments on commit f3cf731

Please sign in to comment.