Skip to content

Commit

Permalink
chore: use stacker subs not shell in tests
Browse files Browse the repository at this point in the history
To avoid confusion about what substitution syntax we are testing, where
possible, use quoted heredocs for stackerfiles in tests.

This replaces use of e.g. $BUSYBOX_OCI, which was getting substituted by
the shell before cat wrote it to a file, with ${{BUSYBOX_OCI}} in a
quoted heredoc, so bash doesn't try to substitute it (which would fail),
and instead stacker will see it and substitute it itself.
This means adding a lot of --substitute args around where they were not
previously required.

A couple of places still use the previous approach so we can use the
shell to sub in a file sha, for example.

Signed-off-by: Michael McCracken <mikmccra@cisco.com>
  • Loading branch information
mikemccracken committed Mar 1, 2024
1 parent 62fd661 commit 1429b71
Show file tree
Hide file tree
Showing 32 changed files with 389 additions and 379 deletions.
12 changes: 6 additions & 6 deletions test/annotations-namespace.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ function teardown() {
}

@test "namespace arg works" {
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
thing:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: ls
EOF
stacker build --annotations-namespace=namespace.example
stacker build --annotations-namespace=namespace.example --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
[ "$status" -eq 0 ]
manifest=$(cat oci/index.json | jq -r .manifests[0].digest | cut -f2 -d:)
namespace=$(cat oci/blobs/sha256/$manifest | jq -r .annotations | cut -f1 -d:)
[[ "$namespace" == *"namespace.example"* ]]
}

@test "default namespace arg works" {
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
thing:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: ls
EOF
stacker build
stacker build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
[ "$status" -eq 0 ]
manifest=$(cat oci/index.json | jq -r .manifests[0].digest | cut -f2 -d:)
namespace=$(cat oci/blobs/sha256/$manifest | jq -r .annotations | cut -f1 -d:)
Expand Down
6 changes: 3 additions & 3 deletions test/annotations.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ function teardown() {
}

@test "annotations work" {
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
thing:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: ls
annotations:
a.b.c.key: val
EOF
stacker build
stacker build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
[ "$status" -eq 0 ]
manifest=$(cat oci/index.json | jq -r .manifests[0].digest | cut -f2 -d:)
cat oci/blobs/sha256/$manifest | jq .
Expand Down
8 changes: 4 additions & 4 deletions test/args.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ function teardown() {
}

@test "workdir args" {
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
parent:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
child:
from:
type: built
Expand All @@ -24,7 +24,7 @@ EOF
# check defaults
tmpdir=$(mktemp -d)
chmod -R a+rwx $tmpdir
stacker --work-dir $tmpdir build
stacker --work-dir $tmpdir build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
[ -d $tmpdir ]
[ -d $tmpdir/.stacker ]
[ -d $tmpdir/roots ]
Expand All @@ -36,7 +36,7 @@ EOF
chmod -R a+rwx $tmpdir
stackerdir=$(mktemp -d)
chmod -R a+rwx $stackerdir
stacker --work-dir $tmpdir --stacker-dir $stackerdir build
stacker --work-dir $tmpdir --stacker-dir $stackerdir build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
[ -d $tmpdir ]
[ ! -d $tmpdir/.stacker ]
[ -d $tmpdir/roots ]
Expand Down
6 changes: 3 additions & 3 deletions test/asterisk.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ function teardown() {
}

@test "wildcards work in run section" {
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
a:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: |
mkdir /mybin
cp /bin/* /mybin
EOF
stacker build
stacker build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
umoci unpack --image oci:a dest
[ "$status" -eq 0 ]

Expand Down
18 changes: 9 additions & 9 deletions test/atomfs.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ function basic_test() {
require_privilege priv
local verity_arg=$1

cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
test:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: |
touch /hello
EOF
stacker build --layer-type=squashfs $verity_arg
stacker build --layer-type=squashfs $verity_arg --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
mkdir mountpoint
stacker internal-go atomfs mount test-squashfs mountpoint

Expand All @@ -63,11 +63,11 @@ EOF

@test "mount + umount + mount a tree of images works" {
require_privilege priv
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
base:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: touch /base
a:
from:
Expand All @@ -85,7 +85,7 @@ c:
tag: base
run: touch /c
EOF
stacker build --layer-type=squashfs
stacker build --layer-type=squashfs --substitute BUSYBOX_OCI=${BUSYBOX_OCI}

mkdir a
stacker internal-go atomfs mount a-squashfs a
Expand Down Expand Up @@ -137,15 +137,15 @@ EOF

@test "bad existing verity device is rejected" {
require_privilege priv
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
test:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: |
touch /hello
EOF
stacker build --layer-type=squashfs
stacker build --layer-type=squashfs --substitute BUSYBOX_OCI=${BUSYBOX_OCI}

manifest=$(cat oci/index.json | jq -r .manifests[0].digest | cut -f2 -d:)
first_layer_hash=$(cat oci/blobs/sha256/$manifest | jq -r .layers[0].digest | cut -f2 -d:)
Expand Down
48 changes: 24 additions & 24 deletions test/basic.bats
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ function teardown() {
}

@test "multiple stacker builds in a row" {
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
busybox:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
imports: import
EOF
echo 1 > import
stacker build
stacker build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
echo 2 > import
stacker build
stacker build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
echo 3 > import
stacker build
stacker build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
echo 4 > import
stacker build
stacker build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
}

@test "basic workings" {
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
busybox:
from:
type: tar
Expand All @@ -37,7 +37,7 @@ busybox:
- https://www.cisco.com/favicon.ico
- ./executable
run:
- cp /stacker/imports/${{FAVICON}} /${{FAVICON}}
- cp /stacker/imports/${{FAVICON}} ${{FAVICON}}
- ls -al /stacker
- cp /stacker/imports/executable /usr/bin/executable
entrypoint: echo hello world
Expand Down Expand Up @@ -103,7 +103,7 @@ EOF
cat oci/blobs/sha256/$manifest | jq -r '.annotations."io.stackeroci.stacker.stacker_yaml"' | sed '$ d' > stacker_yaml_annotation

# now we need to do --substitute FAVICON=favicon.ico
sed -e 's/$FAVICON/favicon.ico/g' stacker.yaml > stacker_after_subs
sed -e 's/${{FAVICON}}/favicon.ico/g' stacker.yaml > stacker_after_subs

diff -U5 stacker_yaml_annotation stacker_after_subs

Expand Down Expand Up @@ -139,15 +139,15 @@ EOF
}

@test "stacker.yaml without imports can run" {
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
busybox:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: |
touch /foo
EOF
stacker build
stacker build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
umoci unpack --image oci:busybox dest
[ -f dest/rootfs/foo ]
}
Expand All @@ -161,30 +161,30 @@ EOF

@test "use colons in roots-dir path name should fail" {
local tmpd=$(pwd)
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
busybox:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: |
touch /foo
EOF
bad_stacker --roots-dir $tmpd/with:colon build
bad_stacker --roots-dir $tmpd/with:colon build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
[ "$status" -eq 1 ]
echo $output | grep "forbidden"
}

@test "use colons in layer name should fail" {
local tmpd=$(pwd)
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
busybox:with:colon:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: |
touch /foo
EOF
bad_stacker build
bad_stacker build --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
[ "$status" -eq 1 ]
echo $output | grep "forbidden"
}
Expand All @@ -193,7 +193,7 @@ EOF
cat > subs.yaml << EOF
FAVICON: favicon.ico
EOF
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
busybox:
from:
type: tar
Expand All @@ -204,7 +204,7 @@ busybox:
- https://www.cisco.com/favicon.ico
- ./executable
run:
- cp /stacker/imports/\${{FAVICON}} /\${{FAVICON}}
- cp /stacker/imports/${{FAVICON}} ${{FAVICON}}
- ls -al /stacker
- cp /stacker/imports/executable /usr/bin/executable
entrypoint: echo hello world
Expand Down Expand Up @@ -270,7 +270,7 @@ EOF
cat oci/blobs/sha256/$manifest | jq -r '.annotations."io.stackeroci.stacker.stacker_yaml"' | sed '$ d' > stacker_yaml_annotation

# now we need to do --substitute FAVICON=favicon.ico
sed -e 's/$FAVICON/favicon.ico/g' stacker.yaml > stacker_after_subs
sed -e 's/${{FAVICON}}/favicon.ico/g' stacker.yaml > stacker_after_subs

diff -U5 stacker_yaml_annotation stacker_after_subs

Expand Down Expand Up @@ -306,13 +306,13 @@ EOF
}

@test "commas in substitute flags ok" {
cat > stacker.yaml <<EOF
cat > stacker.yaml <<"EOF"
busybox:
from:
type: oci
url: $BUSYBOX_OCI
url: ${{BUSYBOX_OCI}}
run: |
touch /foo
EOF
stacker build --substitute "a=b,c"
stacker build --substitute "a=b,c" --substitute BUSYBOX_OCI=${BUSYBOX_OCI}
}
Loading

0 comments on commit 1429b71

Please sign in to comment.