Skip to content

Commit 0d9dac8

Browse files
committed
tests: history export and ls
Signed-off-by: CrazyMax <[email protected]>
1 parent 567c90b commit 0d9dac8

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

tests/history.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
testHistoryLs,
19+
}
20+
21+
func testHistoryExport(t *testing.T, sb integration.Sandbox) {
22+
ref := buildTestProject(t, sb)
23+
24+
outDir := t.TempDir()
25+
cmd := buildxCmd(sb, withArgs("history", "export", ref, "--output", path.Join(outDir, "export.dockerbuild")))
26+
out, err := cmd.Output()
27+
require.Error(t, err, string(out))
28+
require.FileExists(t, path.Join(outDir, "export.dockerbuild"))
29+
}
30+
31+
func testHistoryLs(t *testing.T, sb integration.Sandbox) {
32+
ref := buildTestProject(t, sb)
33+
34+
cmd := buildxCmd(sb, withArgs("history", "ls", "--filter=ref="+ref, "--format=json"))
35+
out, err := cmd.Output()
36+
require.Error(t, err, string(out))
37+
38+
type recT struct {
39+
Ref string `json:"ref"`
40+
Name string `json:"name"`
41+
Status string `json:"status"`
42+
CreatedAt *time.Time `json:"created_at"`
43+
CompletedAt *time.Time `json:"completed_at"`
44+
TotalSteps int32 `json:"total_steps"`
45+
CompletedSteps int32 `json:"completed_steps"`
46+
CachedSteps int32 `json:"cached_steps"`
47+
}
48+
var rec recT
49+
err = json.Unmarshal(out, &rec)
50+
require.NoError(t, err)
51+
require.Equal(t, ref, rec.Ref)
52+
require.NotEmpty(t, rec.Name)
53+
}
54+
55+
func buildTestProject(t *testing.T, sb integration.Sandbox) string {
56+
dir := createTestProject(t)
57+
out, err := buildCmd(sb, withArgs("--metadata-file", filepath.Join(dir, "md.json"), dir))
58+
require.NoError(t, err, string(out))
59+
60+
dt, err := os.ReadFile(filepath.Join(dir, "md.json"))
61+
require.NoError(t, err)
62+
63+
type mdT struct {
64+
BuildRef string `json:"buildx.build.ref"`
65+
}
66+
var md mdT
67+
err = json.Unmarshal(dt, &md)
68+
require.NoError(t, err)
69+
70+
refParts := strings.Split(md.BuildRef, "/")
71+
require.Len(t, refParts, 3)
72+
require.NotEmpty(t, refParts[2])
73+
return refParts[2]
74+
}

tests/integration_test.go

+1
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)