Skip to content

Commit

Permalink
fix: support dest path for import-http
Browse files Browse the repository at this point in the history
Fixes issue #456

Signed-off-by: Ramkumar Chinchani <rchincha@cisco.com>
  • Loading branch information
rchincha committed May 1, 2023
1 parent b4f7294 commit 493210a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/stacker/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func acquireUrl(c types.StackerConfig, storage types.Storage, i string, cache st
return "", errors.Errorf("The requested hash of %s import is different than the actual hash: %s != %s",
i, expectedHash, remoteHash)
}
return Download(cache, i, progress, expectedHash, remoteHash, remoteSize, mode, uid, gid)
return Download(cache, i, progress, expectedHash, remoteHash, remoteSize, idest, mode, uid, gid)
} else if url.Scheme == "stacker" {
// we always Grab() things from stacker://, because we need to
// mount the container's rootfs to get them and don't
Expand Down
9 changes: 7 additions & 2 deletions pkg/stacker/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ import (

// download with caching support in the specified cache dir.
func Download(cacheDir string, url string, progress bool, expectedHash, remoteHash, remoteSize string,
mode *fs.FileMode, uid, gid int,
idest string, mode *fs.FileMode, uid, gid int,
) (string, error) {
name := path.Join(cacheDir, path.Base(url))
var name string
if idest != "" && idest[len(idest)-1:] != "/" {
name = path.Join(cacheDir, path.Base(idest))
} else {
name = path.Join(cacheDir, path.Base(url))
}

if fi, err := os.Stat(name); err == nil {
// Couldn't get remoteHash then use cached copy of import
Expand Down
30 changes: 30 additions & 0 deletions test/import-http.bats
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,34 @@ function teardown() {
[ "$(sha reference/nm_orig)" == "$(sha dest/img/rootfs/root/nm)" ]
}

@test "importing to a dest" {
cat > img/stacker1.yaml <<EOF
centos_base:
from:
type: oci
url: $CENTOS_OCI
run: |
ls
EOF
cat > img/stacker2.yaml <<EOF
img:
from:
type: oci
url: $(pwd)/oci:centos_base
import:
- path: https://www.cisco.com/favicon.ico
dest: /dest/icon
run: |
[ -f /dest/icon ]
[ ! -f /dest/favicon.ico ]
[ ! -f /stacker/favicon.ico ]
EOF
# Build base image
stacker build -f img/stacker1.yaml
umoci ls --layout oci
# First execution creates the cache
stacker build -f img/stacker2.yaml
umoci ls --layout oci
}

# Ideally there would tests to hit/miss cache for servers which provide a hash

0 comments on commit 493210a

Please sign in to comment.