github.com/StackPointCloud/packer@v0.10.2-0.20180716202532-b28098e0f79b/test/builder_lxc.bats (about)

     1  #!/usr/bin/env bats
     2  #
     3  # This tests the lxc builder by creating minimal containers and checking that
     4  # custom lxc container configuration files are successfully applied. The
     5  # teardown function will delete any images in the output-lxc-* folders along
     6  # with the auto-generated lxc container configuration files and hook scripts.
     7  
     8  #load test_helper
     9  #fixtures builder-lxc
    10  FIXTURE_ROOT="$BATS_TEST_DIRNAME/fixtures/builder-lxc"
    11  
    12  have_command() {
    13      command -v "$1" >/dev/null 2>&1
    14  }
    15  
    16  # Required parameters
    17  have_command lxc-create || {
    18      echo "'lxc-create' must be installed via the lxc (or lxc1 for ubuntu >=16.04) package" >&2
    19      exit 1
    20  }
    21  
    22  DESTROY_HOOK_SCRIPT=$FIXTURE_ROOT/destroy-hook.sh
    23  DESTROY_HOOK_LOG=$FIXTURE_ROOT/destroy-hook.log
    24  printf > "$DESTROY_HOOK_SCRIPT" '
    25  echo "$LXC_NAME" > "%s"
    26  ' "$DESTROY_HOOK_LOG"
    27  chmod +x "$DESTROY_HOOK_SCRIPT"
    28  
    29  INIT_CONFIG=$FIXTURE_ROOT/lxc.custom.conf
    30  printf > "$INIT_CONFIG" '
    31  lxc.hook.destroy = %s
    32  ' "$DESTROY_HOOK_SCRIPT"
    33  
    34  teardown() {
    35      for f in "$INIT_CONFIG" "$DESTROY_HOOK_SCRIPT" "$DESTROY_HOOK_LOG"; do
    36          [ -e "$f" ] && rm -f "$f"
    37      done
    38  
    39      rm -rf output-lxc-*
    40  }
    41  
    42  assert_build() {
    43      local template_name="$1"
    44      shift
    45  
    46      local build_status=0
    47  
    48      run packer build -var template_name="$template_name"  "$@"
    49  
    50      [ "$status" -eq 0 ] || {
    51          echo "${template_name} build exited badly: $status" >&2
    52          echo "$output" >&2
    53          build_status="$status"
    54      }
    55  
    56      for expected in "output-lxc-${template_name}"/{rootfs.tar.gz,lxc-config}; do
    57          [ -f "$expected" ] || {
    58              echo "missing expected artifact '${expected}'" >&2
    59              build_status=1
    60          }
    61      done
    62  
    63      return $build_status
    64  }
    65  
    66  assert_container_name() {
    67      local container_name="$1"
    68  
    69      [ -f "$DESTROY_HOOK_LOG" ] || {
    70          echo "missing expected lxc.hook.destroy logfile '$DESTROY_HOOK_LOG'"
    71          return 1
    72      }
    73  
    74      read -r lxc_name < "$DESTROY_HOOK_LOG"
    75  
    76      [ "$lxc_name" = "$container_name" ]
    77  }
    78  
    79  @test "lxc: build centos minimal.json" {
    80      have_command yum || skip "'yum' must be installed to build centos containers"
    81      local container_name=packer-lxc-centos
    82      assert_build centos -var init_config="$INIT_CONFIG" \
    83          -var container_name="$container_name" \
    84          $FIXTURE_ROOT/minimal.json
    85      assert_container_name "$container_name"
    86  }
    87  
    88  @test "lxc: build trusty minimal.json" {
    89      have_command debootstrap || skip "'debootstrap' must be installed to build ubuntu containers"
    90      local container_name=packer-lxc-ubuntu
    91      assert_build ubuntu -var init_config="$INIT_CONFIG" \
    92          -var container_name="$container_name" \
    93          -var template_parameters="SUITE=trusty" \
    94          $FIXTURE_ROOT/minimal.json
    95      assert_container_name "$container_name"
    96  }
    97  
    98  @test "lxc: build debian minimal.json" {
    99      have_command debootstrap || skip "'debootstrap' must be installed to build debian containers"
   100      local container_name=packer-lxc-debian
   101      assert_build debian -var init_config="$INIT_CONFIG" \
   102          -var container_name="$container_name" \
   103          -var template_parameters="SUITE=jessie" \
   104          $FIXTURE_ROOT/minimal.json
   105      assert_container_name "$container_name"
   106  }