Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
Test case to cover conversion of compose file into build.Option
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Oct 2, 2019
1 parent 6f3e3fe commit 2218cc1
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 14 deletions.
9 changes: 5 additions & 4 deletions e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package e2e

import (
"encoding/json"
"github.com/deislabs/cnab-go/bundle"
"gotest.tools/assert"
"gotest.tools/fs"
"io/ioutil"
"path"
"testing"

"github.com/deislabs/cnab-go/bundle"
"gotest.tools/assert"
"gotest.tools/fs"

"gotest.tools/icmd"
)

Expand All @@ -29,7 +30,7 @@ func TestBuild(t *testing.T) {
err = json.Unmarshal(data, &bndl)
assert.NilError(t, err)

built := []string { bndl.InvocationImages[0].Digest, bndl.Images["web"].Digest, bndl.Images["worker"].Digest }
built := []string{bndl.InvocationImages[0].Digest, bndl.Images["web"].Digest, bndl.Images["worker"].Digest}
for _, ref := range built {
cmd.Command = dockerCli.Command("inspect", ref)
icmd.RunCmd(cmd).Assert(t, icmd.Success)
Expand Down
11 changes: 3 additions & 8 deletions e2e/images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ package e2e
import (
"fmt"
"path/filepath"
"regexp"
"testing"

"gotest.tools/assert"
"gotest.tools/fs"
"gotest.tools/icmd"
)

var (
reg = regexp.MustCompile("Digest is (.*).")
)

func insertBundles(t *testing.T, cmd icmd.Cmd, dir *fs.Dir, info dindSwarmAndRegistryInfo) {
func insertBundles(t *testing.T, cmd icmd.Cmd, info dindSwarmAndRegistryInfo) {
// Push an application so that we can later pull it by digest
cmd.Command = dockerCli.Command("app", "build", "--tag", info.registryAddress+"/c-myapp", filepath.Join("testdata", "push-pull", "push-pull.dockerapp"))
icmd.RunCmd(cmd).Assert(t, icmd.Success)
Expand All @@ -37,7 +32,7 @@ func TestImageList(t *testing.T) {
dir := fs.NewDir(t, "")
defer dir.Remove()

insertBundles(t, cmd, dir, info)
insertBundles(t, cmd, info)

expected := `APP IMAGE APP NAME
%s push-pull
Expand All @@ -55,7 +50,7 @@ func TestImageRm(t *testing.T) {
dir := fs.NewDir(t, "")
defer dir.Remove()

insertBundles(t, cmd, dir, info)
insertBundles(t, cmd, info)

cmd.Command = dockerCli.Command("app", "image", "rm", info.registryAddress+"/c-myapp:latest")
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
Expand Down
6 changes: 4 additions & 2 deletions internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -160,7 +161,7 @@ func runBuild(dockerCli command.Cli, application string, opt buildOptions) (refe
return ref, nil
}

func computeDigest(bundle *bundle.Bundle) (reference.Named, error) {
func computeDigest(bundle io.WriterTo) (reference.Named, error) {
b := bytes.Buffer{}
_, err := bundle.WriteTo(&b)
if err != nil {
Expand Down Expand Up @@ -245,7 +246,7 @@ func parseCompose(app *types.App, options buildOptions) (map[string]build.Option
}
var buildContext string
dockerfilePath := "Dockerfile"
buildargs := map[string]string{}
var buildargs map[string]string
switch bc.(type) {
case string:
buildContext = bc.(string)
Expand All @@ -258,6 +259,7 @@ func parseCompose(app *types.App, options buildOptions) (map[string]build.Option
if a, ok := buildconfig["args"]; ok {
switch a.(type) {
case map[string]interface{}:
buildargs = make(map[string]string)
for k, v := range a.(map[string]interface{}) {
buildargs[k] = v.(string)
}
Expand Down
72 changes: 72 additions & 0 deletions internal/commands/build_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package commands

import (
"reflect"
"testing"

"github.com/docker/app/internal/packager"
"github.com/docker/buildx/build"
"gotest.tools/assert"
)

func Test_parseCompose(t *testing.T) {
tests := []struct {
name string
service string
want build.Options
wantErr bool
}{
{
name: "simple",
service: "web",
want: build.Options{
Inputs: build.Inputs{
ContextPath: "testdata/web",
DockerfilePath: "testdata/web/Dockerfile",
},
},
},
{
name: "context",
service: "web",
want: build.Options{
Inputs: build.Inputs{
ContextPath: "testdata/web",
DockerfilePath: "testdata/web/Dockerfile.custom",
},
},
},
{
name: "withargs",
service: "web",
want: build.Options{
Inputs: build.Inputs{
ContextPath: "testdata/web",
DockerfilePath: "testdata/web/Dockerfile",
},
BuildArgs: map[string]string{"foo": "bar"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

app, err := packager.Extract("testdata/" + tt.name)
assert.NilError(t, err)

got, err := parseCompose(app, buildOptions{})
if (err != nil) != tt.wantErr {
t.Errorf("parseCompose() error = %v, wantErr %v", err, tt.wantErr)
return
}
opt, ok := got[tt.service]
if !ok {
t.Errorf("parseCompose() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(opt, tt.want) {
t.Errorf("parseCompose() got = %v, want = %v", opt, tt.want)
}
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: "3.6"
services:
web:
build:
context: ./web
dockerfile: Dockerfile.custom
6 changes: 6 additions & 0 deletions internal/commands/testdata/context.dockerapp/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 1.1.0-beta1
name: simple
description: "new fancy webapp with microservices"
maintainers:
- name: John Developer
email: john.dev@example.com
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: "3.6"
services:
web:
build: ./web
6 changes: 6 additions & 0 deletions internal/commands/testdata/simple.dockerapp/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 1.1.0-beta1
name: simple
description: "new fancy webapp with microservices"
maintainers:
- name: John Developer
email: john.dev@example.com
1 change: 1 addition & 0 deletions internal/commands/testdata/simple.dockerapp/parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions internal/commands/testdata/web/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM scratch
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: "3.6"
services:
web:
build:
context: ./web
args:
foo: bar
6 changes: 6 additions & 0 deletions internal/commands/testdata/withargs.dockerapp/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 1.1.0-beta1
name: simple
description: "new fancy webapp with microservices"
maintainers:
- name: John Developer
email: john.dev@example.com
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 2218cc1

Please sign in to comment.