github.com/opensuse/umoci@v0.4.2/test/insert.bats (about)

     1  #!/usr/bin/env bats -t
     2  # umoci: Umoci Modifies Open Containers' Images
     3  # Copyright (C) 2018 Cisco
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #   http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  
    18  load helpers
    19  
    20  function setup() {
    21  	setup_image
    22  }
    23  
    24  function teardown() {
    25  	teardown_tmpdirs
    26  	teardown_image
    27  }
    28  
    29  @test "umoci insert" {
    30  	# fail with too few arguments
    31  	umoci insert --image "${IMAGE}:${TAG}"
    32  	[ "$status" -ne 0 ]
    33  	image-verify "${IMAGE}"
    34  
    35  	# ...and too many
    36  	umoci insert --image "${IMAGE}:${TAG}" asdf 123 456
    37  	[ "$status" -ne 0 ]
    38  	image-verify "${IMAGE}"
    39  
    40  	# Some things to insert.
    41  	INSERTDIR="$(setup_tmpdir)"
    42  	mkdir -p "${INSERTDIR}/test"
    43  	touch "${INSERTDIR}/test/a"
    44  	touch "${INSERTDIR}/test/b"
    45  	chmod +x "${INSERTDIR}/test/b"
    46  
    47  	# Make sure rootless mode works.
    48  	mkdir -p "${INSERTDIR}/some/path"
    49  	touch "${INSERTDIR}/some/path/hidden"
    50  	chmod 000 "${INSERTDIR}/some/path"
    51  
    52  	# Do a few inserts.
    53  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/test/a" /tester/a
    54  	[ "$status" -eq 0 ]
    55  	image-verify "${IMAGE}"
    56  
    57  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/test/b" /tester/b
    58  	[ "$status" -eq 0 ]
    59  	image-verify "${IMAGE}"
    60  
    61  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/test" /recursive
    62  	[ "$status" -eq 0 ]
    63  	image-verify "${IMAGE}"
    64  
    65  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/some" /rootless
    66  	[ "$status" -eq 0 ]
    67  	image-verify "${IMAGE}"
    68  
    69  	# Unpack after the inserts.
    70  	new_bundle_rootfs
    71  	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
    72  	[ "$status" -eq 0 ]
    73  	bundle-verify "$BUNDLE"
    74  
    75  	# ... and check to make sure it worked.
    76  	[ -f "$ROOTFS/tester/a" ]
    77  	[[ "$(stat -c '%f' "${INSERTDIR}/test/b")" == "$(stat -c '%f' "$ROOTFS/tester/b")" ]]
    78  	[ -f "$ROOTFS/recursive/a" ]
    79  	[ -f "$ROOTFS/recursive/b" ]
    80  
    81  	# ... as well as the rootless portion.
    82  	[ -d "$ROOTFS/rootless/path" ]
    83  	[[ "$(stat -c '%f' "${INSERTDIR}/some/path")" == "$(stat -c '%f' "$ROOTFS/rootless/path")" ]]
    84  	chmod a+rwx "$ROOTFS/rootless/path"
    85  	[ -f "$ROOTFS/rootless/path/hidden" ]
    86  
    87  	image-verify "${IMAGE}"
    88  }
    89  
    90  @test "umoci insert --opaque" {
    91  	# Some things to insert.
    92  	INSERTDIR="$(setup_tmpdir)"
    93  	mkdir -p "${INSERTDIR}/etc"
    94  	touch "${INSERTDIR}/etc/foo"
    95  
    96  	# Insert our /etc.
    97  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/etc" /etc
    98  	[ "$status" -eq 0 ]
    99  	image-verify "${IMAGE}"
   100  
   101  	# Make sure that the /etc/foo is there.
   102  	new_bundle_rootfs
   103  	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
   104  	[ "$status" -eq 0 ]
   105  	bundle-verify "$BUNDLE"
   106  
   107  	# Make sure that it's merged!
   108  	[ -f "$ROOTFS/etc/shadow" ]
   109  	[ -f "$ROOTFS/etc/foo" ]
   110  
   111  	# Now make it opaque to make sure it isn't included.
   112  	INSERTDIR="$(setup_tmpdir)"
   113  	mkdir -p "${INSERTDIR}/etc"
   114  	touch "${INSERTDIR}/etc/bar"
   115  	touch "${INSERTDIR}/should_be_fine"
   116  
   117  	# Insert our /etc.
   118  	umoci insert --image "${IMAGE}:${TAG}" --opaque "${INSERTDIR}/etc" /etc
   119  	[ "$status" -eq 0 ]
   120  	image-verify "${IMAGE}"
   121  	# And try to make a file opaque just to see what happens (should be nothing).
   122  	umoci insert --image "${IMAGE}:${TAG}" --opaque "${INSERTDIR}/should_be_fine" /should_be_fine
   123  	[ "$status" -eq 0 ]
   124  	image-verify "${IMAGE}"
   125  
   126  	# Make sure that now only /etc/bar is around.
   127  	new_bundle_rootfs
   128  	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
   129  	[ "$status" -eq 0 ]
   130  	bundle-verify "$BUNDLE"
   131  
   132  	# Make sure that it's _not_ merged!
   133  	! [ -f "$ROOTFS/etc/shadow" ]
   134  	! [ -f "$ROOTFS/etc/foo" ]
   135  	# And that bar is there.
   136  	[ -f "$ROOTFS/etc/bar" ]
   137  	# And that should_be_fine is around.
   138  	[ -f "$ROOTFS/should_be_fine" ]
   139  
   140  	image-verify "${IMAGE}"
   141  }
   142  
   143  @test "umoci insert --whiteout" {
   144  	# Some things to insert.
   145  	INSERTDIR="$(setup_tmpdir)"
   146  	touch "${INSERTDIR}/rm_file"
   147  	mkdir "${INSERTDIR}/rm_dir"
   148  
   149  	# Add our things.
   150  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/rm_file" /rm_file
   151  	[ "$status" -eq 0 ]
   152  	image-verify "${IMAGE}"
   153  	umoci insert --image "${IMAGE}:${TAG}" "${INSERTDIR}/rm_dir" /rm_dir
   154  	[ "$status" -eq 0 ]
   155  	image-verify "${IMAGE}"
   156  
   157  	# Unpack after the inserts.
   158  	new_bundle_rootfs
   159  	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
   160  	[ "$status" -eq 0 ]
   161  	bundle-verify "$BUNDLE"
   162  
   163  	[ -d "$ROOTFS/etc" ]
   164  	[ -d "$ROOTFS/rm_dir" ]
   165  	[ -f "$ROOTFS/rm_file" ]
   166  
   167  	# Directory whiteout.
   168  	umoci insert --image "${IMAGE}:${TAG}" --whiteout /rm_dir
   169  	[ "$status" -eq 0 ]
   170  	image-verify "${IMAGE}"
   171  
   172  	# (Another) directory whiteout.
   173  	umoci insert --image "${IMAGE}:${TAG}" --whiteout /etc
   174  	[ "$status" -eq 0 ]
   175  	image-verify "${IMAGE}"
   176  
   177  	# File whiteout.
   178  	umoci insert --image "${IMAGE}:${TAG}" --whiteout /rm_file
   179  	[ "$status" -eq 0 ]
   180  	image-verify "${IMAGE}"
   181  
   182  	# Unpack after the inserts.
   183  	new_bundle_rootfs
   184  	umoci unpack --image "${IMAGE}:${TAG}" "$BUNDLE"
   185  	[ "$status" -eq 0 ]
   186  	bundle-verify "$BUNDLE"
   187  
   188  	! [ -d "$ROOTFS/etc" ]
   189  	! [ -d "$ROOTFS/rm_dir" ]
   190  	! [ -f "$ROOTFS/rm_file" ]
   191  
   192  	image-verify "${IMAGE}"
   193  }