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

Commit 3064a5d

Browse files
committed
use docker/reference.Store to manage tags/digest by app image ID
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent df1c57e commit 3064a5d

File tree

19 files changed

+579
-578
lines changed

19 files changed

+579
-578
lines changed

Gopkg.lock

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/build_test.go

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,16 @@ func TestBuild(t *testing.T) {
3030
cmd.Command = dockerCli.Command("app", "build", "--tag", "single:1.0.0", "--iidfile", iidfile, "-f", path.Join(testDir, "single.dockerapp"), testDir)
3131
icmd.RunCmd(cmd).Assert(t, icmd.Success)
3232

33+
_, err := os.Stat(iidfile)
34+
assert.NilError(t, err)
35+
bytes, err := ioutil.ReadFile(iidfile)
36+
assert.NilError(t, err)
37+
iid := string(bytes)
38+
3339
cfg := getDockerConfigDir(t, cmd)
3440

35-
f := path.Join(cfg, "app", "bundles", "docker.io", "library", "single", "_tags", "1.0.0", image.BundleFilename)
41+
s := strings.Split(iid, ":")
42+
f := path.Join(cfg, "app", "bundles", "contents", s[0], s[1], image.BundleFilename)
3643
bndl, err := image.FromFile(f)
3744
assert.NilError(t, err)
3845

@@ -47,11 +54,6 @@ func TestBuild(t *testing.T) {
4754
assert.Assert(t, img.Image == "" || strings.Contains(img.Image, "@sha256:"))
4855
}
4956

50-
_, err = os.Stat(iidfile)
51-
assert.NilError(t, err)
52-
bytes, err := ioutil.ReadFile(iidfile)
53-
assert.NilError(t, err)
54-
iid := string(bytes)
5557
actualID, err := store.FromAppImage(bndl)
5658
assert.NilError(t, err)
5759
assert.Equal(t, iid, fmt.Sprintf("sha256:%s", actualID.String()))
@@ -67,31 +69,6 @@ func TestBuildMultiTag(t *testing.T) {
6769
tags := []string{"1.0.0", "latest"}
6870
cmd.Command = dockerCli.Command("app", "build", "--tag", "single:"+tags[0], "--tag", "single:"+tags[1], "--iidfile", iidfile, "-f", path.Join(testDir, "single.dockerapp"), testDir)
6971
icmd.RunCmd(cmd).Assert(t, icmd.Success)
70-
71-
cfg := getDockerConfigDir(t, cmd)
72-
73-
for _, tag := range tags {
74-
f := path.Join(cfg, "app", "bundles", "docker.io", "library", "single", "_tags", tag, image.BundleFilename)
75-
img, err := image.FromFile(f)
76-
assert.NilError(t, err)
77-
built := []string{img.InvocationImages[0].Digest, img.Images["web"].Digest, img.Images["worker"].Digest}
78-
for _, ref := range built {
79-
cmd.Command = dockerCli.Command("inspect", ref)
80-
icmd.RunCmd(cmd).Assert(t, icmd.Success)
81-
}
82-
for _, img := range img.Images {
83-
// Check all image not being built locally get a fixed reference
84-
assert.Assert(t, img.Image == "" || strings.Contains(img.Image, "@sha256:"))
85-
}
86-
_, err = os.Stat(iidfile)
87-
assert.NilError(t, err)
88-
bytes, err := ioutil.ReadFile(iidfile)
89-
assert.NilError(t, err)
90-
iid := string(bytes)
91-
actualID, err := store.FromAppImage(img)
92-
assert.NilError(t, err)
93-
assert.Equal(t, iid, fmt.Sprintf("sha256:%s", actualID.String()))
94-
}
9572
})
9673
}
9774

@@ -126,13 +103,13 @@ func TestBuildWithoutTag(t *testing.T) {
126103

127104
cfg := getDockerConfigDir(t, cmd)
128105

129-
f := path.Join(cfg, "app", "bundles", "_ids")
106+
f := path.Join(cfg, "app", "bundles", "contents", "sha256")
130107
infos, err := ioutil.ReadDir(f)
131108
assert.NilError(t, err)
132109
assert.Equal(t, len(infos), 1)
133110
id := infos[0].Name()
134111

135-
f = path.Join(cfg, "app", "bundles", "_ids", id, image.BundleFilename)
112+
f = path.Join(cfg, "app", "bundles", "contents", "sha256", id, image.BundleFilename)
136113
data, err := ioutil.ReadFile(f)
137114
assert.NilError(t, err)
138115
var bndl bundle.Bundle
@@ -157,13 +134,13 @@ func TestBuildWithArgs(t *testing.T) {
157134

158135
cfg := getDockerConfigDir(t, cmd)
159136

160-
f := path.Join(cfg, "app", "bundles", "_ids")
137+
f := path.Join(cfg, "app", "bundles", "contents", "sha256")
161138
infos, err := ioutil.ReadDir(f)
162139
assert.NilError(t, err)
163140
assert.Equal(t, len(infos), 1)
164141
id := infos[0].Name()
165142

166-
f = path.Join(cfg, "app", "bundles", "_ids", id, image.BundleFilename)
143+
f = path.Join(cfg, "app", "bundles", "contents", "sha256", id, image.BundleFilename)
167144
data, err := ioutil.ReadFile(f)
168145
assert.NilError(t, err)
169146
var bndl bundle.Bundle

e2e/compatibility_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"testing"
1414
"time"
1515

16+
"github.com/opencontainers/go-digest"
17+
1618
"gotest.tools/assert"
1719
"k8s.io/apimachinery/pkg/util/wait"
1820

@@ -72,9 +74,13 @@ func TestBackwardsCompatibilityV1(t *testing.T) {
7274
data, err := ioutil.ReadFile(filepath.Join("testdata", "compatibility", "bundle-v0.9.0.json"))
7375
assert.NilError(t, err)
7476
// update bundle
75-
bundleDir := filepath.Join(info.configDir, "app", "bundles", "docker.io", "library", "app-e2e", "_tags", "v0.9.0")
77+
dg := digest.SHA256.FromBytes(data)
78+
bundleDir := filepath.Join(info.configDir, "app", "bundles", "contents", dg.Algorithm().String(), dg.Encoded())
7679
assert.NilError(t, os.MkdirAll(bundleDir, os.FileMode(0777)))
7780
assert.NilError(t, ioutil.WriteFile(filepath.Join(bundleDir, "bundle.json"), data, os.FileMode(0644)))
81+
metadata := filepath.Join(info.configDir, "app", "bundles", "repositories.json")
82+
json := fmt.Sprintf("{\"Repositories\":{\"app-e2e\":{\"app-e2e:v0.9.0\":\"%s\"}}}", dg.String())
83+
assert.NilError(t, ioutil.WriteFile(metadata, []byte(json), 0777))
7884

7985
// load images build with an old Docker App version
8086
assert.NilError(t, loadAndTagImage(info, info.tmpDir, "app-e2e:0.1.0-invoc", "https://github.com/docker/app-e2e/raw/master/images/v0.9.0/app-e2e-invoc.tar"))

e2e/helper_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,14 @@ func runWithDindSwarmAndRegistry(t *testing.T, todo func(dindSwarmAndRegistryInf
122122
todo(runner)
123123
}
124124

125-
func build(t *testing.T, cmd icmd.Cmd, dockerCli dockerCliCommand, ref, path string) {
126-
cmd.Command = dockerCli.Command("app", "build", "-t", ref, path)
125+
func build(t *testing.T, cmd icmd.Cmd, dockerCli dockerCliCommand, ref, path string) string {
126+
iidfile := fs.NewFile(t, "iid")
127+
defer iidfile.Remove()
128+
cmd.Command = dockerCli.Command("app", "build", "--iidfile", iidfile.Path(), "-t", ref, path)
127129
icmd.RunCmd(cmd).Assert(t, icmd.Success)
130+
bytes, err := ioutil.ReadFile(iidfile.Path())
131+
assert.NilError(t, err)
132+
return string(bytes)
128133
}
129134

130135
// Container represents a docker container

e2e/images_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Deleted: b-simple-app:latest`,
148148
cmd.Command = dockerCli.Command("app", "image", "rm", "b-simple-app")
149149
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
150150
ExitCode: 1,
151-
Err: `b-simple-app:latest: reference not found`,
151+
Err: `b-simple-app: reference not found`,
152152
})
153153

154154
expectedOutput := "REPOSITORY TAG APP IMAGE ID APP NAME \n"

e2e/pushpull_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ func TestPushUnknown(t *testing.T) {
1919
cmd.Command = dockerCli.Command("app", "push", "unknown")
2020
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
2121
ExitCode: 1,
22-
Err: `could not push "unknown:latest": no such App image: failed to read bundle "docker.io/library/unknown:latest": unknown:latest: reference not found`,
22+
Err: `could not push "unknown": unknown: reference not found`,
2323
})
2424
})
2525

2626
t.Run("push invalid reference", func(t *testing.T) {
2727
cmd.Command = dockerCli.Command("app", "push", "@")
2828
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
2929
ExitCode: 1,
30-
Err: `could not push "@": invalid reference format`,
30+
Err: `could not push "@": could not parse "@" as a valid reference: invalid reference format`,
3131
})
3232
})
3333
}

e2e/relocation_test.go

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,6 @@ import (
1313
"gotest.tools/icmd"
1414
)
1515

16-
func TestRelocationMapCreatedOnPull(t *testing.T) {
17-
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
18-
cmd := info.configuredCmd
19-
cfg := getDockerConfigDir(t, cmd)
20-
21-
path := filepath.Join("testdata", "local")
22-
ref := info.registryAddress + "/test/local:a-tag"
23-
bundlePath := filepath.Join(cfg, "app", "bundles", strings.Replace(info.registryAddress, ":", "_", 1), "test", "local", "_tags", "a-tag")
24-
25-
// Given a pushed application
26-
build(t, cmd, dockerCli, ref, path)
27-
cmd.Command = dockerCli.Command("app", "push", ref)
28-
icmd.RunCmd(cmd).Assert(t, icmd.Success)
29-
// And given application files are remove
30-
assert.NilError(t, os.RemoveAll(bundlePath))
31-
_, err := os.Stat(filepath.Join(bundlePath, image.BundleFilename))
32-
assert.Assert(t, os.IsNotExist(err))
33-
34-
// When application is pulled
35-
cmd.Command = dockerCli.Command("app", "pull", ref)
36-
icmd.RunCmd(cmd).Assert(t, icmd.Success)
37-
38-
// Then the relocation map should exist
39-
_, err = os.Stat(filepath.Join(bundlePath, image.RelocationMapFilename))
40-
assert.NilError(t, err)
41-
})
42-
}
43-
4416
func TestRelocationMapRun(t *testing.T) {
4517
runWithDindSwarmAndRegistry(t, func(info dindSwarmAndRegistryInfo) {
4618
cmd := info.configuredCmd
@@ -82,7 +54,12 @@ func TestRelocationMapRun(t *testing.T) {
8254
t.Run("without-relocation-map", func(t *testing.T) {
8355
name := "test-relocation-map-run-without-relocation-map"
8456
// And given the relocation map is removed after the pull
85-
assert.NilError(t, os.RemoveAll(filepath.Join(bundlePath, image.RelocationMapFilename)))
57+
assert.NilError(t, filepath.Walk(filepath.Join(cfg, "app", "bundles", "contents"), func(path string, info os.FileInfo, err error) error {
58+
if info.Name() == image.RelocationMapFilename {
59+
os.Remove(path)
60+
}
61+
return nil
62+
}))
8663

8764
// Then the application cannot be run
8865
cmd.Command = dockerCli.Command("app", "run", "--name", name, ref)
@@ -98,19 +75,13 @@ func TestPushPulledApplication(t *testing.T) {
9875

9976
path := filepath.Join("testdata", "local")
10077
ref := info.registryAddress + "/test/local:a-tag"
101-
bundlePath := filepath.Join(cfg, "app", "bundles", strings.Replace(info.registryAddress, ":", "_", 1), "test", "local", "_tags", "a-tag")
10278

10379
// Given an application pushed on a registry
10480
build(t, cmd, dockerCli, ref, path)
10581
cmd.Command = dockerCli.Command("app", "push", ref)
10682
icmd.RunCmd(cmd).Assert(t, icmd.Success)
10783
// And given local images are removed
108-
cmd.Command = dockerCli.Command("rmi", "web", "local:1.1.0-beta1-invoc", "worker")
109-
icmd.RunCmd(cmd).Assert(t, icmd.Success)
110-
// And given application files are remove
111-
assert.NilError(t, os.RemoveAll(bundlePath))
112-
_, err := os.Stat(filepath.Join(bundlePath, image.BundleFilename))
113-
assert.Assert(t, os.IsNotExist(err))
84+
assert.NilError(t, os.RemoveAll(filepath.Join(cfg, "app", "bundles")))
11485

11586
// And given application is pulled from the registry
11687
cmd.Command = dockerCli.Command("app", "pull", ref)
@@ -119,13 +90,6 @@ func TestPushPulledApplication(t *testing.T) {
11990
// Then the application can still be pushed
12091
cmd.Command = dockerCli.Command("app", "push", ref)
12192
icmd.RunCmd(cmd).Assert(t, icmd.Success)
122-
123-
// If relocation map is removed
124-
assert.NilError(t, os.RemoveAll(filepath.Join(bundlePath, image.RelocationMapFilename)))
125-
126-
// Then the application cannot be pushed
127-
cmd.Command = dockerCli.Command("app", "push", ref)
128-
icmd.RunCmd(cmd).Assert(t, icmd.Expected{ExitCode: 1})
12993
})
13094
}
13195

internal/cnab/cnab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func PullBundle(dockerCli command.Cli, imageStore appstore.ImageStore, tagRef re
123123
return nil, err
124124
}
125125
relocatedBundle := &image.AppImage{Bundle: bndl, RelocationMap: relocationMap}
126-
if _, err := imageStore.Store(tagRef, relocatedBundle); err != nil {
126+
if _, err := imageStore.Store(relocatedBundle, tagRef); err != nil {
127127
return nil, err
128128
}
129129
return relocatedBundle, nil

internal/commands/image/list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type imageStoreStubForListCmd struct {
2222
refList []reference.Reference
2323
}
2424

25-
func (b *imageStoreStubForListCmd) Store(ref reference.Reference, bndl *image.AppImage) (reference.Digested, error) {
25+
func (b *imageStoreStubForListCmd) Store(bndl *image.AppImage, ref reference.Reference) (reference.Digested, error) {
2626
b.refMap[ref] = bndl
2727
b.refList = append(b.refList, ref)
2828
return store.FromAppImage(bndl)
@@ -142,7 +142,7 @@ func testRunList(t *testing.T, refs []reference.Reference, bundles []image.AppIm
142142
refList: []reference.Reference{},
143143
}
144144
for i, ref := range refs {
145-
_, err = imageStore.Store(ref, &bundles[i])
145+
_, err = imageStore.Store(&bundles[i], ref)
146146
assert.NilError(t, err)
147147
}
148148
err = runList(dockerCli, options, imageStore)

internal/commands/image/tag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ func storeBundle(bundle *image.AppImage, name string, imageStore store.ImageStor
7373
if err != nil {
7474
return err
7575
}
76-
_, err = imageStore.Store(cnabRef, bundle)
76+
_, err = imageStore.Store(bundle, cnabRef)
7777
return err
7878
}

0 commit comments

Comments
 (0)