diff --git a/.github/scripts/azurebs/run-int.sh b/.github/scripts/azurebs/run-int.sh new file mode 100755 index 0000000..7d6bb1e --- /dev/null +++ b/.github/scripts/azurebs/run-int.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +set -euo pipefail + +script_dir="$( cd "$(dirname "${0}")" && pwd )" +repo_root="$(cd "${script_dir}/../../.." && pwd)" + +: "${azure_storage_account:?}" +: "${azure_storage_key:?}" +: "${environment:=AzureCloud}" + +export ACCOUNT_NAME="${azure_storage_account}" +export ACCOUNT_KEY="${azure_storage_key}" +export ENVIRONMENT="${environment}" + +pushd "${script_dir}" > /dev/null + source utils.sh + container_name="$(read_container_name_from_file "${environment}")" + : "${container_name:?}" + export CONTAINER_NAME="${container_name}" +popd > /dev/null + +pushd "${repo_root}" > /dev/null + echo -e "\n running tests with $(go version)..." + ginkgo -r azurebs/integration/ +popd > /dev/null + diff --git a/.github/scripts/azurebs/setup.sh b/.github/scripts/azurebs/setup.sh new file mode 100755 index 0000000..1130763 --- /dev/null +++ b/.github/scripts/azurebs/setup.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -euo pipefail + +script_dir="$( cd "$(dirname "${0}")" && pwd )" +repo_root="$(cd "${script_dir}/../../.." && pwd)" + + +: "${azure_storage_account:?}" +: "${azure_storage_key:?}" +: "${environment:=AzureCloud}" + +export AZURE_STORAGE_ACCOUNT="${azure_storage_account}" +export AZURE_STORAGE_KEY="${azure_storage_key}" + + + +pushd "${script_dir}" + source utils.sh + generate_container_name "${environment}" + create_container "${environment}" +popd diff --git a/.github/scripts/azurebs/teardown.sh b/.github/scripts/azurebs/teardown.sh new file mode 100755 index 0000000..e75d56e --- /dev/null +++ b/.github/scripts/azurebs/teardown.sh @@ -0,0 +1,21 @@ +#!/usr/bin/env bash +set -euo pipefail + +script_dir="$( cd "$(dirname "${0}")" && pwd )" +repo_root="$(cd "${script_dir}/../../.." && pwd)" + + +: "${azure_storage_account:?}" +: "${azure_storage_key:?}" +: "${environment:=AzureCloud}" + +export AZURE_STORAGE_ACCOUNT="${azure_storage_account}" +export AZURE_STORAGE_KEY="${azure_storage_key}" + + + +pushd "${script_dir}" + source utils.sh + delete_container "${environment}" + delete_container_name_file "${environment}" +popd diff --git a/.github/scripts/azurebs/utils.sh b/.github/scripts/azurebs/utils.sh new file mode 100755 index 0000000..f7823c8 --- /dev/null +++ b/.github/scripts/azurebs/utils.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +TMP_DIR="/tmp/storage-cli-azurebs-${GITHUB_RUN_ID:-${USER}}" + +# generate a random container name with "azurebs-" prefix +function random_name { + echo "azurebs-$(openssl rand -hex 20)" +} + + +# create a file with .lock suffix and store the container name inside it +function generate_container_name { + local file_name="${1}.lock" + local container_name="$(random_name)" + mkdir -p "${TMP_DIR}" + echo "${container_name}" > "${TMP_DIR}/${file_name}" +} + + +# retrieve the container name from the .lock file +function read_container_name_from_file { + local file_name="$1" + cat "${TMP_DIR}/${file_name}.lock" +} + +# delete the .lock file +function delete_container_name_file { + local file_name="$1" + rm -f "${TMP_DIR}/${file_name}.lock" +} + + +function create_container { + local container_name="$(read_container_name_from_file "$1")" + + az storage container create --account-name "${AZURE_STORAGE_ACCOUNT}" --account-key "${AZURE_STORAGE_KEY}" --name "${container_name}" + +} + + +function delete_container { + local container_name="$(read_container_name_from_file "$1")" + + az storage container delete --account-name "${AZURE_STORAGE_ACCOUNT}" --account-key "${AZURE_STORAGE_KEY}" --name "${container_name}" + +} \ No newline at end of file diff --git a/.github/workflows/azurebs-integration.yml b/.github/workflows/azurebs-integration.yml new file mode 100644 index 0000000..eb77473 --- /dev/null +++ b/.github/workflows/azurebs-integration.yml @@ -0,0 +1,42 @@ +name: Azurebs Integration Tests + +on: + workflow_dispatch: + pull_request: + paths: + - ".github/workflows/azurebs-integration.yml" + - "azurebs/**" + push: + branches: + - main + +jobs: + azurecloud-environment-integration-tests: + name: AzureCloud Environment Integration Tests + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v5 + - name: Set up Go + uses: actions/setup-go@v6 + with: + go-version-file: go.mod + - name: Install Ginkgo + run: go install github.com/onsi/ginkgo/v2/ginkgo@latest + - name: Setup Azurebs Test Environment + run: | + export azure_storage_account="${{ secrets.AZURE_STORAGE_ACCOUNT }}" + export azure_storage_key="${{ secrets.AZURE_STORAGE_KEY }}" + ./.github/scripts/azurebs/setup.sh + - name: Run Tests + run: | + export azure_storage_account="${{ secrets.AZURE_STORAGE_ACCOUNT }}" + export azure_storage_key="${{ secrets.AZURE_STORAGE_KEY }}" + ./.github/scripts/azurebs/run-int.sh + - name: Teardown Azurebs Test Environment + if: always() + run: | + export azure_storage_account="${{ secrets.AZURE_STORAGE_ACCOUNT }}" + export azure_storage_key="${{ secrets.AZURE_STORAGE_KEY }}" + ./.github/scripts/azurebs/teardown.sh + \ No newline at end of file diff --git a/azurebs/README.md b/azurebs/README.md index 4b94514..7977ba0 100644 --- a/azurebs/README.md +++ b/azurebs/README.md @@ -1,7 +1,7 @@ # Azure Storage CLI The Azure Storage CLI is for uploading, fetching and deleting content to and from an Azure blobstore. -It is highly inspired by the https://github.com/cloudfoundry/bosh-s3cli. +It is highly inspired by the [storage-cli/s3](https://github.com/cloudfoundry/storage-cli/blob/6058f516e9b81471b64a50b01e228158a05731f0/s3) ## Usage @@ -49,35 +49,47 @@ curl -X PUT -H "x-ms-blob-type: blockblob" -F 'fileX=' ``` -## Running tests - -### Unit tests - -Using ginkgo: - -``` bash -go install github.com/onsi/ginkgo/v2/ginkgo -ginkgo --skip-package=integration --randomize-all --cover -v -r -``` - -Using go test: - -``` bash -go test $(go list ./... | grep -v integration) -``` - -### Integration tests - -1. Export the following variables into your environment: - - ``` bash - export ACCOUNT_NAME= - export ACCOUNT_KEY= - export CONTAINER_NAME= - ``` - -2. Run integration tests - - ```bash - go test ./integration/... - ``` +## Running Tests + +### Unit Tests +**Note:** Run the following commands from the repository root directory + +- Using ginkgo: + + ``` bash + go install github.com/onsi/ginkgo/v2/ginkgo + + ginkgo --skip-package=integration --randomize-all --cover -v -r ./azurebs/... + ``` + +- Using go test: + + ``` bash + go test $(go list ./azurebs/... | grep -v integration) + ``` + +### Integration Tests +- To run the integration tests with your existing container + 1. Export the following variables into your environment. + + ```bash + export ACCOUNT_NAME= + export ACCOUNT_KEY= + export CONTAINER_NAME= + ``` + + 1. Navigate to project's root folder and run the command below: + + ```bash + go test ./azurebs/integration/... + ``` + +- To run it from scratch; create a new container, run tests, delete the container + 1. Create a storage account in your azure subscription. + 1. Get `account name` and `access key` from you storage account. + 1. Export `account name` with command `export azure_storage_account=`. + 1. Export `access key` with command `export azure_storage_key=`. + 1. Navigate to project's root folder. + 1. Run environment setup script to create container `./.github/scripts/azurebs/setup.sh`. + 1. Run tests `./.github/scripts/azurebs/run-int.sh`. + 1. Run environment teardown script to delete test resources `./.github/scripts/azurebs/teardown.sh`. diff --git a/azurebs/integration/assertions.go b/azurebs/integration/assertions.go index 5baba9e..7fb90bb 100644 --- a/azurebs/integration/assertions.go +++ b/azurebs/integration/assertions.go @@ -57,7 +57,7 @@ func AssertPutTimesOut(cliPath string, cfg *config.AZStorageConfig) { defer os.Remove(configPath) //nolint:errcheck const mb = 1024 * 1024 - big := bytes.Repeat([]byte("x"), 25*mb) + big := bytes.Repeat([]byte("x"), 250*mb) content := MakeContentFile(string(big)) defer os.Remove(content) //nolint:errcheck blob := GenerateRandomString()