github.com/opencontainers/umoci@v0.4.8-0.20240508124516-656e4836fb0d/test/insert.bats (about)

     1  #!/usr/bin/env bats -t
     2  # umoci: Umoci Modifies Open Containers' Images
     3  # Copyright (C) 2016-2020 SUSE LLC
     4  # Copyright (C) 2018 Cisco Systems
     5  #
     6  # Licensed under the Apache License, Version 2.0 (the "License");
     7  # you may not use this file except in compliance with the License.
     8  # You may obtain a copy of the License at
     9  #
    10  #   http://www.apache.org/licenses/LICENSE-2.0
    11  #
    12  # Unless required by applicable law or agreed to in writing, software
    13  # distributed under the License is distributed on an "AS IS" BASIS,
    14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  # See the License for the specific language governing permissions and
    16  # limitations under the License.
    17  
    18  
    19  load helpers
    20  
    21  function setup() {
    22  	setup_tmpdirs
    23  	setup_image
    24  }
    25  
    26  function teardown() {
    27  	teardown_tmpdirs
    28  	teardown_image
    29  }
    30  
    31  @test "umoci insert" {
    32  	# fail with too few arguments
    33  	umoci insert --image "${IMAGE}:${TAG}"
    34  	[ "$status" -ne 0 ]
    35  	image-verify "${IMAGE}"
    36  
    37  	# ...and too many
    38  	umoci insert --image "${IMAGE}:${TAG}" asdf 123 456
    39  	[ "$status" -ne 0 ]
    40  	image-verify "${IMAGE}"
    41  
    42  	# Some things to insert.
    43  	INSERTDIR="$(setup_tmpdir)"
    44  	mkdir -p "${INSERTDIR}/test"
    45  	touch "${INSERTDIR}/test/a"
    46  	touch "${INSERTDIR}/test/b"
    47  	chmod +x "${INSERTDIR}/test/b"
    48  	echo "foo" > "${INSERTDIR}/test/smallfile"
    49  
    50  	# Make sure rootless mode works.
    51  	mkdir -p "${INSERTDIR}/some/path"
    52  	touch "${INSERTDIR}/some/path/hidden"
    53  	chmod 000 "${INSERTDIR}/some/path"
    54  
    55  	# Do a few inserts.
    56  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/test/a" /tester/a
    57  	[ "$status" -eq 0 ]
    58  	image-verify "${IMAGE}"
    59  
    60  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/test/b" /tester/b
    61  	[ "$status" -eq 0 ]
    62  	image-verify "${IMAGE}"
    63  
    64  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/test" /recursive
    65  	[ "$status" -eq 0 ]
    66  	image-verify "${IMAGE}"
    67  
    68  	umoci insert --image "${IMAGE}:${TAG}" --tag "${TAG}-new" "${INSERTDIR}/some" /rootless
    69  	[ "$status" -eq 0 ]
    70  	image-verify "${IMAGE}"
    71  
    72  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/test/smallfile" /tester/smallfile
    73  	[ "$status" -eq 0 ]
    74  	image-verify "${IMAGE}"
    75  
    76  	# Unpack after the inserts.
    77  	new_bundle_rootfs
    78  	umoci unpack --image "${IMAGE}:${TAG}-new" "$BUNDLE"
    79  	[ "$status" -eq 0 ]
    80  	bundle-verify "$BUNDLE"
    81  
    82  	# ... and check to make sure it worked.
    83  	[ -f "$ROOTFS/tester/a" ]
    84  	[[ "$(stat -c '%f' "${INSERTDIR}/test/b")" == "$(stat -c '%f' "$ROOTFS/tester/b")" ]]
    85  	[ -f "$ROOTFS/recursive/a" ]
    86  	[ -f "$ROOTFS/recursive/b" ]
    87  
    88  	# ... as well as the rootless portion.
    89  	[ -d "$ROOTFS/rootless/path" ]
    90  	[[ "$(stat -c '%f' "${INSERTDIR}/some/path")" == "$(stat -c '%f' "$ROOTFS/rootless/path")" ]]
    91  	chmod a+rwx "$ROOTFS/rootless/path"
    92  	[ -f "$ROOTFS/rootless/path/hidden" ]
    93  
    94  	image-verify "${IMAGE}"
    95  }
    96  
    97  @test "umoci insert [invalid arguments]" {
    98  	# Some things to insert.
    99  	INSERTDIR="$(setup_tmpdir)"
   100  	touch "$INSERTDIR/foobar"
   101  
   102  	# Missing --image, source, and target argument.
   103  	umoci insert
   104  	[ "$status" -ne 0 ]
   105  	image-verify "${IMAGE}"
   106  
   107  	# Missing --image and target argument.
   108  	umoci insert "$INSERTDIR"
   109  	[ "$status" -ne 0 ]
   110  	image-verify "${IMAGE}"
   111  
   112  	# Missing source and target argument.
   113  	umoci insert --image "${IMAGE}:${TAG}"
   114  	[ "$status" -ne 0 ]
   115  	image-verify "${IMAGE}"
   116  
   117  	# Missing target argument.
   118  	umoci insert --image "${IMAGE}:${TAG}" "$INSERTDIR"
   119  	[ "$status" -ne 0 ]
   120  	image-verify "${IMAGE}"
   121  
   122  	# Unknown flag argument.
   123  	umoci insert --this-is-an-invalid-argument \
   124  		--image "${IMAGE}:${TAG}" "$INSERTDIR" /foo/bar
   125  	[ "$status" -ne 0 ]
   126  	image-verify "${IMAGE}"
   127  
   128  	# Empty positional arguments.
   129  	umoci insert --image "${IMAGE}:${TAG}" "$INSERTDIR" ""
   130  	[ "$status" -ne 0 ]
   131  	image-verify "${IMAGE}"
   132  
   133  	umoci insert --image "${IMAGE}:${TAG}" "" /foo/bar
   134  	[ "$status" -ne 0 ]
   135  	image-verify "${IMAGE}"
   136  
   137  	# Empty positional arguments (--whiteout).
   138  	umoci insert --image "${IMAGE}:${TAG}" --whiteout ""
   139  	[ "$status" -ne 0 ]
   140  	image-verify "${IMAGE}"
   141  
   142  	# Too many positional arguments.
   143  	umoci insert --image "${IMAGE}:${TAG}" "$INSERTDIR" /foo/bar this-is-an-invalid-argument
   144  	[ "$status" -ne 0 ]
   145  	image-verify "${IMAGE}"
   146  
   147  	# Too many positional arguments (--whiteout).
   148  	umoci insert --image "${IMAGE}:${TAG}" --whiteout /foo/bar this-is-an-invalid-argument
   149  	[ "$status" -ne 0 ]
   150  	image-verify "${IMAGE}"
   151  
   152  	# Empty image path.
   153  	umoci insert --image ":${TAG}" "$INSERTDIR" /foo/bar
   154  	[ "$status" -ne 0 ]
   155  	image-verify "${IMAGE}"
   156  
   157  	# Non-existent image path.
   158  	umoci insert --image "${IMAGE}-doesnotexist:${TAG}" "$INSERTDIR" /foo/bar
   159  	[ "$status" -ne 0 ]
   160  	image-verify "${IMAGE}"
   161  
   162  	# Empty image source tag.
   163  	umoci insert --image "${IMAGE}:" "$INSERTDIR" /foo/bar
   164  	[ "$status" -ne 0 ]
   165  	image-verify "${IMAGE}"
   166  
   167  	# Non-existent image source tag.
   168  	umoci insert --image "${IMAGE}:${TAG}-doesnotexist" "$INSERTDIR" /foo/bar
   169  	[ "$status" -ne 0 ]
   170  	image-verify "${IMAGE}"
   171  
   172  	# Invalid image source tag.
   173  	umoci insert --image "${IMAGE}:${INVALID_TAG}" "$INSERTDIR" /foo/bar
   174  	[ "$status" -ne 0 ]
   175  	image-verify "${IMAGE}"
   176  
   177  	# Empty image destination tag.
   178  	umoci insert --image "${IMAGE}:${TAG}" --tag "" "$INSERTDIR" /foo/bar
   179  	[ "$status" -ne 0 ]
   180  	image-verify "${IMAGE}"
   181  
   182  	# Invalid image destination tag.
   183  	umoci insert --image "${IMAGE}:${TAG}" --tag "${INVALID_TAG}" "$INSERTDIR" /foo/bar
   184  	[ "$status" -ne 0 ]
   185  	image-verify "${IMAGE}"
   186  
   187  	# Insertion of a file with a .wh. prefix must fail.
   188  	umoci insert --image "${IMAGE}:${TAG}" "$INSERTDIR" /foo/bar/.wh.invalid-path
   189  	[ "$status" -ne 0 ]
   190  	image-verify "${IMAGE}"
   191  
   192  	# Whiteout of a file with a .wh. prefix must fail.
   193  	umoci insert --image "${IMAGE}:${TAG}" --whiteout /foo/bar/.wh.invalid-whiteout
   194  	[ "$status" -ne 0 ]
   195  	image-verify "${IMAGE}"
   196  }
   197  
   198  @test "umoci insert --opaque" {
   199  	# Some things to insert.
   200  	INSERTDIR="$(setup_tmpdir)"
   201  	mkdir -p "${INSERTDIR}/etc"
   202  	touch "${INSERTDIR}/etc/foo"
   203  
   204  	# Insert our /etc.
   205  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/etc" /etc
   206  	[ "$status" -eq 0 ]
   207  	image-verify "${IMAGE}"
   208  
   209  	# Make sure that the /etc/foo is there.
   210  	new_bundle_rootfs
   211  	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
   212  	[ "$status" -eq 0 ]
   213  	bundle-verify "$BUNDLE"
   214  
   215  	# Make sure that it's merged!
   216  	[ -f "$ROOTFS/etc/shadow" ]
   217  	[ -f "$ROOTFS/etc/foo" ]
   218  
   219  	# Now make it opaque to make sure it isn't included.
   220  	INSERTDIR="$(setup_tmpdir)"
   221  	mkdir -p "${INSERTDIR}/etc"
   222  	touch "${INSERTDIR}/etc/bar"
   223  	touch "${INSERTDIR}/should_be_fine"
   224  
   225  	# Insert our /etc.
   226  	umoci insert --image "${IMAGE}:${TAG}" --opaque "${INSERTDIR}/etc" /etc
   227  	[ "$status" -eq 0 ]
   228  	image-verify "${IMAGE}"
   229  	# And try to make a file opaque just to see what happens (should be nothing).
   230  	umoci insert --image "${IMAGE}:${TAG}" --opaque "${INSERTDIR}/should_be_fine" /should_be_fine
   231  	[ "$status" -eq 0 ]
   232  	image-verify "${IMAGE}"
   233  
   234  	# Make sure that now only /etc/bar is around.
   235  	new_bundle_rootfs
   236  	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
   237  	[ "$status" -eq 0 ]
   238  	bundle-verify "$BUNDLE"
   239  
   240  	# Make sure that it's _not_ merged!
   241  	! [ -f "$ROOTFS/etc/shadow" ]
   242  	! [ -f "$ROOTFS/etc/foo" ]
   243  	# And that bar is there.
   244  	[ -f "$ROOTFS/etc/bar" ]
   245  	# And that should_be_fine is around.
   246  	[ -f "$ROOTFS/should_be_fine" ]
   247  
   248  	image-verify "${IMAGE}"
   249  }
   250  
   251  @test "umoci insert --whiteout" {
   252  	# Some things to insert.
   253  	INSERTDIR="$(setup_tmpdir)"
   254  	touch "${INSERTDIR}/rm_file"
   255  	mkdir "${INSERTDIR}/rm_dir"
   256  
   257  	# Add our things.
   258  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/rm_file" /rm_file
   259  	[ "$status" -eq 0 ]
   260  	image-verify "${IMAGE}"
   261  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/rm_dir" /rm_dir
   262  	[ "$status" -eq 0 ]
   263  	image-verify "${IMAGE}"
   264  
   265  	# Unpack after the inserts.
   266  	new_bundle_rootfs
   267  	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
   268  	[ "$status" -eq 0 ]
   269  	bundle-verify "$BUNDLE"
   270  
   271  	[ -d "$ROOTFS/etc" ]
   272  	[ -d "$ROOTFS/rm_dir" ]
   273  	[ -f "$ROOTFS/rm_file" ]
   274  
   275  	# Directory whiteout.
   276  	umoci insert --image "${IMAGE}:${TAG}" --whiteout /rm_dir
   277  	[ "$status" -eq 0 ]
   278  	image-verify "${IMAGE}"
   279  
   280  	# (Another) directory whiteout.
   281  	umoci insert --image "${IMAGE}:${TAG}" --whiteout /etc
   282  	[ "$status" -eq 0 ]
   283  	image-verify "${IMAGE}"
   284  
   285  	# File whiteout.
   286  	umoci insert --image "${IMAGE}:${TAG}" --whiteout /rm_file
   287  	[ "$status" -eq 0 ]
   288  	image-verify "${IMAGE}"
   289  
   290  	# Unpack after the inserts.
   291  	new_bundle_rootfs
   292  	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
   293  	[ "$status" -eq 0 ]
   294  	bundle-verify "$BUNDLE"
   295  
   296  	! [ -d "$ROOTFS/etc" ]
   297  	! [ -d "$ROOTFS/rm_dir" ]
   298  	! [ -f "$ROOTFS/rm_file" ]
   299  
   300  	image-verify "${IMAGE}"
   301  }
   302  
   303  @test "umoci insert --history.*" {
   304  	# Some things to insert.
   305  	INSERTDIR="$(setup_tmpdir)"
   306  	mkdir -p "${INSERTDIR}/etc"
   307  	touch "${INSERTDIR}/etc/foo"
   308  
   309  	# umoci-insert will overwrite the tag.
   310  	umoci tag --image "${IMAGE}:${TAG}" "${TAG}-new"
   311  	[ "$status" -eq 0 ]
   312  
   313  	# Insert something into the image, setting history values.
   314  	now="$(date --iso-8601=seconds --utc)"
   315  	umoci insert --image "${IMAGE}:${TAG}-new" \
   316  		--history.author="Some Author <jane@blogg.com>" \
   317  		--history.comment="Made a_small_change." \
   318  		--history.created_by="insert ${INSERTDIR}" \
   319  		--history.created="$now" "${INSERTDIR}/etc" /etc
   320  	[ "$status" -eq 0 ]
   321  	image-verify "${IMAGE}"
   322  
   323  	# Make sure that the history was modified.
   324  	umoci stat --image "${IMAGE}:${TAG}" --json
   325  	[ "$status" -eq 0 ]
   326  	numLinesA="$(echo "$output" | jq -SMr '.history | length')"
   327  
   328  	umoci stat --image "${IMAGE}:${TAG}-new" --json
   329  	[ "$status" -eq 0 ]
   330  	numLinesB="$(echo "$output" | jq -SMr '.history | length')"
   331  
   332  	# Number of lines should be greater.
   333  	[ "$numLinesB" -gt "$numLinesA" ]
   334  	# The final layer should not be an empty_layer now.
   335  	[[ "$(echo "$output" | jq -SMr '.history[-1].empty_layer')" == "null" ]]
   336  	# The author should've changed to --history.author.
   337  	[[ "$(echo "$output" | jq -SMr '.history[-1].author')" == "Some Author <jane@blogg.com>" ]]
   338  	# The comment should be added.
   339  	[[ "$(echo "$output" | jq -SMr '.history[-1].comment')" == "Made a_small_change." ]]
   340  	# The created_by should be set.
   341  	[[ "$(echo "$output" | jq -SMr '.history[-1].created_by')" == "insert ${INSERTDIR}" ]]
   342  	# The created should be set.
   343  	[[ "$(date --iso-8601=seconds --utc --date="$(echo "$output" | jq -SMr '.history[-1].created')")" == "$now" ]]
   344  
   345  	image-verify "${IMAGE}"
   346  }
   347  
   348  @test "umoci insert --no-history" {
   349  	# Some things to insert.
   350  	INSERTDIR="$(setup_tmpdir)"
   351  	mkdir -p "${INSERTDIR}/etc"
   352  	touch "${INSERTDIR}/etc/foo"
   353  
   354  	# umoci-insert will overwrite the tag.
   355  	umoci tag --image "${IMAGE}:${TAG}" "${TAG}-new"
   356  	[ "$status" -eq 0 ]
   357  
   358  	# Insert something into the image, but with no history change.
   359  	umoci insert --no-history --image "${IMAGE}:${TAG}-new" "${INSERTDIR}/etc" /etc
   360  	[ "$status" -eq 0 ]
   361  	image-verify "${IMAGE}"
   362  
   363  	# Make sure we *did not* add a new history entry.
   364  	umoci stat --image "${IMAGE}:${TAG}" --json
   365  	[ "$status" -eq 0 ]
   366  	hashA="$(sha256sum <<<"$output")"
   367  
   368  	umoci stat --image "${IMAGE}:${TAG}-new" --json
   369  	[ "$status" -eq 0 ]
   370  	hashB="$(sha256sum <<<"$output")"
   371  
   372  	# umoci-stat output should be identical.
   373  	[[ "$hashA" == "$hashB" ]]
   374  
   375  	image-verify "${IMAGE}"
   376  }