forked from Azure/dalec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_build_test.go
86 lines (73 loc) · 2.39 KB
/
source_build_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package dalec
import (
"context"
"testing"
)
func TestSourceBuild(t *testing.T) {
src := Source{
Build: &SourceBuild{
Source: Source{
Inline: &SourceInline{
File: &SourceInlineFile{
Contents: `
FROM busybox:latest
RUN echo hello
`,
},
},
},
},
}
ctx := context.Background()
ops := getSourceOp(ctx, t, src)
xID := "docker-image://docker.io/library/busybox:latest"
id := ops[0].GetSource().Identifier
if id != xID {
t.Errorf("expected identifier %q, got %q", xID, id)
}
// To reuse code, let's craft an equivalent SourceDockerImage with cmd's
// We'll use that to validate the ops we got from the build source with [checkCmd]
srcDI := SourceDockerImage{
Ref: xID,
Cmd: &Command{
Dir: "/", // Dockerfile defaults to /
Env: map[string]string{
// The dockerfile frontend auto-injects these env vars
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
},
Steps: []*BuildStep{
{Command: "echo hello"},
},
},
}
checkCmd(t, ops[1:], &Source{DockerImage: &srcDI}, [][]expectMount{noMountCheck, noMountCheck})
t.Run("with filters", func(t *testing.T) {
t.Run("subdir", func(t *testing.T) {
src := src
src.Path = "subdir"
// for build source, we expect to have a copy operation as the last op
ops := getSourceOp(ctx, t, src)
checkCmd(t, ops[1:len(ops)-1], &Source{DockerImage: &srcDI}, [][]expectMount{{rootMount}, {rootMount, expectMount{dest: "subdir"}}})
checkFilter(t, ops[len(ops)-1].GetFile(), &src)
})
t.Run("include and exclude", func(t *testing.T) {
src := src
src.Includes = []string{"foo", "bar"}
src.Excludes = []string{"baz"}
// for build source, we expect to have a copy operation as the last op
ops := getSourceOp(ctx, t, src)
checkCmd(t, ops[1:len(ops)-1], &Source{DockerImage: &srcDI}, [][]expectMount{noMountCheck, noMountCheck})
checkFilter(t, ops[len(ops)-1].GetFile(), &src)
})
t.Run("subpath with include-exclude", func(t *testing.T) {
src := src
src.Path = "subdir"
src.Includes = []string{"foo", "bar"}
src.Excludes = []string{"baz"}
// for build source, we expect to have a copy operation as the last op
ops := getSourceOp(ctx, t, src)
checkCmd(t, ops[1:len(ops)-1], &Source{DockerImage: &srcDI}, [][]expectMount{{rootMount}, {rootMount, expectMount{dest: "subdir"}}})
checkFilter(t, ops[len(ops)-1].GetFile(), &src)
})
})
}