Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit df06ada

Browse files
committed
Test case to cover conversion of compose file into build.Option
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
1 parent 2f00844 commit df06ada

14 files changed

Lines changed: 123 additions & 14 deletions

File tree

e2e/build_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package e2e
22

33
import (
44
"encoding/json"
5-
"github.com/deislabs/cnab-go/bundle"
6-
"gotest.tools/assert"
7-
"gotest.tools/fs"
85
"io/ioutil"
96
"path"
107
"testing"
118

9+
"github.com/deislabs/cnab-go/bundle"
10+
"gotest.tools/assert"
11+
"gotest.tools/fs"
12+
1213
"gotest.tools/icmd"
1314
)
1415

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

32-
built := []string { bndl.InvocationImages[0].Digest, bndl.Images["web"].Digest, bndl.Images["worker"].Digest }
33+
built := []string{bndl.InvocationImages[0].Digest, bndl.Images["web"].Digest, bndl.Images["worker"].Digest}
3334
for _, ref := range built {
3435
cmd.Command = dockerCli.Command("inspect", ref)
3536
icmd.RunCmd(cmd).Assert(t, icmd.Success)

e2e/images_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@ package e2e
33
import (
44
"fmt"
55
"path/filepath"
6-
"regexp"
76
"testing"
87

98
"gotest.tools/assert"
109
"gotest.tools/fs"
1110
"gotest.tools/icmd"
1211
)
1312

14-
var (
15-
reg = regexp.MustCompile("Digest is (.*).")
16-
)
17-
18-
func insertBundles(t *testing.T, cmd icmd.Cmd, dir *fs.Dir, info dindSwarmAndRegistryInfo) {
13+
func insertBundles(t *testing.T, cmd icmd.Cmd, info dindSwarmAndRegistryInfo) {
1914
// Push an application so that we can later pull it by digest
2015
cmd.Command = dockerCli.Command("app", "build", "--tag", info.registryAddress+"/c-myapp", filepath.Join("testdata", "push-pull", "push-pull.dockerapp"))
2116
icmd.RunCmd(cmd).Assert(t, icmd.Success)
@@ -37,7 +32,7 @@ func TestImageList(t *testing.T) {
3732
dir := fs.NewDir(t, "")
3833
defer dir.Remove()
3934

40-
insertBundles(t, cmd, dir, info)
35+
insertBundles(t, cmd, info)
4136

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

58-
insertBundles(t, cmd, dir, info)
53+
insertBundles(t, cmd, info)
5954

6055
cmd.Command = dockerCli.Command("app", "image", "rm", info.registryAddress+"/c-myapp:latest")
6156
icmd.RunCmd(cmd).Assert(t, icmd.Expected{

internal/commands/build.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"io"
89
"io/ioutil"
910
"os"
1011
"path"
@@ -160,7 +161,7 @@ func runBuild(dockerCli command.Cli, application string, opt buildOptions) (refe
160161
return ref, nil
161162
}
162163

163-
func computeDigest(bundle *bundle.Bundle) (reference.Named, error) {
164+
func computeDigest(bundle io.WriterTo) (reference.Named, error) {
164165
b := bytes.Buffer{}
165166
_, err := bundle.WriteTo(&b)
166167
if err != nil {
@@ -245,7 +246,7 @@ func parseCompose(app *types.App, options buildOptions) (map[string]build.Option
245246
}
246247
var buildContext string
247248
dockerfilePath := "Dockerfile"
248-
buildargs := map[string]string{}
249+
var buildargs map[string]string
249250
switch bc.(type) {
250251
case string:
251252
buildContext = bc.(string)
@@ -258,6 +259,7 @@ func parseCompose(app *types.App, options buildOptions) (map[string]build.Option
258259
if a, ok := buildconfig["args"]; ok {
259260
switch a.(type) {
260261
case map[string]interface{}:
262+
buildargs = make(map[string]string)
261263
for k, v := range a.(map[string]interface{}) {
262264
buildargs[k] = v.(string)
263265
}

internal/commands/build_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package commands
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
7+
"github.com/docker/app/internal/packager"
8+
"github.com/docker/buildx/build"
9+
"gotest.tools/assert"
10+
)
11+
12+
func Test_parseCompose(t *testing.T) {
13+
tests := []struct {
14+
name string
15+
service string
16+
want build.Options
17+
wantErr bool
18+
}{
19+
{
20+
name: "simple",
21+
service: "web",
22+
want: build.Options{
23+
Inputs: build.Inputs{
24+
ContextPath: "testdata/web",
25+
DockerfilePath: "testdata/web/Dockerfile",
26+
},
27+
},
28+
},
29+
{
30+
name: "context",
31+
service: "web",
32+
want: build.Options{
33+
Inputs: build.Inputs{
34+
ContextPath: "testdata/web",
35+
DockerfilePath: "testdata/web/Dockerfile.custom",
36+
},
37+
},
38+
},
39+
{
40+
name: "withargs",
41+
service: "web",
42+
want: build.Options{
43+
Inputs: build.Inputs{
44+
ContextPath: "testdata/web",
45+
DockerfilePath: "testdata/web/Dockerfile",
46+
},
47+
BuildArgs: map[string]string{"foo": "bar"},
48+
},
49+
},
50+
}
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
54+
app, err := packager.Extract("testdata/" + tt.name)
55+
assert.NilError(t, err)
56+
57+
got, err := parseCompose(app, buildOptions{})
58+
if (err != nil) != tt.wantErr {
59+
t.Errorf("parseCompose() error = %v, wantErr %v", err, tt.wantErr)
60+
return
61+
}
62+
opt, ok := got[tt.service]
63+
if !ok {
64+
t.Errorf("parseCompose() error = %v, wantErr %v", err, tt.wantErr)
65+
return
66+
}
67+
if !reflect.DeepEqual(opt, tt.want) {
68+
t.Errorf("parseCompose() got = %v, want = %v", opt, tt.want)
69+
}
70+
})
71+
}
72+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: "3.6"
2+
services:
3+
web:
4+
build:
5+
context: ./web
6+
dockerfile: Dockerfile.custom
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 1.1.0-beta1
2+
name: simple
3+
description: "new fancy webapp with microservices"
4+
maintainers:
5+
- name: John Developer
6+
email: john.dev@example.com
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: "3.6"
2+
services:
3+
web:
4+
build: ./web
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 1.1.0-beta1
2+
name: simple
3+
description: "new fancy webapp with microservices"
4+
maintainers:
5+
- name: John Developer
6+
email: john.dev@example.com
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)