-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdockerfile_test.go
87 lines (77 loc) · 2.12 KB
/
dockerfile_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
87
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestDockerfileApplier_ApplyHeader(t *testing.T) {
tc := newTagContext(t)
defer func() { _ = tc.templateFiles.dTemplateFile.Close() }()
tmpDir := t.TempDir()
files := []string{
"Dockerfile.nodirectives",
"Dockerfile.comments",
"Dockerfile.single-directive",
"Dockerfile.multiple-directives",
}
d := dockerfileApplier{}
for _, f := range files {
fileName := f
t.Run(fileName, func(t *testing.T) {
testfile := filepath.Join(tmpDir, fileName)
copyFile(t, "./testdata/"+fileName, testfile)
err := d.ApplyHeader(testfile, &tc)
if err != nil {
t.Fatalf("failed to apply header to %s: %+v", testfile, err)
}
compareFiles(t, testfile, "./testdata/"+fileName+".golden")
})
}
}
func TestDockerfileApplier_CheckHeader(t *testing.T) {
files := []string{
// The non-golden files don't have a header present
"Dockerfile.nodirectives",
"Dockerfile.comments",
"Dockerfile.single-directive",
"Dockerfile.multiple-directives",
// The golden files should have the header present
"Dockerfile.nodirectives.golden",
"Dockerfile.comments.golden",
"Dockerfile.single-directive.golden",
"Dockerfile.multiple-directives.golden",
}
d := dockerfileApplier{}
tc := newTagContext(t)
defer func() { _ = tc.templateFiles.dTemplateFile.Close() }()
for _, f := range files {
fileName := f
t.Run(fileName, func(t *testing.T) {
f, err := os.Open("./testdata/" + fileName)
if err != nil {
t.Fatalf("failed to optn file %s: %+v", fileName, err)
}
defer func() { _ = f.Close() }()
found, err := d.CheckHeader(f, &tc)
if err != nil {
t.Fatalf("failed to check header: %+v", err)
}
expected := strings.HasSuffix(fileName, ".golden")
if found != expected {
t.Fail()
}
})
}
}
func newTagContext(t *testing.T) TagContext {
t.Helper()
templateFile, err := loadTemplate("./testdata/templates/", "dockerfile.txt")
if err != nil {
t.Fatalf("failed to load dockerfile template")
}
return TagContext{
templateFiles: TemplateFiles{dTemplateFile: templateFile},
templatePath: "./testdata/templates/",
}
}