Skip to content

Commit b8b6ac6

Browse files
committed
tests: history cmds
Signed-off-by: CrazyMax <[email protected]>
1 parent ea02006 commit b8b6ac6

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

tests/history.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package tests
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
"path"
7+
"path/filepath"
8+
"strings"
9+
"testing"
10+
"time"
11+
12+
"github.com/moby/buildkit/util/testutil/integration"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
var historyTests = []func(t *testing.T, sb integration.Sandbox){
17+
testHistoryExport,
18+
testHistoryExportFinalize,
19+
testHistoryInspect,
20+
testHistoryLs,
21+
testHistoryRm,
22+
}
23+
24+
func testHistoryExport(t *testing.T, sb integration.Sandbox) {
25+
ref := buildTestProject(t, sb)
26+
require.NotEmpty(t, ref.Ref)
27+
28+
outFile := path.Join(t.TempDir(), "export.dockerbuild")
29+
cmd := buildxCmd(sb, withArgs("history", "export", ref.Ref, "--output", outFile))
30+
out, err := cmd.Output()
31+
require.NoError(t, err, string(out))
32+
require.FileExists(t, outFile)
33+
}
34+
35+
func testHistoryExportFinalize(t *testing.T, sb integration.Sandbox) {
36+
ref := buildTestProject(t, sb)
37+
require.NotEmpty(t, ref.Ref)
38+
39+
outFile := path.Join(t.TempDir(), "export.dockerbuild")
40+
cmd := buildxCmd(sb, withArgs("history", "export", ref.Ref, "--finalize", "--output", outFile))
41+
out, err := cmd.Output()
42+
require.NoError(t, err, string(out))
43+
require.FileExists(t, outFile)
44+
}
45+
46+
func testHistoryInspect(t *testing.T, sb integration.Sandbox) {
47+
ref := buildTestProject(t, sb)
48+
require.NotEmpty(t, ref.Ref)
49+
50+
cmd := buildxCmd(sb, withArgs("history", "inspect", ref.Ref, "--format=json"))
51+
out, err := cmd.Output()
52+
require.NoError(t, err, string(out))
53+
54+
type recT struct {
55+
Name string
56+
Ref string
57+
Context string
58+
Dockerfile string
59+
StartedAt *time.Time
60+
CompletedAt *time.Time
61+
Duration time.Duration
62+
Status string
63+
NumCompletedSteps int32
64+
NumTotalSteps int32
65+
NumCachedSteps int32
66+
}
67+
var rec recT
68+
err = json.Unmarshal(out, &rec)
69+
require.NoError(t, err)
70+
require.Equal(t, ref.Ref, rec.Ref)
71+
require.NotEmpty(t, rec.Name)
72+
}
73+
74+
func testHistoryLs(t *testing.T, sb integration.Sandbox) {
75+
ref := buildTestProject(t, sb)
76+
require.NotEmpty(t, ref.Ref)
77+
78+
cmd := buildxCmd(sb, withArgs("history", "ls", "--filter=ref="+ref.Ref, "--format=json"))
79+
out, err := cmd.Output()
80+
require.NoError(t, err, string(out))
81+
82+
type recT struct {
83+
Ref string `json:"ref"`
84+
Name string `json:"name"`
85+
Status string `json:"status"`
86+
CreatedAt *time.Time `json:"created_at"`
87+
CompletedAt *time.Time `json:"completed_at"`
88+
TotalSteps int32 `json:"total_steps"`
89+
CompletedSteps int32 `json:"completed_steps"`
90+
CachedSteps int32 `json:"cached_steps"`
91+
}
92+
var rec recT
93+
err = json.Unmarshal(out, &rec)
94+
require.NoError(t, err)
95+
require.Equal(t, ref.String(), rec.Ref)
96+
require.NotEmpty(t, rec.Name)
97+
}
98+
99+
func testHistoryRm(t *testing.T, sb integration.Sandbox) {
100+
ref := buildTestProject(t, sb)
101+
require.NotEmpty(t, ref.Ref)
102+
103+
cmd := buildxCmd(sb, withArgs("history", "rm", ref.Ref))
104+
out, err := cmd.Output()
105+
require.NoError(t, err, string(out))
106+
}
107+
108+
type buildRef struct {
109+
Builder string
110+
Node string
111+
Ref string
112+
}
113+
114+
func (b buildRef) String() string {
115+
return b.Builder + "/" + b.Node + "/" + b.Ref
116+
}
117+
118+
func buildTestProject(t *testing.T, sb integration.Sandbox) buildRef {
119+
dir := createTestProject(t)
120+
out, err := buildCmd(sb, withArgs("--metadata-file", filepath.Join(dir, "md.json"), dir))
121+
require.NoError(t, err, string(out))
122+
123+
dt, err := os.ReadFile(filepath.Join(dir, "md.json"))
124+
require.NoError(t, err)
125+
126+
type mdT struct {
127+
BuildRef string `json:"buildx.build.ref"`
128+
}
129+
var md mdT
130+
err = json.Unmarshal(dt, &md)
131+
require.NoError(t, err)
132+
133+
refParts := strings.Split(md.BuildRef, "/")
134+
require.Len(t, refParts, 3)
135+
136+
return buildRef{
137+
Builder: refParts[0],
138+
Node: refParts[1],
139+
Ref: refParts[2],
140+
}
141+
}

tests/integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestIntegration(t *testing.T) {
2424
tests = append(tests, commonTests...)
2525
tests = append(tests, buildTests...)
2626
tests = append(tests, bakeTests...)
27+
tests = append(tests, historyTests...)
2728
tests = append(tests, inspectTests...)
2829
tests = append(tests, lsTests...)
2930
tests = append(tests, imagetoolsTests...)

0 commit comments

Comments
 (0)