github.com/opentofu/opentofu@v1.7.1/internal/command/e2etest/make-archive.sh (about)

     1  #!/bin/bash
     2  # Copyright (c) The OpenTofu Authors
     3  # SPDX-License-Identifier: MPL-2.0
     4  # Copyright (c) 2023 HashiCorp, Inc.
     5  # SPDX-License-Identifier: MPL-2.0
     6  
     7  
     8  # For normal use this package can just be tested with "go test" as standard,
     9  # but this script is an alternative to allow the tests to be run somewhere
    10  # other than where they are built.
    11  
    12  # The primary use for this is cross-compilation, where e.g. we can produce an
    13  # archive that can be extracted on a Windows system to run the e2e tests there:
    14  #    $ GOOS=windows GOARCH=amd64 ./make-archive.sh
    15  #
    16  # This will produce a zip file build/terraform-e2etest_windows_amd64.zip which
    17  # can be shipped off to a Windows amd64 system, extracted to some directory,
    18  # and then executed as follows:
    19  #    set TF_ACC=1
    20  #    ./e2etest.exe
    21  #
    22  # Because separated e2etest harnesses are intended for testing against "real"
    23  # release executables, the generated archives don't include a copy of
    24  # the Terraform executable. Instead, the caller of the tests must retrieve
    25  # and extract a release package into the working directory before running
    26  # the e2etest executable, so that "e2etest" can find and execute it.
    27  
    28  set +euo pipefail
    29  
    30  # Always run from the directory where this script lives
    31  cd "$( dirname "${BASH_SOURCE[0]}" )"
    32  
    33  GOOS="$(go env GOOS)"
    34  GOARCH="$(go env GOARCH)"
    35  GOEXE="$(go env GOEXE)"
    36  OUTDIR="build/${GOOS}_${GOARCH}"
    37  OUTFILE="tofu-e2etest_${GOOS}_${GOARCH}.zip"
    38  
    39  LDFLAGS="-X github.com/opentofu/opentofu/internal/command/e2etest.tofuBin=./tofu$GOEXE"
    40  # Caller may pass in the environment variable GO_LDFLAGS with additional
    41  # flags we'll use when building.
    42  if [ -n "${GO_LDFLAGS+set}" ]; then
    43      LDFLAGS="${GO_LDFLAGS} ${LDFLAGS}"
    44  fi
    45  
    46  mkdir -p "$OUTDIR"
    47  
    48  # We need the test fixtures available when we run the tests.
    49  cp -r testdata "$OUTDIR/testdata"
    50  
    51  # Build the test program
    52  go test -o "$OUTDIR/e2etest$GOEXE" -c -ldflags "$LDFLAGS" github.com/opentofu/opentofu/internal/command/e2etest
    53  
    54  # Now bundle it all together for easy shipping!
    55  cd "$OUTDIR"
    56  zip -r "../$OUTFILE" *
    57  
    58  echo "e2etest archive created at build/$OUTFILE"