Navigation Menu

Skip to content

Commit

Permalink
Fix: setting the container name to the image
Browse files Browse the repository at this point in the history
In commit 47ac961 the image name that is used
for setting the container name is taken from the resolved image
unless it is empty.

The image has the "Names" field and right now the first name is
taken. However, when the image is a tagged image, the container name
will end up using the original name instead of the given one.

For example:

$ buildah tag busybox busybox1
$ buildah from busybox1

Will set the name of the container as "busybox-working-container"
while it was expected to be "busybox1-working-container".

This patch fixes this particular issue.

Signed-off-by: Boaz Shuster <ripcurld.github@gmail.com>

Closes: #399
Approved by: rhatdan
  • Loading branch information
boaz0 authored and rh-atomic-bot committed Jan 26, 2018
1 parent 2dbb2a1 commit b68f88c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
22 changes: 19 additions & 3 deletions new.go
Expand Up @@ -77,6 +77,24 @@ func pullAndFindImage(store storage.Store, imageName string, options BuilderOpti
return img, ref, nil
}

func getImageName(name string, img *storage.Image) string {
imageName := name
if len(img.Names) > 0 {
imageName = img.Names[0]
// When the image used by the container is a tagged image
// the container name might be set to the original image instead of
// the image given in the "form" command line.
// This loop is supposed to fix this.
for _, n := range img.Names {
if strings.Contains(n, name) {
imageName = n
break
}
}
}
return imageName
}

func imageNamePrefix(imageName string) string {
prefix := imageName
s := strings.Split(imageName, "/")
Expand Down Expand Up @@ -207,9 +225,7 @@ func newBuilder(store storage.Store, options BuilderOptions) (*Builder, error) {
image := options.FromImage
imageID := ""
if img != nil {
if len(img.Names) > 0 {
image = img.Names[0]
}
image = getImageName(image, img)
imageID = img.ID
}
if manifest, config, err = imageManifestAndConfig(ref, systemContext); err != nil {
Expand Down
28 changes: 28 additions & 0 deletions new_test.go
@@ -0,0 +1,28 @@
package buildah

import (
"testing"

"github.com/containers/storage"
)

func TestGetImageName(t *testing.T) {
tt := []struct {
caseName string
name string
names []string
expected string
}{
{"tagged image", "busybox1", []string{"docker.io/library/busybox:latest", "docker.io/library/busybox1:latest"}, "docker.io/library/busybox1:latest"},
{"image name not in the resolved image names", "image1", []string{"docker.io/library/busybox:latest", "docker.io/library/busybox1:latest"}, "docker.io/library/busybox:latest"},
{"resolved image with empty name list", "image1", []string{}, "image1"},
}

for _, tc := range tt {
img := &storage.Image{Names: tc.names}
res := getImageName(tc.name, img)
if res != tc.expected {
t.Errorf("test case '%s' failed: expected %#v but got %#v", tc.caseName, tc.expected, res)
}
}
}
12 changes: 11 additions & 1 deletion tests/from.bats
Expand Up @@ -73,7 +73,6 @@ load helpers
}

@test "from-authenticate-cert-and-creds" {

mkdir -p ${TESTDIR}/auth
# Create creds and store in ${TESTDIR}/auth/htpasswd
# docker run --entrypoint htpasswd registry:2 -Bbn testuser testpassword > ${TESTDIR}/auth/htpasswd
Expand Down Expand Up @@ -112,3 +111,14 @@ load helpers
# buildah rm $ctrid
# buildah rmi -f $(buildah --debug=false images -q)
}

@test "from-tagged-image" {
# Github #396: Make sure the container name starts with the correct image even when it's tagged.
cid=$(buildah from --pull=false --signature-policy ${TESTSDIR}/policy.json scratch)
buildah commit --signature-policy ${TESTSDIR}/policy.json "$cid" scratch2
buildah rm $cid
buildah tag scratch2 scratch3
cid=$(buildah from --signature-policy ${TESTSDIR}/policy.json scratch3)
[ "$cid" == scratch3-working-container ]
buildah rmi -f --all
}

0 comments on commit b68f88c

Please sign in to comment.