diff --git a/.github/workflows/add_to_octokit_project.yml b/.github/workflows/add-to-octokit-project.yaml similarity index 61% rename from .github/workflows/add_to_octokit_project.yml rename to .github/workflows/add-to-octokit-project.yaml index 8621996e47..4a3fe11300 100644 --- a/.github/workflows/add_to_octokit_project.yml +++ b/.github/workflows/add-to-octokit-project.yaml @@ -1,4 +1,4 @@ -name: Add PRs and issues to Octokit org project +name: Add PRs & Issues to Octokit Org Project on: issues: @@ -6,13 +6,19 @@ on: pull_request_target: types: [reopened, opened] +permissions: read-all + jobs: add-to-project: - name: Add issue to project + name: Add to Project runs-on: ubuntu-latest continue-on-error: true + defaults: + run: + shell: bash steps: - - uses: actions/add-to-project@v1.0.2 + - name: Add to project + uses: actions/add-to-project@244f685bbc3b7adfa8466e08b698b5577571133e # v1.0.2 with: project-url: https://github.com/orgs/octokit/projects/10 github-token: ${{ secrets.OCTOKITBOT_PROJECT_ACTION_TOKEN }} diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 0000000000..454942122a --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,32 @@ +name: GitHub Actions CI + +on: + workflow_dispatch: + push: + branches: [main] + pull_request: {} + +permissions: read-all + +jobs: + ci: + name: CI + runs-on: ubuntu-latest + defaults: + run: + shell: bash + env: + GITHUB_TEST_ORGANIZATION: kfcampbell-terraform-provider + steps: + - name: Checkout + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + - name: Set-up Go + uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 + with: + go-version-file: go.mod + cache: true + - run: make tools + - run: make lint + - run: make website-lint + - run: make build + - run: make test diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml deleted file mode 100644 index f62bdfd353..0000000000 --- a/.github/workflows/ci.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: GitHub Actions CI - -on: - push: - branches: [main] - pull_request: {} - -jobs: - ci: - runs-on: ubuntu-latest - env: - GITHUB_TEST_ORGANIZATION: 'kfcampbell-terraform-provider' - steps: - - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - run: make tools - - run: make lint - - run: make website-lint - - run: make build - - run: make test diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yaml similarity index 84% rename from .github/workflows/codeql.yml rename to .github/workflows/codeql.yaml index b78f487ffc..0fbc6fbd95 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yaml @@ -1,6 +1,7 @@ -name: "CodeQL" +name: CodeQL on: + workflow_dispatch: push: branches: [ "main" ] pull_request: @@ -9,6 +10,8 @@ on: schedule: - cron: '16 7 * * 5' +permissions: read-all + jobs: analyze: name: Analyze @@ -17,17 +20,19 @@ jobs: actions: read contents: read security-events: write - strategy: fail-fast: false matrix: language: [ 'go' ] - + defaults: + run: + shell: bash steps: - - name: Checkout repository + - name: Checkout uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 + - name: Set-up Go + uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 with: go-version-file: 'go.mod' cache: true diff --git a/.github/workflows/dotcom-acceptance-tests-all.yml b/.github/workflows/dotcom-acceptance-tests-all.yml deleted file mode 100644 index 3d9c35918c..0000000000 --- a/.github/workflows/dotcom-acceptance-tests-all.yml +++ /dev/null @@ -1,97 +0,0 @@ -name: Dotcom Acceptance Tests (All) - -on: - schedule: - # ┌───────────── minute (0 - 59) - # │ ┌───────────── hour (0 - 23) - # │ │ ┌───────────── day of the month (1 - 31) - # │ │ │ ┌───────────── month (1 - 12 or JAN-DEC) - # │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT) - # * * * * * - - cron: '0 0 * * 3' - -jobs: - - acceptance-tests-anonymous: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - name: Acceptance Tests (Anonymous) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - RUN_ALL: true - - acceptance-tests-individual: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - - name: Acceptance Tests (Individual) - id: acceptance-tests-individual - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - RUN_ALL: true - GITHUB_OWNER: github-terraform-test-user - GITHUB_TEST_USER_TOKEN: ${{ secrets.DOTCOM_TEST_USER_TOKEN }} - GITHUB_TEST_ORGANIZATION: terraformtesting - - - name: Failed Acceptance Tests (Individual) - if: ${{ failure() }} - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: DEBUG - RUN_ALLOWED: ${{ steps.acceptance-tests-individual.outputs.failed }} - GITHUB_OWNER: github-terraform-test-user - GITHUB_TEST_USER_TOKEN: ${{ secrets.DOTCOM_TEST_USER_TOKEN }} - GITHUB_TEST_ORGANIZATION: terraformtesting - - acceptance-tests-organization: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - - name: Acceptance Tests (Organization) - id: acceptance-tests-organization - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - RUN_ALL: true - GITHUB_ORGANIZATION: terraformtesting - GITHUB_TEST_USER_TOKEN: ${{ secrets.DOTCOM_TEST_USER_TOKEN }} - GITHUB_TEST_OWNER: github-terraform-test-user - - - name: Failed Acceptance Tests (Organization) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - if: ${{ failure() }} - with: - TF_LOG: DEBUG - RUN_ALLOWED: ${{ steps.acceptance-tests-organization.outputs.failed }} - GITHUB_ORGANIZATION: terraformtesting - GITHUB_TEST_USER_TOKEN: ${{ secrets.DOTCOM_TEST_USER_TOKEN }} - GITHUB_TEST_OWNER: github-terraform-test-user diff --git a/.github/workflows/dotcom-acceptance-tests-manual.yml b/.github/workflows/dotcom-acceptance-tests-manual.yml deleted file mode 100644 index d607d9cd3a..0000000000 --- a/.github/workflows/dotcom-acceptance-tests-manual.yml +++ /dev/null @@ -1,115 +0,0 @@ -name: Dotcom Acceptance Tests (manual) - -on: - pull_request: - types: [labeled] - -jobs: - - acceptance-tests-anonymous: - runs-on: ubuntu-latest - if: contains(join(github.event.pull_request.labels.*.name, ', '), 'test/') - steps: - - name: Parse Args - id: args - run: | - echo "::set-output name=run_allowed::$( - jq -rc .label.name $GITHUB_EVENT_PATH | cut -d/ -f 2 - )" - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.sha }} - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - name: Acceptance Tests (Anonymous) - id: acceptance-tests-anonymous - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - RUN_ALLOWED: ${{ steps.args.outputs.run_allowed }} - - name: Failed Acceptance Tests (Anonymous) - if: ${{ failure() }} - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: DEBUG - RUN_ALLOWED: ${{ steps.acceptance-tests-anonymous.outputs.run_allowed }} - - - acceptance-tests-individual: - runs-on: ubuntu-latest - if: contains(join(github.event.pull_request.labels.*.name, ', '), 'test') - steps: - - name: Parse Args - id: args - run: | - echo "::set-output name=run_allowed::$( - jq -rc .label.name $GITHUB_EVENT_PATH | cut -d/ -f 2 - )" - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.sha }} - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - name: Acceptance Tests (Individual) - id: acceptance-tests-individual - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - RUN_ALLOWED: ${{ steps.args.outputs.run_allowed }} - GITHUB_OWNER: github-terraform-test-user - GITHUB_TEST_USER_TOKEN: ${{ secrets.DOTCOM_TEST_USER_TOKEN }} - GITHUB_TEST_ORGANIZATION: terraformtesting - - name: Failed Acceptance Tests (Individual) - if: ${{ failure() }} - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: DEBUG - RUN_ALLOWED: ${{ steps.args.outputs.run_allowed }} - GITHUB_OWNER: github-terraform-test-user - GITHUB_TEST_USER_TOKEN: ${{ secrets.DOTCOM_TEST_USER_TOKEN }} - GITHUB_TEST_ORGANIZATION: terraformtesting - - acceptance-tests-organization: - runs-on: ubuntu-latest - if: contains(join(github.event.pull_request.labels.*.name, ', '), 'test') - steps: - - name: Parse Args - id: args - run: | - echo "::set-output name=run_allowed::$( - jq -rc .label.name $GITHUB_EVENT_PATH | cut -d/ -f 2 - )" - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.sha }} - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - - name: Acceptance Tests (Organization) - id: acceptance-tests-organization - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - RUN_ALLOWED: ${{ steps.args.outputs.run_allowed }} - GITHUB_ORGANIZATION: terraformtesting - GITHUB_TEST_USER_TOKEN: ${{ secrets.DOTCOM_TEST_USER_TOKEN }} - GITHUB_TEST_OWNER: github-terraform-test-user - - - name: Failed Acceptance Tests (Organization) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - if: ${{ failure() }} - with: - TF_LOG: DEBUG - RUN_ALLOWED: ${{ steps.args.outputs.run_allowed }} - GITHUB_ORGANIZATION: terraformtesting - GITHUB_TEST_USER_TOKEN: ${{ secrets.DOTCOM_TEST_USER_TOKEN }} - GITHUB_TEST_OWNER: github-terraform-test-user diff --git a/.github/workflows/dotcom-acceptance-tests.yaml b/.github/workflows/dotcom-acceptance-tests.yaml new file mode 100644 index 0000000000..3fb66dc7fe --- /dev/null +++ b/.github/workflows/dotcom-acceptance-tests.yaml @@ -0,0 +1,75 @@ +name: Acceptance Tests (github.com) + +on: + workflow_dispatch: + pull_request_target: + types: + - opened + - synchronize + - reopened + branches: + - main + +permissions: read-all + +jobs: + test: + name: Test ${{ matrix.mode }} + # if: contains(github.event.pull_request.labels.*.name, 'acctest') + # environment: + # name: dotcom + runs-on: ubuntu-latest + permissions: + contents: read + strategy: + matrix: + mode: [anonymous, individual, organization] # team, enterprise + fail-fast: false + defaults: + run: + shell: bash + steps: + - name: Checkout + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + - name: Set-up Go + uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 + with: + go-version-file: go.mod + cache: true + + - name: Check credentials + if: matrix.mode != 'anonymous' + id: credentials + run: | + set -eou pipefail + + token="${{ secrets.DOTCOM_TEST_USER_TOKEN }}" + + if [[ -z "${token}" ]]; then + echo "Missing credentials" >&2 + exit 1 + fi + + echo "token=${token}" >> "${GITHUB_OUTPUT}" + + - name: Run tests + env: + TF_ACC: "1" + TF_LOG: INFO + GITHUB_BASE_URL: https://api.github.com/ + GITHUB_TEST_TYPE: ${{ matrix.mode }} + GITHUB_OWNER: ${{ (matrix.mode == 'individual' && 'github-terraform-test-user') || (matrix.mode == 'organization' && 'terraformtesting') || '' }} + GITHUB_USERNAME: github-terraform-test-user + GITHUB_TOKEN: ${{ matrix.mode != 'anonymous' && steps.credentials.outputs.token || '' }} + GITHUB_ENTERPRISE_SLUG: "" + GITHUB_TEST_USER_REPOSITORY: test-xk24f + GITHUB_TEST_ORG_USER: "" + GITHUB_TEST_ORG_REPOSITORY: test-repo + GITHUB_TEST_ORG_TEMPLATE_REPOSITORY: terraform-template-module + GITHUB_TEST_ORG_APP_INSTALLATION_ID: "" + GITHUB_TEST_EXTERNAL_USER: "" + GITHUB_TEST_EXTERNAL_USER_TOKEN: "" + GITHUB_TEST_EXTERNAL_USER2: "" + GITHUB_TEST_ADVANCED_SECURITY: "true" + run: go test -run "^TestAcc*" ./github -v -race -coverprofile=coverage.txt -covermode=atomic -sweep=tf-acc- -timeout 120m -count=1 diff --git a/.github/workflows/dotcom-acceptance-tests.yml b/.github/workflows/dotcom-acceptance-tests.yml deleted file mode 100644 index 28aa777c42..0000000000 --- a/.github/workflows/dotcom-acceptance-tests.yml +++ /dev/null @@ -1,61 +0,0 @@ -name: Dotcom Acceptance Tests - -on: - push: - branches: - - test/** - -jobs: - - acceptance-tests-anonymous: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - name: Acceptance Tests (Anonymous) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - - acceptance-tests-individual: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - name: Acceptance Tests (Individual) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - GITHUB_OWNER: github-terraform-test-user - GITHUB_TEST_USER_TOKEN: ${{ secrets.DOTCOM_TEST_USER_TOKEN }} - GITHUB_TEST_ORGANIZATION: terraformtesting - - acceptance-tests-organization: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - name: Acceptance Tests (Organization) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - GITHUB_ORGANIZATION: terraformtesting - GITHUB_TEST_USER_TOKEN: ${{ secrets.DOTCOM_TEST_USER_TOKEN }} - GITHUB_TEST_OWNER: github-terraform-test-user diff --git a/.github/workflows/ghes-acceptance-tests-all.yml b/.github/workflows/ghes-acceptance-tests-all.yml deleted file mode 100644 index 78d064ba82..0000000000 --- a/.github/workflows/ghes-acceptance-tests-all.yml +++ /dev/null @@ -1,112 +0,0 @@ -name: GHES Acceptance Tests (All) - -on: - schedule: - # ┌───────────── minute (0 - 59) - # │ ┌───────────── hour (0 - 23) - # │ │ ┌───────────── day of the month (1 - 31) - # │ │ │ ┌───────────── month (1 - 12 or JAN-DEC) - # │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT) - # * * * * * - - cron: '0 0 * * 3' - -jobs: - runtime: - runs-on: ubuntu-latest - steps: - - name: Query server address - id: server-address - run: | - SERVER_ADDRESS=$(dig +short terraformtesting-ghe.eastus.cloudapp.azure.com) - echo "::set-output name=server-address::${SERVER_ADDRESS}" - outputs: - server-address: ${{ steps.server-address.outputs.server-address }} - - acceptance-tests-anonymous: - needs: [runtime] - if: ${{ needs.runtime.outputs.server-address != '255.255.255.255' }} - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - name: Acceptance Tests (Anonymous) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - GITHUB_BASE_URL: "https://terraformtesting-ghe.eastus.cloudapp.azure.com/" - - acceptance-tests-individual: - needs: [runtime] - if: ${{ needs.runtime.outputs.server-address != '255.255.255.255' }} - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - - name: Acceptance Tests (Individual) - id: acceptance-tests-individual - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - RUN_ALL: true - GITHUB_BASE_URL: "https://terraformtesting-ghe.eastus.cloudapp.azure.com/" - GITHUB_OWNER: administrator - GITHUB_TEST_USER_TOKEN: ${{ secrets.GHES_TEST_USER_TOKEN }} - - - name: Failed Acceptance Tests (Individual) - if: ${{ failure() }} - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: DEBUG - RUN_ALLOWED: ${{ steps.acceptance-tests-individual.outputs.failed }} - GITHUB_BASE_URL: "https://terraformtesting-ghe.eastus.cloudapp.azure.com/" - GITHUB_OWNER: administrator - GITHUB_TEST_USER_TOKEN: ${{ secrets.GHES_TEST_USER_TOKEN }} - - acceptance-tests-organization: - needs: [runtime] - if: ${{ needs.runtime.outputs.server-address != '255.255.255.255' }} - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - - name: Acceptance Tests (Organization) - id: acceptance-tests-organization - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - TF_LOG: INFO - RUN_ALL: true - GITHUB_BASE_URL: "https://terraformtesting-ghe.eastus.cloudapp.azure.com/" - GITHUB_ORGANIZATION: terraformtesting - GITHUB_TEST_USER_TOKEN: ${{ secrets.GHES_TEST_USER_TOKEN }} - - - name: Failed Acceptance Tests (Organization) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - if: ${{ failure() }} - with: - TF_LOG: DEBUG - RUN_ALLOWED: ${{ steps.acceptance-tests-organization.outputs.failed }} - GITHUB_BASE_URL: "https://terraformtesting-ghe.eastus.cloudapp.azure.com/" - GITHUB_ORGANIZATION: terraformtesting - GITHUB_TEST_USER_TOKEN: ${{ secrets.GHES_TEST_USER_TOKEN }} diff --git a/.github/workflows/ghes-acceptance-tests.yaml b/.github/workflows/ghes-acceptance-tests.yaml new file mode 100644 index 0000000000..f28b32ef7e --- /dev/null +++ b/.github/workflows/ghes-acceptance-tests.yaml @@ -0,0 +1,91 @@ +name: Acceptance Tests (GHES) + +on: + workflow_dispatch: + # pull_request_target: + # types: + # - opened + # - synchronize + # - reopened + # branches: + # - main + +permissions: read-all + +jobs: + test: + name: Test GHES + # if: contains(github.event.pull_request.labels.*.name, 'acctest') + # environment: + # name: ghes + runs-on: ubuntu-latest + permissions: + contents: read + defaults: + run: + shell: bash + steps: + - name: Checkout + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + + - name: Set-up Go + uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 + with: + go-version-file: go.mod + cache: true + + - name: Check server address + id: server + run: | + set -eou pipefail + + host="${{ secrets.GHES_TEST_SERVER_HOST }}" + + if [[ -z "${host}" ]]; then + echo "Missing server address" >&2 + exit 1 + fi + + test="$(dig +short "${host}")" + + if [[ "${test}" != "255.255.255.255" ]]; then + echo "Invalid server address" >&2 + exit 1 + fi + + echo "address=https://${host}/" >> "${GITHUB_OUTPUT}" + + - name: Check credentials + id: credentials + run: | + set -eou pipefail + + token="${{ secrets.GHES_TEST_USER_TOKEN }}" + + if [[ -z "${token}" ]]; then + echo "Missing credentials" >&2 + exit 1 + fi + + echo "token=${token}" >> "${GITHUB_OUTPUT}" + + - name: Run tests + env: + TF_ACC: "1" + TF_LOG: INFO + TESTARGS: "-race -coverprofile=coverage.txt -covermode=atomic -sweep=tf-acc-" + GITHUB_BASE_URL: ${{ steps.server.outputs.address }} + GITHUB_TEST_TYPE: enterprise + GITHUB_OWNER: "" + GITHUB_USERNAME: "" + GITHUB_TOKEN: ${{ steps.credentials.outputs.token }} + GITHUB_ENTERPRISE_SLUG: "" + GITHUB_TEST_USER_REPOSITORY: "" + GITHUB_TEST_ORG_USER: "" + GITHUB_TEST_ORG_REPOSITORY: "" + GITHUB_TEST_ORG_TEMPLATE_REPOSITORY: "" + GITHUB_TEST_ORG_APP_INSTALLATION_ID: "" + GITHUB_TEST_EXTERNAL_USER: "" + GITHUB_TEST_EXTERNAL_USER_TOKEN: "" + GITHUB_TEST_EXTERNAL_USER2: "" + run: go test -run "^TestAcc*" ./github -v -race -coverprofile=coverage.txt -covermode=atomic -sweep=tf-acc- -timeout 120m -count=1 diff --git a/.github/workflows/ghes-acceptance-tests.yml b/.github/workflows/ghes-acceptance-tests.yml deleted file mode 100644 index 78318088b4..0000000000 --- a/.github/workflows/ghes-acceptance-tests.yml +++ /dev/null @@ -1,79 +0,0 @@ -name: GHES Acceptance Tests - -on: - push: - branches: - - test/** - -jobs: - runtime: - runs-on: ubuntu-latest - steps: - - name: Query server address - id: server-address - run: | - SERVER_ADDRESS=$(dig +short terraformtesting-ghe.eastus.cloudapp.azure.com) - echo "::set-output name=server-address::${SERVER_ADDRESS}" - outputs: - server-address: ${{ steps.server-address.outputs.server-address }} - - acceptance-tests-anonymous: - needs: [runtime] - if: ${{ needs.runtime.outputs.server-address != '255.255.255.255' }} - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - name: Acceptance Tests (Anonymous) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - GITHUB_BASE_URL: "https://terraformtesting-ghe.eastus.cloudapp.azure.com/" - - acceptance-tests-individual: - needs: [runtime] - if: ${{ needs.runtime.outputs.server-address != '255.255.255.255' }} - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - name: Acceptance Tests (Individual) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - GITHUB_BASE_URL: "https://terraformtesting-ghe.eastus.cloudapp.azure.com/" - GITHUB_OWNER: administrator - GITHUB_TEST_USER_TOKEN: ${{ secrets.GHES_TEST_USER_TOKEN }} - - acceptance-tests-organization: - needs: [runtime] - if: ${{ needs.runtime.outputs.server-address != '255.255.255.255' }} - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - ref: ${{ github.event.pull_request.head.ref }} - fetch-depth: 2 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 - with: - go-version-file: 'go.mod' - cache: true - - name: Acceptance Tests (Organization) - uses: terraformtesting/acceptance-tests@66f4842d934555dde0f59bf1a00abd0fc710ece4 # v2.2.0 - with: - GITHUB_BASE_URL: "https://terraformtesting-ghe.eastus.cloudapp.azure.com/" - GITHUB_ORGANIZATION: terraformtesting - GITHUB_TEST_USER_TOKEN: ${{ secrets.GHES_TEST_USER_TOKEN }} diff --git a/.github/workflows/immediate-response.yml b/.github/workflows/immediate-response.yaml similarity index 64% rename from .github/workflows/immediate-response.yml rename to .github/workflows/immediate-response.yaml index 8a29b200fc..2fd4789537 100644 --- a/.github/workflows/immediate-response.yml +++ b/.github/workflows/immediate-response.yaml @@ -1,7 +1,4 @@ -name: Issue/PR response -permissions: - issues: write - pull-requests: write +name: Issue/PR Response on: issues: types: @@ -9,21 +6,27 @@ on: pull_request_target: types: - opened + +permissions: read-all + jobs: - respond-to-issue: + respond: + name: Respond to Issue or PR if: ${{ github.actor != 'dependabot[bot]' && github.actor != 'renovate[bot]' && github.actor != 'githubactions[bot]' && github.actor != 'octokitbot' }} runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write + defaults: + run: + shell: bash steps: - - name: Determine issue or PR number - id: extract - run: echo "NUMBER=${{ github.event.issue.number || github.event.pull_request.number }}" >> "$GITHUB_OUTPUT" - - - name: Respond to issue or PR - uses: peter-evans/create-or-update-comment@v4 + - name: Comment + uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0 with: - issue-number: ${{ steps.extract.outputs.NUMBER }} + issue-number: ${{ github.event.issue.number || github.event.pull_request.number }} body: > 👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! - We have a [process in place](https://github.com/octokit/.github/blob/main/community/prioritization_response.md#overview) for prioritizing and responding to your input. + We have a [process in place](https://github.com/octokit/.github/blob/main/community/prioritization_response.md#overview) for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with `Status: Up for grabs`. You & others like you are the reason all of this works! So thank you & happy coding! 🚀 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yaml similarity index 71% rename from .github/workflows/release.yml rename to .github/workflows/release.yaml index 58111722a9..850ffa747c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yaml @@ -8,21 +8,28 @@ on: tags: - 'v*' -# Releases need permissions to read and write the repository contents. -# GitHub considers creating releases and uploading assets as writing contents. -permissions: - contents: write +permissions: read-all jobs: goreleaser: + name: GoReleaser runs-on: ubuntu-latest + # Releases need permissions to read and write the repository contents. + # GitHub considers creating releases and uploading assets as writing contents. + permissions: + contents: write + defaults: + run: + shell: bash steps: - - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + - name: Checkout + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: # Allow goreleaser to access older tag information. fetch-depth: 0 - - uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 + - name: Set-up Go + uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0 with: go-version-file: 'go.mod' cache: true @@ -36,10 +43,10 @@ jobs: - name: Run GoReleaser uses: goreleaser/goreleaser-action@9ed2f89a662bf1735a48bc8557fd212fa902bebf # v6.1.0 - with: - args: release --clean - version: latest env: # GitHub sets the GITHUB_TOKEN secret automatically. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} + with: + args: release --clean + version: latest diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yaml similarity index 78% rename from .github/workflows/stale.yml rename to .github/workflows/stale.yaml index c94fc36e3d..0c7ad6ff35 100644 --- a/.github/workflows/stale.yml +++ b/.github/workflows/stale.yaml @@ -1,14 +1,26 @@ -name: 'Close stale issues and PRs' +name: Close Stale Issues & PRs + on: workflow_dispatch: schedule: - cron: '30 1 * * *' +permissions: read-all + jobs: stale: + name: Stale runs-on: ubuntu-latest + permissions: + contents: write # only for delete-branch option + issues: write + pull-requests: write + defaults: + run: + shell: bash steps: - - uses: actions/stale@28ca1036281a5e5922ead5184a1bbf96e5fc984e # v9.0.0 + - name: Check for stale issues & PRs + uses: actions/stale@28ca1036281a5e5922ead5184a1bbf96e5fc984e # v9.0.0 with: stale-issue-message: > 👋 Hey Friends, this issue has been automatically marked as `stale` because it has no recent activity. diff --git a/.golangci.yml b/.golangci.yml index 1b2e11d4f5..c6f350e1a8 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -2,11 +2,11 @@ # and information on other useful linters run: - deadline: 3m + timeout: 3m modules-download-mode: vendor issues: - max-per-linter: 0 + max-issues-per-linter: 0 max-same-issues: 0 linters: @@ -14,7 +14,7 @@ linters: enable: - durationcheck - errcheck - - exportloopref + - copyloopvar # - forcetypeassert # - godot - gofmt @@ -29,8 +29,8 @@ linters: - unconvert # - unparam - unused - - vet - + - govet linters-settings: errcheck: - ignore: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema:ForceNew|Set,fmt:.*,io:Close + exclude-functions: + - (*github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema.ResourceData).Set diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f1526ef9a6..7370cc0738 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -39,8 +39,10 @@ Once you have the repository cloned, there's a couple of additional steps you'll - If you haven't already, generate a Personal Access Token (PAT) for authenticating your test runs. - Export the necessary configuration for authenticating your provider with GitHub ```sh - export GITHUB_TOKEN= - export GITHUB_ORGANIZATION= + export GITHUB_TEST_AUTH_MODE="organization" + export GITHUB_OWNER="" + export GITHUB_USERNAME="" + export GITHUB_TOKEN="" ``` - Build the project with `make build` - Try an example test run from the default (`main`) branch, like `TF_LOG=DEBUG TF_ACC=1 go test -v ./... -run ^TestAccGithubRepositories`. All those tests should pass. @@ -137,34 +139,46 @@ The following provider development overrides are set in the CLI configuration: Commonly required environment variables are listed below: ```sh -# enable debug logging +# Enable debug logging export TF_LOG=DEBUG -# enable testing of organization scenarios instead of individual or anonymous -export GITHUB_ORGANIZATION= +# Enables acceptance tests +export TF_ACC="1" -# enable testing of individual scenarios instead of organization or anonymous +# Configure the URL override for GHES. +export GITHUB_BASE_URL= + +# Configure acceptance testing mode; one of anonymous, individual, organization, team or enterprise. If not set will default to anonymous +export GITHUB_TEST_AUTH_MODE= + +# Configure authentication for testing export GITHUB_OWNER= +export GITHUB_USERNAME= +export GITHUB_TOKEN= -# enable testing of enterprise appliances -export GITHUB_BASE_URL= +# Configure user level values +export GITHUB_TEST_USER_REPOSITORY= -# leverage helper accounts for tests requiring them -# examples include: -# - https://github.com/github-terraform-test-user -# - https://github.com/terraformtesting -export GITHUB_TEST_OWNER= -export GITHUB_TEST_ORGANIZATION= -export GITHUB_TEST_USER_TOKEN= -``` +# Configure for the org under test +export GITHUB_TEST_ORG_USER= +export GITHUB_TEST_ORG_REPOSITORY= +export GITHUB_TEST_ORG_TEMPLATE_REPOSITORY= +export GITHUB_TEST_ORG_APP_INSTALLATION_ID= + +# Configure external (non-org) users +export GITHUB_TEST_EXTERNAL_USER= +export GITHUB_TEST_EXTERNAL_USER_TOKEN= +export GITHUB_TEST_EXTERNAL_USER2= -See [this project](https://github.com/terraformtesting/acceptance-tests) for more information on our old system for automated testing. +# Configure test options +export GITHUB_TEST_ADVANCED_SECURITY= +``` There are also a small amount of unit tests in the provider. Due to the nature of the provider, such tests are currently only recommended for exercising functionality completely internal to the provider. These may be executed by running `make test`. ### GitHub Organization -If you do not have an organization already that you are comfortable running tests against, you will need to [create one](https://help.github.com/en/articles/creating-a-new-organization-from-scratch). The free "Team for Open Source" org type is fine for these tests. The name of the organization must then be exported in your environment as `GITHUB_ORGANIZATION`. +If you do not have an organization already that you are comfortable running tests against, you will need to [create one](https://help.github.com/en/articles/creating-a-new-organization-from-scratch). The free "Team for Open Source" org type is fine for these tests. The name of the organization must then be exported in your environment as `GITHUB_OWNER`. Make sure that your organization has a `terraform-template-module` repository ([terraformtesting/terraform-template-module](https://github.com/terraformtesting/terraform-template-module) is an example you can clone) and that its "Template repository" item in Settings is checked. diff --git a/GNUmakefile b/GNUmakefile index 3ad7c52274..e9ade49ef1 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -3,11 +3,13 @@ GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor) WEBSITE_REPO=github.com/hashicorp/terraform-website PKG_NAME=github +export TESTARGS=-race -coverprofile=coverage.txt -covermode=atomic + default: build tools: - go install github.com/client9/misspell/cmd/misspell@v0.3.4 - go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51.1 + go install github.com/client9/misspell/cmd/misspell + go install github.com/golangci/golangci-lint/cmd/golangci-lint build: fmtcheck go build ./... @@ -27,8 +29,8 @@ test: go test ./... # commenting this out for release tooling, please run testacc instead -testacc: fmtcheck - TF_ACC=1 go test $(TEST) -v $(TESTARGS) -timeout 120m +testacc: + TF_ACC=1 go test -run "^TestAcc*" ./github -v $(TESTARGS) -timeout 120m -count=1 test-compile: @if [ "$(TEST)" = "./..." ]; then \ diff --git a/github/acc_test.go b/github/acc_test.go new file mode 100644 index 0000000000..1dc91ec3cd --- /dev/null +++ b/github/acc_test.go @@ -0,0 +1,222 @@ +package github + +import ( + "context" + "fmt" + "log" + "os" + "slices" + "strconv" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +type testMode string + +const ( + anonymous testMode = "anonymous" + individual testMode = "individual" + organization testMode = "organization" + team testMode = "team" + enterprise testMode = "enterprise" +) + +type testAccConfig struct { + // Target configuration + baseUrl string + + // Auth configuration + authMode testMode + owner string + username string + token string + + // Enterprise configuration + enterpriseSlug string + + // Global test configuration + testPublicRepository string + testPublicRepositoryOwner string + testPublicReleaseId int + testPublicTemplateRepository string + testPublicTemplateRepositoryOwner string + testGHActionsAppInstallationId int + + // User test configuration + testUserRepository string + + // Org test configuration + testOrgUser string + testOrgRepository string + testOrgTemplateRepository string + testOrgAppInstallationId int + + // External test configuration + testExternalUser string + testExternalUserToken string + testExternalUser2 string + + // Test options + testAdvancedSecurity bool +} + +var testAccConf *testAccConfig + +// providerFactories are used to instantiate a provider during acceptance testing. +// The factory function will be invoked for every Terraform CLI command executed +// to create a provider server to which the CLI can reattach. +var providerFactories = map[string]func() (*schema.Provider, error){ + "github": func() (*schema.Provider, error) { + return Provider(), nil + }, +} + +func TestMain(m *testing.M) { + authMode := testMode(os.Getenv("GITHUB_TEST_AUTH_MODE")) + if len(authMode) == 0 { + authMode = anonymous + } + + config := testAccConfig{ + baseUrl: os.Getenv("GITHUB_BASE_URL"), + authMode: authMode, + testPublicRepository: "terraform-provider-github", + testPublicRepositoryOwner: "integrations", + testPublicReleaseId: 186531906, + testPublicTemplateRepository: "template-repository", + testPublicTemplateRepositoryOwner: "template-repository", + testGHActionsAppInstallationId: 15368, + testUserRepository: os.Getenv("GITHUB_TEST_USER_REPOSITORY"), + testOrgUser: os.Getenv("GITHUB_TEST_ORG_USER"), + testOrgRepository: os.Getenv("GITHUB_TEST_ORG_REPOSITORY"), + testOrgTemplateRepository: os.Getenv("GITHUB_TEST_ORG_TEMPLATE_REPOSITORY"), + testExternalUser: os.Getenv("GITHUB_TEST_EXTERNAL_USER"), + testExternalUserToken: os.Getenv("GITHUB_TEST_EXTERNAL_USER_TOKEN"), + testExternalUser2: os.Getenv("GITHUB_TEST_EXTERNAL_USER2"), + testAdvancedSecurity: os.Getenv("GITHUB_TEST_ADVANCED_SECURITY") == "true", + } + + if config.authMode != anonymous { + config.owner = os.Getenv("GITHUB_OWNER") + config.username = os.Getenv("GITHUB_USERNAME") + config.token = os.Getenv("GITHUB_TOKEN") + + if len(config.owner) == 0 { + fmt.Println("GITHUB_OWNER environment variable not set") + os.Exit(1) + } + + if len(config.username) == 0 { + fmt.Println("GITHUB_USERNAME environment variable not set") + os.Exit(1) + } + + if len(config.token) == 0 { + fmt.Println("GITHUB_TOKEN environment variable not set") + os.Exit(1) + } + } + + if config.authMode == enterprise { + config.enterpriseSlug = os.Getenv("GITHUB_ENTERPRISE_SLUG") + + if len(config.enterpriseSlug) == 0 { + fmt.Println("GITHUB_ENTERPRISE_SLUG environment variable not set") + os.Exit(1) + } + } + + i, err := strconv.Atoi(os.Getenv("GITHUB_TEST_ORG_APP_INSTALLATION_ID")) + if err == nil { + config.testOrgAppInstallationId = i + } + + testAccConf = &config + + if testAccConf.authMode != anonymous { + meta, err := getTestMeta() + if err != nil { + fmt.Println("could not get test meta for sweepers: %w", err) + os.Exit(1) + } + + resource.TestMain(m) + + resource.AddTestSweepers("github_repository", &resource.Sweeper{ + Name: "github_repository", + F: getSweepRepositoriesFunc(meta), + }) + } + + m.Run() +} + +func getTestMeta() (*Owner, error) { + config := Config{ + Token: testAccConf.token, + Owner: testAccConf.owner, + BaseURL: testAccConf.baseUrl, + } + + meta, err := config.Meta() + if err != nil { + return nil, fmt.Errorf("error getting GitHub meta parameter") + } + + return meta.(*Owner), nil +} + +func getSweepRepositoriesFunc(meta *Owner) func(string) error { + return func(prefix string) error { + client := meta.v3client + owner := meta.name + + repos, _, err := client.Repositories.ListByUser(context.TODO(), owner, nil) + if err != nil { + return err + } + + for _, r := range repos { + if name := r.GetName(); strings.HasPrefix(name, prefix) { + log.Printf("[DEBUG] Destroying Repository %s", name) + + if _, err := client.Repositories.Delete(context.TODO(), owner, name); err != nil { + return err + } + } + } + + return nil + } +} + +func skipUnauthenticated(t *testing.T) { + if testAccConf.authMode == anonymous { + t.Skip("Skipping as test mode not authenticated") + } +} + +func skipUnlessHasOrgs(t *testing.T) { + orgModes := []testMode{organization, team, enterprise} + + if !slices.Contains(orgModes, testAccConf.authMode) { + t.Skip("Skipping as test mode doesn't have orgs") + } +} + +func skipUnlessHasPaidOrgs(t *testing.T) { + orgModes := []testMode{team, enterprise} + + if !slices.Contains(orgModes, testAccConf.authMode) { + t.Skip("Skipping as test mode doesn't have orgs") + } +} + +func skipUnlessMode(t *testing.T, testModes ...testMode) { + if !slices.Contains(testModes, testAccConf.authMode) { + t.Skip("Skipping as not supported test mode") + } +} diff --git a/github/config_test.go b/github/config_test.go index 45cafe69d9..865930ee12 100644 --- a/github/config_test.go +++ b/github/config_test.go @@ -8,14 +8,7 @@ import ( ) func TestAccConfigMeta(t *testing.T) { - - // FIXME: Skip test runs during travis lint checking - if testToken == "" { - return - } - t.Run("returns an anonymous client for the v3 REST API", func(t *testing.T) { - config := Config{BaseURL: "https://api.github.com/"} meta, err := config.Meta() if err != nil { @@ -28,20 +21,13 @@ func TestAccConfigMeta(t *testing.T) { if err != nil { t.Fatalf("failed to validate returned client without error: %s", err.Error()) } - - }) - - t.Run("returns an anonymous client for the v4 GraphQL API", func(t *testing.T) { - - // https://developer.github.com/v4/guides/forming-calls/#authenticating-with-graphql - t.Skip("anonymous client for the v4 GraphQL API is unsupported") - }) t.Run("returns a v3 REST API client to manage individual resources", func(t *testing.T) { + skipUnlessMode(t, individual) config := Config{ - Token: testToken, + Token: testAccConf.token, BaseURL: "https://api.github.com/", } meta, err := config.Meta() @@ -55,13 +41,13 @@ func TestAccConfigMeta(t *testing.T) { if err != nil { t.Fatalf("failed to validate returned client without error: %s", err.Error()) } - }) t.Run("returns a v3 REST API client with max retries", func(t *testing.T) { + skipUnlessMode(t, individual) config := Config{ - Token: testToken, + Token: testAccConf.token, BaseURL: "https://api.github.com/", RetryableErrors: map[int]bool{ 500: true, @@ -80,13 +66,13 @@ func TestAccConfigMeta(t *testing.T) { if err != nil { t.Fatalf("failed to validate returned client without error: %s", err.Error()) } - }) t.Run("returns a v4 GraphQL API client to manage individual resources", func(t *testing.T) { + skipUnlessMode(t, individual) config := Config{ - Token: testToken, + Token: testAccConf.token, BaseURL: "https://api.github.com/", } meta, err := config.Meta() @@ -104,15 +90,15 @@ func TestAccConfigMeta(t *testing.T) { if err != nil { t.Fatalf("failed to validate returned client without error: %s", err.Error()) } - }) t.Run("returns a v3 REST API client to manage organization resources", func(t *testing.T) { + skipUnlessHasOrgs(t) config := Config{ - Token: testToken, + Token: testAccConf.token, BaseURL: "https://api.github.com/", - Owner: testOrganization, + Owner: testAccConf.owner, } meta, err := config.Meta() if err != nil { @@ -121,19 +107,19 @@ func TestAccConfigMeta(t *testing.T) { ctx := context.Background() client := meta.(*Owner).v3client - _, _, err = client.Organizations.Get(ctx, testOrganization) + _, _, err = client.Organizations.Get(ctx, testAccConf.owner) if err != nil { t.Fatalf("failed to validate returned client without error: %s", err.Error()) } - }) t.Run("returns a v4 GraphQL API client to manage organization resources", func(t *testing.T) { + skipUnlessHasOrgs(t) config := Config{ - Token: testToken, + Token: testAccConf.token, BaseURL: "https://api.github.com/", - Owner: testOrganization, + Owner: testAccConf.owner, } meta, err := config.Meta() if err != nil { @@ -148,7 +134,7 @@ func TestAccConfigMeta(t *testing.T) { } `graphql:"organization(login: $login)"` } variables := map[string]interface{}{ - "login": githubv4.String(testOrganization), + "login": githubv4.String(testAccConf.owner), } err = client.Query(context.Background(), &query, variables) if err != nil { @@ -158,7 +144,5 @@ func TestAccConfigMeta(t *testing.T) { if query.Organization.ViewerCanAdminister != true { t.Fatalf("unexpected response when validating client") } - }) - } diff --git a/github/data_source_github_actions_environment_secrets_test.go b/github/data_source_github_actions_environment_secrets_test.go index 766e37ad31..10a6223bf5 100644 --- a/github/data_source_github_actions_environment_secrets_test.go +++ b/github/data_source_github_actions_environment_secrets_test.go @@ -9,7 +9,6 @@ import ( ) func TestAccGithubActionsEnvironmentSecretsDataSource(t *testing.T) { - t.Run("queries actions secrets from an environment", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -47,25 +46,19 @@ func TestAccGithubActionsEnvironmentSecretsDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_actions_environment_secrets.test", "secrets.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_actions_environment_variables_test.go b/github/data_source_github_actions_environment_variables_test.go index e929e53c97..999fe523ab 100644 --- a/github/data_source_github_actions_environment_variables_test.go +++ b/github/data_source_github_actions_environment_variables_test.go @@ -10,7 +10,6 @@ import ( ) func TestAccGithubActionsEnvironmentVariablesDataSource(t *testing.T) { - t.Run("queries actions variables from an environment", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -47,25 +46,19 @@ func TestAccGithubActionsEnvironmentVariablesDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_actions_environment_variables.test", "variables.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_actions_organization_oidc_subject_claim_customization_template_test.go b/github/data_source_github_actions_organization_oidc_subject_claim_customization_template_test.go index 9bf2f5c92b..a72d201208 100644 --- a/github/data_source_github_actions_organization_oidc_subject_claim_customization_template_test.go +++ b/github/data_source_github_actions_organization_oidc_subject_claim_customization_template_test.go @@ -7,9 +7,7 @@ import ( ) func TestAccGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateDataSource(t *testing.T) { - t.Run("get an organization oidc subject claim customization template without error", func(t *testing.T) { - config := ` resource "github_actions_organization_oidc_subject_claim_customization_template" "test" { include_claim_keys = ["actor", "actor_id", "head_ref", "repository"] @@ -28,33 +26,19 @@ func TestAccGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateDataSo resource.TestCheckResourceAttr("data.github_actions_organization_oidc_subject_claim_customization_template.test", "include_claim_keys.3", "repository"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_actions_organization_public_key_test.go b/github/data_source_github_actions_organization_public_key_test.go index 386467ebe1..05cbb70671 100644 --- a/github/data_source_github_actions_organization_public_key_test.go +++ b/github/data_source_github_actions_organization_public_key_test.go @@ -7,9 +7,7 @@ import ( ) func TestAccGithubActionsOrganizationPublicKeyDataSource(t *testing.T) { - t.Run("queries an organization public key without error", func(t *testing.T) { - config := ` data "github_actions_organization_public_key" "test" {} ` @@ -20,30 +18,15 @@ func TestAccGithubActionsOrganizationPublicKeyDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/data_source_github_actions_organization_registration_token_test.go b/github/data_source_github_actions_organization_registration_token_test.go index e920cdf8e5..92be4997b3 100644 --- a/github/data_source_github_actions_organization_registration_token_test.go +++ b/github/data_source_github_actions_organization_registration_token_test.go @@ -7,9 +7,7 @@ import ( ) func TestAccGithubActionsOrganizationRegistrationTokenDataSource(t *testing.T) { - t.Run("get an organization registration token without error", func(t *testing.T) { - config := ` data "github_actions_organization_registration_token" "test" { } @@ -20,30 +18,15 @@ func TestAccGithubActionsOrganizationRegistrationTokenDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_actions_organization_registration_token.test", "expires_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/data_source_github_actions_organization_secrets_test.go b/github/data_source_github_actions_organization_secrets_test.go index 615ff93d44..0324cb69cf 100644 --- a/github/data_source_github_actions_organization_secrets_test.go +++ b/github/data_source_github_actions_organization_secrets_test.go @@ -10,7 +10,6 @@ import ( ) func TestAccGithubActionsOrganizationSecretsDataSource(t *testing.T) { - t.Run("queries organization actions secrets from a repository", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -35,25 +34,19 @@ func TestAccGithubActionsOrganizationSecretsDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_actions_organization_secrets.test", "secrets.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_actions_organization_variables_test.go b/github/data_source_github_actions_organization_variables_test.go index a1b0b1eaf2..53501498c1 100644 --- a/github/data_source_github_actions_organization_variables_test.go +++ b/github/data_source_github_actions_organization_variables_test.go @@ -10,7 +10,6 @@ import ( ) func TestAccGithubActionsOrganizationVariablesDataSource(t *testing.T) { - t.Run("queries actions variables from an organization", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -36,25 +35,19 @@ func TestAccGithubActionsOrganizationVariablesDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_actions_organization_variables.test", "variables.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_actions_public_key_test.go b/github/data_source_github_actions_public_key_test.go index 6c69e15d93..1178529c38 100644 --- a/github/data_source_github_actions_public_key_test.go +++ b/github/data_source_github_actions_public_key_test.go @@ -9,11 +9,9 @@ import ( ) func TestAccGithubActionsPublicKeyDataSource(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("queries a repository public key without error", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%[1]s" @@ -31,30 +29,15 @@ func TestAccGithubActionsPublicKeyDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/data_source_github_actions_registration_token_test.go b/github/data_source_github_actions_registration_token_test.go index ca96873df9..46e4444d92 100644 --- a/github/data_source_github_actions_registration_token_test.go +++ b/github/data_source_github_actions_registration_token_test.go @@ -9,11 +9,9 @@ import ( ) func TestAccGithubActionsRegistrationTokenDataSource(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("get a repository registration token without error", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%[1]s" @@ -31,30 +29,15 @@ func TestAccGithubActionsRegistrationTokenDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_actions_registration_token.test", "expires_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/data_source_github_actions_repository_oidc_subject_claim_customization_template_test.go b/github/data_source_github_actions_repository_oidc_subject_claim_customization_template_test.go index 0cf992939b..efdb1b77ff 100644 --- a/github/data_source_github_actions_repository_oidc_subject_claim_customization_template_test.go +++ b/github/data_source_github_actions_repository_oidc_subject_claim_customization_template_test.go @@ -9,17 +9,15 @@ import ( ) func TestAccGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplateDataSource(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("get an repository oidc subject claim customization template without error", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" visibility = "private" } - + resource "github_actions_repository_oidc_subject_claim_customization_template" "test" { repository = github_repository.test.name use_default = false @@ -38,7 +36,7 @@ func TestAccGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplateDataSour name = "tf-acc-test-%s" visibility = "private" } - + resource "github_actions_repository_oidc_subject_claim_customization_template" "test" { repository = github_repository.test.name use_default = true @@ -64,41 +62,27 @@ func TestAccGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplateDataSour resource.TestCheckResourceAttr("data.github_actions_repository_oidc_subject_claim_customization_template.test", "include_claim_keys.#", "0"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check1, - }, - { - Config: config3, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config4, - Check: check2, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check1, + }, + { + Config: config3, + Check: resource.ComposeTestCheckFunc(), + }, + { + Config: config4, + Check: check2, + }, + }, }) }) } diff --git a/github/data_source_github_actions_secrets_test.go b/github/data_source_github_actions_secrets_test.go index da78842432..3c788e44ba 100644 --- a/github/data_source_github_actions_secrets_test.go +++ b/github/data_source_github_actions_secrets_test.go @@ -9,7 +9,6 @@ import ( ) func TestAccGithubActionsSecretsDataSource(t *testing.T) { - t.Run("queries actions secrets from a repository", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -40,25 +39,19 @@ func TestAccGithubActionsSecretsDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_actions_secrets.test", "secrets.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_actions_variables_test.go b/github/data_source_github_actions_variables_test.go index 37c01940a7..5d53c924db 100644 --- a/github/data_source_github_actions_variables_test.go +++ b/github/data_source_github_actions_variables_test.go @@ -9,7 +9,6 @@ import ( ) func TestAccGithubActionsVariablesDataSource(t *testing.T) { - t.Run("queries actions variables from a repository", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -41,25 +40,19 @@ func TestAccGithubActionsVariablesDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_actions_variables.test", "variables.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, "organization") + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_app_test.go b/github/data_source_github_app_test.go new file mode 100644 index 0000000000..5130b7042a --- /dev/null +++ b/github/data_source_github_app_test.go @@ -0,0 +1,33 @@ +package github + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccGithubAppDataSource(t *testing.T) { + t.Run("queries a global app", func(t *testing.T) { + config := ` + data "github_app" "test" { + slug = "github-actions" + } + ` + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet( + "data.github_app.test", "name", + ), + ) + + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + }) +} diff --git a/github/data_source_github_app_token_test.go b/github/data_source_github_app_token_test.go index d6a693eb53..6c8c46d2f2 100644 --- a/github/data_source_github_app_token_test.go +++ b/github/data_source_github_app_token_test.go @@ -9,18 +9,20 @@ import ( "github.com/google/go-github/v66/github" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/stretchr/testify/assert" ) func TestAccGithubAppTokenDataSource(t *testing.T) { - expectedAccessToken := "W+2e/zjiMTweDAr2b35toCF+h29l7NW92rJIPvFrCJQK" + t.Run("creates a application token without error", func(t *testing.T) { + expectedAccessToken := "W+2e/zjiMTweDAr2b35toCF+h29l7NW92rJIPvFrCJQK" - owner := "test-owner" + owner := "test-owner" - pemData, err := os.ReadFile(testGitHubAppPrivateKeyFile) - assert.Nil(t, err) + pemData, err := os.ReadFile(testGitHubAppPrivateKeyFile) + if err != nil { + t.Logf("Unexpected error: %s", err) + t.Fail() + } - t.Run("creates a application token without error", func(t *testing.T) { ts := githubApiMock([]*mockResponse{ { ExpectedUri: fmt.Sprintf("/api/v3/app/installations/%s/access_tokens", testGitHubAppInstallationID), @@ -59,8 +61,15 @@ func TestAccGithubAppTokenDataSource(t *testing.T) { "token": "", }) - err := dataSourceGithubAppTokenRead(schema, meta) - assert.Nil(t, err) - assert.Equal(t, expectedAccessToken, schema.Get("token")) + err = dataSourceGithubAppTokenRead(schema, meta) + if err != nil { + t.Logf("Unexpected error: %s", err) + t.Fail() + } + + if schema.Get("token") != expectedAccessToken { + t.Logf("Expected %s, got %s", expectedAccessToken, schema.Get("token")) + t.Fail() + } }) } diff --git a/github/data_source_github_branch_protection_rules_test.go b/github/data_source_github_branch_protection_rules_test.go index 3c4a5175b2..29a78787d3 100644 --- a/github/data_source_github_branch_protection_rules_test.go +++ b/github/data_source_github_branch_protection_rules_test.go @@ -9,7 +9,6 @@ import ( ) func TestAccGithubBranchProtectionRulesDataSource(t *testing.T) { - t.Run("queries branch protection rules without error", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -28,31 +27,16 @@ func TestAccGithubBranchProtectionRulesDataSource(t *testing.T) { resource.TestCheckResourceAttr("data.github_branch_protection_rules.all", "rules.#", "0"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("queries branch protection", func(t *testing.T) { @@ -81,33 +65,18 @@ func TestAccGithubBranchProtectionRulesDataSource(t *testing.T) { resource.TestCheckResourceAttr("data.github_branch_protection_rules.all", "rules.0.pattern", "main*"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) - }) } diff --git a/github/data_source_github_branch_test.go b/github/data_source_github_branch_test.go index 3012caaf51..ae2d4feaee 100644 --- a/github/data_source_github_branch_test.go +++ b/github/data_source_github_branch_test.go @@ -10,7 +10,6 @@ import ( ) func TestAccGithubBranchDataSource(t *testing.T) { - t.Run("queries an existing branch without error", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -32,78 +31,49 @@ func TestAccGithubBranchDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - t.Run("queries an invalid branch without error", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - - config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "tf-acc-test-%[1]s" - auto_init = true - } - - data "github_branch" "test" { - repository = github_repository.test.name - branch = "xxxxxx" - } - `, randomID) - - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "data.github_branch.test", "ref", "", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - - }) + // Can't test due to SDK and test framework limitations + // t.Run("queries an invalid branch without error", func(t *testing.T) { + // randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + + // config := fmt.Sprintf(` + // resource "github_repository" "test" { + // name = "tf-acc-test-%[1]s" + // auto_init = true + // } + + // data "github_branch" "test" { + // repository = github_repository.test.name + // branch = "xxxxxx" + // } + // `, randomID) + + // check := resource.ComposeTestCheckFunc( + // resource.TestCheckResourceAttr( + // "data.github_branch.test", "ref", "", + // ), + // ) + + // resource.Test(t, resource.TestCase{ + // PreCheck: func() { skipUnauthenticated(t) }, + // ProviderFactories: providerFactories, + // Steps: []resource.TestStep{ + // { + // Config: config, + // Check: check, + // }, + // }, + // }) + // }) } diff --git a/github/data_source_github_codespaces_organization_public_key_test.go b/github/data_source_github_codespaces_organization_public_key_test.go index cdf07c1f17..15ca95ff65 100644 --- a/github/data_source_github_codespaces_organization_public_key_test.go +++ b/github/data_source_github_codespaces_organization_public_key_test.go @@ -7,43 +7,23 @@ import ( ) func TestAccGithubCodespacesOrganizationPublicKeyDataSource(t *testing.T) { - t.Run("queries an organization public key without error", func(t *testing.T) { - config := ` data "github_codespaces_organization_public_key" "test" {} ` - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet( - "data.github_codespaces_organization_public_key.test", "key", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_codespaces_organization_public_key.test", "key_id"), + resource.TestCheckResourceAttrSet("data.github_codespaces_organization_public_key.test", "key"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/data_source_github_codespaces_organization_secrets_test.go b/github/data_source_github_codespaces_organization_secrets_test.go index 6f04a2d8eb..88ff440bde 100644 --- a/github/data_source_github_codespaces_organization_secrets_test.go +++ b/github/data_source_github_codespaces_organization_secrets_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -10,50 +9,37 @@ import ( ) func TestAccGithubCodespacesOrganizationSecretsDataSource(t *testing.T) { - t.Run("queries organization codespaces secrets from a repository", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + secretName := fmt.Sprintf("ORG_CS_SECRET_1_%s", randomID) config := fmt.Sprintf(` - resource "github_codespaces_organization_secret" "test" { - secret_name = "org_cs_secret_1_%s" - plaintext_value = "foo" - visibility = "private" - } - `, randomID) - - config2 := config + ` - data "github_codespaces_organization_secrets" "test" { - } - ` - - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.github_codespaces_organization_secrets.test", "secrets.#", "1"), - resource.TestCheckResourceAttr("data.github_codespaces_organization_secrets.test", "secrets.0.name", strings.ToUpper(fmt.Sprintf("ORG_CS_SECRET_1_%s", randomID))), - resource.TestCheckResourceAttr("data.github_codespaces_organization_secrets.test", "secrets.0.visibility", "private"), - resource.TestCheckResourceAttrSet("data.github_codespaces_organization_secrets.test", "secrets.0.created_at"), - resource.TestCheckResourceAttrSet("data.github_codespaces_organization_secrets.test", "secrets.0.updated_at"), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, - }, - }) + resource "github_codespaces_organization_secret" "test" { + secret_name = %s" + plaintext_value = "foo" + visibility = "private" } - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + data "github_codespaces_organization_secrets" "test" { + depends_on = [github_codespaces_organization_secret.test] + } + `, secretName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.github_codespaces_organization_secrets.test", "secrets.#", "1"), + resource.TestCheckResourceAttr("data.github_codespaces_organization_secrets.test", "secrets.0.name", secretName), + resource.TestCheckResourceAttr("data.github_codespaces_organization_secrets.test", "secrets.0.visibility", "private"), + resource.TestCheckResourceAttrSet("data.github_codespaces_organization_secrets.test", "secrets.0.created_at"), + resource.TestCheckResourceAttrSet("data.github_codespaces_organization_secrets.test", "secrets.0.updated_at"), + ), + }, + }, }) }) } diff --git a/github/data_source_github_codespaces_public_key_test.go b/github/data_source_github_codespaces_public_key_test.go index 381630f852..33d3617775 100644 --- a/github/data_source_github_codespaces_public_key_test.go +++ b/github/data_source_github_codespaces_public_key_test.go @@ -9,11 +9,9 @@ import ( ) func TestAccGithubCodespacesPublicKeyDataSource(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("queries a repository public key without error", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%[1]s" @@ -31,30 +29,15 @@ func TestAccGithubCodespacesPublicKeyDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/data_source_github_codespaces_secrets_test.go b/github/data_source_github_codespaces_secrets_test.go index 368c1137b6..27a3b053ae 100644 --- a/github/data_source_github_codespaces_secrets_test.go +++ b/github/data_source_github_codespaces_secrets_test.go @@ -9,7 +9,6 @@ import ( ) func TestAccGithubCodespacesSecretsDataSource(t *testing.T) { - t.Run("queries codespaces secrets from a repository", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -40,33 +39,19 @@ func TestAccGithubCodespacesSecretsDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_codespaces_secrets.test", "secrets.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_codespaces_user_public_key_test.go b/github/data_source_github_codespaces_user_public_key_test.go index 0b55a18a23..44fdd34cd1 100644 --- a/github/data_source_github_codespaces_user_public_key_test.go +++ b/github/data_source_github_codespaces_user_public_key_test.go @@ -7,9 +7,7 @@ import ( ) func TestAccGithubCodespacesUserPublicKeyDataSource(t *testing.T) { - t.Run("queries an user public key without error", func(t *testing.T) { - config := ` data "github_codespaces_user_public_key" "test" {} ` @@ -20,30 +18,15 @@ func TestAccGithubCodespacesUserPublicKeyDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - t.Skip("organization account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - }) } diff --git a/github/data_source_github_codespaces_user_secrets_test.go b/github/data_source_github_codespaces_user_secrets_test.go index 2192c41f2d..6db857dd70 100644 --- a/github/data_source_github_codespaces_user_secrets_test.go +++ b/github/data_source_github_codespaces_user_secrets_test.go @@ -10,7 +10,6 @@ import ( ) func TestAccGithubCodespacesUserSecretsDataSource(t *testing.T) { - t.Run("queries user codespaces secrets from a repository", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -33,25 +32,19 @@ func TestAccGithubCodespacesUserSecretsDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_codespaces_user_secrets.test", "secrets.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, individual) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_collaborators_test.go b/github/data_source_github_collaborators_test.go index 6ef2da71ae..668c2d8da9 100644 --- a/github/data_source_github_collaborators_test.go +++ b/github/data_source_github_collaborators_test.go @@ -8,59 +8,10 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -func TestAccGithubCollaboratorsDataSource_basic(t *testing.T) { - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) - } - - dsn := "data.github_collaborators.test" - repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5)) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { - testAccPreCheck(t) - }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCheckGithubCollaboratorsDataSourceConfig(repoName), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet(dsn, "collaborator.#"), - resource.TestCheckResourceAttr(dsn, "affiliation", "all"), - ), - }, - }, - }) -} - -func TestAccGithubCollaboratorsDataSource_withPermission(t *testing.T) { - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) - } - - dsn := "data.github_collaborators.test" - repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5)) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { - testAccPreCheck(t) - }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCheckGithubCollaboratorsDataSourcePermissionConfig(repoName), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet(dsn, "collaborator.#"), - resource.TestCheckResourceAttr(dsn, "affiliation", "all"), - resource.TestCheckResourceAttr(dsn, "permission", "admin"), - ), - }, - }, - }) -} - -func testAccCheckGithubCollaboratorsDataSourceConfig(repo string) string { - return fmt.Sprintf(` +func TestAccGithubCollaboratorsDataSource(t *testing.T) { + t.Run("gets all collaborators", func(t *testing.T) { + repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5)) + config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" } @@ -69,10 +20,28 @@ data "github_collaborators" "test" { owner = "%s" repository = "${github_repository.test.name}" } -`, repo, testOwner) -} -func testAccCheckGithubCollaboratorsDataSourcePermissionConfig(repo string) string { - return fmt.Sprintf(` +`, repoName, testAccConf.owner) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_collaborators.test", "collaborator.#"), + resource.TestCheckResourceAttr("data.github_collaborators.test", "affiliation", "all"), + ) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + }) + + t.Run("gets admin collaborators", func(t *testing.T) { + repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5)) + config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" } @@ -82,5 +51,23 @@ data "github_collaborators" "test" { repository = "${github_repository.test.name}" permission = "admin" } -`, repo, testOwner) +`, repoName, testAccConf.owner) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_collaborators.test", "collaborator.#"), + resource.TestCheckResourceAttr("data.github_collaborators.test", "affiliation", "all"), + resource.TestCheckResourceAttr("data.github_collaborators.test", "permission", "admin"), + ) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + }) } diff --git a/github/data_source_github_dependabot_organization_public_key_test.go b/github/data_source_github_dependabot_organization_public_key_test.go index a4f249b3ae..2e6ac3ef04 100644 --- a/github/data_source_github_dependabot_organization_public_key_test.go +++ b/github/data_source_github_dependabot_organization_public_key_test.go @@ -7,9 +7,7 @@ import ( ) func TestAccGithubDependabotOrganizationPublicKeyDataSource(t *testing.T) { - t.Run("queries an organization public key without error", func(t *testing.T) { - config := ` data "github_dependabot_organization_public_key" "test" {} ` @@ -20,30 +18,15 @@ func TestAccGithubDependabotOrganizationPublicKeyDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/data_source_github_dependabot_organization_secrets_test.go b/github/data_source_github_dependabot_organization_secrets_test.go index e227b08a21..d70aac505c 100644 --- a/github/data_source_github_dependabot_organization_secrets_test.go +++ b/github/data_source_github_dependabot_organization_secrets_test.go @@ -10,7 +10,6 @@ import ( ) func TestAccGithubDependabotOrganizationSecretsDataSource(t *testing.T) { - t.Run("queries organization dependabot secrets from a repository", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -35,25 +34,19 @@ func TestAccGithubDependabotOrganizationSecretsDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_dependabot_organization_secrets.test", "secrets.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_dependabot_public_key_test.go b/github/data_source_github_dependabot_public_key_test.go index 5ebff1a674..101e5d2034 100644 --- a/github/data_source_github_dependabot_public_key_test.go +++ b/github/data_source_github_dependabot_public_key_test.go @@ -9,11 +9,9 @@ import ( ) func TestAccGithubDependabotPublicKeyDataSource(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("queries a repository public key without error", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%[1]s" @@ -31,30 +29,15 @@ func TestAccGithubDependabotPublicKeyDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/data_source_github_dependabot_secrets_test.go b/github/data_source_github_dependabot_secrets_test.go index 992b69603d..88c699ce62 100644 --- a/github/data_source_github_dependabot_secrets_test.go +++ b/github/data_source_github_dependabot_secrets_test.go @@ -9,7 +9,6 @@ import ( ) func TestAccGithubDependabotSecretsDataSource(t *testing.T) { - t.Run("queries dependabot secrets from a repository", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -40,25 +39,19 @@ func TestAccGithubDependabotSecretsDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_dependabot_secrets.test", "secrets.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_enterprise_test.go b/github/data_source_github_enterprise_test.go index 4ec28ace2b..e9729ee61d 100644 --- a/github/data_source_github_enterprise_test.go +++ b/github/data_source_github_enterprise_test.go @@ -2,45 +2,36 @@ package github import ( "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccGithubEnterpriseDataSource(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - config := fmt.Sprintf(` data "github_enterprise" "test" { slug = "%s" } `, - testEnterprise, + testAccConf.enterpriseSlug, ) check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.github_enterprise.test", "slug", testEnterprise), + resource.TestCheckResourceAttr("data.github_enterprise.test", "slug", testAccConf.enterpriseSlug), resource.TestCheckResourceAttrSet("data.github_enterprise.test", "name"), resource.TestCheckResourceAttrSet("data.github_enterprise.test", "created_at"), resource.TestCheckResourceAttrSet("data.github_enterprise.test", "url"), ) - resource.Test( - t, - resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, enterprise) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, }, + }, ) } diff --git a/github/data_source_github_ip_ranges_test.go b/github/data_source_github_ip_ranges_test.go index f527f59005..abffbcd3d5 100644 --- a/github/data_source_github_ip_ranges_test.go +++ b/github/data_source_github_ip_ranges_test.go @@ -7,9 +7,7 @@ import ( ) func TestAccGithubIpRangesDataSource(t *testing.T) { - t.Run("reads IP ranges without error", func(t *testing.T) { - config := `data "github_ip_ranges" "test" {}` check := resource.ComposeTestCheckFunc( @@ -42,30 +40,14 @@ func TestAccGithubIpRangesDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_ip_ranges.test", "dependabot_ipv6.#"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/data_source_github_issue_labels_test.go b/github/data_source_github_issue_labels_test.go index 172c47ba2a..a5eae0b89c 100644 --- a/github/data_source_github_issue_labels_test.go +++ b/github/data_source_github_issue_labels_test.go @@ -29,29 +29,15 @@ func TestAccGithubIssueLabelsDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/data_source_github_membership_test.go b/github/data_source_github_membership_test.go index 148250259b..4077d3fcd6 100644 --- a/github/data_source_github_membership_test.go +++ b/github/data_source_github_membership_test.go @@ -9,84 +9,54 @@ import ( ) func TestAccGithubMembershipDataSource(t *testing.T) { + if len(testAccConf.testOrgUser) == 0 { + t.Skip("No org user provided") + } t.Run("queries the membership for a user in a specified organization", func(t *testing.T) { - config := fmt.Sprintf(` data "github_membership" "test" { username = "%s" organization = "%s" } - `, testOwner, testOrganization) + `, testAccConf.testOrgUser, testAccConf.owner) check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.github_membership.test", "username", testOwner), + resource.TestCheckResourceAttr("data.github_membership.test", "username", testAccConf.testOrgUser), resource.TestCheckResourceAttrSet("data.github_membership.test", "role"), resource.TestCheckResourceAttrSet("data.github_membership.test", "etag"), resource.TestCheckResourceAttrSet("data.github_membership.test", "state"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("errors when querying with non-existent user", func(t *testing.T) { - config := fmt.Sprintf(` data "github_membership" "test" { - username = "%s" + username = "!%s" organization = "%s" } - `, "!"+testOwner, testOrganization) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile(`Not Found`), - }, + `, testAccConf.testOrgUser, testAccConf.owner) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile(`Not Found`), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - } diff --git a/github/data_source_github_organization_custom_role_test.go b/github/data_source_github_organization_custom_role_test.go index 1fa91d6f83..e496057aa6 100644 --- a/github/data_source_github_organization_custom_role_test.go +++ b/github/data_source_github_organization_custom_role_test.go @@ -9,7 +9,6 @@ import ( ) func TestAccGithubOrganizationCustomRoleDataSource(t *testing.T) { - t.Run("queries a custom repo role", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -53,33 +52,19 @@ func TestAccGithubOrganizationCustomRoleDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_organization_external_identities_test.go b/github/data_source_github_organization_external_identities_test.go index 7b328befc0..823734a134 100644 --- a/github/data_source_github_organization_external_identities_test.go +++ b/github/data_source_github_organization_external_identities_test.go @@ -7,10 +7,6 @@ import ( ) func TestAccGithubOrganizationExternalIdentities(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - t.Run("queries without error", func(t *testing.T) { config := `data "github_organization_external_identities" "test" {}` @@ -20,28 +16,15 @@ func TestAccGithubOrganizationExternalIdentities(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_organization_external_identities.test", "identities.0.saml_identity.name_id"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/data_source_github_organization_ip_allow_list_test.go b/github/data_source_github_organization_ip_allow_list_test.go index 06e68015ce..2647ec6ea5 100644 --- a/github/data_source_github_organization_ip_allow_list_test.go +++ b/github/data_source_github_organization_ip_allow_list_test.go @@ -7,47 +7,25 @@ import ( ) func TestAccGithubOrganizationIpAllowListDataSource(t *testing.T) { - t.Run("queries without error", func(t *testing.T) { - config := ` + data "github_organization_ip_allow_list" "all" {} ` check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.id"), - resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.name"), - resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.allow_list_value"), - resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.is_active"), - resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.created_at"), - resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.0.updated_at"), + resource.TestCheckResourceAttrSet("data.github_organization_ip_allow_list.all", "ip_allow_list.#"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) - } diff --git a/github/data_source_github_organization_team_sync_groups_test.go b/github/data_source_github_organization_team_sync_groups_test.go index bd465a4d7b..2c27319a81 100644 --- a/github/data_source_github_organization_team_sync_groups_test.go +++ b/github/data_source_github_organization_team_sync_groups_test.go @@ -7,18 +7,12 @@ import ( ) func TestAccGithubOrganizationTeamSyncGroupsDataSource_existing(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { - testAccPreCheck(t) - }, - Providers: testAccProviders, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, Steps: []resource.TestStep{ { - Config: testAccCheckGithubOrganizationTeamSyncGroupsDataSourceConfig(), + Config: `data "github_organization_team_sync_groups" "test" {}`, Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.github_organization_team_sync_groups.test", "groups.#"), resource.TestCheckResourceAttrSet("data.github_organization_team_sync_groups.test", "groups.0.group_id"), @@ -28,7 +22,3 @@ func TestAccGithubOrganizationTeamSyncGroupsDataSource_existing(t *testing.T) { }, }) } - -func testAccCheckGithubOrganizationTeamSyncGroupsDataSourceConfig() string { - return `data "github_organization_team_sync_groups" "test" {}` -} diff --git a/github/data_source_github_organization_teams.go b/github/data_source_github_organization_teams.go index 9754dbd040..c4c10890cc 100644 --- a/github/data_source_github_organization_teams.go +++ b/github/data_source_github_organization_teams.go @@ -1,6 +1,10 @@ package github import ( + "context" + "strconv" + + "github.com/google/go-github/v66/github" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "github.com/shurcooL/githubv4" @@ -67,9 +71,18 @@ func dataSourceGithubOrganizationTeams() *schema.Resource { Elem: &schema.Schema{Type: schema.TypeString}, }, "parent": { - Type: schema.TypeMap, + Deprecated: "Use parent_team_id and parent_team_slug instead.", + Type: schema.TypeMap, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "parent_team_id": { + Type: schema.TypeString, + Computed: true, + }, + "parent_team_slug": { + Type: schema.TypeString, Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, }, }, }, @@ -84,6 +97,7 @@ func dataSourceGithubOrganizationTeamsRead(d *schema.ResourceData, meta interfac return err } + clientv3 := meta.(*Owner).v3client client := meta.(*Owner).v4client orgName := meta.(*Owner).name rootTeamsOnly := d.Get("root_teams_only").(bool) @@ -107,7 +121,10 @@ func dataSourceGithubOrganizationTeamsRead(d *schema.ResourceData, meta interfac return err } - additionalTeams := flattenGitHubTeams(query) + additionalTeams, err := flattenGitHubTeams(clientv3, meta.(*Owner).StopContext, orgName, query) + if err != nil { + return err + } teams = append(teams, additionalTeams...) if !query.Organization.Teams.PageInfo.HasNextPage { @@ -125,11 +142,11 @@ func dataSourceGithubOrganizationTeamsRead(d *schema.ResourceData, meta interfac return nil } -func flattenGitHubTeams(tq TeamsQuery) []interface{} { +func flattenGitHubTeams(client *github.Client, ctx context.Context, org string, tq TeamsQuery) ([]interface{}, error) { teams := tq.Organization.Teams.Nodes if len(teams) == 0 { - return make([]interface{}, 0) + return make([]interface{}, 0), nil } flatTeams := make([]interface{}, len(teams)) @@ -152,6 +169,18 @@ func flattenGitHubTeams(tq TeamsQuery) []interface{} { t["members"] = flatMembers + var parentTeamId string + if len(team.Parent.Slug) != 0 { + parentTeam, _, err := client.Teams.GetTeamBySlug(ctx, org, string(team.Parent.Slug)) + if err != nil { + return nil, err + } + parentTeamId = strconv.FormatInt(parentTeam.GetID(), 10) + } + + t["parent_team_id"] = parentTeamId + t["parent_team_slug"] = team.Parent.Slug + parentTeam := make(map[string]interface{}) parentTeam["id"] = team.Parent.ID parentTeam["slug"] = team.Parent.Slug @@ -171,5 +200,5 @@ func flattenGitHubTeams(tq TeamsQuery) []interface{} { flatTeams[i] = t } - return flatTeams + return flatTeams, nil } diff --git a/github/data_source_github_organization_teams_test.go b/github/data_source_github_organization_teams_test.go index a4ebdc5534..9878587651 100644 --- a/github/data_source_github_organization_teams_test.go +++ b/github/data_source_github_organization_teams_test.go @@ -1,172 +1,229 @@ package github import ( + "fmt" "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccGithubOrganizationTeamsDataSource(t *testing.T) { - - t.Run("queries without error", func(t *testing.T) { - + t.Run("queries no org teams without error", func(t *testing.T) { config := ` data "github_organization_teams" "all" {} ` check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.id"), - resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.node_id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.#"), + resource.TestCheckResourceAttr("data.github_organization_teams.all", "teams.#", "0"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) - t.Run("queries root teams only without error", func(t *testing.T) { - - config := ` - data "github_organization_teams" "root_teams" { - root_teams_only = true + t.Run("queries without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + teamName0 := fmt.Sprintf("tf-acc-test-0-%s", randomID) + teamName1 := fmt.Sprintf("tf-acc-test-1-%s", randomID) + teamName2 := fmt.Sprintf("tf-acc-test-2-%s", randomID) + + config0 := fmt.Sprintf(` + resource "github_team" "test_0" { + name = "%s" } - ` - - check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet("data.github_organization_teams.root_teams", "teams.0.id"), - resource.TestCheckResourceAttrSet("data.github_organization_teams.root_teams", "teams.0.node_id"), - resource.TestCheckResourceAttr("data.github_organization_teams.root_teams", "teams.0.parent.id", ""), - ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - }, - }) - } + resource "github_team" "test_1" { + name = "%s" + privacy = "closed" + } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) + resource "github_team" "test_2" { + name = "%s" + privacy = "closed" + parent_team_id = github_team.test_1.id + } - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) + data "github_organization_teams" "all" { + depends_on = [github_team.test_0, github_team.test_1, github_team.test_2] + } + `, teamName0, teamName1, teamName2) - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) + config1 := fmt.Sprintf(` + resource "github_team" "test_0" { + name = "%s" + } - }) + resource "github_team" "test_1" { + name = "%s" + privacy = "closed" + } - t.Run("queries summary only without error", func(t *testing.T) { + resource "github_team" "test_2" { + name = "%s" + privacy = "closed" + parent_team_id = github_team.test_1.id + } - config := ` data "github_organization_teams" "all" { - summary_only = true + root_teams_only = true + + depends_on = [github_team.test_0, github_team.test_1, github_team.test_2] } - ` + `, teamName0, teamName1, teamName2) - check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.id"), - resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.node_id"), - resource.TestCheckNoResourceAttr("data.github_organization_teams.all", "teams.0.members.0"), - resource.TestCheckNoResourceAttr("data.github_organization_teams.all", "teams.0.repositories.0"), - ) + config2 := fmt.Sprintf(` + resource "github_team" "test_0" { + name = "%s" + } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - }, - }) - } + resource "github_team" "test_1" { + name = "%s" + privacy = "closed" + } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) + resource "github_team" "test_2" { + name = "%s" + privacy = "closed" + parent_team_id = github_team.test_1.id + } - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) + data "github_organization_teams" "all" { + summary_only = true - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + depends_on = [github_team.test_0, github_team.test_1, github_team.test_2] + } + `, teamName0, teamName1, teamName2) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config0, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.#"), + resource.TestCheckResourceAttr("data.github_organization_teams.all", "teams.#", "3"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.node_id"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.slug", "github_team.test_0", "slug"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.name", "github_team.test_0", "name"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.description", "github_team.test_0", "description"), + // resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.privacy", "github_team.test_0", "privacy"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.parent_team_id", "github_team.test_0", "parent_team_id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.members.#"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.repositories.#"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.1.id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.1.node_id"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.slug", "github_team.test_1", "slug"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.name", "github_team.test_1", "name"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.description", "github_team.test_1", "description"), + // resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.privacy", "github_team.test_1", "privacy"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.parent_team_id", "github_team.test_1", "parent_team_id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.1.members.#"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.1.repositories.#"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.2.id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.2.node_id"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.2.slug", "github_team.test_2", "slug"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.2.name", "github_team.test_2", "name"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.2.description", "github_team.test_2", "description"), + // resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.2.privacy", "github_team.test_2", "privacy"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.2.parent_team_id", "github_team.test_2", "parent_team_id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.2.members.#"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.2.repositories.#"), + ), + }, + { + Config: config1, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.#"), + resource.TestCheckResourceAttr("data.github_organization_teams.all", "teams.#", "2"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.node_id"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.slug", "github_team.test_0", "slug"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.name", "github_team.test_0", "name"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.description", "github_team.test_0", "description"), + // resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.privacy", "github_team.test_0", "privacy"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.parent_team_id", "github_team.test_0", "parent_team_id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.members.#"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.repositories.#"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.1.id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.1.node_id"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.slug", "github_team.test_1", "slug"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.name", "github_team.test_1", "name"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.description", "github_team.test_1", "description"), + // resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.privacy", "github_team.test_1", "privacy"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.parent_team_id", "github_team.test_1", "parent_team_id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.1.members.#"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.1.repositories.#"), + ), + }, + { + Config: config2, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.#"), + resource.TestCheckResourceAttr("data.github_organization_teams.all", "teams.#", "3"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.node_id"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.slug", "github_team.test_0", "slug"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.name", "github_team.test_0", "name"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.description", "github_team.test_0", "description"), + // resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.privacy", "github_team.test_0", "privacy"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.0.parent_team_id", "github_team.test_0", "parent_team_id"), + resource.TestCheckNoResourceAttr("data.github_organization_teams.all", "teams.0.members.#"), + resource.TestCheckNoResourceAttr("data.github_organization_teams.all", "teams.0.repositories.#"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.1.id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.1.node_id"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.slug", "github_team.test_1", "slug"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.name", "github_team.test_1", "name"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.description", "github_team.test_1", "description"), + // resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.privacy", "github_team.test_1", "privacy"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.1.parent_team_id", "github_team.test_1", "parent_team_id"), + resource.TestCheckNoResourceAttr("data.github_organization_teams.all", "teams.1.members.#"), + resource.TestCheckNoResourceAttr("data.github_organization_teams.all", "teams.1.repositories.#"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.2.id"), + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.2.node_id"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.2.slug", "github_team.test_2", "slug"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.2.name", "github_team.test_2", "name"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.2.description", "github_team.test_2", "description"), + // resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.2.privacy", "github_team.test_2", "privacy"), + resource.TestCheckResourceAttrPair("data.github_organization_teams.all", "teams.2.parent_team_id", "github_team.test_2", "parent_team_id"), + resource.TestCheckNoResourceAttr("data.github_organization_teams.all", "teams.2.members.#"), + resource.TestCheckNoResourceAttr("data.github_organization_teams.all", "teams.2.repositories.#"), + ), + }, + }, }) - }) t.Run("queries results_per_page only without error", func(t *testing.T) { - config := ` - data "github_organization_teams" "all" { - results_per_page = 50 - } + data "github_organization_teams" "all" { + results_per_page = 50 + } ` - check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.id"), - resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.0.node_id"), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_organization_teams.all", "teams.#"), + resource.TestCheckResourceAttr("data.github_organization_teams.all", "teams.#", "0"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - } diff --git a/github/data_source_github_organization_test.go b/github/data_source_github_organization_test.go index d0be7a002c..72a4ae59c5 100644 --- a/github/data_source_github_organization_test.go +++ b/github/data_source_github_organization_test.go @@ -9,21 +9,17 @@ import ( ) func TestAccGithubOrganizationDataSource(t *testing.T) { - t.Run("queries for an organization without error", func(t *testing.T) { - config := fmt.Sprintf(` data "github_organization" "test" { name = "%s" } - `, testOrganization) + `, testAccConf.owner) check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.github_organization.test", "login", testOrganization), - resource.TestCheckResourceAttrSet("data.github_organization.test", "name"), - resource.TestCheckResourceAttrSet("data.github_organization.test", "orgname"), + resource.TestCheckResourceAttr("data.github_organization.test", "login", testAccConf.owner), + resource.TestCheckResourceAttr("data.github_organization.test", "orgname", testAccConf.owner), resource.TestCheckResourceAttrSet("data.github_organization.test", "node_id"), - resource.TestCheckResourceAttrSet("data.github_organization.test", "description"), resource.TestCheckResourceAttrSet("data.github_organization.test", "plan"), resource.TestCheckResourceAttrSet("data.github_organization.test", "repositories.#"), resource.TestCheckResourceAttrSet("data.github_organization.test", "members.#"), @@ -47,31 +43,16 @@ func TestAccGithubOrganizationDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_organization.test", "secret_scanning_push_protection_enabled_for_new_repositories"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("queries for an organization with archived repos", func(t *testing.T) { @@ -97,62 +78,44 @@ func TestAccGithubOrganizationDataSource(t *testing.T) { github_repository.archived, ] } - + output "should_be_false" { value = contains(data.github_organization.skip_archived.repositories, github_repository.archived.full_name) } output "should_be_true" { value = contains(data.github_organization.all_repos.repositories, github_repository.archived.full_name) } - `, randomID, testOrganization, testOrganization) + `, randomID, testAccConf.owner, testAccConf.owner) check := resource.ComposeTestCheckFunc( resource.TestCheckOutput("should_be_false", "false"), resource.TestCheckOutput("should_be_true", "true"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("queries for an organization summary_only without error", func(t *testing.T) { - config := fmt.Sprintf(` data "github_organization" "test" { name = "%s" summary_only = true } - `, testOrganization) + `, testAccConf.owner) check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("data.github_organization.test", "login", testOrganization), - resource.TestCheckResourceAttrSet("data.github_organization.test", "name"), - resource.TestCheckResourceAttrSet("data.github_organization.test", "orgname"), + resource.TestCheckResourceAttr("data.github_organization.test", "login", testAccConf.owner), + resource.TestCheckResourceAttr("data.github_organization.test", "orgname", testAccConf.owner), resource.TestCheckResourceAttrSet("data.github_organization.test", "node_id"), - resource.TestCheckResourceAttrSet("data.github_organization.test", "description"), resource.TestCheckResourceAttrSet("data.github_organization.test", "plan"), resource.TestCheckNoResourceAttr("data.github_organization.test", "repositories.#"), resource.TestCheckNoResourceAttr("data.github_organization.test", "members.#"), @@ -176,30 +139,15 @@ func TestAccGithubOrganizationDataSource(t *testing.T) { resource.TestCheckNoResourceAttr("data.github_organization.test", "secret_scanning_push_protection_enabled_for_new_repositories"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/data_source_github_organization_webhooks_test.go b/github/data_source_github_organization_webhooks_test.go index 6343ee0f61..91b40f99af 100644 --- a/github/data_source_github_organization_webhooks_test.go +++ b/github/data_source_github_organization_webhooks_test.go @@ -9,57 +9,37 @@ import ( func TestAccGithubOrganizationWebhooksDataSource(t *testing.T) { t.Run("manages organization webhooks", func(t *testing.T) { config := ` - resource "github_organization_webhook" "test" { - configuration { - url = "https://google.de/webhook" - content_type = "json" - insecure_ssl = true - } - - events = ["pull_request"] + resource "github_organization_webhook" "test" { + configuration { + url = "https://google.de/webhook" + content_type = "json" + insecure_ssl = true } - ` - - config2 := config + ` - data "github_organization_webhooks" "test" {} - ` - const resourceName = "data.github_organization_webhooks.test" - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "webhooks.#", "1"), - resource.TestCheckResourceAttr(resourceName, "webhooks.0.name", "web"), - resource.TestCheckResourceAttr(resourceName, "webhooks.0.url", "https://google.de/webhook"), - resource.TestCheckResourceAttr(resourceName, "webhooks.0.active", "true"), - resource.TestCheckResourceAttrSet(resourceName, "webhooks.0.id"), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, - }, - }) + events = ["pull_request"] } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) + data "github_organization_webhooks" "test" { + depends_on = [github_organization_webhook.test] + } + ` - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_organization_webhooks.test", "webhooks.#"), + resource.TestCheckResourceAttr("data.github_organization_webhooks.test", "webhooks.#", "1"), + resource.TestCheckResourceAttrSet("data.github_organization_webhooks.test", "webhooks.0.id"), + resource.TestCheckResourceAttr("data.github_organization_webhooks.test", "webhooks.0.name", "web"), + resource.TestCheckResourceAttr("data.github_organization_webhooks.test", "webhooks.0.active", "true"), + resource.TestCheckResourceAttrSet("data.github_organization_webhooks.test", "webhooks.0.url"), + ), + }, + }, }) }) } diff --git a/github/data_source_github_ref_test.go b/github/data_source_github_ref_test.go index 4806da7796..30e1a7ca29 100644 --- a/github/data_source_github_ref_test.go +++ b/github/data_source_github_ref_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -10,10 +9,8 @@ import ( ) func TestAccGithubRefDataSource(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("queries an existing branch ref without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%[1]s" @@ -21,89 +18,57 @@ func TestAccGithubRefDataSource(t *testing.T) { } data "github_ref" "test" { - repository = github_repository.test.id + owner = "%s" + repository = github_repository.test.name ref = "heads/main" } - `, randomID) - - check := resource.ComposeTestCheckFunc( - resource.TestMatchResourceAttr( - "data.github_ref.test", "id", regexp.MustCompile(randomID), - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + `, randomID, testAccConf.owner) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_ref.test", "id"), + resource.TestCheckResourceAttrSet("data.github_ref.test", "sha"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - // TODO: This still fails on missing id attribute - t.Run("queries an invalid ref without error", func(t *testing.T) { - - config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "tf-acc-test-%[1]s" - auto_init = true - } - - data "github_ref" "test" { - repository = github_repository.test.id - ref = "heads/xxxxxx" - } - `, randomID) - - check := resource.ComposeTestCheckFunc( - resource.TestCheckNoResourceAttr( - "data.github_ref.test", "id", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - - }) + // Can't test due to SDK and test framework limitations + // t.Run("queries an invalid ref without error", func(t *testing.T) { + // randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + // config := fmt.Sprintf(` + // resource "github_repository" "test" { + // name = "tf-acc-test-%[1]s" + // auto_init = true + // } + + // data "github_ref" "test" { + // repository = github_repository.test.id + // ref = "heads/xxxxxx" + // } + // `, randomID) + + // check := resource.ComposeTestCheckFunc( + // resource.TestCheckNoResourceAttr( + // "data.github_ref.test", "id", + // ), + // ) + + // resource.Test(t, resource.TestCase{ + // PreCheck: func() { skipUnauthenticated(t) }, + // ProviderFactories: providerFactories, + // Steps: []resource.TestStep{ + // { + // Config: config, + // Check: check, + // }, + // }, + // }) + // }) } diff --git a/github/data_source_github_release_test.go b/github/data_source_github_release_test.go index 3247ae6323..ba9f0f7d43 100644 --- a/github/data_source_github_release_test.go +++ b/github/data_source_github_release_test.go @@ -2,70 +2,47 @@ package github import ( "fmt" - "os" "regexp" + "strconv" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccGithubReleaseDataSource(t *testing.T) { - - testReleaseRepository := os.Getenv("GITHUB_TEMPLATE_REPOSITORY") - testReleaseID := os.Getenv("GITHUB_TEMPLATE_REPOSITORY_RELEASE_ID") - testReleaseOwner := testOrganizationFunc() - t.Run("queries latest release", func(t *testing.T) { - config := fmt.Sprintf(` data "github_release" "test" { repository = "%s" owner = "%s" retrieve_by = "latest" } - `, testReleaseRepository, testReleaseOwner) + `, testAccConf.testPublicRepository, testAccConf.testPublicRepositoryOwner) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( - "data.github_release.test", "id", testReleaseID, + "data.github_release.test", "id", strconv.Itoa(testAccConf.testPublicReleaseId), ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("queries release by ID or tag", func(t *testing.T) { - config := fmt.Sprintf(` data "github_release" "by_id" { repository = "%[1]s" owner = "%[2]s" retrieve_by = "id" - release_id = "%[3]s" + release_id = "%[3]d" } data "github_release" "by_tag" { @@ -74,151 +51,84 @@ func TestAccGithubReleaseDataSource(t *testing.T) { retrieve_by = "tag" release_tag = data.github_release.by_id.release_tag } - `, testReleaseRepository, testReleaseOwner, testReleaseID) + `, testAccConf.testPublicRepository, testAccConf.testPublicRepositoryOwner, testAccConf.testPublicReleaseId) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( - "data.github_release.by_id", "id", testReleaseID, + "data.github_release.by_id", "id", strconv.Itoa(testAccConf.testPublicReleaseId), ), resource.TestCheckResourceAttr( - "data.github_release.by_tag", "id", testReleaseID, + "data.github_release.by_tag", "id", strconv.Itoa(testAccConf.testPublicReleaseId), ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("errors when querying with non-existent ID", func(t *testing.T) { - - config := ` + config := fmt.Sprintf(` data "github_release" "test" { - repository = "test" - owner = "test" + repository = "%s" + owner = "%s" retrieve_by = "id" } - ` - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile("`release_id` must be set when `retrieve_by` = `id`"), - }, + `, testAccConf.testPublicRepository, testAccConf.testPublicRepositoryOwner) + + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile("`release_id` must be set when `retrieve_by` = `id`"), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - t.Run("errors when querying with non-existent repository", func(t *testing.T) { - - config := ` + t.Run("errors when querying with non-existent tag", func(t *testing.T) { + config := fmt.Sprintf(` data "github_release" "test" { - repository = "test" - owner = "test" - retrieve_by = "latest" + repository = "%s" + owner = "%s" + retrieve_by = "tag" } - ` - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile(`Not Found`), - }, + `, testAccConf.testPublicRepository, testAccConf.testPublicRepositoryOwner) + + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile("`release_tag` must be set when `retrieve_by` = `tag`"), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - t.Run("errors when querying with non-existent tag", func(t *testing.T) { - + t.Run("errors when querying with non-existent repository", func(t *testing.T) { config := ` data "github_release" "test" { repository = "test" owner = "test" - retrieve_by = "tag" + retrieve_by = "latest" } ` - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile("`release_tag` must be set when `retrieve_by` = `tag`"), - }, + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile(`Not Found`), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - } diff --git a/github/data_source_github_repositories_test.go b/github/data_source_github_repositories_test.go index 139c985284..cf3afcab0f 100644 --- a/github/data_source_github_repositories_test.go +++ b/github/data_source_github_repositories_test.go @@ -9,22 +9,20 @@ import ( ) func TestAccGithubRepositoriesDataSource(t *testing.T) { - // FIXME: Find a way to reduce amount of `GET /search/repositories` // t.Skip("Skipping due to API rate limits exceeding") t.Run("queries a list of repositories without error", func(t *testing.T) { - config := fmt.Sprintf(` data "github_repositories" "test" { query = "org:%s" } - `, testOrganization) + `, testAccConf.owner) check := resource.ComposeTestCheckFunc( resource.TestMatchResourceAttr( "data.github_repositories.test", "full_names.0", - regexp.MustCompile(`^`+testOrganization), + regexp.MustCompile(`^`+testAccConf.owner), ), resource.TestCheckResourceAttrSet( "data.github_repositories.test", "names.0", @@ -38,47 +36,31 @@ func TestAccGithubRepositoriesDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("queries a list of repositories with repo_ids and results_per_page without error", func(t *testing.T) { - config := fmt.Sprintf(` data "github_repositories" "test" { query = "org:%s" include_repo_id = true results_per_page = 20 } - `, testOrganization) + `, testAccConf.owner) check := resource.ComposeTestCheckFunc( resource.TestMatchResourceAttr( "data.github_repositories.test", "full_names.0", - regexp.MustCompile(`^`+testOrganization), + regexp.MustCompile(`^`+testAccConf.owner), ), resource.TestCheckResourceAttrSet( "data.github_repositories.test", "names.0", @@ -92,35 +74,19 @@ func TestAccGithubRepositoriesDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("returns an empty list given an invalid query", func(t *testing.T) { - // FIXME: Find a way to reduce amount of `GET /search/repositories` // t.Skip("Skipping due to API rate limits exceeding") @@ -141,30 +107,15 @@ func TestAccGithubRepositoriesDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/data_source_github_repository_autolink_references_test.go b/github/data_source_github_repository_autolink_references_test.go index e8f524ebc3..f3ecf95b1f 100644 --- a/github/data_source_github_repository_autolink_references_test.go +++ b/github/data_source_github_repository_autolink_references_test.go @@ -9,11 +9,9 @@ import ( ) func TestAccGithubRepositoryAutolinkReferencesDataSource(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("queries autolink references", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%[1]s" @@ -40,33 +38,18 @@ func TestAccGithubRepositoryAutolinkReferencesDataSource(t *testing.T) { resource.TestCheckResourceAttr("data.github_repository_autolink_references.all", "autolink_references.0.is_alphanumeric", "true"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) - }) } diff --git a/github/data_source_github_repository_branches_test.go b/github/data_source_github_repository_branches_test.go index b1d7210b05..23465a357b 100644 --- a/github/data_source_github_repository_branches_test.go +++ b/github/data_source_github_repository_branches_test.go @@ -29,29 +29,15 @@ func TestAccGithubRepositoryBranchesDataSource(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "branches.0.protected", "false"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) @@ -104,33 +90,19 @@ func TestAccGithubRepositoryBranchesDataSource(t *testing.T) { resource.TestCheckResourceAttr(nonProtectedResourceName, "branches.0.protected", "false"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_repository_deploy_keys_test.go b/github/data_source_github_repository_deploy_keys_test.go index 8babf7e631..e5dc51711a 100644 --- a/github/data_source_github_repository_deploy_keys_test.go +++ b/github/data_source_github_repository_deploy_keys_test.go @@ -3,6 +3,7 @@ package github import ( "fmt" "path/filepath" + "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -11,63 +12,44 @@ import ( func TestAccGithubRepositoryDeployKeysDataSource(t *testing.T) { t.Run("manages deploy keys", func(t *testing.T) { - keyPath := filepath.Join("test-fixtures", "id_rsa.pub") + keyPath := strings.ReplaceAll(filepath.Join("test-fixtures", "id_rsa.pub"), "\\", "/") repoName := fmt.Sprintf("tf-acc-test-deploy-keys-%s", acctest.RandString(5)) config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "%s" - auto_init = true - } - - resource "github_repository_deploy_key" "test" { - repository = github_repository.test.name - key = "${file("%s")}" - title = "title1" - } - `, repoName, keyPath) - - config2 := config + ` - data "github_repository_deploy_keys" "test" { - repository = github_repository.test.name - } - ` - - const resourceName = "data.github_repository_deploy_keys.test" - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "keys.#", "1"), - resource.TestCheckResourceAttr(resourceName, "keys.0.title", "title1"), - resource.TestCheckResourceAttrSet(resourceName, "keys.0.id"), - resource.TestCheckResourceAttr(resourceName, "keys.0.verified", "true"), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, - }, - }) + resource "github_repository" "test" { + name = "%s" + auto_init = true } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) + resource "github_repository_deploy_key" "test" { + repository = github_repository.test.name + key = file("%s") + title = "title1" + read_only = true + } - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) + data "github_repository_deploy_keys" "test" { + repository = github_repository.test.name - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + depends_on = [github_repository_deploy_key.test] + } + `, repoName, keyPath) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_repository_deploy_keys.test", "keys.#"), + resource.TestCheckResourceAttr("data.github_repository_deploy_keys.test", "keys.#", "1"), + resource.TestCheckResourceAttr("data.github_repository_deploy_keys.test", "keys.0.title", "title1"), + resource.TestCheckResourceAttrSet("data.github_repository_deploy_keys.test", "keys.0.id"), + resource.TestCheckResourceAttr("data.github_repository_deploy_keys.test", "keys.0.verified", "true"), + ), + }, + }, }) }) } diff --git a/github/data_source_github_repository_deployment_branch_policies_test.go b/github/data_source_github_repository_deployment_branch_policies_test.go index bf8ae0936b..983b219801 100644 --- a/github/data_source_github_repository_deployment_branch_policies_test.go +++ b/github/data_source_github_repository_deployment_branch_policies_test.go @@ -9,11 +9,8 @@ import ( ) func TestAccGithubRepositoryDeploymentBranchPolicies(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("queries deployment branch policies", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -48,33 +45,18 @@ func TestAccGithubRepositoryDeploymentBranchPolicies(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_repository_deployment_branch_policies.all", "deployment_branch_policies.0.id"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) - }) } diff --git a/github/data_source_github_repository_environments_test.go b/github/data_source_github_repository_environments_test.go index 55cbb5f16b..e1bc555581 100644 --- a/github/data_source_github_repository_environments_test.go +++ b/github/data_source_github_repository_environments_test.go @@ -9,11 +9,9 @@ import ( ) func TestAccGithubRepositoryEnvironmentsDataSource(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("queries environments", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%[1]s" @@ -36,33 +34,18 @@ func TestAccGithubRepositoryEnvironmentsDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_repository_environments.all", "environments.0.node_id"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) - }) } diff --git a/github/data_source_github_repository_file_test.go b/github/data_source_github_repository_file_test.go index c4b3005a59..d531567b6a 100644 --- a/github/data_source_github_repository_file_test.go +++ b/github/data_source_github_repository_file_test.go @@ -1,32 +1,24 @@ package github import ( - "context" - "encoding/base64" - "encoding/json" "fmt" - "net/http" - "net/url" "testing" - "github.com/google/go-github/v66/github" - "github.com/stretchr/testify/assert" - - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) func TestAccGithubRepositoryFileDataSource(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - - t.Run("create and read a file with a branch name provided", func(t *testing.T) { + t.Run("reads a file with a branch name provided without erroring", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - resource "github_repository" "test" { name = "tf-acc-test-%s" auto_init = true + + lifecycle { + ignore_changes = all + } } resource "github_repository_file" "test" { @@ -46,67 +38,91 @@ func TestAccGithubRepositoryFileDataSource(t *testing.T) { } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "data.github_repository_file.test", "content", - "bar", - ), - resource.TestCheckResourceAttr( - "data.github_repository_file.test", "sha", - "ba0e162e1c47469e3fe4b393a8bf8c569f302116", - ), - resource.TestCheckResourceAttr( - "data.github_repository_file.test", "ref", - "main", - ), - resource.TestCheckResourceAttrSet( - "data.github_repository_file.test", "commit_author", - ), - resource.TestCheckResourceAttrSet( - "data.github_repository_file.test", "commit_email", - ), - resource.TestCheckResourceAttrSet( - "data.github_repository_file.test", "commit_message", - ), - resource.TestCheckResourceAttrSet( - "data.github_repository_file.test", "commit_sha", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "id"), + resource.TestCheckResourceAttr("data.github_repository_file.test", "ref", "main"), + resource.TestCheckResourceAttr("data.github_repository_file.test", "content", "bar"), + resource.TestCheckResourceAttr("data.github_repository_file.test", "sha", "ba0e162e1c47469e3fe4b393a8bf8c569f302116"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_sha"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_message"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_author"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_email"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) + }, }) + }) - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) + t.Run("reads a file from a short repo name without erroring", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repoName := fmt.Sprintf("tf-acc-test-%s", randomID) + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "%s" + auto_init = true - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) + lifecycle { + ignore_changes = all + } + } + resource "github_repository_file" "test" { + repository = github_repository.test.name + branch = "main" + file = "test" + content = "bar" + commit_message = "Managed by Terraform" + commit_author = "Terraform User" + commit_email = "terraform@example.com" + } + + data "github_repository_file" "test" { + repository = "%[1]s" + branch = "main" + file = github_repository_file.test.file + + depends_on = [github_repository_file.test] + } + `, repoName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "id"), + resource.TestCheckResourceAttr("data.github_repository_file.test", "ref", "main"), + resource.TestCheckResourceAttr("data.github_repository_file.test", "content", "bar"), + resource.TestCheckResourceAttr("data.github_repository_file.test", "sha", "ba0e162e1c47469e3fe4b393a8bf8c569f302116"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_sha"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_message"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_author"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_email"), + ), + }, + }, + }) }) t.Run("create and read a file without providing a branch name", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" auto_init = true + + lifecycle { + ignore_changes = all + } } resource "github_branch" "test" { @@ -135,374 +151,105 @@ func TestAccGithubRepositoryFileDataSource(t *testing.T) { } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "data.github_repository_file.test", "content", - "bar", - ), - resource.TestCheckResourceAttr( - "data.github_repository_file.test", "sha", - "ba0e162e1c47469e3fe4b393a8bf8c569f302116", - ), - resource.TestCheckResourceAttr( - "data.github_repository_file.test", "ref", - "test", - ), - resource.TestCheckResourceAttrSet( - "data.github_repository_file.test", "commit_author", - ), - resource.TestCheckResourceAttrSet( - "data.github_repository_file.test", "commit_email", - ), - resource.TestCheckResourceAttrSet( - "data.github_repository_file.test", "commit_message", - ), - resource.TestCheckResourceAttrSet( - "data.github_repository_file.test", "commit_sha", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "id"), + resource.TestCheckNoResourceAttr("data.github_repository_file.test", "branch"), + resource.TestCheckResourceAttr("data.github_repository_file.test", "ref", "test"), + resource.TestCheckResourceAttr("data.github_repository_file.test", "content", "bar"), + resource.TestCheckResourceAttr("data.github_repository_file.test", "sha", "ba0e162e1c47469e3fe4b393a8bf8c569f302116"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_sha"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_message"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_author"), + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "commit_email"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) -} - -// TODO: This test is failing, needs review. -func TestDataSourceGithubRepositoryFileRead(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - - // helper function to simplify marshalling. - marshal := func(t *testing.T, msg interface{}) string { - data, err := json.MarshalIndent(msg, "", " ") - if err != nil { - t.Fatalf("cant encode to json: %v", err) - } - - return string(data) - } - - sha := "some-test-sha" - committerName := "some-test-user" - committerEmail := "some-test-user@github.com" - commitMessage := "test commit message" - - enc := "base64" - fileContent := "here-goes-content-of-our-glorious-config.json" - b64FileContent := base64.StdEncoding.EncodeToString([]byte(fileContent)) - - fileName := "test-file.json" - branch := "main" - - // setting up some org/owner info - owner := "test-owner" - org := "test-org" - repo := "test-repo" - - apiUrl := fmt.Sprintf("https://api.github.com/repos/%s/%s/contents/%s?ref=%s", owner, repo, fileName, branch) - - // preparing mashalled objects - repoContentRespBody := marshal(t, &github.RepositoryContent{ - Encoding: &enc, - Content: &b64FileContent, - SHA: &sha, - URL: &apiUrl, - }) - repoCommit := &github.RepositoryCommit{ - SHA: &sha, - Committer: &github.User{ - Name: &committerName, - Email: &committerEmail, - }, - Commit: &github.Commit{ - Message: &commitMessage, - }, - Files: []*github.CommitFile{ - { - Filename: &fileName, }, - }, - } - repoCommitRespBody := marshal(t, repoCommit) - listCommitRespBody := marshal(t, []*github.RepositoryCommit{repoCommit}) - - t.Run("extracting org and repo if full_name is passed", func(t *testing.T) { - // test setup - repositoryFullName := fmt.Sprintf("%s/%s", org, repo) - expectedID := fmt.Sprintf("%s/%s", repo, fileName) - expectedRepo := "test-repo" - - ts := githubApiMock([]*mockResponse{ - { - ExpectedUri: fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", org, repo, fileName, branch), - ResponseBody: repoContentRespBody, - StatusCode: http.StatusOK, - }, - { - ExpectedUri: fmt.Sprintf("/repos/%s/%s/commits?path=%s&sha=%s", org, repo, fileName, branch), - ResponseBody: listCommitRespBody, - StatusCode: http.StatusOK, - }, - { - ExpectedUri: fmt.Sprintf("/repos/%s/%s/commits/%s", org, repo, sha), - ResponseBody: repoCommitRespBody, - StatusCode: http.StatusOK, - }, - }) - defer ts.Close() - - httpCl := http.DefaultClient - httpCl.Transport = http.DefaultTransport - - client := github.NewClient(httpCl) - u, _ := url.Parse(ts.URL + "/") - client.BaseURL = u - - meta := &Owner{ - name: owner, - v3client: client, - } - - testSchema := map[string]*schema.Schema{ - "repository": {Type: schema.TypeString}, - "file": {Type: schema.TypeString}, - "branch": {Type: schema.TypeString}, - "commit_sha": {Type: schema.TypeString}, - "commit_email": {Type: schema.TypeString}, - "commit_author": {Type: schema.TypeString}, - "commit_message": {Type: schema.TypeString}, - "content": {Type: schema.TypeString}, - "id": {Type: schema.TypeString}, - } - - schema := schema.TestResourceDataRaw(t, testSchema, map[string]interface{}{ - "repository": repositoryFullName, - "file": fileName, - "branch": branch, - "commit_sha": sha, - "commit_email": committerEmail, - "commit_author": committerName, - "commit_message": commitMessage, - "content": "", - "id": "", }) - - // actual call - diags := dataSourceGithubRepositoryFileRead(context.Background(), schema, meta) - - // assertions - for _, diagnostic := range diags { - assert.Equal(t, diag.Warning, diagnostic.Severity) - } - assert.Equal(t, expectedRepo, schema.Get("repository")) - assert.Equal(t, fileContent, schema.Get("content")) - assert.Equal(t, expectedID, schema.Get("id")) - }) - t.Run("using user as owner if just name is passed", func(t *testing.T) { - // test setup - repositoryFullName := repo - expectedID := fmt.Sprintf("%s/%s", repo, fileName) - expectedRepo := "test-repo" - - ts := githubApiMock([]*mockResponse{ - { - ExpectedUri: fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", owner, repo, fileName, branch), - ResponseBody: repoContentRespBody, - StatusCode: http.StatusOK, - }, - { - ExpectedUri: fmt.Sprintf("/repos/%s/%s/commits?path=%s&sha=%s", owner, repo, fileName, branch), - ResponseBody: listCommitRespBody, - StatusCode: http.StatusOK, - }, - { - ExpectedUri: fmt.Sprintf("/repos/%s/%s/commits/%s", owner, repo, sha), - ResponseBody: repoCommitRespBody, - StatusCode: http.StatusOK, - }, - }) - defer ts.Close() - - httpCl := http.DefaultClient - httpCl.Transport = http.DefaultTransport - - client := github.NewClient(httpCl) - u, _ := url.Parse(ts.URL + "/") - client.BaseURL = u - - meta := &Owner{ - name: owner, - v3client: client, - } - - testSchema := map[string]*schema.Schema{ - "repository": {Type: schema.TypeString}, - "file": {Type: schema.TypeString}, - "branch": {Type: schema.TypeString}, - "commit_sha": {Type: schema.TypeString}, - "commit_email": {Type: schema.TypeString}, - "commit_author": {Type: schema.TypeString}, - "commit_message": {Type: schema.TypeString}, - "content": {Type: schema.TypeString}, - "id": {Type: schema.TypeString}, - } - - schema := schema.TestResourceDataRaw(t, testSchema, map[string]interface{}{ - "repository": repositoryFullName, - "file": fileName, - "branch": branch, - "commit_sha": sha, - "commit_email": committerEmail, - "commit_author": committerName, - "commit_message": commitMessage, - "content": "", - "id": "", - }) - - // actual call - diags := dataSourceGithubRepositoryFileRead(context.Background(), schema, meta) - - // assertions - for _, diagnostic := range diags { - assert.Equal(t, diag.Warning, diagnostic.Severity) - } - assert.Equal(t, expectedRepo, schema.Get("repository")) - assert.Equal(t, fileContent, schema.Get("content")) - assert.Equal(t, expectedID, schema.Get("id")) }) - t.Run("try reading a non-existent file without an error", func(t *testing.T) { - + // Can't test due to SDK and test framework limitations + // t.Run("try reading a non-existent file without an error", func(t *testing.T) { + // randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + // config := fmt.Sprintf(` + // resource "github_repository" "test" { + // name = "tf-acc-test-%s" + // auto_init = true + + // lifecycle { + // ignore_changes = all + // } + // } + + // data "github_repository_file" "test" { + // repository = github_repository.test.name + // file = "test" + // } + // `, randomID) + + // resource.Test(t, resource.TestCase{ + // PreCheck: func() { skipUnauthenticated(t) }, + // ProviderFactories: providerFactories, + // Steps: []resource.TestStep{ + // { + // Config: config, + // Check: resource.ComposeTestCheckFunc( + // resource.TestCheckNoResourceAttr("data.github_repository_file.test", "id"), + // resource.TestCheckNoResourceAttr("data.github_repository_file.test", "branch"), + // resource.TestCheckNoResourceAttr("data.github_repository_file.test", "ref"), + // resource.TestCheckNoResourceAttr("data.github_repository_file.test", "content"), + // resource.TestCheckNoResourceAttr("data.github_repository_file.test", "sha"), + // resource.TestCheckNoResourceAttr("data.github_repository_file.test", "commit_sha"), + // resource.TestCheckNoResourceAttr("data.github_repository_file.test", "commit_message"), + // resource.TestCheckNoResourceAttr("data.github_repository_file.test", "commit_author"), + // resource.TestCheckNoResourceAttr("data.github_repository_file.test", "commit_email"), + // ), + // }, + // }, + // }) + // }) + + t.Run("reads a directory without erroring", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - resource "github_repository" "test" { name = "tf-acc-test-%s" auto_init = true + + lifecycle { + ignore_changes = all + } } data "github_repository_file" "test" { repository = github_repository.test.name - file = "test" + file = "." } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckNoResourceAttr( - "data.github_repository_file.test", "id", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_repository_file.test", "id"), + resource.TestCheckNoResourceAttr("data.github_repository_file.test", "ref"), + resource.TestCheckNoResourceAttr("data.github_repository_file.test", "content"), + resource.TestCheckNoResourceAttr("data.github_repository_file.test", "sha"), + resource.TestCheckNoResourceAttr("data.github_repository_file.test", "commit_sha"), + resource.TestCheckNoResourceAttr("data.github_repository_file.test", "commit_message"), + resource.TestCheckNoResourceAttr("data.github_repository_file.test", "commit_author"), + resource.TestCheckNoResourceAttr("data.github_repository_file.test", "commit_email"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - - }) - - repoContentDirectoryRespBody := marshal(t, []github.RepositoryContent{ - { - Encoding: &enc, - Content: &b64FileContent, - SHA: &sha, - URL: &apiUrl, - }, - }) - - t.Run("extract only non-file data if the path is for a directory", func(t *testing.T) { - // test setup - repositoryFullName := fmt.Sprintf("%s/%s", org, repo) - - expectedID := fmt.Sprintf("%s/%s", repo, fileName) - expectedRepo := "test-repo" - - ts := githubApiMock([]*mockResponse{ - { - ExpectedUri: fmt.Sprintf("/repos/%s/%s/contents/%s?ref=%s", org, repo, fileName, branch), - ResponseBody: repoContentDirectoryRespBody, - StatusCode: http.StatusOK, }, }) - defer ts.Close() - - httpCl := http.DefaultClient - httpCl.Transport = http.DefaultTransport - - client := github.NewClient(httpCl) - u, _ := url.Parse(ts.URL + "/") - client.BaseURL = u - - meta := &Owner{ - name: owner, - v3client: client, - } - - testSchema := map[string]*schema.Schema{ - "repository": {Type: schema.TypeString}, - "file": {Type: schema.TypeString}, - "branch": {Type: schema.TypeString}, - "commit_sha": {Type: schema.TypeString}, - "content": {Type: schema.TypeString}, - "id": {Type: schema.TypeString}, - } - - schema := schema.TestResourceDataRaw(t, testSchema, map[string]interface{}{ - "repository": repositoryFullName, - "file": fileName, - "branch": branch, - "commit_sha": sha, - }) - - // actual call - diags := dataSourceGithubRepositoryFileRead(context.Background(), schema, meta) - - // assertions - for _, diagnostic := range diags { - assert.Equal(t, diagnostic.Severity, diag.Warning) - } - assert.Equal(t, expectedRepo, schema.Get("repository")) - assert.Equal(t, expectedID, schema.Get("id")) - assert.Equal(t, "", schema.Get("content")) - assert.Equal(t, nil, schema.Get("sha")) }) } diff --git a/github/data_source_github_repository_milestone_test.go b/github/data_source_github_repository_milestone_test.go index 702772145e..562f6b8333 100644 --- a/github/data_source_github_repository_milestone_test.go +++ b/github/data_source_github_repository_milestone_test.go @@ -10,11 +10,8 @@ import ( ) func TestAccGithubRepositoryMilestoneDataSource(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("queries a repository milestone", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -45,30 +42,15 @@ func TestAccGithubRepositoryMilestoneDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/data_source_github_repository_pull_request_test.go b/github/data_source_github_repository_pull_request_test.go index 14057e0197..2568e15a84 100644 --- a/github/data_source_github_repository_pull_request_test.go +++ b/github/data_source_github_repository_pull_request_test.go @@ -68,29 +68,15 @@ func TestAccGithubRepositoryPullRequestDataSource(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/data_source_github_repository_pull_requests_test.go b/github/data_source_github_repository_pull_requests_test.go index 5403820b49..69430b17ad 100644 --- a/github/data_source_github_repository_pull_requests_test.go +++ b/github/data_source_github_repository_pull_requests_test.go @@ -78,29 +78,15 @@ func TestAccGithubRepositoryPullRequestsDataSource(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "results.0.updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/data_source_github_repository_teams_test.go b/github/data_source_github_repository_teams_test.go index ca9fa16a4f..f7f1525325 100644 --- a/github/data_source_github_repository_teams_test.go +++ b/github/data_source_github_repository_teams_test.go @@ -9,7 +9,6 @@ import ( ) func TestAccGithubRepositoryTeamsDataSource(t *testing.T) { - t.Run("queries teams of an existing repository", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -43,25 +42,19 @@ func TestAccGithubRepositoryTeamsDataSource(t *testing.T) { resource.TestCheckResourceAttr("data.github_repository_teams.test", "teams.0.permission", "push"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(), }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) }) } diff --git a/github/data_source_github_repository_test.go b/github/data_source_github_repository_test.go index 7c362ebf91..b853f3d912 100644 --- a/github/data_source_github_repository_test.go +++ b/github/data_source_github_repository_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -10,49 +9,82 @@ import ( ) func TestAccGithubRepositoryDataSource(t *testing.T) { + t.Run("queries a public repository without error", func(t *testing.T) { + config := fmt.Sprintf(` + data "github_repository" "test" { + full_name = "%s/%s" + } + `, testAccConf.testPublicRepositoryOwner, testAccConf.testPublicRepository) - t.Run("anonymously queries a repository without error", func(t *testing.T) { + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "data.github_repository.test", "full_name", + fmt.Sprintf("%s/%s", testAccConf.testPublicRepositoryOwner, testAccConf.testPublicRepository)), + ) + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + }) + + t.Run("queries repository belonging to the current user without error", func(t *testing.T) { + if len(testAccConf.testUserRepository) == 0 { + t.Skip("No test user repository provided") + } config := fmt.Sprintf(` - data "github_repositories" "test" { - query = "org:%s" + data "github_repository" "test" { + full_name = "%s/%s" } + `, testAccConf.username, testAccConf.testUserRepository) + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "data.github_repository.test", "full_name", + fmt.Sprintf("%s/%s", testAccConf.username, testAccConf.testUserRepository)), + ) + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) + }) + + t.Run("queries an org repository without error", func(t *testing.T) { + config := fmt.Sprintf(` data "github_repository" "test" { - full_name = data.github_repositories.test.full_names.0 + full_name = "%s/%s" } - `, testOrganization) + `, testAccConf.owner, testAccConf.testOrgRepository) check := resource.ComposeTestCheckFunc( - resource.TestMatchResourceAttr( - "data.github_repositories.test", "full_names.0", - regexp.MustCompile(`^`+testOrganization)), - resource.TestMatchResourceAttr( + resource.TestCheckResourceAttr( "data.github_repository.test", "full_name", - regexp.MustCompile(`^`+testOrganization)), + fmt.Sprintf("%s/%s", testAccConf.owner, testAccConf.testOrgRepository)), ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) + }, }) - }) t.Run("queries a repository with pages configured", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` @@ -78,113 +110,61 @@ func TestAccGithubRepositoryDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("checks defaults on a new repository", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repoName := fmt.Sprintf("tf-acc-%s", randomID) config := fmt.Sprintf(` resource "github_repository" "test" { - name = "tf-acc-%s" + name = "%s" auto_init = true } data "github_repository" "test" { name = github_repository.test.name } - `, randomID) + `, repoName) check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "data.github_repository.test", "name", "tf-acc-"+randomID, - ), - resource.TestCheckResourceAttrSet( - "data.github_repository.test", "has_projects", - ), - resource.TestCheckResourceAttr( - "data.github_repository.test", "description", "", - ), - resource.TestCheckResourceAttr( - "data.github_repository.test", "homepage_url", "", - ), - resource.TestCheckResourceAttr( - "data.github_repository.test", "pages.#", "0", - ), - resource.TestCheckResourceAttr( - "data.github_repository.test", "fork", "false", - ), - resource.TestCheckResourceAttr( - "data.github_repository.test", "allow_update_branch", "true", - ), - resource.TestCheckResourceAttr( - "data.github_repository.test", "delete_branch_on_merge", "true", - ), + resource.TestCheckResourceAttr("data.github_repository.test", "name", repoName), + resource.TestCheckResourceAttrSet("data.github_repository.test", "has_projects"), + resource.TestCheckResourceAttr("data.github_repository.test", "description", ""), + resource.TestCheckResourceAttr("data.github_repository.test", "homepage_url", ""), + resource.TestCheckResourceAttr("data.github_repository.test", "pages.#", "0"), + resource.TestCheckResourceAttr("data.github_repository.test", "fork", "false"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - t.Run("queries a repository that is a template", func(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - + t.Run("queries a public repository that is a template", func(t *testing.T) { config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "tf-acc-%s" - is_template = true - } - data "github_repository" "test" { - name = github_repository.test.name + full_name = "%s/%s" } - `, randomID) + `, testAccConf.testPublicTemplateRepositoryOwner, testAccConf.testPublicTemplateRepository) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -193,50 +173,64 @@ func TestAccGithubRepositoryDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) + }, + }) + }) + + t.Run("queries an org repository that is a template", func(t *testing.T) { + if len(testAccConf.testOrgTemplateRepository) == 0 { + t.Skip("No org template repository provided") } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) + config := fmt.Sprintf(` + data "github_repository" "test" { + full_name = "%s/%s" + } + `, testAccConf.owner, testAccConf.testOrgTemplateRepository) - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "data.github_repository.test", "is_template", + "true", + ), + ) - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, }) - }) t.Run("queries a repository that was generated from a template", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-%s" template { - owner = "template-repository" - repository = "template-repository" + owner = "%s" + repository = "%s" } } data "github_repository" "test" { name = github_repository.test.name } - `, randomID) + `, randomID, testAccConf.testPublicTemplateRepositoryOwner, testAccConf.testPublicTemplateRepository) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -249,35 +243,19 @@ func TestAccGithubRepositoryDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("queries a repository that has no primary_language", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` @@ -297,35 +275,19 @@ func TestAccGithubRepositoryDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("queries a repository that has go as primary_language", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` @@ -351,40 +313,24 @@ func TestAccGithubRepositoryDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - // Not doing any checks since the language doesnt have time to be updated on the first apply - Config: config, - }, - { - // Re-running the terraform will refresh the language since the go-file has been created - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + // Not doing any checks since the language doesnt have time to be updated on the first apply + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + // Re-running the terraform will refresh the language since the go-file has been created + Config: config, + Check: check, + }, + }, }) - }) t.Run("queries a repository that has a license", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` @@ -419,30 +365,15 @@ EOT ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/data_source_github_repository_webhooks_test.go b/github/data_source_github_repository_webhooks_test.go index 55c9f563e7..e14a687f40 100644 --- a/github/data_source_github_repository_webhooks_test.go +++ b/github/data_source_github_repository_webhooks_test.go @@ -13,66 +13,46 @@ func TestAccGithubRepositoryWebhooksDataSource(t *testing.T) { repoName := fmt.Sprintf("tf-acc-test-webhooks-%s", acctest.RandString(5)) config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "%s" - auto_init = true - } - - resource "github_repository_webhook" "test" { - repository = github_repository.test.name - - configuration { - url = "https://google.de/webhook" - content_type = "json" - insecure_ssl = true - } + resource "github_repository" "test" { + name = "%s" + auto_init = true + } - events = ["pull_request"] - } - `, repoName) + resource "github_repository_webhook" "test" { + repository = github_repository.test.name - config2 := config + ` - data "github_repository_webhooks" "test" { - repository = github_repository.test.name + configuration { + url = "https://google.de/webhook" + content_type = "json" + insecure_ssl = true } - ` - - const resourceName = "data.github_repository_webhooks.test" - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(resourceName, "webhooks.#", "1"), - resource.TestCheckResourceAttr(resourceName, "webhooks.0.name", "web"), - resource.TestCheckResourceAttr(resourceName, "webhooks.0.url", "https://google.de/webhook"), - resource.TestCheckResourceAttr(resourceName, "webhooks.0.active", "true"), - resource.TestCheckResourceAttrSet(resourceName, "webhooks.0.id"), - ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, - }, - }) + events = ["pull_request"] } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) + data "github_repository_webhooks" "test" { + repository = github_repository.test.name - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + depends_on = [github_repository_webhook.test] + } + `, repoName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("data.github_repository_webhooks.test", "webhooks.#"), + resource.TestCheckResourceAttr("data.github_repository_webhooks.test", "webhooks.#", "1"), + resource.TestCheckResourceAttrSet("data.github_repository_webhooks.test", "webhooks.0.id"), + resource.TestCheckResourceAttr("data.github_repository_webhooks.test", "webhooks.0.name", "web"), + resource.TestCheckResourceAttr("data.github_repository_webhooks.test", "webhooks.0.active", "true"), + resource.TestCheckResourceAttrSet("data.github_repository_webhooks.test", "webhooks.0.url"), + ), + }, + }, }) }) } diff --git a/github/data_source_github_rest_api_test.go b/github/data_source_github_rest_api_test.go index 8805921484..52dad7971f 100644 --- a/github/data_source_github_rest_api_test.go +++ b/github/data_source_github_rest_api_test.go @@ -11,11 +11,9 @@ import ( ) func TestAccGithubRestApiDataSource(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("queries an existing branch without error", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%[1]s" @@ -38,35 +36,19 @@ func TestAccGithubRestApiDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_rest_api.test", "headers"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("queries a collection without error", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%[1]s" @@ -82,35 +64,19 @@ func TestAccGithubRestApiDataSource(t *testing.T) { resource.TestMatchResourceAttr("data.github_rest_api.test", "body", regexp.MustCompile(`\[.*refs/heads/.*\]`)), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("queries an invalid branch without error", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%[1]s" @@ -133,67 +99,36 @@ func TestAccGithubRestApiDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_rest_api.test", "headers"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("fails for invalid endpoint", func(t *testing.T) { - // 4096 characters is the maximum length for a URL - var endpoint = strings.Repeat("x", 4096) + endpoint := strings.Repeat("x", 4096) config := fmt.Sprintf(` data "github_rest_api" "test" { endpoint = "/%v" } `, endpoint) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile("Error: GET https://api.github.com/xx.*: 414"), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile("Error: GET https://api.github.com/xx.*: 414"), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/data_source_github_ssh_keys_test.go b/github/data_source_github_ssh_keys_test.go index 369b74f79d..372045dea4 100644 --- a/github/data_source_github_ssh_keys_test.go +++ b/github/data_source_github_ssh_keys_test.go @@ -7,39 +7,21 @@ import ( ) func TestAccGithubSshKeysDataSource(t *testing.T) { - t.Run("reads SSH keys without error", func(t *testing.T) { - config := `data "github_ssh_keys" "test" {}` check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("data.github_ssh_keys.test", "keys.#"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - testCase(t, anonymous) - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/data_source_github_team.go b/github/data_source_github_team.go index ea1ca66c39..d83ff1a860 100644 --- a/github/data_source_github_team.go +++ b/github/data_source_github_team.go @@ -43,9 +43,10 @@ func dataSourceGithubTeam() *schema.Resource { Elem: &schema.Schema{Type: schema.TypeString}, }, "repositories": { - Type: schema.TypeList, - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, + Deprecated: "Use repositories_detailed instead.", + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, }, "repositories_detailed": { Type: schema.TypeList, @@ -56,6 +57,10 @@ func dataSourceGithubTeam() *schema.Resource { Type: schema.TypeInt, Computed: true, }, + "repo_name": { + Type: schema.TypeString, + Computed: true, + }, "role_name": { Type: schema.TypeString, Computed: true, @@ -168,7 +173,7 @@ func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error { } } - repositories_detailed = make([]interface{}, 0, resultsPerPage) //removed this from the loop + repositories_detailed = make([]interface{}, 0, resultsPerPage) // removed this from the loop for { repository, resp, err := client.Teams.ListTeamReposByID(ctx, orgId, team.GetID(), &options.ListOptions) @@ -180,6 +185,7 @@ func dataSourceGithubTeamRead(d *schema.ResourceData, meta interface{}) error { repositories = append(repositories, v.GetName()) repositories_detailed = append(repositories_detailed, map[string]interface{}{ "repo_id": v.GetID(), + "repo_name": v.GetName(), "role_name": v.GetRoleName(), }) } diff --git a/github/data_source_github_team_repository_test.go b/github/data_source_github_team_repository_test.go deleted file mode 100644 index afdc8e0fed..0000000000 --- a/github/data_source_github_team_repository_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package github - -import ( - "fmt" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" -) - -func TestAccGithubTeamRepositories(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - - t.Run("Get Repositories By Teams", func(t *testing.T) { - - config := fmt.Sprintf(` - - resource "github_repository" "test" { - name = "tf-acc-test-%s" - auto_init = true - } - - resource "github_team" "test" { - name = "tf-acc-test-%[1]s" - } - - resource "github_team_repository" "test" { - team_id = "${github_team.test.id}" - repository = "${github_repository.test.name}" - } - - data "github_team" "example" { - depends_on = ["github_repository.test", "github_team.test", "github_team_repository.test"] - slug = github_team.test.slug - } - `, randomID) - - check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("data.github_team.example", "repositories.#", "1"), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - ExpectNonEmptyPlan: true, - }, - }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - - }) - -} diff --git a/github/data_source_github_team_test.go b/github/data_source_github_team_test.go index c9a76d8a85..b172a53c91 100644 --- a/github/data_source_github_team_test.go +++ b/github/data_source_github_team_test.go @@ -10,11 +10,8 @@ import ( ) func TestAccGithubTeamDataSource(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("queries an existing team without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-test-%s" @@ -30,35 +27,20 @@ func TestAccGithubTeamDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_team.test", "node_id"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("queries an existing team without error with immediate membership", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-test-%s" @@ -75,69 +57,39 @@ func TestAccGithubTeamDataSource(t *testing.T) { resource.TestCheckResourceAttr("data.github_team.test", "name", fmt.Sprintf("tf-acc-test-%s", randomID)), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("errors when querying a non-existing team", func(t *testing.T) { - config := ` data "github_team" "test" { slug = "" } ` - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile(`Not Found`), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile(`Not Found`), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("queries an existing team without error in summary_only mode", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-test-%s" @@ -156,35 +108,20 @@ func TestAccGithubTeamDataSource(t *testing.T) { resource.TestCheckResourceAttr("data.github_team.test", "repositories.#", "0"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("queries an existing team without error with results_per_page reduced", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-test-%s" @@ -201,35 +138,72 @@ func TestAccGithubTeamDataSource(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_team.test", "node_id"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) + }, + }) + }) + + t.Run("get team with repositories without erroring", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) + resource "github_team" "test" { + name = "tf-acc-test-%[1]s" + } - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) + resource "github_team_repository" "test" { + team_id = github_team.test.id + repository = github_repository.test.name + permission = "admin" + } - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) + data "github_team" "test" { + slug = github_team.test.slug + depends_on = ["github_repository.test", "github_team.test", "github_team_repository.test"] + } + `, randomID) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrPair("data.github_team.test", "id", "github_team.test", "id"), + resource.TestCheckResourceAttrPair("data.github_team.test", "node_id", "github_team.test", "node_id"), + resource.TestCheckResourceAttrPair("data.github_team.test", "slug", "github_team.test", "slug"), + resource.TestCheckResourceAttrPair("data.github_team.test", "name", "github_team.test", "name"), + resource.TestCheckResourceAttrPair("data.github_team.test", "description", "github_team.test", "description"), + resource.TestCheckResourceAttrPair("data.github_team.test", "privacy", "github_team.test", "privacy"), + resource.TestCheckResourceAttrSet("data.github_team.test", "repositories.#"), + resource.TestCheckResourceAttr("data.github_team.test", "repositories.#", "1"), + resource.TestCheckResourceAttrPair("data.github_team.test", "repositories.0", "github_repository.test", "name"), + resource.TestCheckResourceAttrSet("data.github_team.test", "repositories_detailed.#"), + resource.TestCheckResourceAttr("data.github_team.test", "repositories_detailed.#", "1"), + resource.TestCheckResourceAttrPair("data.github_team.test", "repositories_detailed.0.repo_id", "github_repository.test", "repo_id"), + resource.TestCheckResourceAttrPair("data.github_team.test", "repositories_detailed.0.repo_name", "github_repository.test", "name"), + resource.TestCheckResourceAttrPair("data.github_team.test", "repositories_detailed.0.role_name", "github_team_repository.test", "permission"), + ), + }, + }, + }) }) t.Run("queries an existing team with connected repositories", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-test-%s" @@ -255,38 +229,23 @@ func TestAccGithubTeamDataSource(t *testing.T) { resource.TestCheckResourceAttr("github_repository.test", "name", "tf-acc-test"), resource.TestCheckResourceAttr("data.github_team.test", "repositories_detailed.#", "1"), resource.TestCheckResourceAttrPair("data.github_team.test", "repositories_detailed.0.repo_id", "github_repository.test", "repo_id"), + resource.TestCheckResourceAttrPair("data.github_team.test", "repositories_detailed.0.repo_name", "github_repository.test", "name"), resource.TestCheckResourceAttrPair("data.github_team.test", "repositories_detailed.0.role_name", "github_team_repository.test", "permission"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeAggregateTestCheckFunc(), - }, - { - Config: config2, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeAggregateTestCheckFunc(), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check, + }, + }, }) - }) - } diff --git a/github/data_source_github_tree_test.go b/github/data_source_github_tree_test.go index e5b5f78358..25fb86818d 100644 --- a/github/data_source_github_tree_test.go +++ b/github/data_source_github_tree_test.go @@ -9,9 +9,8 @@ import ( ) func TestAccGithubTreeDataSource(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("get tree", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "this" { auto_init = true @@ -44,29 +43,15 @@ func TestAccGithubTreeDataSource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/data_source_github_user_external_identity_test.go b/github/data_source_github_user_external_identity_test.go index 81c53fd9b4..8a48c7cd68 100644 --- a/github/data_source_github_user_external_identity_test.go +++ b/github/data_source_github_user_external_identity_test.go @@ -7,16 +7,8 @@ import ( ) func TestAccGithubUserExternalIdentity(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - t.Run("queries without error", func(t *testing.T) { - config := ` - data "github_user_external_identity" "test" { - - - }` + config := `data "github_user_external_identity" "test" {}` check := resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttrSet("data.github_user_external_identity.test", "login"), @@ -24,28 +16,15 @@ func TestAccGithubUserExternalIdentity(t *testing.T) { resource.TestCheckResourceAttrSet("data.github_user_external_identity.test", "scim_identity.username"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an user accoy", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/data_source_github_user_test.go b/github/data_source_github_user_test.go index 68f767b61a..1c2cf63f62 100644 --- a/github/data_source_github_user_test.go +++ b/github/data_source_github_user_test.go @@ -9,79 +9,50 @@ import ( ) func TestAccGithubUserDataSource(t *testing.T) { + if len(testAccConf.testExternalUser) == 0 { + t.Skip("No external user provided") + } t.Run("queries an existing individual account without error", func(t *testing.T) { - config := fmt.Sprintf(` data "github_user" "test" { username = "%s" } - `, testOwnerFunc()) + `, testAccConf.testExternalUser) check := resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttrSet("data.github_user.test", "login"), resource.TestCheckResourceAttrSet("data.github_user.test", "id"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("errors when querying a non-existing individual account", func(t *testing.T) { - config := fmt.Sprintf(` data "github_user" "test" { username = "!%s" } - `, testOwnerFunc()) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile(`Not Found`), - }, + `, testAccConf.testExternalUser) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile(`Not Found`), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/data_source_github_users_test.go b/github/data_source_github_users_test.go index 704f23c33f..85359b3c99 100644 --- a/github/data_source_github_users_test.go +++ b/github/data_source_github_users_test.go @@ -9,52 +9,38 @@ import ( // TODO: this is failing func TestAccGithubUsersDataSource(t *testing.T) { + if len(testAccConf.testExternalUser) == 0 { + t.Skip("No external user provided") + } t.Run("queries multiple accounts", func(t *testing.T) { - config := fmt.Sprintf(` data "github_users" "test" { usernames = ["%[1]s", "!%[1]s"] } - `, testOwnerFunc()) + `, testAccConf.testExternalUser) check := resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.github_users.test", "logins.#", "1"), - resource.TestCheckResourceAttr("data.github_users.test", "logins.0", testOwnerFunc()), + resource.TestCheckResourceAttr("data.github_users.test", "logins.0", testAccConf.testExternalUser), resource.TestCheckResourceAttr("data.github_users.test", "node_ids.#", "1"), resource.TestCheckResourceAttr("data.github_users.test", "unknown_logins.#", "1"), - resource.TestCheckResourceAttr("data.github_users.test", "unknown_logins.0", fmt.Sprintf("!%s", testOwnerFunc())), + resource.TestCheckResourceAttr("data.github_users.test", "unknown_logins.0", fmt.Sprintf("!%s", testAccConf.testExternalUser)), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("does not fail if called with empty list of usernames", func(t *testing.T) { - config := ` data "github_users" "test" { usernames = [] @@ -67,30 +53,15 @@ func TestAccGithubUsersDataSource(t *testing.T) { resource.TestCheckResourceAttr("data.github_users.test", "unknown_logins.#", "0"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/provider.go b/github/provider.go index a9a04d0474..d025700095 100644 --- a/github/provider.go +++ b/github/provider.go @@ -323,7 +323,7 @@ func providerConfigure(p *schema.Provider) schema.ConfigureContextFunc { // an explicitly set value in a provider block), but is necessary // for backwards compatibility. We could remove this backwards compatibility // code in a future major release. - env, _ := OwnerOrOrgEnvDefaultFunc() + env, _ := ownerOrOrgEnvDefaultFunc() if env.(string) != "" { owner = env.(string) } @@ -490,3 +490,13 @@ func tokenFromGhCli(baseURL string, isGithubDotCom bool) (string, error) { log.Printf("[INFO] Using the token from GitHub CLI") return strings.TrimSpace(string(out)), nil } + +func ownerOrOrgEnvDefaultFunc() (interface{}, error) { + if organization := os.Getenv("GITHUB_ORGANIZATION"); organization != "" { + log.Printf("[INFO] Selecting owner %s from GITHUB_ORGANIZATION environment variable", organization) + return organization, nil + } + owner := os.Getenv("GITHUB_OWNER") + log.Printf("[INFO] Selecting owner %s from GITHUB_OWNER environment variable", owner) + return owner, nil +} diff --git a/github/provider_test.go b/github/provider_test.go index ef925a901a..23276fd649 100644 --- a/github/provider_test.go +++ b/github/provider_test.go @@ -8,34 +8,11 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) -var testAccProviders map[string]*schema.Provider -var testAccProviderFactories func(providers *[]*schema.Provider) map[string]func() (*schema.Provider, error) -var testAccProvider *schema.Provider - -func init() { - testAccProvider = Provider() - testAccProviders = map[string]*schema.Provider{ - "github": testAccProvider, - } - testAccProviderFactories = func(providers *[]*schema.Provider) map[string]func() (*schema.Provider, error) { - return map[string]func() (*schema.Provider, error){ - "github": func() (*schema.Provider, error) { - p := Provider() - *providers = append(*providers, p) - return p, nil - }, - } - } -} - func TestProvider(t *testing.T) { - t.Run("runs internal validation without error", func(t *testing.T) { - if err := Provider().InternalValidate(); err != nil { t.Fatalf("err: %s", err) } - }) t.Run("has an implementation", func(t *testing.T) { @@ -46,113 +23,101 @@ func TestProvider(t *testing.T) { var _ schema.Provider = *Provider() }) - } -// TODO: this is failing func TestAccProviderConfigure(t *testing.T) { - t.Run("can be configured to run anonymously", func(t *testing.T) { - config := ` - provider "github" {} + provider "github" { + token = "" + } + data "github_ip_ranges" "test" {} ` resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, anonymous) }, - Providers: testAccProviders, + ProviderFactories: providerFactories, Steps: []resource.TestStep{ { Config: config, + PlanOnly: true, ExpectNonEmptyPlan: false, }, }, }) - }) t.Run("can be configured to run insecurely", func(t *testing.T) { - - config := fmt.Sprintf(` - provider "github" { - token = "%s" - insecure = true - }`, - testToken, - ) + config := ` + provider "github" { + token = "" + insecure = true + } + data "github_ip_ranges" "test" {} + ` resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, anonymous) }, - Providers: testAccProviders, + ProviderFactories: providerFactories, Steps: []resource.TestStep{ { Config: config, + PlanOnly: true, ExpectNonEmptyPlan: false, }, }, }) - }) t.Run("can be configured with an individual account", func(t *testing.T) { - config := fmt.Sprintf(` provider "github" { token = "%s" owner = "%s" - }`, - testToken, testOwnerFunc(), - ) + } + `, testAccConf.token, testAccConf.owner) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, individual) }, - Providers: testAccProviders, + PreCheck: func() { skipUnlessMode(t, individual) }, + ProviderFactories: providerFactories, Steps: []resource.TestStep{ { Config: config, + PlanOnly: true, ExpectNonEmptyPlan: false, }, }, }) - }) t.Run("can be configured with an organization account", func(t *testing.T) { - config := fmt.Sprintf(` provider "github" { token = "%s" organization = "%s" - }`, - testToken, testOrganizationFunc(), - ) + }`, testAccConf.token, testAccConf.owner) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, organization) }, - Providers: testAccProviders, + PreCheck: func() { skipUnlessMode(t, organization) }, + ProviderFactories: providerFactories, Steps: []resource.TestStep{ { Config: config, + PlanOnly: true, ExpectNonEmptyPlan: false, }, }, }) - }) t.Run("can be configured with a GHES deployment", func(t *testing.T) { - config := fmt.Sprintf(` provider "github" { token = "%s" base_url = "%s" - }`, - testToken, testBaseURLGHES, - ) + }`, testAccConf.token, testAccConf.owner) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, individual) }, - Providers: testAccProviders, + PreCheck: func() { skipUnlessMode(t, individual) }, + ProviderFactories: providerFactories, Steps: []resource.TestStep{ { Config: config, @@ -160,23 +125,19 @@ func TestAccProviderConfigure(t *testing.T) { }, }, }) - }) t.Run("can be configured with max retries", func(t *testing.T) { - config := fmt.Sprintf(` provider "github" { token = "%s" owner = "%s" max_retries = 3 - }`, - testToken, testOwnerFunc(), - ) + }`, testAccConf.token, testAccConf.owner) resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, individual) }, - Providers: testAccProviders, + PreCheck: func() { skipUnlessMode(t, individual) }, + ProviderFactories: providerFactories, Steps: []resource.TestStep{ { Config: config, @@ -184,7 +145,5 @@ func TestAccProviderConfigure(t *testing.T) { }, }, }) - }) - } diff --git a/github/provider_utils.go b/github/provider_utils.go deleted file mode 100644 index 2e3b44a095..0000000000 --- a/github/provider_utils.go +++ /dev/null @@ -1,136 +0,0 @@ -package github - -import ( - "fmt" - "log" - "os" - "testing" -) - -var testCollaborator = os.Getenv("GITHUB_TEST_COLLABORATOR") -var isEnterprise = os.Getenv("ENTERPRISE_ACCOUNT") -var testEnterprise = os.Getenv("ENTERPRISE_SLUG") -var testOrganization = testOrganizationFunc() -var testOwner = os.Getenv("GITHUB_OWNER") -var testToken = os.Getenv("GITHUB_TOKEN") -var testBaseURLGHES = os.Getenv("GHES_BASE_URL") - -func testAccPreCheck(t *testing.T) { - if v := os.Getenv("GITHUB_TOKEN"); v == "" { - t.Fatal("GITHUB_TOKEN must be set for acceptance tests") - } - if v := os.Getenv("GITHUB_ORGANIZATION"); v == "" && os.Getenv("GITHUB_OWNER") == "" { - t.Fatal("GITHUB_ORGANIZATION or GITHUB_OWNER must be set for acceptance tests") - } - if v := os.Getenv("GITHUB_TEST_USER"); v == "" { - t.Fatal("GITHUB_TEST_USER must be set for acceptance tests") - } - if v := os.Getenv("GITHUB_TEST_COLLABORATOR"); v == "" { - t.Fatal("GITHUB_TEST_COLLABORATOR must be set for acceptance tests") - } - if v := os.Getenv("GITHUB_TEMPLATE_REPOSITORY"); v == "" { - t.Fatal("GITHUB_TEMPLATE_REPOSITORY must be set for acceptance tests") - } - if v := os.Getenv("GITHUB_TEMPLATE_REPOSITORY_RELEASE_ID"); v == "" { - t.Fatal("GITHUB_TEMPLATE_REPOSITORY_RELEASE_ID must be set for acceptance tests") - } -} - -func skipUnlessMode(t *testing.T, providerMode string) { - switch providerMode { - case anonymous: - if os.Getenv("GITHUB_BASE_URL") != "" && - os.Getenv("GITHUB_BASE_URL") != "https://api.github.com/" { - t.Log("anonymous mode not supported for GHES deployments") - break - } - - if os.Getenv("GITHUB_TOKEN") == "" { - return - } else { - t.Log("GITHUB_TOKEN environment variable should be empty") - } - case enterprise: - if os.Getenv("GITHUB_TOKEN") == "" { - t.Log("GITHUB_TOKEN environment variable should be set") - } else { - return - } - - case individual: - if os.Getenv("GITHUB_TOKEN") != "" && os.Getenv("GITHUB_OWNER") != "" { - return - } else { - t.Log("GITHUB_TOKEN and GITHUB_OWNER environment variables should be set") - } - case organization: - if os.Getenv("GITHUB_TOKEN") != "" && os.Getenv("GITHUB_ORGANIZATION") != "" { - return - } else { - t.Log("GITHUB_TOKEN and GITHUB_ORGANIZATION environment variables should be set") - } - } - - t.Skipf("Skipping %s which requires %s mode", t.Name(), providerMode) -} - -func testAccCheckOrganization() error { - - baseURL := os.Getenv("GITHUB_BASE_URL") - token := os.Getenv("GITHUB_TOKEN") - - owner := os.Getenv("GITHUB_OWNER") - if owner == "" { - organization := os.Getenv("GITHUB_ORGANIZATION") - if organization == "" { - return fmt.Errorf("neither `GITHUB_OWNER` or `GITHUB_ORGANIZATION` set in environment") - } - owner = organization - } - - config := Config{ - BaseURL: baseURL, - Token: token, - Owner: owner, - } - - meta, err := config.Meta() - if err != nil { - return err - } - if !meta.(*Owner).IsOrganization { - return fmt.Errorf("configured owner %q is a user, not an organization", meta.(*Owner).name) - } - return nil -} - -func OwnerOrOrgEnvDefaultFunc() (interface{}, error) { - if organization := os.Getenv("GITHUB_ORGANIZATION"); organization != "" { - log.Printf("[INFO] Selecting owner %s from GITHUB_ORGANIZATION environment variable", organization) - return organization, nil - } - owner := os.Getenv("GITHUB_OWNER") - log.Printf("[INFO] Selecting owner %s from GITHUB_OWNER environment variable", owner) - return owner, nil -} - -func testOrganizationFunc() string { - organization := os.Getenv("GITHUB_ORGANIZATION") - if organization == "" { - organization = os.Getenv("GITHUB_TEST_ORGANIZATION") - } - return organization -} - -func testOwnerFunc() string { - owner := os.Getenv("GITHUB_OWNER") - if owner == "" { - owner = os.Getenv("GITHUB_TEST_OWNER") - } - return owner -} - -const anonymous = "anonymous" -const individual = "individual" -const organization = "organization" -const enterprise = "enterprise" diff --git a/github/resource_github_actions_environment_secret_test.go b/github/resource_github_actions_environment_secret_test.go index 0f55863370..c6baaa1e6d 100644 --- a/github/resource_github_actions_environment_secret_test.go +++ b/github/resource_github_actions_environment_secret_test.go @@ -12,10 +12,8 @@ import ( ) func TestAccGithubActionsEnvironmentSecret(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates and updates secrets without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value")) updatedSecretValue := base64.StdEncoding.EncodeToString([]byte("updated_super_secret_value")) @@ -79,40 +77,26 @@ func TestAccGithubActionsEnvironmentSecret(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - secretValue, - updatedSecretValue, 2), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + secretValue, + updatedSecretValue, 2), + Check: checks["after"], + }, + }, }) - }) t.Run("deletes secrets without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value")) config := fmt.Sprintf(` @@ -140,31 +124,15 @@ func TestAccGithubActionsEnvironmentSecret(t *testing.T) { } `, randomID, secretValue, secretValue) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - } diff --git a/github/resource_github_actions_environment_variable_test.go b/github/resource_github_actions_environment_variable_test.go index cbab1e20d2..b63684d83c 100644 --- a/github/resource_github_actions_environment_variable_test.go +++ b/github/resource_github_actions_environment_variable_test.go @@ -11,10 +11,8 @@ import ( ) func TestAccGithubActionsEnvironmentVariable(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates and updates environment variables without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) value := "my_variable_value" updatedValue := "my_updated_variable_value" @@ -63,39 +61,26 @@ func TestAccGithubActionsEnvironmentVariable(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - value, - updatedValue, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + value, + updatedValue, 1), + Check: checks["after"], + }, + }, }) }) t.Run("deletes environment variables without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -114,34 +99,20 @@ func TestAccGithubActionsEnvironmentVariable(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("imports environment variables without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) value := "my_variable_value" envName := "environment / test" varName := "test_variable" @@ -164,34 +135,20 @@ func TestAccGithubActionsEnvironmentVariable(t *testing.T) { } `, randomID, envName, varName, value) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: "github_actions_environment_variable.variable", - ImportStateId: fmt.Sprintf(`tf-acc-test-%s:%s:%s`, randomID, envName, varName), - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_actions_environment_variable.variable", + ImportStateId: fmt.Sprintf(`tf-acc-test-%s:%s:%s`, randomID, envName, varName), + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) } diff --git a/github/resource_github_actions_organization_oidc_subject_claim_customization_template_test.go b/github/resource_github_actions_organization_oidc_subject_claim_customization_template_test.go index bbec3e89e0..3fd221fa73 100644 --- a/github/resource_github_actions_organization_oidc_subject_claim_customization_template_test.go +++ b/github/resource_github_actions_organization_oidc_subject_claim_customization_template_test.go @@ -9,7 +9,6 @@ import ( func TestAccGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate(t *testing.T) { t.Run("creates organization oidc subject claim customization template without error", func(t *testing.T) { - config := ` resource "github_actions_organization_oidc_subject_claim_customization_template" "test" { include_claim_keys = ["repo", "context", "job_workflow_ref"] @@ -33,31 +32,19 @@ func TestAccGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate(t *te "include_claim_keys.2", "job_workflow_ref", ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - t.Run("run with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - t.Run("run with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - t.Run("run with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("updates organization oidc subject claim customization template without error", func(t *testing.T) { - resourceTemplate := ` resource "github_actions_organization_oidc_subject_claim_customization_template" "test" { include_claim_keys = %s @@ -113,33 +100,19 @@ func TestAccGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate(t *te ), ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: configs["before"], - Check: checks["before"], - }, - { - Config: configs["after"], - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: configs["before"], + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configs["after"], + Check: checks["after"], + }, + }, }) }) @@ -168,33 +141,20 @@ func TestAccGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate(t *te ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_actions_organization_oidc_subject_claim_customization_template.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_actions_organization_oidc_subject_claim_customization_template.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) } diff --git a/github/resource_github_actions_organization_permissions_test.go b/github/resource_github_actions_organization_permissions_test.go index 97a01f1719..0197a83463 100644 --- a/github/resource_github_actions_organization_permissions_test.go +++ b/github/resource_github_actions_organization_permissions_test.go @@ -9,9 +9,7 @@ import ( ) func TestAccGithubActionsOrganizationPermissions(t *testing.T) { - t.Run("test setting of basic actions organization permissions", func(t *testing.T) { - allowedActions := "local_only" enabledRepositories := "all" @@ -31,26 +29,19 @@ func TestAccGithubActionsOrganizationPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("imports entire set of github action organization permissions without error", func(t *testing.T) { - allowedActions := "selected" enabledRepositories := "selected" githubOwnedAllowed := true @@ -93,31 +84,24 @@ func TestAccGithubActionsOrganizationPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_actions_organization_permissions.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_actions_organization_permissions.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) t.Run("test setting of organization allowed actions", func(t *testing.T) { - allowedActions := "selected" enabledRepositories := "all" githubOwnedAllowed := true @@ -148,26 +132,19 @@ func TestAccGithubActionsOrganizationPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("test not setting of organization allowed actions without error", func(t *testing.T) { - allowedActions := "selected" enabledRepositories := "all" @@ -191,26 +168,19 @@ func TestAccGithubActionsOrganizationPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("test setting of organization enabled repositories", func(t *testing.T) { - allowedActions := "all" enabledRepositories := "selected" githubOwnedAllowed := true @@ -252,22 +222,15 @@ func TestAccGithubActionsOrganizationPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) - } diff --git a/github/resource_github_actions_organization_secret_repositories_test.go b/github/resource_github_actions_organization_secret_repositories_test.go index 2967be19b5..4adf359826 100644 --- a/github/resource_github_actions_organization_secret_repositories_test.go +++ b/github/resource_github_actions_organization_secret_repositories_test.go @@ -10,7 +10,6 @@ import ( ) func TestAccGithubActionsOrganizationSecretRepositories(t *testing.T) { - const ORG_SECRET_NAME = "ORG_SECRET_NAME" randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) secret_name, exists := os.LookupEnv(ORG_SECRET_NAME) @@ -51,29 +50,15 @@ func TestAccGithubActionsOrganizationSecretRepositories(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/resource_github_actions_organization_secret_test.go b/github/resource_github_actions_organization_secret_test.go index fee7aba027..f3e7db959d 100644 --- a/github/resource_github_actions_organization_secret_test.go +++ b/github/resource_github_actions_organization_secret_test.go @@ -63,35 +63,21 @@ func TestAccGithubActionsOrganizationSecret(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - secretValue, - updatedSecretValue, 2), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + secretValue, + updatedSecretValue, 2), + Check: checks["after"], + }, + }, }) }) @@ -108,29 +94,15 @@ func TestAccGithubActionsOrganizationSecret(t *testing.T) { } ` - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) @@ -152,35 +124,21 @@ func TestAccGithubActionsOrganizationSecret(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_actions_organization_secret.test_secret", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"plaintext_value"}, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_actions_organization_secret.test_secret", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"plaintext_value"}, + }, + }, }) }) } diff --git a/github/resource_github_actions_organization_variable_test.go b/github/resource_github_actions_organization_variable_test.go index 5d22d6dae1..408cbe5db4 100644 --- a/github/resource_github_actions_organization_variable_test.go +++ b/github/resource_github_actions_organization_variable_test.go @@ -53,35 +53,21 @@ func TestAccGithubActionsOrganizationVariable(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - value, - updatedValue, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + value, + updatedValue, 1), + Check: checks["after"], + }, + }, }) }) @@ -125,29 +111,15 @@ func TestAccGithubActionsOrganizationVariable(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) @@ -160,31 +132,16 @@ func TestAccGithubActionsOrganizationVariable(t *testing.T) { } ` - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("imports an organization variable without error", func(t *testing.T) { @@ -199,34 +156,20 @@ func TestAccGithubActionsOrganizationVariable(t *testing.T) { } `, varName, value) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: "github_actions_organization_variable.variable", - ImportStateId: varName, - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_actions_organization_variable.variable", + ImportStateId: varName, + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) } diff --git a/github/resource_github_actions_repository_access_level_test.go b/github/resource_github_actions_repository_access_level_test.go index 795dc58aee..884c8c7d50 100644 --- a/github/resource_github_actions_repository_access_level_test.go +++ b/github/resource_github_actions_repository_access_level_test.go @@ -32,29 +32,15 @@ func TestAccGithubActionsRepositoryAccessLevel(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, individual) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - t.Skip("organization account not supported for this input") + }, }) }) @@ -81,29 +67,15 @@ func TestAccGithubActionsRepositoryAccessLevel(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this input") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/resource_github_actions_repository_oidc_subject_claim_customization_template_test.go b/github/resource_github_actions_repository_oidc_subject_claim_customization_template_test.go index ffeb62ba4c..3dc17e14c0 100644 --- a/github/resource_github_actions_repository_oidc_subject_claim_customization_template_test.go +++ b/github/resource_github_actions_repository_oidc_subject_claim_customization_template_test.go @@ -9,11 +9,8 @@ import ( ) func TestAccGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplate(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates repository oidc subject claim customization template without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -47,31 +44,20 @@ func TestAccGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplate(t *test "include_claim_keys.2", "job_workflow_ref", ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - t.Run("run with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - t.Run("run with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - t.Run("run with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("updates repository oidc subject claim customization template without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) configTemplate := ` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -163,41 +149,28 @@ func TestAccGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplate(t *test ), ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: configs["before"], - Check: checks["before"], - }, - { - Config: configs["after"], - Check: checks["after"], - }, - { - Config: configs["reset_to_default"], - Check: checks["reset_to_default"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: configs["before"], + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configs["after"], + Check: checks["after"], + }, + { + Config: configs["reset_to_default"], + Check: checks["reset_to_default"], + }, + }, }) }) t.Run("imports repository oidc subject claim customization template without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -232,33 +205,20 @@ func TestAccGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplate(t *test ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_actions_repository_oidc_subject_claim_customization_template.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_actions_repository_oidc_subject_claim_customization_template.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) } diff --git a/github/resource_github_actions_repository_permissions_test.go b/github/resource_github_actions_repository_permissions_test.go index 7df2eb8f99..9ab8191ca2 100644 --- a/github/resource_github_actions_repository_permissions_test.go +++ b/github/resource_github_actions_repository_permissions_test.go @@ -9,9 +9,7 @@ import ( ) func TestAccGithubActionsRepositoryPermissions(t *testing.T) { - t.Run("test setting of basic actions repository permissions", func(t *testing.T) { - allowedActions := "local_only" randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -34,35 +32,19 @@ func TestAccGithubActionsRepositoryPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("imports entire set of github action repository permissions without error", func(t *testing.T) { - allowedActions := "selected" githubOwnedAllowed := true verifiedAllowed := true @@ -95,40 +77,24 @@ func TestAccGithubActionsRepositoryPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_actions_repository_permissions.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_actions_repository_permissions.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) - }) t.Run("test setting of repository allowed actions", func(t *testing.T) { - allowedActions := "selected" githubOwnedAllowed := true verifiedAllowed := true @@ -161,35 +127,19 @@ func TestAccGithubActionsRepositoryPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("test not setting of repository allowed actions without error", func(t *testing.T) { - allowedActions := "selected" randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -217,35 +167,19 @@ func TestAccGithubActionsRepositoryPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("test disabling actions on a repository", func(t *testing.T) { - actionsEnabled := false randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -268,31 +202,16 @@ func TestAccGithubActionsRepositoryPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) // https://github.com/integrations/terraform-provider-github/issues/2182 @@ -326,29 +245,15 @@ func TestAccGithubActionsRepositoryPermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/resource_github_actions_runner_group_test.go b/github/resource_github_actions_runner_group_test.go index 9eddb970c9..ed58aa2c83 100644 --- a/github/resource_github_actions_runner_group_test.go +++ b/github/resource_github_actions_runner_group_test.go @@ -12,13 +12,8 @@ import ( ) func TestAccGithubActionsRunnerGroup(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates runner groups without error", func(t *testing.T) { - - // t.Skip("requires an enterprise cloud account") - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -49,7 +44,7 @@ func TestAccGithubActionsRunnerGroup(t *testing.T) { resource "github_actions_runner_group" "test" { depends_on = [github_repository_file.workflow_file] - + name = github_repository.test.name visibility = "all" restricted_to_workflows = true @@ -79,7 +74,6 @@ func TestAccGithubActionsRunnerGroup(t *testing.T) { "1", ), func(state *terraform.State) error { - githubRepository := state.RootModule().Resources["github_repository.test"].Primary fullName := githubRepository.Attributes["full_name"] @@ -100,36 +94,20 @@ func TestAccGithubActionsRunnerGroup(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("manages runner visibility", func(t *testing.T) { - - // t.Skip("requires an enterprise cloud account") - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -163,33 +141,20 @@ func TestAccGithubActionsRunnerGroup(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("imports an all runner group without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -208,38 +173,30 @@ func TestAccGithubActionsRunnerGroup(t *testing.T) { resource.TestCheckResourceAttr("github_actions_runner_group.test", "name", fmt.Sprintf(`tf-acc-test-%s`, randomID)), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_actions_runner_group.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_actions_runner_group.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) t.Run("imports a private runner group without error", func(t *testing.T) { + // Note: this test is skipped because when setting visibility 'private', it always fails with: + // Step 0 error: After applying this step, the plan was not empty: + // visibility: "all" => "private" + t.Skip("This is not supported") + + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -257,43 +214,25 @@ func TestAccGithubActionsRunnerGroup(t *testing.T) { resource.TestCheckResourceAttrSet("github_actions_runner_group.test", "visibility"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_actions_runner_group.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - // Note: this test is skipped because when setting visibility 'private', it always fails with: - // Step 0 error: After applying this step, the plan was not empty: - // visibility: "all" => "private" - t.Skip("always shows a diff for visibility 'all' => 'private'") - testCase(t, organization) + { + ResourceName: "github_actions_runner_group.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) t.Run("imports a selected runner group without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -317,30 +256,20 @@ func TestAccGithubActionsRunnerGroup(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_actions_runner_group.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_actions_runner_group.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) } diff --git a/github/resource_github_actions_secret_test.go b/github/resource_github_actions_secret_test.go index 21bf90233a..cde7c0d458 100644 --- a/github/resource_github_actions_secret_test.go +++ b/github/resource_github_actions_secret_test.go @@ -12,11 +12,9 @@ import ( ) func TestAccGithubActionsSecret(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("reads a repository public key without error", func(t *testing.T) { - config := fmt.Sprintf(` resource "github_repository" "test" { @@ -38,31 +36,16 @@ func TestAccGithubActionsSecret(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("creates and updates secrets without error", func(t *testing.T) { @@ -122,35 +105,21 @@ func TestAccGithubActionsSecret(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - secretValue, - updatedSecretValue, 2), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + secretValue, + updatedSecretValue, 2), + Check: checks["after"], + }, + }, }) }) @@ -220,35 +189,21 @@ func TestAccGithubActionsSecret(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - repoName, - updatedRepoName, 2), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + repoName, + updatedRepoName, 2), + Check: checks["after"], + }, + }, }) }) @@ -269,30 +224,15 @@ func TestAccGithubActionsSecret(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/resource_github_actions_variable_test.go b/github/resource_github_actions_variable_test.go index 07aa4164c7..f5d7230b22 100644 --- a/github/resource_github_actions_variable_test.go +++ b/github/resource_github_actions_variable_test.go @@ -11,10 +11,8 @@ import ( ) func TestAccGithubActionsVariable(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates and updates repository variables without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) value := "my_variable_value" updatedValue := "my_updated_variable_value" @@ -57,39 +55,26 @@ func TestAccGithubActionsVariable(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - value, - updatedValue, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + value, + updatedValue, 1), + Check: checks["after"], + }, + }, }) }) t.Run("deletes repository variables without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -102,34 +87,20 @@ func TestAccGithubActionsVariable(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("imports repository variables without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) varName := "test_variable" value := "variable_value" @@ -145,34 +116,20 @@ func TestAccGithubActionsVariable(t *testing.T) { } `, randomID, varName, value) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: "github_actions_variable.variable", - ImportStateId: fmt.Sprintf(`tf-acc-test-%s:%s`, randomID, varName), - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_actions_variable.variable", + ImportStateId: fmt.Sprintf(`tf-acc-test-%s:%s`, randomID, varName), + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) } diff --git a/github/resource_github_app_installation_repositories_test.go b/github/resource_github_app_installation_repositories_test.go index 45bd468f68..4972013bb6 100644 --- a/github/resource_github_app_installation_repositories_test.go +++ b/github/resource_github_app_installation_repositories_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "os" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -10,37 +9,33 @@ import ( ) func TestAccGithubAppInstallationRepositories(t *testing.T) { - - const APP_INSTALLATION_ID = "APP_INSTALLATION_ID" - randomID1 := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - randomID2 := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - installation_id, exists := os.LookupEnv(APP_INSTALLATION_ID) + if testAccConf.testOrgAppInstallationId == 0 { + t.Skip("No org app installation id provided") + } t.Run("installs an app to multiple repositories", func(t *testing.T) { - - if !exists { - t.Skipf("%s environment variable is missing", APP_INSTALLATION_ID) - } + randomId := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repoName0 := fmt.Sprintf("tf-acc-test-0-%s", randomId) + repoName1 := fmt.Sprintf("tf-acc-test-1-%s", randomId) config := fmt.Sprintf(` - - resource "github_repository" "test1" { - name = "tf-acc-test-%s" + resource "github_repository" "test_0" { + name = "%s" auto_init = true } - resource "github_repository" "test2" { + resource "github_repository" "test_1" { name = "tf-acc-test-%s" auto_init = true } resource "github_app_installation_repositories" "test" { # The installation id of the app (in the organization). - installation_id = "%s" - selected_repositories = [github_repository.test1.name, github_repository.test2.name] + installation_id = "%d" + selected_repositories = [github_repository.test_0.name, github_repository.test_1.name] } - `, randomID1, randomID2, installation_id) + `, repoName0, repoName1, testAccConf.testOrgAppInstallationId) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet( @@ -51,31 +46,15 @@ func TestAccGithubAppInstallationRepositories(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - } diff --git a/github/resource_github_app_installation_repository_test.go b/github/resource_github_app_installation_repository_test.go index 1a62ef8b78..db3a0632cf 100644 --- a/github/resource_github_app_installation_repository_test.go +++ b/github/resource_github_app_installation_repository_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "os" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -10,17 +9,12 @@ import ( ) func TestAccGithubAppInstallationRepository(t *testing.T) { - - const APP_INSTALLATION_ID = "APP_INSTALLATION_ID" - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - installation_id, exists := os.LookupEnv(APP_INSTALLATION_ID) + if testAccConf.testOrgAppInstallationId == 0 { + t.Skip("No org app installation id provided") + } t.Run("installs an app to a repository", func(t *testing.T) { - - if !exists { - t.Skipf("%s environment variable is missing", APP_INSTALLATION_ID) - } - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -30,11 +24,11 @@ func TestAccGithubAppInstallationRepository(t *testing.T) { resource "github_app_installation_repository" "test" { # The installation id of the app (in the organization). - installation_id = "%s" + installation_id = "%d" repository = github_repository.test.name } - `, randomID, installation_id) + `, randomID, testAccConf.testOrgAppInstallationId) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet( @@ -45,31 +39,15 @@ func TestAccGithubAppInstallationRepository(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) - } diff --git a/github/resource_github_branch_default_test.go b/github/resource_github_branch_default_test.go index 65c83e1bda..799cc5cf5c 100644 --- a/github/resource_github_branch_default_test.go +++ b/github/resource_github_branch_default_test.go @@ -9,11 +9,8 @@ import ( ) func TestAccGithubBranchDefault(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates and manages branch defaults", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -38,46 +35,31 @@ func TestAccGithubBranchDefault(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("replaces the default_branch of a repository", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" auto_init = true } - + resource "github_branch" "test" { repository = github_repository.test.name branch = "test" } - + resource "github_branch_default" "test"{ repository = github_repository.test.name branch = github_branch.test.branch @@ -92,80 +74,42 @@ func TestAccGithubBranchDefault(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("replaces the default_branch of a repository without creating a branch resource prior to", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" auto_init = true } - + resource "github_branch_default" "test"{ repository = github_repository.test.name branch = "development" rename = true } - `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_branch_default.test", "branch", - "development", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttr("github_branch_default.test", "branch", "development")), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/resource_github_branch_protection_test.go b/github/resource_github_branch_protection_test.go index 2cbe21e777..5c1154a80c 100644 --- a/github/resource_github_branch_protection_test.go +++ b/github/resource_github_branch_protection_test.go @@ -13,7 +13,6 @@ import ( ) func TestAccGithubBranchProtectionV4(t *testing.T) { - t.Run("configures default settings when empty", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -57,49 +56,34 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_branch_protection.test", - ImportState: true, - ImportStateVerify: true, - ImportStateIdFunc: importBranchProtectionByRepoName( - fmt.Sprintf("tf-acc-test-%s", randomID), "main", - ), - }, - { - ResourceName: "github_branch_protection.test", - ImportState: true, - ExpectError: regexp.MustCompile( - `could not find a branch protection rule with the pattern 'no-such-pattern'`, - ), - ImportStateIdFunc: importBranchProtectionByRepoName( - fmt.Sprintf("tf-acc-test-%s", randomID), "no-such-pattern", - ), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_branch_protection.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: importBranchProtectionByRepoName( + fmt.Sprintf("tf-acc-test-%s", randomID), "main", + ), + }, + { + ResourceName: "github_branch_protection.test", + ImportState: true, + ExpectError: regexp.MustCompile( + `could not find a branch protection rule with the pattern 'no-such-pattern'`, + ), + ImportStateIdFunc: importBranchProtectionByRepoName( + fmt.Sprintf("tf-acc-test-%s", randomID), "no-such-pattern", + ), + }, + }, }) - }) t.Run("configures default settings when conversation resolution is true", func(t *testing.T) { @@ -149,49 +133,34 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_branch_protection.test", - ImportState: true, - ImportStateVerify: true, - ImportStateIdFunc: importBranchProtectionByRepoName( - fmt.Sprintf("tf-acc-test-%s", randomID), "main", - ), - }, - { - ResourceName: "github_branch_protection.test", - ImportState: true, - ExpectError: regexp.MustCompile( - `could not find a branch protection rule with the pattern 'no-such-pattern'`, - ), - ImportStateIdFunc: importBranchProtectionByRepoName( - fmt.Sprintf("tf-acc-test-%s", randomID), "no-such-pattern", - ), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_branch_protection.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: importBranchProtectionByRepoName( + fmt.Sprintf("tf-acc-test-%s", randomID), "main", + ), + }, + { + ResourceName: "github_branch_protection.test", + ImportState: true, + ExpectError: regexp.MustCompile( + `could not find a branch protection rule with the pattern 'no-such-pattern'`, + ), + ImportStateIdFunc: importBranchProtectionByRepoName( + fmt.Sprintf("tf-acc-test-%s", randomID), "no-such-pattern", + ), + }, + }, }) - }) t.Run("configures required status checks", func(t *testing.T) { @@ -224,47 +193,32 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_branch_protection.test", - ImportState: true, - ImportStateVerify: true, - ImportStateIdFunc: importBranchProtectionByRepoID( - "github_repository.test", "main"), - }, - { - ResourceName: "github_branch_protection.test", - ImportState: true, - ExpectError: regexp.MustCompile( - `could not find a branch protection rule with the pattern 'no-such-pattern'`, - ), - ImportStateIdFunc: importBranchProtectionByRepoID( - "github_repository.test", "no-such-pattern"), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_branch_protection.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: importBranchProtectionByRepoID( + "github_repository.test", "main"), + }, + { + ResourceName: "github_branch_protection.test", + ImportState: true, + ExpectError: regexp.MustCompile( + `could not find a branch protection rule with the pattern 'no-such-pattern'`, + ), + ImportStateIdFunc: importBranchProtectionByRepoID( + "github_repository.test", "no-such-pattern"), + }, + }, }) - }) t.Run("configures required pull request reviews", func(t *testing.T) { @@ -310,31 +264,16 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("configures branch push restrictions", func(t *testing.T) { @@ -367,31 +306,16 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("configures branch push restrictions with node_id", func(t *testing.T) { @@ -418,7 +342,7 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ] } } - `, randomID, testOwnerFunc()) + `, randomID, testAccConf.username) check := resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr( @@ -432,37 +356,21 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("configures branch push restrictions with username", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - user := fmt.Sprintf("/%s", testOwnerFunc()) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -476,11 +384,11 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { restrict_pushes { push_allowances = [ - "%s", + "/%s", ] } } - `, randomID, user) + `, randomID, testAccConf.username) check := resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr( @@ -494,31 +402,16 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("configures branch push restrictions with blocksCreations false", func(t *testing.T) { @@ -553,31 +446,16 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("configures force pushes and deletions", func(t *testing.T) { @@ -602,7 +480,7 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { allows_force_pushes = true } - `, randomID, testOwnerFunc()) + `, randomID, testAccConf.username) check := resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr( @@ -613,31 +491,16 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("configures non-empty list of force push bypassers", func(t *testing.T) { @@ -665,7 +528,7 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { } - `, randomID, testOwnerFunc()) + `, randomID, testAccConf.username) check := resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr( @@ -673,31 +536,16 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("configures allow force push with a team as bypasser", func(t *testing.T) { @@ -729,7 +577,7 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ] } - `, randomID, randomID, testOrganization) + `, randomID, randomID, testAccConf.owner) check := resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr( @@ -740,24 +588,16 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - // This test only works with an organization account - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("configures empty list of force push bypassers", func(t *testing.T) { @@ -787,37 +627,21 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("configures non-empty list of pull request bypassers", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - user := fmt.Sprintf("/%s", testOwnerFunc()) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -832,13 +656,13 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { required_pull_request_reviews { pull_request_bypassers = [ - "%s", + "/%s", ] } } - `, randomID, user) + `, randomID, testAccConf.username) check := resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr( @@ -846,31 +670,16 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("configures empty list of pull request bypassers", func(t *testing.T) { @@ -902,33 +711,17 @@ func TestAccGithubBranchProtectionV4(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - } func importBranchProtectionByRepoName(repo, pattern string) resource.ImportStateIdFunc { diff --git a/github/resource_github_branch_protection_v3.go b/github/resource_github_branch_protection_v3.go index 9c4942d473..c2111c789f 100644 --- a/github/resource_github_branch_protection_v3.go +++ b/github/resource_github_branch_protection_v3.go @@ -59,6 +59,7 @@ func resourceGithubBranchProtectionV3() *schema.Resource { "contexts": { Type: schema.TypeSet, Optional: true, + Computed: true, Deprecated: "GitHub is deprecating the use of `contexts`. Use a `checks` array instead.", Elem: &schema.Schema{ Type: schema.TypeString, @@ -67,10 +68,12 @@ func resourceGithubBranchProtectionV3() *schema.Resource { "checks": { Type: schema.TypeSet, Optional: true, + Computed: true, Description: "The list of status checks to require in order to merge into this branch. No status checks are required by default. Checks should be strings containing the 'context' and 'app_id' like so 'context:app_id'", Elem: &schema.Schema{ Type: schema.TypeString, }, + ConflictsWith: []string{"required_status_checks.0.contexts"}, }, }, }, diff --git a/github/resource_github_branch_protection_v3_test.go b/github/resource_github_branch_protection_v3_test.go index a585221ce4..4695f718ad 100644 --- a/github/resource_github_branch_protection_v3_test.go +++ b/github/resource_github_branch_protection_v3_test.go @@ -8,14 +8,10 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -func TestAccGithubBranchProtectionV3_defaults(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - +func TestAccGithubBranchProtectionV3(t *testing.T) { t.Run("configures default settings when empty", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - resource "github_repository" "test" { name = "tf-acc-test-%s" auto_init = true @@ -27,66 +23,30 @@ func TestAccGithubBranchProtectionV3_defaults(t *testing.T) { branch = "main" } - - `, randomID) - - check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "branch", "main", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "require_signed_commits", "false", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "require_conversation_resolution", "false", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_status_checks.#", "0", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_pull_request_reviews.#", "0", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "push_restrictions.#", "0", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + `, randomID) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "branch", "main"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "require_signed_commits", "false"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "require_conversation_resolution", "false"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "required_status_checks.#", "0"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "required_pull_request_reviews.#", "0"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "push_restrictions.#", "0"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) -} - -func TestAccGithubBranchProtectionV3_conversation_resolution(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - - t.Run("configures default settings when empty", func(t *testing.T) { + t.Run("configures conversation resolution", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - resource "github_repository" "test" { name = "tf-acc-test-%s" auto_init = true @@ -99,201 +59,108 @@ func TestAccGithubBranchProtectionV3_conversation_resolution(t *testing.T) { require_conversation_resolution = true } - - `, randomID) - - check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "branch", "main", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "require_signed_commits", "false", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "require_conversation_resolution", "true", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_status_checks.#", "0", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_pull_request_reviews.#", "0", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "push_restrictions.#", "0", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + `, randomID) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "branch", "main"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "require_signed_commits", "false"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "require_conversation_resolution", "true"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "required_status_checks.#", "0"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "required_pull_request_reviews.#", "0"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "push_restrictions.#", "0"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) -} - -func TestAccGithubBranchProtectionV3_required_status_checks(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("configures required status checks", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } - resource "github_repository" "test" { - name = "tf-acc-test-%s" - auto_init = true - } - - resource "github_branch_protection_v3" "test" { - - repository = github_repository.test.name - branch = "main" - - required_status_checks { - strict = true - checks = [ - "github/foo", - "github/bar:-1", - "github:foo:baz:1", - ] - } - + resource "github_branch_protection_v3" "test" { + repository = github_repository.test.name + branch = "main" + + required_status_checks { + strict = true + checks = [ + "github/foo", + "github/bar:-1", + "github:foo:baz:1", + ] } + } `, randomID) - check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_status_checks.#", "1", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_status_checks.strict", "true", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_status_checks.checks.#", "3", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_status_checks.checks.0", "github/foo", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_status_checks.checks.1", "github/bar", - ), - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_status_checks.checks.2", "github:foo:baz", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("github_branch_protection_v3.test", "required_status_checks.#"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "required_status_checks.#", "1"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "required_status_checks.0.strict", "true"), + resource.TestCheckResourceAttrSet("github_branch_protection_v3.test", "required_status_checks.0.checks.#"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "required_status_checks.0.checks.#", "3"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) -} - -func TestAccGithubBranchProtectionV3_required_status_contexts(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - - t.Run("configures required status checks", func(t *testing.T) { + t.Run("configures required status checks context", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-%s" + auto_init = true + } - resource "github_repository" "test" { - name = "tf-acc-test-%s" - auto_init = true - } - - resource "github_branch_protection_v3" "test" { - - repository = github_repository.test.name - branch = "main" - - required_status_checks { - strict = true - contexts = ["github/foo"] - } + resource "github_branch_protection_v3" "test" { + repository = github_repository.test.name + branch = "main" + required_status_checks { + strict = true + contexts = ["github/foo"] } - - `, randomID) - - check := resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr( - "github_branch_protection_v3.test", "required_status_checks.#", "1", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - }, - }) } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + `, randomID) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("github_branch_protection_v3.test", "required_status_checks.#"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "required_status_checks.#", "1"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "required_status_checks.0.strict", "true"), + resource.TestCheckResourceAttrSet("github_branch_protection_v3.test", "required_status_checks.0.checks.#"), + resource.TestCheckResourceAttr("github_branch_protection_v3.test", "required_status_checks.0.checks.#", "1"), + ), + }, + }, }) - }) -} -func TestAccGithubBranchProtectionV3_required_pull_request_reviews(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("configures required pull request reviews", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -374,39 +241,20 @@ func TestAccGithubBranchProtectionV3_required_pull_request_reviews(t *testing.T) ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) -} - -func TestAccGithubBranchProtectionV3RequiredPullRequestReviewsBypassAllowances(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("configures required pull request reviews with bypass allowances", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -448,40 +296,20 @@ func TestAccGithubBranchProtectionV3RequiredPullRequestReviewsBypassAllowances(t ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) -} - -func TestAccGithubBranchProtectionV3_branch_push_restrictions(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("configures branch push restrictions", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -516,31 +344,15 @@ func TestAccGithubBranchProtectionV3_branch_push_restrictions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) - } diff --git a/github/resource_github_branch_test.go b/github/resource_github_branch_test.go index b5844a6e6e..c8b5a5bbc3 100644 --- a/github/resource_github_branch_test.go +++ b/github/resource_github_branch_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -10,203 +9,100 @@ import ( ) func TestAccGithubBranch(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates a branch directly", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "tf-acc-test-%[1]s" - auto_init = true - } + resource "github_repository" "test" { + name = "tf-acc-test-%[1]s" + auto_init = true + } - resource "github_branch" "test" { - repository = github_repository.test.id - branch = "test" - } + resource "github_branch" "test" { + repository = github_repository.test.name + branch = "test" + } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestMatchResourceAttr( - "github_branch.test", "id", - regexp.MustCompile(fmt.Sprintf("tf-acc-test-%s:test", randomID)), - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_branch.test", - ImportState: true, - ImportStateId: fmt.Sprintf("tf-acc-test-%s:test", randomID), - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"source_sha"}, - }, - { - ResourceName: "github_branch.test", - ImportState: true, - ImportStateId: fmt.Sprintf("tf-acc-test-%s:nonsense", randomID), - ExpectError: regexp.MustCompile( - "Repository tf-acc-test-[a-z0-9]* does not have a branch named nonsense.", - ), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_branch.test", "id"), + resource.TestCheckResourceAttr("github_branch.test", "ref", "refs/heads/test"), + resource.TestCheckResourceAttrSet("github_branch.test", "sha"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("creates a branch named main directly and a repository with a gitignore_template", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "tf-acc-test-%[1]s" - auto_init = true - gitignore_template = "Python" - } + resource "github_repository" "test" { + name = "tf-acc-test-%[1]s" + auto_init = true + gitignore_template = "Python" + } - resource "github_branch" "test" { - repository = github_repository.test.id - branch = "main" - } + resource "github_branch" "test" { + repository = github_repository.test.id + branch = "main" + } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestMatchResourceAttr( - "github_branch.test", "id", - regexp.MustCompile(fmt.Sprintf("tf-acc-test-%s:test", randomID)), - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_branch.test", - ImportState: true, - ImportStateId: fmt.Sprintf("tf-acc-test-%s:test", randomID), - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"source_sha"}, - }, - { - ResourceName: "github_branch.test", - ImportState: true, - ImportStateId: fmt.Sprintf("tf-acc-test-%s:nonsense", randomID), - ExpectError: regexp.MustCompile( - "Repository tf-acc-test-[a-z0-9]* does not have a branch named nonsense.", - ), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_branch.test", "id"), + resource.TestCheckResourceAttr("github_branch.test", "ref", "refs/heads/main"), + resource.TestCheckResourceAttrSet("github_branch.test", "sha"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("creates a branch from a source branch", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "tf-acc-test-%[1]s" - auto_init = true - } + resource "github_repository" "test" { + name = "tf-acc-test-%[1]s" + auto_init = true + } - resource "github_branch" "source" { - repository = github_repository.test.id - branch = "source" - } + resource "github_branch" "source" { + repository = github_repository.test.id + branch = "source" + } - resource "github_branch" "test" { - repository = github_repository.test.id - source_branch = github_branch.source.branch - branch = "test" - } + resource "github_branch" "test" { + repository = github_repository.test.id + source_branch = github_branch.source.branch + branch = "test" + } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestMatchResourceAttr( - "github_branch.test", "id", - regexp.MustCompile(fmt.Sprintf("tf-acc-test-%s:test", randomID)), - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_branch.test", - ImportState: true, - ImportStateId: fmt.Sprintf("tf-acc-test-%s:test:source", randomID), - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"source_sha"}, - }, - { - ResourceName: "github_branch.test", - ImportState: true, - ImportStateId: fmt.Sprintf("tf-acc-test-%s:nonsense:source", randomID), - ExpectError: regexp.MustCompile( - "Repository tf-acc-test-[a-z0-9]* does not have a branch named nonsense.", - ), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_branch.test", "id"), + resource.TestCheckResourceAttr("github_branch.test", "ref", "refs/heads/test"), + resource.TestCheckResourceAttrPair("github_branch.test", "sha", "github_branch.source", "sha"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - } diff --git a/github/resource_github_codespaces_organization_secret_repositories_test.go b/github/resource_github_codespaces_organization_secret_repositories_test.go index 0c373f922e..653d58e820 100644 --- a/github/resource_github_codespaces_organization_secret_repositories_test.go +++ b/github/resource_github_codespaces_organization_secret_repositories_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "os" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -10,67 +9,46 @@ import ( ) func TestAccGithubCodespacesOrganizationSecretRepositories(t *testing.T) { - const ORG_SECRET_NAME = "ORG_SECRET_NAME" - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - secret_name, exists := os.LookupEnv(ORG_SECRET_NAME) - t.Run("set repository allowlist for an organization secret", func(t *testing.T) { - if !exists { - t.Skipf("%s environment variable is missing", ORG_SECRET_NAME) - } - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` + resource "github_actions_organization_secret" "test" { + secret_name = "TEST" + plaintext_value = "Testing 1..2..3.." + visibility = "all" + } + resource "github_repository" "test_repo_1" { name = "tf-acc-test-%s-1" - visibility = "internal" + visibility = "private" } resource "github_repository" "test_repo_2" { name = "tf-acc-test-%s-2" - visibility = "internal" + visibility = "private" } resource "github_codespaces_organization_secret_repositories" "org_secret_repos" { - secret_name = "%s" + secret_name = github_actions_organization_secret.test.secret_name selected_repository_ids = [ github_repository.test_repo_1.repo_id, github_repository.test_repo_2.repo_id ] } - `, randomID, randomID, secret_name) - - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet( - "github_codespaces_organization_secret_repositories.org_secret_repos", "secret_name", - ), - resource.TestCheckResourceAttr( - "github_codespaces_organization_secret_repositories.org_secret_repos", "selected_repository_ids.#", "2", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + `, randomID, randomID) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_codespaces_organization_secret_repositories.org_secret_repos", "secret_name"), + resource.TestCheckResourceAttr("github_codespaces_organization_secret_repositories.org_secret_repos", "selected_repository_ids.#", "2"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/resource_github_codespaces_organization_secret_test.go b/github/resource_github_codespaces_organization_secret_test.go index 597695dcc1..6c71f046cb 100644 --- a/github/resource_github_codespaces_organization_secret_test.go +++ b/github/resource_github_codespaces_organization_secret_test.go @@ -3,7 +3,6 @@ package github import ( "encoding/base64" "fmt" - "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" @@ -12,125 +11,84 @@ import ( func TestAccGithubCodespacesOrganizationSecret(t *testing.T) { t.Run("creates and updates secrets without error", func(t *testing.T) { secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value")) - updatedSecretValue := base64.StdEncoding.EncodeToString([]byte("updated_super_secret_value")) + secretValueUpdated := base64.StdEncoding.EncodeToString([]byte("updated_super_secret_value")) config := fmt.Sprintf(` resource "github_codespaces_organization_secret" "plaintext_secret" { secret_name = "test_plaintext_secret" - plaintext_value = "%s" + plaintext_value = "%[1]s" visibility = "private" } resource "github_codespaces_organization_secret" "encrypted_secret" { secret_name = "test_encrypted_secret" - encrypted_value = "%s" + encrypted_value = "%[1]s" visibility = "private" } - `, secretValue, secretValue) + `, secretValue) - checks := map[string]resource.TestCheckFunc{ - "before": resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_codespaces_organization_secret.plaintext_secret", "plaintext_value", - secretValue, - ), - resource.TestCheckResourceAttr( - "github_codespaces_organization_secret.encrypted_secret", "encrypted_value", - secretValue, - ), - resource.TestCheckResourceAttrSet( - "github_codespaces_organization_secret.plaintext_secret", "created_at", - ), - resource.TestCheckResourceAttrSet( - "github_codespaces_organization_secret.plaintext_secret", "updated_at", - ), - ), - "after": resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_codespaces_organization_secret.plaintext_secret", "plaintext_value", - updatedSecretValue, - ), - resource.TestCheckResourceAttr( - "github_codespaces_organization_secret.encrypted_secret", "encrypted_value", - updatedSecretValue, - ), - resource.TestCheckResourceAttrSet( - "github_codespaces_organization_secret.plaintext_secret", "created_at", - ), - resource.TestCheckResourceAttrSet( - "github_codespaces_organization_secret.plaintext_secret", "updated_at", - ), - ), - } + configUpdated := fmt.Sprintf(` + resource "github_codespaces_organization_secret" "plaintext_secret" { + secret_name = "test_plaintext_secret" + plaintext_value = "%[1]s" + visibility = "private" + } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - secretValue, - updatedSecretValue, 2), - Check: checks["after"], - }, + resource "github_codespaces_organization_secret" "encrypted_secret" { + secret_name = "test_encrypted_secret" + encrypted_value = "%[1]s" + visibility = "private" + } + `, secretValueUpdated) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_codespaces_organization_secret.plaintext_secret", "plaintext_value", secretValue), + resource.TestCheckResourceAttr("github_codespaces_organization_secret.encrypted_secret", "encrypted_value", secretValue), + resource.TestCheckResourceAttrSet("github_codespaces_organization_secret.plaintext_secret", "created_at"), + resource.TestCheckResourceAttrSet("github_codespaces_organization_secret.plaintext_secret", "updated_at"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configUpdated, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_codespaces_organization_secret.plaintext_secret", "plaintext_value", secretValueUpdated), + resource.TestCheckResourceAttr("github_codespaces_organization_secret.encrypted_secret", "encrypted_value", secretValueUpdated), + resource.TestCheckResourceAttrSet("github_codespaces_organization_secret.plaintext_secret", "created_at"), + resource.TestCheckResourceAttrSet("github_codespaces_organization_secret.plaintext_secret", "updated_at"), + ), + }, + }, }) }) t.Run("deletes secrets without error", func(t *testing.T) { config := ` - resource "github_codespaces_organization_secret" "plaintext_secret" { - secret_name = "test_plaintext_secret" - visibility = "private" - } - - resource "github_codespaces_organization_secret" "encrypted_secret" { - secret_name = "test_encrypted_secret" - visibility = "private" - } - ` - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, - }, - }) + resource "github_codespaces_organization_secret" "plaintext_secret" { + secret_name = "test_plaintext_secret" + visibility = "private" } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + resource "github_codespaces_organization_secret" "encrypted_secret" { + secret_name = "test_encrypted_secret" + visibility = "private" + } + ` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, + }, + }, }) }) @@ -138,49 +96,24 @@ func TestAccGithubCodespacesOrganizationSecret(t *testing.T) { secretValue := "super_secret_value" config := fmt.Sprintf(` - resource "github_codespaces_organization_secret" "test_secret" { - secret_name = "test_plaintext_secret" - plaintext_value = "%s" - visibility = "private" - } + resource "github_codespaces_organization_secret" "test_secret" { + secret_name = "test_plaintext_secret" + plaintext_value = "%s" + visibility = "private" + } `, secretValue) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_codespaces_organization_secret.test_secret", "plaintext_value", - secretValue, - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_codespaces_organization_secret.test_secret", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"plaintext_value"}, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_codespaces_organization_secret.test_secret", "plaintext_value", secretValue), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/resource_github_codespaces_secret_test.go b/github/resource_github_codespaces_secret_test.go index 3ed2708b26..54ae4c2c29 100644 --- a/github/resource_github_codespaces_secret_test.go +++ b/github/resource_github_codespaces_secret_test.go @@ -12,11 +12,8 @@ import ( ) func TestAccGithubCodespacesSecret(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("reads a repository public key without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -38,34 +35,20 @@ func TestAccGithubCodespacesSecret(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("creates and updates secrets without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value")) updatedSecretValue := base64.StdEncoding.EncodeToString([]byte("updated_super_secret_value")) @@ -122,39 +105,26 @@ func TestAccGithubCodespacesSecret(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - secretValue, - updatedSecretValue, 2), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + secretValue, + updatedSecretValue, 2), + Check: checks["after"], + }, + }, }) }) t.Run("creates and updates repository name without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) repoName := fmt.Sprintf("tf-acc-test-%s", randomID) updatedRepoName := fmt.Sprintf("tf-acc-test-%s-updated", randomID) secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value")) @@ -218,39 +188,26 @@ func TestAccGithubCodespacesSecret(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - repoName, - updatedRepoName, 2), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + repoName, + updatedRepoName, 2), + Check: checks["after"], + }, + }, }) }) t.Run("deletes secrets without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -267,30 +224,15 @@ func TestAccGithubCodespacesSecret(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/resource_github_codespaces_user_secret_test.go b/github/resource_github_codespaces_user_secret_test.go index befae37e06..aae9a4b0cc 100644 --- a/github/resource_github_codespaces_user_secret_test.go +++ b/github/resource_github_codespaces_user_secret_test.go @@ -61,31 +61,21 @@ func TestAccGithubCodespacesUserSecret(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - secretValue, - updatedSecretValue, 2), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, individual) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + { + Config: strings.Replace(config, + secretValue, + updatedSecretValue, 2), + Check: checks["after"], + }, + }, }) }) @@ -100,25 +90,15 @@ func TestAccGithubCodespacesUserSecret(t *testing.T) { } ` - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, individual) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) }) @@ -139,31 +119,21 @@ func TestAccGithubCodespacesUserSecret(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_codespaces_user_secret.test_secret", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"plaintext_value"}, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, individual) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + { + ResourceName: "github_codespaces_user_secret.test_secret", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"plaintext_value"}, + }, + }, }) }) } diff --git a/github/resource_github_dependabot_organization_secret_repositories_test.go b/github/resource_github_dependabot_organization_secret_repositories_test.go index 80c2ab09a3..bd62f0fa80 100644 --- a/github/resource_github_dependabot_organization_secret_repositories_test.go +++ b/github/resource_github_dependabot_organization_secret_repositories_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "os" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -10,69 +9,48 @@ import ( ) func TestAccGithubDependabotOrganizationSecretRepositories(t *testing.T) { - const ORG_SECRET_NAME = "ORG_SECRET_NAME" - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - secret_name, exists := os.LookupEnv(ORG_SECRET_NAME) - t.Run("set repository allowlist for an organization secret", func(t *testing.T) { - if !exists { - t.Skipf("%s environment variable is missing", ORG_SECRET_NAME) - } - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - resource "github_repository" "test_repo_1" { - name = "tf-acc-test-%s-1" - visibility = "internal" - vulnerability_alerts = "true" - } - - resource "github_repository" "test_repo_2" { - name = "tf-acc-test-%s-2" - visibility = "internal" - vulnerability_alerts = "true" - } - - resource "github_dependabot_organization_secret_repositories" "org_secret_repos" { - secret_name = "%s" - selected_repository_ids = [ - github_repository.test_repo_1.repo_id, - github_repository.test_repo_2.repo_id - ] - } - `, randomID, randomID, secret_name) - - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet( - "github_dependabot_organization_secret_repositories.org_secret_repos", "secret_name", - ), - resource.TestCheckResourceAttr( - "github_dependabot_organization_secret_repositories.org_secret_repos", "selected_repository_ids.#", "2", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - }, - }) + resource "github_actions_organization_secret" "test" { + secret_name = "TEST" + plaintext_value = "Testing 1..2..3.." + visibility = "all" } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) + resource "github_repository" "test_repo_1" { + name = "tf-acc-test-%s-1" + visibility = "private" + vulnerability_alerts = "true" + } - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) + resource "github_repository" "test_repo_2" { + name = "tf-acc-test-%s-2" + visibility = "private" + vulnerability_alerts = "true" + } - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + resource "github_dependabot_organization_secret_repositories" "org_secret_repos" { + secret_name = github_actions_organization_secret.test.secret_name + selected_repository_ids = [ + github_repository.test_repo_1.repo_id, + github_repository.test_repo_2.repo_id + ] + } + `, randomID, randomID) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_dependabot_organization_secret_repositories.org_secret_repos", "secret_name"), + resource.TestCheckResourceAttr("github_dependabot_organization_secret_repositories.org_secret_repos", "selected_repository_ids.#", "2"), + ), + }, + }, }) }) } diff --git a/github/resource_github_dependabot_organization_secret_test.go b/github/resource_github_dependabot_organization_secret_test.go index 50a3a5a81b..90925c52f1 100644 --- a/github/resource_github_dependabot_organization_secret_test.go +++ b/github/resource_github_dependabot_organization_secret_test.go @@ -63,35 +63,21 @@ func TestAccGithubDependabotOrganizationSecret(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - secretValue, - updatedSecretValue, 2), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + secretValue, + updatedSecretValue, 2), + Check: checks["after"], + }, + }, }) }) @@ -108,29 +94,15 @@ func TestAccGithubDependabotOrganizationSecret(t *testing.T) { } ` - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) @@ -152,35 +124,21 @@ func TestAccGithubDependabotOrganizationSecret(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_dependabot_organization_secret.test_secret", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"plaintext_value"}, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_dependabot_organization_secret.test_secret", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"plaintext_value"}, + }, + }, }) }) } diff --git a/github/resource_github_dependabot_secret_test.go b/github/resource_github_dependabot_secret_test.go index fa6488ebaa..fe5b69c921 100644 --- a/github/resource_github_dependabot_secret_test.go +++ b/github/resource_github_dependabot_secret_test.go @@ -12,11 +12,8 @@ import ( ) func TestAccGithubDependabotSecret(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("reads a repository public key without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -38,34 +35,20 @@ func TestAccGithubDependabotSecret(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("creates and updates secrets without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value")) updatedSecretValue := base64.StdEncoding.EncodeToString([]byte("updated_super_secret_value")) @@ -122,39 +105,26 @@ func TestAccGithubDependabotSecret(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - secretValue, - updatedSecretValue, 2), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + secretValue, + updatedSecretValue, 2), + Check: checks["after"], + }, + }, }) }) t.Run("creates and updates repository name without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) repoName := fmt.Sprintf("tf-acc-test-%s", randomID) updatedRepoName := fmt.Sprintf("tf-acc-test-%s-updated", randomID) secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value")) @@ -220,39 +190,26 @@ func TestAccGithubDependabotSecret(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - repoName, - updatedRepoName, 2), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + repoName, + updatedRepoName, 2), + Check: checks["after"], + }, + }, }) }) t.Run("deletes secrets without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -269,30 +226,15 @@ func TestAccGithubDependabotSecret(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/resource_github_enterprise_actions_permissions_test.go b/github/resource_github_enterprise_actions_permissions_test.go index d04773fa33..1baa99175e 100644 --- a/github/resource_github_enterprise_actions_permissions_test.go +++ b/github/resource_github_enterprise_actions_permissions_test.go @@ -9,9 +9,7 @@ import ( ) func TestAccGithubActionsEnterprisePermissions(t *testing.T) { - t.Run("test setting of basic actions enterprise permissions", func(t *testing.T) { - allowedActions := "local_only" enabledOrganizations := "all" @@ -21,7 +19,7 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { allowed_actions = "%s" enabled_organizations = "%s" } - `, testEnterprise, allowedActions, enabledOrganizations) + `, testAccConf.enterpriseSlug, allowedActions, enabledOrganizations) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -32,32 +30,19 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + }, }) }) t.Run("imports entire set of github action enterprise permissions without error", func(t *testing.T) { - allowedActions := "selected" enabledOrganizations := "selected" githubOwnedAllowed := true @@ -73,7 +58,7 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { data "github_user" "current" { username = "" } - + resource "github_enterprise_organization" "org" { enterprise_slug = "%s" name = "%s" @@ -98,7 +83,7 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { organization_ids = [github_enterprise_organization.org.id] } } - `, testEnterprise, orgName, displayName, desc, testEnterprise, allowedActions, enabledOrganizations, githubOwnedAllowed, verifiedAllowed) + `, testAccConf.enterpriseSlug, orgName, displayName, desc, testAccConf.enterpriseSlug, allowedActions, enabledOrganizations, githubOwnedAllowed, verifiedAllowed) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -115,37 +100,24 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_enterprise_actions_permissions.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + { + ResourceName: "github_enterprise_actions_permissions.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) t.Run("test setting of enterprise allowed actions", func(t *testing.T) { - allowedActions := "selected" enabledOrganizations := "all" githubOwnedAllowed := true @@ -162,7 +134,7 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { verified_allowed = %t } } - `, testEnterprise, allowedActions, enabledOrganizations, githubOwnedAllowed, verifiedAllowed) + `, testAccConf.enterpriseSlug, allowedActions, enabledOrganizations, githubOwnedAllowed, verifiedAllowed) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -176,32 +148,19 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + }, }) }) t.Run("test setting of enterprise enabled organizations", func(t *testing.T) { - allowedActions := "all" enabledOrganizations := "selected" randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) @@ -247,7 +206,7 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { organization_ids = [github_enterprise_organization.org.id, github_enterprise_organization.org2.id] } } - `, testEnterprise, orgName, displayName, desc, testEnterprise, orgName2, displayName2, desc2, testEnterprise, allowedActions, enabledOrganizations) + `, testAccConf.enterpriseSlug, orgName, displayName, desc, testAccConf.enterpriseSlug, orgName2, displayName2, desc2, testAccConf.enterpriseSlug, allowedActions, enabledOrganizations) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -261,28 +220,15 @@ func TestAccGithubActionsEnterprisePermissions(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + }, }) }) - } diff --git a/github/resource_github_enterprise_actions_runner_group_test.go b/github/resource_github_enterprise_actions_runner_group_test.go index 1ffb73e090..3695b870d1 100644 --- a/github/resource_github_enterprise_actions_runner_group_test.go +++ b/github/resource_github_enterprise_actions_runner_group_test.go @@ -10,17 +10,8 @@ import ( ) func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - t.Run("creates enterprise runner groups without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` data "github_enterprise" "enterprise" { slug = "%s" @@ -32,7 +23,7 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { visibility = "all" allows_public_repositories = true } - `, testEnterprise, randomID) + `, testAccConf.enterpriseSlug, randomID) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet( @@ -52,26 +43,20 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - testCase(t, enterprise) + }, }) }) t.Run("manages runner group visibility to selected orgs", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` data "github_enterprise" "enterprise" { slug = "%s" @@ -87,7 +72,7 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { visibility = "selected" selected_organization_ids = [data.github_organization.org.id] } - `, testEnterprise, testOrganization, randomID) + `, testAccConf.enterpriseSlug, testAccConf.owner, randomID) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet( @@ -110,25 +95,20 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - testCase(t, enterprise) + }, }) }) t.Run("imports an all runner group without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` data "github_enterprise" "enterprise" { slug = "%s" @@ -139,7 +119,7 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { name = "tf-acc-test-%s" visibility = "all" } - `, testEnterprise, randomID) + `, testAccConf.enterpriseSlug, randomID) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("github_enterprise_actions_runner_group.test", "name"), @@ -148,32 +128,26 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { resource.TestCheckResourceAttr("github_enterprise_actions_runner_group.test", "name", fmt.Sprintf(`tf-acc-test-%s`, randomID)), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_enterprise_actions_runner_group.test", - ImportState: true, - ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf(`%s/`, testEnterprise), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - testCase(t, enterprise) + { + ResourceName: "github_enterprise_actions_runner_group.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf(`%s/`, testAccConf.enterpriseSlug), + }, + }, }) }) t.Run("imports a runner group with selected orgs without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` data "github_enterprise" "enterprise" { slug = "%s" @@ -189,7 +163,7 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { visibility = "selected" selected_organization_ids = [data.github_organization.org.id] } - `, testEnterprise, testOrganization, randomID) + `, testAccConf.enterpriseSlug, testAccConf.owner, randomID) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttrSet("github_enterprise_actions_runner_group.test", "name"), @@ -202,27 +176,21 @@ func TestAccGithubActionsEnterpriseRunnerGroup(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_enterprise_actions_runner_group.test", - ImportState: true, - ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf(`%s/`, testEnterprise), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - testCase(t, enterprise) + { + ResourceName: "github_enterprise_actions_runner_group.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf(`%s/`, testAccConf.enterpriseSlug), + }, + }, }) }) } diff --git a/github/resource_github_enterprise_organization_test.go b/github/resource_github_enterprise_organization_test.go index 03f78666f8..68497cdd13 100644 --- a/github/resource_github_enterprise_organization_test.go +++ b/github/resource_github_enterprise_organization_test.go @@ -11,7 +11,6 @@ import ( ) func TestAccGithubEnterpriseOrganization(t *testing.T) { - t.Run("creates and updates an enterprise organization without error", func(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) orgName := fmt.Sprintf("tf-acc-test-%s", randomID) @@ -37,7 +36,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { data.github_user.current.login ] } - `, testEnterprise, orgName, desc) + `, testAccConf.enterpriseSlug, orgName, desc) checks := map[string]resource.TestCheckFunc{ "before": resource.ComposeTestCheckFunc( @@ -68,33 +67,21 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - desc, - updatedDesc, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + { + Config: strings.Replace(config, + desc, + updatedDesc, 1), + Check: checks["after"], + }, + }, }) }) @@ -119,34 +106,21 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { data.github_user.current.login ] } - `, testEnterprise, orgName) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + `, testAccConf.enterpriseSlug, orgName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + }, }) }) t.Run("creates and updates org with display name", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) orgName := fmt.Sprintf("tf-acc-test-displayname%s", randomID) @@ -175,7 +149,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { data.github_user.current.login ] } - `, testEnterprise, orgName, displayName, desc) + `, testAccConf.enterpriseSlug, orgName, displayName, desc) checks := map[string]resource.TestCheckFunc{ "before": resource.ComposeTestCheckFunc( @@ -214,42 +188,28 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace( - strings.Replace(config, - displayName, - updatedDisplayName, 1), - desc, - updatedDesc, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + { + Config: strings.Replace( + strings.Replace(config, + displayName, + updatedDisplayName, 1), + desc, + updatedDesc, 1), + Check: checks["after"], + }, + }, }) - }) t.Run("creates org without display name, set and update display name", func(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) orgName := fmt.Sprintf("tf-acc-test-adddisplayname%s", randomID) @@ -277,7 +237,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { data.github_user.current.login ] } - `, testEnterprise, orgName, desc) + `, testAccConf.enterpriseSlug, orgName, desc) configWithDisplayName := fmt.Sprintf(` data "github_enterprise" "enterprise" { @@ -298,7 +258,7 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { data.github_user.current.login ] } - `, testEnterprise, orgName, displayName, desc) + `, testAccConf.enterpriseSlug, orgName, displayName, desc) checks := map[string]resource.TestCheckFunc{ "create": resource.ComposeTestCheckFunc( @@ -359,52 +319,39 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: configWithoutDisplayName, - Check: checks["create"], - }, - { - Config: configWithDisplayName, - Check: checks["set"], - }, - { - Config: strings.Replace(configWithDisplayName, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: configWithoutDisplayName, + Check: checks["create"], + }, + { + Config: configWithDisplayName, + Check: checks["set"], + }, + { + Config: strings.Replace(configWithDisplayName, + displayName, + updatedDisplayName, 1), + Check: checks["updateDisplayName"], + }, + { + Config: strings.Replace( + strings.Replace(configWithDisplayName, displayName, updatedDisplayName, 1), - Check: checks["updateDisplayName"], - }, - { - Config: strings.Replace( - strings.Replace(configWithDisplayName, - displayName, - updatedDisplayName, 1), - desc, - updatedDesc, 1), - Check: checks["updateDesc"], - }, - { - Config: configWithoutDisplayName, - Check: checks["unset"], - }, + desc, + updatedDesc, 1), + Check: checks["updateDesc"], }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + { + Config: configWithoutDisplayName, + Check: checks["unset"], + }, + }, }) - }) t.Run("imports enterprise organization without error", func(t *testing.T) { @@ -428,37 +375,25 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { data.github_user.current.login ] } - `, testEnterprise, orgName) + `, testAccConf.enterpriseSlug, orgName) check := resource.ComposeTestCheckFunc() - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_enterprise_organization.org", - ImportState: true, - ImportStateVerify: true, - ImportStateId: fmt.Sprintf(`%s/%s`, testEnterprise, orgName), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + { + ResourceName: "github_enterprise_organization.org", + ImportState: true, + ImportStateVerify: true, + ImportStateId: fmt.Sprintf(`%s/%s`, testAccConf.enterpriseSlug, orgName), + }, + }, }) }) @@ -484,39 +419,26 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { data.github_user.current.login ] } - `, testEnterprise, orgName) + `, testAccConf.enterpriseSlug, orgName) check := resource.ComposeTestCheckFunc() - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_enterprise_organization.org", - ImportState: true, - ImportStateId: fmt.Sprintf(`%s/%s`, randomID, orgName), - ExpectError: regexp.MustCompile("Could not resolve to a Business with the URL slug of .*"), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + { + ResourceName: "github_enterprise_organization.org", + ImportState: true, + ImportStateId: fmt.Sprintf(`%s/%s`, randomID, orgName), + ExpectError: regexp.MustCompile("Could not resolve to a Business with the URL slug of .*"), + }, + }, }) - }) t.Run("imports enterprise organization invalid organization name", func(t *testing.T) { @@ -541,38 +463,25 @@ func TestAccGithubEnterpriseOrganization(t *testing.T) { data.github_user.current.login ] } - `, testEnterprise, orgName) + `, testAccConf.enterpriseSlug, orgName) check := resource.ComposeTestCheckFunc() - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_enterprise_organization.org", - ImportState: true, - ImportStateId: fmt.Sprintf(`%s/%s`, testEnterprise, randomID), - ExpectError: regexp.MustCompile("Could not resolve to an Organization with the login of .*"), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - testCase(t, enterprise) + { + ResourceName: "github_enterprise_organization.org", + ImportState: true, + ImportStateId: fmt.Sprintf(`%s/%s`, testAccConf.enterpriseSlug, randomID), + ExpectError: regexp.MustCompile("Could not resolve to an Organization with the login of .*"), + }, + }, }) - }) } diff --git a/github/resource_github_issue_label_test.go b/github/resource_github_issue_label_test.go index 0e5e9bc1c0..3e8f85c764 100644 --- a/github/resource_github_issue_label_test.go +++ b/github/resource_github_issue_label_test.go @@ -10,11 +10,8 @@ import ( ) func TestAccGithubIssueLabel(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates and updates labels without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) description := "label_description" updatedDescription := "updated_label_description" @@ -48,36 +45,21 @@ func TestAccGithubIssueLabel(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - description, - updatedDescription, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + description, + updatedDescription, 1), + Check: checks["after"], + }, + }, }) }) - } diff --git a/github/resource_github_issue_labels_test.go b/github/resource_github_issue_labels_test.go index d37efd7395..296a72b585 100644 --- a/github/resource_github_issue_labels_test.go +++ b/github/resource_github_issue_labels_test.go @@ -13,81 +13,67 @@ func TestAccGithubIssueLabels(t *testing.T) { randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) empty := []map[string]interface{}{} - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - // 0. Check if some labels already exist (indicated by non-empty plan) - { - Config: testAccGithubIssueLabelsConfig(randomID, empty), - ExpectNonEmptyPlan: true, - }, - // 1. Check if all the labels are destroyed when the resource is added - { - Config: testAccGithubIssueLabelsConfig(randomID, empty), - Check: resource.TestCheckResourceAttr("github_issue_labels.test", "label.#", "0"), - }, - // 2. Check if a label can be created - { - Config: testAccGithubIssueLabelsConfig(randomID, append(empty, map[string]interface{}{ - "name": "foo", - "color": "000000", - "description": "foo", - })), - Check: resource.TestCheckResourceAttr("github_issue_labels.test", "label.#", "1"), - }, - // 3. Check if a label can be recreated - { - Config: testAccGithubIssueLabelsConfig(randomID, append(empty, map[string]interface{}{ + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + // 0. Check if some labels already exist (indicated by non-empty plan) + { + Config: testAccGithubIssueLabelsConfig(randomID, empty), + ExpectNonEmptyPlan: true, + }, + // 1. Check if all the labels are destroyed when the resource is added + { + Config: testAccGithubIssueLabelsConfig(randomID, empty), + Check: resource.TestCheckResourceAttr("github_issue_labels.test", "label.#", "0"), + }, + // 2. Check if a label can be created + { + Config: testAccGithubIssueLabelsConfig(randomID, append(empty, map[string]interface{}{ + "name": "foo", + "color": "000000", + "description": "foo", + })), + Check: resource.TestCheckResourceAttr("github_issue_labels.test", "label.#", "1"), + }, + // 3. Check if a label can be recreated + { + Config: testAccGithubIssueLabelsConfig(randomID, append(empty, map[string]interface{}{ + "name": "Foo", + "color": "000000", + "description": "foo", + })), + Check: resource.TestCheckResourceAttr("github_issue_labels.test", "label.#", "1"), + }, + // 4. Check if multiple labels can be created + { + Config: testAccGithubIssueLabelsConfig(randomID, append(empty, + map[string]interface{}{ "name": "Foo", "color": "000000", "description": "foo", + }, + map[string]interface{}{ + "name": "bar", + "color": "000000", + "description": "bar", + }, map[string]interface{}{ + "name": "baz", + "color": "000000", + "description": "baz", })), - Check: resource.TestCheckResourceAttr("github_issue_labels.test", "label.#", "1"), - }, - // 4. Check if multiple labels can be created - { - Config: testAccGithubIssueLabelsConfig(randomID, append(empty, - map[string]interface{}{ - "name": "Foo", - "color": "000000", - "description": "foo", - }, - map[string]interface{}{ - "name": "bar", - "color": "000000", - "description": "bar", - }, map[string]interface{}{ - "name": "baz", - "color": "000000", - "description": "baz", - })), - Check: resource.TestCheckResourceAttr("github_issue_labels.test", "label.#", "3"), - }, - // 5. Check if labels can be destroyed - { - Config: testAccGithubIssueLabelsConfig(randomID, nil), - }, - // 6. Check if labels were actually destroyed - { - Config: testAccGithubIssueLabelsConfig(randomID, empty), - Check: resource.TestCheckResourceAttr("github_issue_labels.test", "label.#", "0"), - }, + Check: resource.TestCheckResourceAttr("github_issue_labels.test", "label.#", "3"), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + // 5. Check if labels can be destroyed + { + Config: testAccGithubIssueLabelsConfig(randomID, nil), + }, + // 6. Check if labels were actually destroyed + { + Config: testAccGithubIssueLabelsConfig(randomID, empty), + Check: resource.TestCheckResourceAttr("github_issue_labels.test", "label.#", "0"), + }, + }, }) }) } diff --git a/github/resource_github_issue_test.go b/github/resource_github_issue_test.go index a4309f9022..0e4717f28e 100644 --- a/github/resource_github_issue_test.go +++ b/github/resource_github_issue_test.go @@ -2,19 +2,17 @@ package github import ( "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccGithubIssue(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates an issue without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) title := "issue_title" body := "issue_body" labels := "\"bug\", \"enhancement\"" @@ -47,7 +45,7 @@ func TestAccGithubIssue(t *testing.T) { milestone_number = github_repository_milestone.test.number } ` - config := fmt.Sprintf(issueHCL, randomID, title, body, labels, testOwnerFunc()) + config := fmt.Sprintf(issueHCL, randomID, title, body, labels, testAccConf.owner) checks := map[string]resource.TestCheckFunc{ "before": resource.ComposeTestCheckFunc( @@ -94,37 +92,24 @@ func TestAccGithubIssue(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: fmt.Sprintf(issueHCL, randomID, updatedTitle, updatedBody, updatedLabels, testOwnerFunc()), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: fmt.Sprintf(issueHCL, randomID, updatedTitle, updatedBody, updatedLabels, testAccConf.owner), + Check: checks["after"], + }, + }, }) }) t.Run("imports a issue without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -142,35 +127,20 @@ func TestAccGithubIssue(t *testing.T) { resource.TestCheckResourceAttr("github_issue.test", "title", fmt.Sprintf(`tf-acc-test-%s`, randomID)), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_issue.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_issue.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) - } diff --git a/github/resource_github_membership_test.go b/github/resource_github_membership_test.go index 33064f89b7..8b52cdca3c 100644 --- a/github/resource_github_membership_test.go +++ b/github/resource_github_membership_test.go @@ -11,116 +11,101 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -func TestAccGithubMembership_basic(t *testing.T) { - if testCollaborator == "" { - t.Skip("Skipping because `GITHUB_TEST_COLLABORATOR` is not set") - } - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) +func TestAccGithubMembership(t *testing.T) { + if len(testAccConf.testExternalUser) == 0 { + t.Skip("No external user provided") } - var membership github.Membership - rn := "github_membership.test_org_membership" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubMembershipDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubMembershipConfig(testCollaborator), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubMembershipExists(rn, &membership), - testAccCheckGithubMembershipRoleState(rn, &membership), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateVerify: true, + t.Run("creates organization membership", func(t *testing.T) { + var membership github.Membership + rn := "github_membership.test_org_membership" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubMembershipDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubMembershipConfig(testAccConf.testExternalUser), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubMembershipExists(rn, &membership), + testAccCheckGithubMembershipRoleState(rn, &membership), + ), + }, + { + ResourceName: rn, + ImportState: true, + ImportStateVerify: true, + }, }, - }, + }) }) -} -func TestAccGithubMembership_downgrade(t *testing.T) { - if testCollaborator == "" { - t.Skip("Skipping because `GITHUB_TEST_COLLABORATOR` is not set") - } - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) - } - - var membership github.Membership - rn := "github_membership.test_org_membership" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubMembershipDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubMembershipConfigDowngradable(testCollaborator), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubMembershipExists(rn, &membership), - testAccCheckGithubMembershipRoleState(rn, &membership), - ), - }, - { - ResourceName: rn, - ImportState: true, + t.Run("creates organization membership with downgrade", func(t *testing.T) { + var membership github.Membership + rn := "github_membership.test_org_membership" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubMembershipDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubMembershipConfigDowngradable(testAccConf.testExternalUser), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubMembershipExists(rn, &membership), + testAccCheckGithubMembershipRoleState(rn, &membership), + ), + }, + { + ResourceName: rn, + ImportState: true, + }, }, - }, + }) }) -} - -func TestAccGithubMembership_caseInsensitive(t *testing.T) { - if testCollaborator == "" { - t.Skip("Skipping because `GITHUB_TEST_COLLABORATOR` is not set") - } - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) - } - - var membership github.Membership - var otherMembership github.Membership - - rn := "github_membership.test_org_membership" - otherCase := flipUsernameCase(testCollaborator) - if testCollaborator == otherCase { - t.Skip("Skipping because `GITHUB_TEST_COLLABORATOR` has no letters to flip case") - } - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubMembershipDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubMembershipConfig(testCollaborator), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubMembershipExists(rn, &membership), - ), - }, - { - Config: testAccGithubMembershipConfig(otherCase), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubMembershipExists(rn, &otherMembership), - testAccGithubMembershipTheSame(&membership, &otherMembership), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateVerify: true, + t.Run("creates organization membership with case insensitivity", func(t *testing.T) { + var membership github.Membership + var otherMembership github.Membership + + rn := "github_membership.test_org_membership" + otherCase := flipUsernameCase(testAccConf.testExternalUser) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubMembershipDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubMembershipConfig(testAccConf.testExternalUser), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubMembershipExists(rn, &membership), + ), + }, + { + Config: testAccGithubMembershipConfig(otherCase), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubMembershipExists(rn, &otherMembership), + testAccGithubMembershipTheSame(&membership, &otherMembership), + ), + }, + { + ResourceName: rn, + ImportState: true, + ImportStateVerify: true, + }, }, - }, + }) }) } func testAccCheckGithubMembershipDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*Owner).v3client + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client for _, rs := range s.RootModule().Resources { if rs.Type != "github_membership" { @@ -172,7 +157,12 @@ func testAccCheckGithubMembershipExists(n string, membership *github.Membership) return fmt.Errorf("no membership ID is set") } - conn := testAccProvider.Meta().(*Owner).v3client + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + orgName, username, err := parseTwoPartID(rs.Primary.ID, "organization", "username") if err != nil { return err @@ -198,7 +188,12 @@ func testAccCheckGithubMembershipRoleState(n string, membership *github.Membersh return fmt.Errorf("no membership ID is set") } - conn := testAccProvider.Meta().(*Owner).v3client + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + orgName, username, err := parseTwoPartID(rs.Primary.ID, "organization", "username") if err != nil { return err diff --git a/github/resource_github_organization_custom_role_test.go b/github/resource_github_organization_custom_role_test.go index 33875fe992..25af7af271 100644 --- a/github/resource_github_organization_custom_role_test.go +++ b/github/resource_github_organization_custom_role_test.go @@ -9,170 +9,96 @@ import ( ) func TestAccGithubOrganizationCustomRole(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates custom repo role without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - resource "github_organization_custom_role" "test" { - name = "tf-acc-test-%s" - description = "Test role description" - base_role = "read" - permissions = [ - "reopen_issue", - "reopen_pull_request", - ] - } + resource "github_organization_custom_role" "test" { + name = "tf-acc-test-%s" + description = "Test role description" + base_role = "read" + permissions = [ + "reopen_issue", + "reopen_pull_request", + ] + } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet( - "github_organization_custom_role.test", "name", - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "name", - fmt.Sprintf(`tf-acc-test-%s`, randomID), - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "description", - "Test role description", - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "base_role", - "read", - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "permissions.#", - "2", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_organization_custom_role.test", "name"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "name", fmt.Sprintf(`tf-acc-test-%s`, randomID)), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "description", "Test role description"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "base_role", "read"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "permissions.#", "2"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) // More tests can go here following the same format... t.Run("updates custom repo role without error", func(t *testing.T) { - - configs := map[string]string{ - "before": fmt.Sprintf(` - resource "github_organization_custom_role" "test" { - name = "tf-acc-test-%s" - description = "Updated test role description before" - base_role = "read" - permissions = [ - "reopen_issue", - "reopen_pull_request", - ] - } - `, randomID), - "after": fmt.Sprintf(` - resource "github_organization_custom_role" "test" { - name = "tf-acc-test-rename-%s" - description = "Updated test role description after" - base_role = "write" - permissions = [ - "reopen_issue", - "read_code_scanning", - "reopen_pull_request", - ] - } - `, randomID), + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + config := fmt.Sprintf(` + resource "github_organization_custom_role" "test" { + name = "tf-acc-test-%s" + description = "Updated test role description before" + base_role = "read" + permissions = [ + "reopen_issue", + "reopen_pull_request", + ] } + `, randomID) - checks := map[string]resource.TestCheckFunc{ - "before": resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet( - "github_organization_custom_role.test", "name", - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "name", - fmt.Sprintf(`tf-acc-test-%s`, randomID), - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "description", - "Updated test role description before", - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "base_role", - "read", - ), - resource.TestCheckResourceAttr("github_organization_custom_role.test", "permissions.#", "2"), - ), - "after": resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet( - "github_organization_custom_role.test", "name", - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "name", - fmt.Sprintf(`tf-acc-test-rename-%s`, randomID), - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "description", - "Updated test role description after", - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "base_role", - "write", - ), - resource.TestCheckResourceAttr("github_organization_custom_role.test", "permissions.#", "3"), - )} - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: configs["before"], - Check: checks["before"], - }, - { - Config: configs["after"], - Check: checks["after"], - }, - }, - }) + configUpdated := fmt.Sprintf(` + resource "github_organization_custom_role" "test" { + name = "tf-acc-test-rename-%s" + description = "Updated test role description after" + base_role = "write" + permissions = [ + "reopen_issue", + "read_code_scanning", + "reopen_pull_request", + ] } + `, randomID) - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_organization_custom_role.test", "name"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "name", fmt.Sprintf(`tf-acc-test-%s`, randomID)), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "description", "Updated test role description before"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "base_role", "read"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "permissions.#", "2"), + ), + }, + { + Config: configUpdated, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_organization_custom_role.test", "name"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "name", fmt.Sprintf(`tf-acc-test-rename-%s`, randomID)), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "description", "Updated test role description after"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "base_role", "write"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "permissions.#", "3"), + ), + }, + }, }) }) t.Run("imports custom repo role without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_organization_custom_role" "test" { name = "tf-acc-test-%s" @@ -186,56 +112,21 @@ func TestAccGithubOrganizationCustomRole(t *testing.T) { } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet( - "github_organization_custom_role.test", "name", - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "name", - fmt.Sprintf(`tf-acc-test-%s`, randomID), - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "description", - "Test role description", - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "base_role", - "read", - ), - resource.TestCheckResourceAttr( - "github_organization_custom_role.test", "permissions.#", - "3", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_organization_custom_role.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_organization_custom_role.test", "name"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "name", fmt.Sprintf(`tf-acc-test-%s`, randomID)), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "description", "Test role description"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "base_role", "read"), + resource.TestCheckResourceAttr("github_organization_custom_role.test", "permissions.#", "3"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/resource_github_organization_project.go b/github/resource_github_organization_project.go index 9e4aa15e73..2e3ec5040d 100644 --- a/github/resource_github_organization_project.go +++ b/github/resource_github_organization_project.go @@ -13,6 +13,8 @@ import ( func resourceGithubOrganizationProject() *schema.Resource { return &schema.Resource{ + DeprecationMessage: "This resource is deprecated as classic projects have been removed and this resource will no longer work. This resource will be removed in the next major version.", + Create: resourceGithubOrganizationProjectCreate, Read: resourceGithubOrganizationProjectRead, Update: resourceGithubOrganizationProjectUpdate, diff --git a/github/resource_github_organization_project_test.go b/github/resource_github_organization_project_test.go index f8e9e9eb42..cc07dc8305 100644 --- a/github/resource_github_organization_project_test.go +++ b/github/resource_github_organization_project_test.go @@ -13,40 +13,50 @@ import ( ) func TestAccGithubOrganizationProject_basic(t *testing.T) { - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) - } + t.Skip("Skipping test as the GitHub API no longer supports classic projects") - var project github.Project - - rn := "github_organization_project.test" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGithubOrganizationProjectDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubOrganizationProjectConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubOrganizationProjectExists(rn, &project), - testAccCheckGithubOrganizationProjectAttributes(&project, &testAccGithubOrganizationProjectExpectedAttributes{ - Name: "test-project", - Body: "this is a test project", - }), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateVerify: true, + t.Run("creates organization project", func(t *testing.T) { + var project github.Project + config := ` +resource "github_organization_project" "test" { + name = "test-project" + body = "this is a test project" +} +` + + rn := "github_organization_project.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccGithubOrganizationProjectDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubOrganizationProjectExists(rn, &project), + testAccCheckGithubOrganizationProjectAttributes(&project, &testAccGithubOrganizationProjectExpectedAttributes{ + Name: "test-project", + Body: "this is a test project", + }), + ), + }, + { + ResourceName: rn, + ImportState: true, + ImportStateVerify: true, + }, }, - }, + }) }) } func testAccGithubOrganizationProjectDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*Owner).v3client + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client for _, rs := range s.RootModule().Resources { if rs.Type != "github_organization_project" { @@ -85,7 +95,12 @@ func testAccCheckGithubOrganizationProjectExists(n string, project *github.Proje return err } - conn := testAccProvider.Meta().(*Owner).v3client + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + gotProject, _, err := conn.Projects.GetProject(context.TODO(), projectID) if err != nil { return err @@ -102,7 +117,6 @@ type testAccGithubOrganizationProjectExpectedAttributes struct { func testAccCheckGithubOrganizationProjectAttributes(project *github.Project, want *testAccGithubOrganizationProjectExpectedAttributes) resource.TestCheckFunc { return func(s *terraform.State) error { - if name := project.GetName(); name != want.Name { return fmt.Errorf("got project %q; want %q", name, want.Name) } @@ -116,10 +130,3 @@ func testAccCheckGithubOrganizationProjectAttributes(project *github.Project, wa return nil } } - -const testAccGithubOrganizationProjectConfig = ` -resource "github_organization_project" "test" { - name = "test-project" - body = "this is a test project" -} -` diff --git a/github/resource_github_organization_ruleset_test.go b/github/resource_github_organization_ruleset_test.go index 9aae9f35b9..3657a5f457 100644 --- a/github/resource_github_organization_ruleset_test.go +++ b/github/resource_github_organization_ruleset_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -10,18 +9,8 @@ import ( ) func TestGithubOrganizationRulesets(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("Creates and updates organization rulesets without errors", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_organization_ruleset" "test" { name = "test-%s" @@ -29,6 +18,11 @@ func TestGithubOrganizationRulesets(t *testing.T) { enforcement = "active" conditions { + repository_name { + include = ["~ALL"] + exclude = [] + } + ref_name { include = ["~ALL"] exclude = [] @@ -89,97 +83,96 @@ func TestGithubOrganizationRulesets(t *testing.T) { } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_organization_ruleset.test", "name", - "test", - ), - resource.TestCheckResourceAttr( - "github_organization_ruleset.test", "enforcement", - "active", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_organization_ruleset.test", "name", "test"), + resource.TestCheckResourceAttr("github_organization_ruleset.test", "enforcement", "active"), + ), }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - testCase(t, enterprise) + }, }) - }) t.Run("Updates a ruleset name without error", func(t *testing.T) { - - oldRSName := fmt.Sprintf(`ruleset-%[1]s`, randomID) - newRSName := fmt.Sprintf(`%[1]s-renamed`, randomID) + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + name := fmt.Sprintf("test-acc-ruleset-%s", randomID) + nameUpdated := fmt.Sprintf("test-acc-ruleset-updated-%s", randomID) config := fmt.Sprintf(` - resource "github_organization_ruleset" "test" { - name = "%s" - target = "branch" - enforcement = "active" + resource "github_organization_ruleset" "test" { + name = "%s" + target = "branch" + enforcement = "active" + + conditions { + repository_name { + include = ["~ALL"] + exclude = [] + } - rules { - creation = true + ref_name { + include = ["~ALL"] + exclude = [] } } - `, oldRSName) - - checks := map[string]resource.TestCheckFunc{ - "before": resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_organization_ruleset.test", "name", - oldRSName, - ), - ), - "after": resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_organization_ruleset.test", "name", - newRSName, - ), - ), - } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - // Rename the ruleset to something else - Config: strings.Replace( - config, - oldRSName, - newRSName, 1), - Check: checks["after"], - }, - }, - }) + rules { + creation = true + } } + `, name) + + configUpdated := fmt.Sprintf(` + resource "github_organization_ruleset" "test" { + name = "%s" + target = "branch" + enforcement = "active" + + conditions { + repository_name { + include = ["~ALL"] + exclude = [] + } - t.Run("with an enterprise account", func(t *testing.T) { - testCase(t, enterprise) - }) + ref_name { + include = ["~ALL"] + exclude = [] + } + } + rules { + creation = true + } + } + `, nameUpdated) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_organization_ruleset.test", "name", name), + ), + }, + { + Config: configUpdated, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_organization_ruleset.test", "name", nameUpdated), + ), + }, + }, + }) }) - t.Run("Imports rulesets without error", func(t *testing.T) { - + t.Run("imports rulesets without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_organization_ruleset" "test" { name = "test-%s" @@ -187,6 +180,11 @@ func TestGithubOrganizationRulesets(t *testing.T) { enforcement = "active" conditions { + repository_name { + include = ["~ALL"] + exclude = [] + } + ref_name { include = ["~ALL"] exclude = [] @@ -230,34 +228,26 @@ func TestGithubOrganizationRulesets(t *testing.T) { non_fast_forward = true } } + + `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("github_organization_ruleset.test", "name"), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_organization_ruleset.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasPaidOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - testCase(t, enterprise) + { + ResourceName: "github_organization_ruleset.test", + ImportState: true, + ImportStateVerify: true, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_organization_ruleset.test", "name"), + ), + }, + }, }) - }) - } diff --git a/github/resource_github_organization_security_manager_test.go b/github/resource_github_organization_security_manager_test.go index 070ad9ae4d..c50e0ce95a 100644 --- a/github/resource_github_organization_security_manager_test.go +++ b/github/resource_github_organization_security_manager_test.go @@ -9,9 +9,8 @@ import ( ) func TestAccGithubOrganizationSecurityManagers(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("adds team as security manager", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-%s" @@ -22,37 +21,24 @@ func TestAccGithubOrganizationSecurityManagers(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrPair("github_team.test", "ID", "github_organization_security_manager.test", "ID"), - resource.TestCheckResourceAttrPair("github_team.test", "slug", "github_organization_security_manager.test", "team_slug"), - resource.TestCheckResourceAttr("github_organization_security_manager.test", "team_slug", fmt.Sprintf("tf-acc-%s", randomID)), - ), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("github_team.test", "ID", "github_organization_security_manager.test", "ID"), + resource.TestCheckResourceAttrPair("github_team.test", "slug", "github_organization_security_manager.test", "team_slug"), + resource.TestCheckResourceAttr("github_organization_security_manager.test", "team_slug", fmt.Sprintf("tf-acc-%s", randomID)), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("handles team name changes", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-%s" @@ -73,45 +59,32 @@ func TestAccGithubOrganizationSecurityManagers(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrPair("github_team.test", "ID", "github_organization_security_manager.test", "ID"), - resource.TestCheckResourceAttrPair("github_team.test", "slug", "github_organization_security_manager.test", "team_slug"), - resource.TestCheckResourceAttr("github_organization_security_manager.test", "team_slug", fmt.Sprintf("tf-acc-%s", randomID)), - ), - }, - { - Config: configUpdated, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrPair("github_team.test", "ID", "github_organization_security_manager.test", "ID"), - resource.TestCheckResourceAttrPair("github_team.test", "slug", "github_organization_security_manager.test", "team_slug"), - resource.TestCheckResourceAttr("github_organization_security_manager.test", "team_slug", fmt.Sprintf("tf-acc-updated-%s", randomID)), - ), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("github_team.test", "ID", "github_organization_security_manager.test", "ID"), + resource.TestCheckResourceAttrPair("github_team.test", "slug", "github_organization_security_manager.test", "team_slug"), + resource.TestCheckResourceAttr("github_organization_security_manager.test", "team_slug", fmt.Sprintf("tf-acc-%s", randomID)), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configUpdated, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("github_team.test", "ID", "github_organization_security_manager.test", "ID"), + resource.TestCheckResourceAttrPair("github_team.test", "slug", "github_organization_security_manager.test", "team_slug"), + resource.TestCheckResourceAttr("github_organization_security_manager.test", "team_slug", fmt.Sprintf("tf-acc-updated-%s", randomID)), + ), + }, + }, }) }) t.Run("handles team name changes", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-%s" @@ -132,41 +105,27 @@ func TestAccGithubOrganizationSecurityManagers(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrPair("github_team.test", "ID", "github_organization_security_manager.test", "ID"), - resource.TestCheckResourceAttrPair("github_team.test", "slug", "github_organization_security_manager.test", "team_slug"), - resource.TestCheckResourceAttr("github_organization_security_manager.test", "team_slug", fmt.Sprintf("tf-acc-%s", randomID)), - ), - }, - { - Config: configUpdated, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrPair("github_team.test", "ID", "github_organization_security_manager.test", "ID"), - resource.TestCheckResourceAttrPair("github_team.test", "slug", "github_organization_security_manager.test", "team_slug"), - resource.TestCheckResourceAttr("github_organization_security_manager.test", "team_slug", fmt.Sprintf("tf-acc-updated-%s", randomID)), - ), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("github_team.test", "ID", "github_organization_security_manager.test", "ID"), + resource.TestCheckResourceAttrPair("github_team.test", "slug", "github_organization_security_manager.test", "team_slug"), + resource.TestCheckResourceAttr("github_organization_security_manager.test", "team_slug", fmt.Sprintf("tf-acc-%s", randomID)), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configUpdated, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrPair("github_team.test", "ID", "github_organization_security_manager.test", "ID"), + resource.TestCheckResourceAttrPair("github_team.test", "slug", "github_organization_security_manager.test", "team_slug"), + resource.TestCheckResourceAttr("github_organization_security_manager.test", "team_slug", fmt.Sprintf("tf-acc-updated-%s", randomID)), + ), + }, + }, }) }) } diff --git a/github/resource_github_organization_settings_test.go b/github/resource_github_organization_settings_test.go index c0b6a8b20d..fb2d7f0dc2 100644 --- a/github/resource_github_organization_settings_test.go +++ b/github/resource_github_organization_settings_test.go @@ -9,7 +9,6 @@ import ( func TestAccGithubOrganizationSettings(t *testing.T) { t.Run("creates organization settings without error", func(t *testing.T) { - config := ` resource "github_organization_settings" "test" { billing_email = "test@example.com" @@ -46,29 +45,19 @@ func TestAccGithubOrganizationSettings(t *testing.T) { "billing_email", "test@example.com", ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - t.Run("run with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - t.Run("run with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - t.Run("run with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) + t.Run("updates organization settings without error", func(t *testing.T) { billingEmail := "test1@example.com" company := "Test Company" @@ -102,33 +91,19 @@ func TestAccGithubOrganizationSettings(t *testing.T) { "billing_email", updatedBillingEmail, ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: configs["before"], - Check: checks["before"], - }, - { - Config: configs["after"], - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: configs["before"], + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configs["after"], + Check: checks["after"], + }, + }, }) }) @@ -151,33 +126,20 @@ func TestAccGithubOrganizationSettings(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_organization_settings.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_organization_settings.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) } diff --git a/github/resource_github_organization_webhook_test.go b/github/resource_github_organization_webhook_test.go index ae5fb3f70e..7cf063bb33 100644 --- a/github/resource_github_organization_webhook_test.go +++ b/github/resource_github_organization_webhook_test.go @@ -10,11 +10,8 @@ import ( ) func TestAccGithubOrganizationWebhook(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates and updates webhooks without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -49,40 +46,26 @@ func TestAccGithubOrganizationWebhook(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - "https://google.de/webhook", - "https://google.de/updated", 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + "https://google.de/webhook", + "https://google.de/updated", 1), + Check: checks["after"], + }, + }, }) }) t.Run("imports webhooks without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -109,35 +92,20 @@ func TestAccGithubOrganizationWebhook(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_organization_webhook.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_organization_webhook.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) - } diff --git a/github/resource_github_project_card.go b/github/resource_github_project_card.go index 8b6dfec17c..6ac54547e3 100644 --- a/github/resource_github_project_card.go +++ b/github/resource_github_project_card.go @@ -14,6 +14,8 @@ import ( func resourceGithubProjectCard() *schema.Resource { return &schema.Resource{ + DeprecationMessage: "This resource is deprecated as classic projects have been removed and this resource will no longer work. This resource will be removed in the next major version.", + Create: resourceGithubProjectCardCreate, Read: resourceGithubProjectCardRead, Update: resourceGithubProjectCardUpdate, @@ -184,7 +186,6 @@ func resourceGithubProjectCardDelete(d *schema.ResourceData, meta interface{}) e } func resourceGithubProjectCardImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - cardIDStr := d.Id() cardID, err := strconv.ParseInt(cardIDStr, 10, 64) if err != nil { @@ -205,5 +206,4 @@ func resourceGithubProjectCardImport(d *schema.ResourceData, meta interface{}) ( } return []*schema.ResourceData{d}, nil - } diff --git a/github/resource_github_project_card_test.go b/github/resource_github_project_card_test.go index 4172f851d9..c5aa7e4833 100644 --- a/github/resource_github_project_card_test.go +++ b/github/resource_github_project_card_test.go @@ -2,19 +2,19 @@ package github import ( "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccGithubProjectCard(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + t.Skip("Skipping test as the GitHub API no longer supports classic projects") t.Run("creates a project card using a note", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_organization_project" "project" { @@ -40,35 +40,20 @@ func TestAccGithubProjectCard(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("creates a project card using an issue", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -97,7 +82,7 @@ func TestAccGithubProjectCard(t *testing.T) { resource "github_project_card" "test" { column_id = github_project_column.test.column_id content_id = github_issue.test.issue_id - content_type = "Issue" + content_type = "Issue" } `, randomID) @@ -117,30 +102,15 @@ func TestAccGithubProjectCard(t *testing.T) { }, ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/resource_github_project_column.go b/github/resource_github_project_column.go index fa54106fe2..ee348282cf 100644 --- a/github/resource_github_project_column.go +++ b/github/resource_github_project_column.go @@ -13,6 +13,8 @@ import ( func resourceGithubProjectColumn() *schema.Resource { return &schema.Resource{ + DeprecationMessage: "This resource is deprecated as classic projects have been removed and this resource will no longer work. This resource will be removed in the next major version.", + Create: resourceGithubProjectColumnCreate, Read: resourceGithubProjectColumnRead, Update: resourceGithubProjectColumnUpdate, diff --git a/github/resource_github_project_column_test.go b/github/resource_github_project_column_test.go index bc2e0dc75d..1feebc0d4d 100644 --- a/github/resource_github_project_column_test.go +++ b/github/resource_github_project_column_test.go @@ -12,48 +12,52 @@ import ( ) func TestAccGithubProjectColumn_basic(t *testing.T) { - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) - } - - var column github.ProjectColumn - - rn := "github_project_column.column" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccGithubProjectColumnDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubProjectColumnConfig("new column name"), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubProjectColumnExists(rn, &column), - testAccCheckGithubProjectColumnAttributes(&column, &testAccGithubProjectColumnExpectedAttributes{ - Name: "new column name", - }), - ), - }, - { - Config: testAccGithubProjectColumnConfig("updated column name"), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubProjectColumnExists(rn, &column), - testAccCheckGithubProjectColumnAttributes(&column, &testAccGithubProjectColumnExpectedAttributes{ - Name: "updated column name", - }), - ), + t.Skip("Skipping test as the GitHub API no longer supports classic projects") + + t.Run("creates and updates a project column", func(t *testing.T) { + var column github.ProjectColumn + + rn := "github_project_column.column" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccGithubProjectColumnDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubProjectColumnConfig("new column name"), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubProjectColumnExists(rn, &column), + testAccCheckGithubProjectColumnAttributes(&column, &testAccGithubProjectColumnExpectedAttributes{ + Name: "new column name", + }), + ), + }, + { + Config: testAccGithubProjectColumnConfig("updated column name"), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubProjectColumnExists(rn, &column), + testAccCheckGithubProjectColumnAttributes(&column, &testAccGithubProjectColumnExpectedAttributes{ + Name: "updated column name", + }), + ), + }, + { + ResourceName: rn, + ImportState: true, + ImportStateVerify: true, + }, }, - { - ResourceName: rn, - ImportState: true, - ImportStateVerify: true, - }, - }, + }) }) } func testAccGithubProjectColumnDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*Owner).v3client + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client for _, rs := range s.RootModule().Resources { if rs.Type != "github_project_column" { @@ -91,7 +95,12 @@ func testAccCheckGithubProjectColumnExists(n string, project *github.ProjectColu return err } - conn := testAccProvider.Meta().(*Owner).v3client + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + gotColumn, _, err := conn.Projects.GetProjectColumn(context.TODO(), columnID) if err != nil { return err @@ -107,7 +116,6 @@ type testAccGithubProjectColumnExpectedAttributes struct { func testAccCheckGithubProjectColumnAttributes(column *github.ProjectColumn, want *testAccGithubProjectColumnExpectedAttributes) resource.TestCheckFunc { return func(s *terraform.State) error { - if name := column.GetName(); name != want.Name { return fmt.Errorf("got project column %q; want %q", name, want.Name) } diff --git a/github/resource_github_release_test.go b/github/resource_github_release_test.go index d523a10c7b..b9f28a8195 100644 --- a/github/resource_github_release_test.go +++ b/github/resource_github_release_test.go @@ -2,18 +2,17 @@ package github import ( "fmt" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "log" "testing" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccGithubReleaseResource(t *testing.T) { - t.Run("create a release with defaults", func(t *testing.T) { - randomRepoPart := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) randomVersion := fmt.Sprintf("v1.0.%d", acctest.RandIntRange(0, 9999)) @@ -56,42 +55,26 @@ func TestAccGithubReleaseResource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_release.test", - ImportState: true, - ImportStateVerify: true, - ImportStateIdFunc: importReleaseByResourcePaths( - "github_repository.test", "github_release.test"), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_release.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: importReleaseByResourcePaths( + "github_repository.test", "github_release.test"), + }, + }, }) - }) t.Run("create a release on branch", func(t *testing.T) { - randomRepoPart := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) randomVersion := fmt.Sprintf("v1.0.%d", acctest.RandIntRange(0, 9999)) testBranchName := "test" @@ -144,40 +127,24 @@ func TestAccGithubReleaseResource(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_release.test", - ImportState: true, - ImportStateVerify: true, - ImportStateIdFunc: importReleaseByResourcePaths( - "github_repository.test", "github_release.test"), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_release.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: importReleaseByResourcePaths( + "github_repository.test", "github_release.test"), + }, + }, }) - }) - } func importReleaseByResourcePaths(repoLogicalName, releaseLogicalName string) resource.ImportStateIdFunc { diff --git a/github/resource_github_repository.go b/github/resource_github_repository.go index f28cc4c6e8..c6ef6e32f1 100644 --- a/github/resource_github_repository.go +++ b/github/resource_github_repository.go @@ -320,6 +320,7 @@ func resourceGithubRepository() *schema.Resource { "vulnerability_alerts": { Type: schema.TypeBool, Optional: true, + Computed: true, Description: "Set to 'true' to enable security alerts for vulnerable dependencies. Enabling requires alerts to be enabled on the owner level. (Note for importing: GitHub enables the alerts on public repos but disables them on private repos by default). Note that vulnerability alerts have not been successfully tested on any GitHub Enterprise instance and may be unavailable in those settings.", }, "ignore_vulnerability_alerts_during_read": { @@ -412,7 +413,6 @@ func resourceGithubRepository() *schema.Resource { } func calculateVisibility(d *schema.ResourceData) string { - if value, ok := d.GetOk("visibility"); ok { return value.(string) } @@ -619,6 +619,11 @@ func resourceGithubRepositoryCreate(d *schema.ResourceData, meta interface{}) er } } + err := updateVulnerabilityAlerts(d, client, ctx, owner, repoName) + if err != nil { + return err + } + return resourceGithubRepositoryUpdate(d, meta) } @@ -817,12 +822,7 @@ func resourceGithubRepositoryUpdate(d *schema.ResourceData, meta interface{}) er } if d.HasChange("vulnerability_alerts") { - updateVulnerabilityAlerts := client.Repositories.DisableVulnerabilityAlerts - if vulnerabilityAlerts, ok := d.GetOk("vulnerability_alerts"); ok && vulnerabilityAlerts.(bool) { - updateVulnerabilityAlerts = client.Repositories.EnableVulnerabilityAlerts - } - - _, err = updateVulnerabilityAlerts(ctx, owner, repoName) + err = updateVulnerabilityAlerts(d, client, ctx, owner, repoName) if err != nil { return err } @@ -957,13 +957,19 @@ func flattenPages(pages *github.Pages) []interface{} { return []interface{}{} } - sourceMap := make(map[string]interface{}) - sourceMap["branch"] = pages.GetSource().GetBranch() - sourceMap["path"] = pages.GetSource().GetPath() - pagesMap := make(map[string]interface{}) - pagesMap["source"] = []interface{}{sourceMap} - pagesMap["build_type"] = pages.GetBuildType() + buildType := pages.GetBuildType() + pagesMap["build_type"] = buildType + + if buildType == "legacy" { + sourceMap := make(map[string]interface{}) + sourceMap["branch"] = pages.GetSource().GetBranch() + sourceMap["path"] = pages.GetSource().GetPath() + pagesMap["source"] = []interface{}{sourceMap} + } else { + pagesMap["source"] = nil + } + pagesMap["url"] = pages.GetURL() pagesMap["status"] = pages.GetStatus() pagesMap["cname"] = pages.GetCNAME() @@ -1038,7 +1044,8 @@ func flattenSecurityAndAnalysis(securityAndAnalysis *github.SecurityAndAnalysis) // resourceGithubParseFullName will return "myorg", "myrepo", true when full_name is "myorg/myrepo". func resourceGithubParseFullName(resourceDataLike interface { GetOk(string) (interface{}, bool) -}) (string, string, bool) { +}, +) (string, string, bool) { x, ok := resourceDataLike.GetOk("full_name") if !ok { return "", "", false @@ -1062,3 +1069,13 @@ func customDiffFunction(_ context.Context, diff *schema.ResourceDiff, v interfac } return nil } + +func updateVulnerabilityAlerts(d *schema.ResourceData, client *github.Client, ctx context.Context, owner, repoName string) error { + updateVulnerabilityAlerts := client.Repositories.DisableVulnerabilityAlerts + if vulnerabilityAlerts, ok := d.GetOk("vulnerability_alerts"); ok && vulnerabilityAlerts.(bool) { + updateVulnerabilityAlerts = client.Repositories.EnableVulnerabilityAlerts + } + + _, err := updateVulnerabilityAlerts(ctx, owner, repoName) + return err +} diff --git a/github/resource_github_repository_autolink_reference_test.go b/github/resource_github_repository_autolink_reference_test.go index 87c01a2cc4..7151cf8be6 100644 --- a/github/resource_github_repository_autolink_reference_test.go +++ b/github/resource_github_repository_autolink_reference_test.go @@ -10,11 +10,8 @@ import ( ) func TestAccGithubRepositoryAutolinkReference(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates repository autolink reference without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "test-%s" @@ -95,35 +92,20 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("imports repository autolink reference without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "test-%s" @@ -204,63 +186,48 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - // autolink_default - { - ResourceName: "github_repository_autolink_reference.autolink_default", - ImportState: true, - ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), - }, - // autolink_alphanumeric - { - ResourceName: "github_repository_autolink_reference.autolink_alphanumeric", - ImportState: true, - ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), - }, - // autolink_numeric - { - ResourceName: "github_repository_autolink_reference.autolink_numeric", - ImportState: true, - ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), - }, - // autolink_with_port - { - ResourceName: "github_repository_autolink_reference.autolink_with_port", - ImportState: true, - ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + // autolink_default + { + ResourceName: "github_repository_autolink_reference.autolink_default", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), + }, + // autolink_alphanumeric + { + ResourceName: "github_repository_autolink_reference.autolink_alphanumeric", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), + }, + // autolink_numeric + { + ResourceName: "github_repository_autolink_reference.autolink_numeric", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), + }, + // autolink_with_port + { + ResourceName: "github_repository_autolink_reference.autolink_with_port", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), + }, + }, }) - }) t.Run("imports repository autolink reference by key prefix without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "oof" { name = "oof-%s" @@ -275,46 +242,31 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - ResourceName: "github_repository_autolink_reference.autolink", - ImportState: true, - ImportStateVerify: true, - ImportStateId: fmt.Sprintf("oof-%s/OOF-", randomID), - }, - { - ResourceName: "github_repository_autolink_reference.autolink", - ImportState: true, - ImportStateId: fmt.Sprintf("oof-%s/OCTOCAT-", randomID), - ExpectError: regexp.MustCompile(`cannot find autolink reference`), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_repository_autolink_reference.autolink", + ImportState: true, + ImportStateVerify: true, + ImportStateId: fmt.Sprintf("oof-%s/OOF-", randomID), + }, + { + ResourceName: "github_repository_autolink_reference.autolink", + ImportState: true, + ImportStateId: fmt.Sprintf("oof-%s/OCTOCAT-", randomID), + ExpectError: regexp.MustCompile(`cannot find autolink reference`), + }, + }, }) - }) t.Run("deletes repository autolink reference without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "test-%s" @@ -329,29 +281,15 @@ func TestAccGithubRepositoryAutolinkReference(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Destroy: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Destroy: true, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/resource_github_repository_collaborator_test.go b/github/resource_github_repository_collaborator_test.go index a55b5e1025..762a8ac322 100644 --- a/github/resource_github_repository_collaborator_test.go +++ b/github/resource_github_repository_collaborator_test.go @@ -2,7 +2,6 @@ package github import ( "fmt" - "os" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -10,13 +9,12 @@ import ( ) func TestAccGithubRepositoryCollaborator(t *testing.T) { - - t.Skip("update below to unskip this test run") - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + if len(testAccConf.testExternalUser) == 0 { + t.Skip("No external user provided") + } t.Run("creates invitations without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -25,10 +23,10 @@ func TestAccGithubRepositoryCollaborator(t *testing.T) { resource "github_repository_collaborator" "test_repo_collaborator" { repository = "${github_repository.test.name}" - username = "" + username = "%s" permission = "triage" } - `, randomID) + `, randomID, testAccConf.testExternalUser) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -37,40 +35,20 @@ func TestAccGithubRepositoryCollaborator(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("creates invitations when repository contains the org name", func(t *testing.T) { - - orgName := os.Getenv("GITHUB_ORGANIZATION") - - if orgName == "" { - t.Skip("Set GITHUB_ORGANIZATION to unskip this test run") - } - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) configWithOwner := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -82,7 +60,7 @@ func TestAccGithubRepositoryCollaborator(t *testing.T) { username = "" permission = "triage" } - `, randomID, orgName) + `, randomID, testAccConf.owner) checkWithOwner := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -91,29 +69,15 @@ func TestAccGithubRepositoryCollaborator(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: configWithOwner, - Check: checkWithOwner, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: configWithOwner, + Check: checkWithOwner, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) } diff --git a/github/resource_github_repository_collaborators_test.go b/github/resource_github_repository_collaborators_test.go index 4e4d60ae12..8cbdbd2a5b 100644 --- a/github/resource_github_repository_collaborators_test.go +++ b/github/resource_github_repository_collaborators_test.go @@ -3,7 +3,6 @@ package github import ( "context" "fmt" - "os" "strings" "testing" @@ -14,32 +13,21 @@ import ( ) func TestAccGithubRepositoryCollaborators(t *testing.T) { - - inOrgUser := os.Getenv("GITHUB_IN_ORG_USER") - inOrgUser2 := os.Getenv("GITHUB_IN_ORG_USER2") - - if inOrgUser == "" || inOrgUser2 == "" { - t.Skip("set inOrgUser and inOrgUser2 to unskip this test run") - } - - if inOrgUser == testOwnerFunc() || inOrgUser2 == testOwnerFunc() { - t.Skip("inOrgUser and inOrgUser2 can't be same as owner") + if len(testAccConf.testExternalUser) == 0 { + t.Skip("No external user provided") } - config := Config{BaseURL: "https://api.github.com/", Owner: testOwnerFunc(), Token: testToken} - meta, err := config.Meta() + meta, err := getTestMeta() if err != nil { - t.Fatalf("failed to return meta without error: %s", err.Error()) + t.Fatal(err) } - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - - t.Run("creates collaborators without error", func(t *testing.T) { - - conn := meta.(*Owner).v3client + t.Run("adds user collaborator", func(t *testing.T) { + conn := meta.v3client + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) repoName := fmt.Sprintf("tf-acc-test-%s", randomID) - individualConfig := fmt.Sprintf(` + config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" auto_init = true @@ -54,9 +42,60 @@ func TestAccGithubRepositoryCollaborators(t *testing.T) { permission = "push" } } - `, repoName, inOrgUser) + `, repoName, testAccConf.testExternalUser) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "user.#"), + resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "team.#"), + resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "user.#", "1"), + resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "team.#", "0"), + func(state *terraform.State) error { + owner := testAccConf.owner + + collaborators := state.RootModule().Resources["github_repository_collaborators.test_repo_collaborators"].Primary + for name, val := range collaborators.Attributes { + if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".username") && val != testAccConf.testExternalUser { + return fmt.Errorf("expected user.*.username to be set to %s, was %s", testAccConf.testExternalUser, val) + } + if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".permission") && val != "push" { + return fmt.Errorf("expected user.*.permission to be set to push, was %s", val) + } + } + + invites, _, err := conn.Repositories.ListInvitations(context.TODO(), owner, repoName, nil) + if err != nil { + return err + } + if len(invites) != 1 { + return fmt.Errorf("expected an invite for %s but not found", testAccConf.testExternalUser) + } + if invites[0].GetInvitee().GetLogin() != testAccConf.testExternalUser { + return fmt.Errorf("expected an invite for %s for repo %s/%s", testAccConf.testExternalUser, owner, repoName) + } + perm := invites[0].GetPermissions() + if perm != "write" { + return fmt.Errorf("expected the invite for %s to have push perms for for %s/%s, found %s", testAccConf.testExternalUser, owner, repoName, perm) + } + return nil + }, + ), + }, + }, + }) + }) + + t.Run("adds team collaborator", func(t *testing.T) { + conn := meta.v3client + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repoName := fmt.Sprintf("tf-acc-test-%s", randomID) - orgConfig := fmt.Sprintf(` + config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" auto_init = true @@ -79,124 +118,78 @@ func TestAccGithubRepositoryCollaborators(t *testing.T) { permission = "pull" } } - `, repoName, inOrgUser) - - testCase := func(t *testing.T, mode, config string, testCheck func(state *terraform.State) error) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: testCheck, - }, - }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "user.#"), - resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "team.#"), - resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "user.#", "1"), - resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "team.#", "0"), - func(state *terraform.State) error { - owner := meta.(*Owner).name - - collaborators := state.RootModule().Resources["github_repository_collaborators.test_repo_collaborators"].Primary - for name, val := range collaborators.Attributes { - if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".username") && val != inOrgUser { - return fmt.Errorf("expected user.*.username to be set to %s, was %s", inOrgUser, val) - } - if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".permission") && val != "push" { - return fmt.Errorf("expected user.*.permission to be set to push, was %s", val) - } - } - - invites, _, err := conn.Repositories.ListInvitations(context.TODO(), owner, repoName, nil) - if err != nil { - return err - } - if len(invites) != 1 { - return fmt.Errorf("expected an invite for %s but not found", inOrgUser) - } - if invites[0].GetInvitee().GetLogin() != inOrgUser { - return fmt.Errorf("expected an invite for %s for repo %s/%s", inOrgUser, owner, repoName) - } - perm := invites[0].GetPermissions() - if perm != "write" { - return fmt.Errorf("expected the invite for %s to have push perms for for %s/%s, found %s", inOrgUser, owner, repoName, perm) - } - return nil + `, repoName, testAccConf.testExternalUser) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "user.#"), + resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "team.#"), + resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "user.#", "1"), + resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "team.#", "1"), + func(state *terraform.State) error { + owner := testAccConf.owner + + teamAttrs := state.RootModule().Resources["github_team.test"].Primary.Attributes + collaborators := state.RootModule().Resources["github_repository_collaborators.test_repo_collaborators"].Primary + for name, val := range collaborators.Attributes { + if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".username") && val != testAccConf.testExternalUser { + return fmt.Errorf("expected user.*.username to be set to %s, was %s", testAccConf.testExternalUser, val) + } + if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".permission") && val != "admin" { + return fmt.Errorf("expected user.*.permission to be set to admin, was %s", val) + } + if strings.HasPrefix(name, "team.") && strings.HasSuffix(name, ".team_id") && val != teamAttrs["id"] { + return fmt.Errorf("expected team.*.team_id to be set to %s, was %s", teamAttrs["id"], val) + } + if strings.HasPrefix(name, "team.") && strings.HasSuffix(name, ".permission") && val != "pull" { + return fmt.Errorf("expected team.*.permission to be set to pull, was %s", val) + } + } + users, _, err := conn.Repositories.ListCollaborators(context.TODO(), owner, repoName, &github.ListCollaboratorsOptions{Affiliation: "direct"}) + if err != nil { + return err + } + if len(users) != 1 { + return fmt.Errorf("expected %s to be a collaborator for repo %s/%s", testAccConf.testExternalUser, owner, repoName) + } + perm := getPermission(users[0].GetRoleName()) + if perm != "admin" { + return fmt.Errorf("expected %s to have admin perms for repo %s/%s, found %s", testAccConf.testExternalUser, owner, repoName, perm) + } + teams, _, err := conn.Repositories.ListTeams(context.TODO(), owner, repoName, nil) + if err != nil { + return err + } + if len(teams) != 1 { + return fmt.Errorf("expected team %s to be a collaborator for %s/%s", repoName, owner, repoName) + } + perm = getPermission(teams[0].GetPermission()) + if perm != "pull" { + return fmt.Errorf("expected team %s to have pull perms for repo %s/%s, found %s", repoName, owner, repoName, perm) + } + return nil + }, + ), }, - ) - testCase(t, individual, individualConfig, check) - }) - - t.Run("with an organization account", func(t *testing.T) { - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "user.#"), - resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "team.#"), - resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "user.#", "1"), - resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "team.#", "1"), - func(state *terraform.State) error { - owner := testOrganizationFunc() - - teamAttrs := state.RootModule().Resources["github_team.test"].Primary.Attributes - collaborators := state.RootModule().Resources["github_repository_collaborators.test_repo_collaborators"].Primary - for name, val := range collaborators.Attributes { - if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".username") && val != inOrgUser { - return fmt.Errorf("expected user.*.username to be set to %s, was %s", inOrgUser, val) - } - if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".permission") && val != "admin" { - return fmt.Errorf("expected user.*.permission to be set to admin, was %s", val) - } - if strings.HasPrefix(name, "team.") && strings.HasSuffix(name, ".team_id") && val != teamAttrs["id"] { - return fmt.Errorf("expected team.*.team_id to be set to %s, was %s", teamAttrs["id"], val) - } - if strings.HasPrefix(name, "team.") && strings.HasSuffix(name, ".permission") && val != "pull" { - return fmt.Errorf("expected team.*.permission to be set to pull, was %s", val) - } - } - users, _, err := conn.Repositories.ListCollaborators(context.TODO(), owner, repoName, &github.ListCollaboratorsOptions{Affiliation: "direct"}) - if err != nil { - return err - } - if len(users) != 1 { - return fmt.Errorf("expected %s to be a collaborator for repo %s/%s", inOrgUser, owner, repoName) - } - perm := getPermission(users[0].GetRoleName()) - if perm != "admin" { - return fmt.Errorf("expected %s to have admin perms for repo %s/%s, found %s", inOrgUser, owner, repoName, perm) - } - teams, _, err := conn.Repositories.ListTeams(context.TODO(), owner, repoName, nil) - if err != nil { - return err - } - if len(teams) != 1 { - return fmt.Errorf("expected team %s to be a collaborator for %s/%s", repoName, owner, repoName) - } - perm = getPermission(teams[0].GetPermission()) - if perm != "pull" { - return fmt.Errorf("expected team %s to have pull perms for repo %s/%s, found %s", repoName, owner, repoName, perm) - } - return nil - }, - ) - testCase(t, organization, orgConfig, check) + }, }) }) - t.Run("updates collaborators without error", func(t *testing.T) { + t.Run("updates user collaborators without error", func(t *testing.T) { + if len(testAccConf.testExternalUser2) == 0 { + t.Skip("No additional external user provided") + } - conn := meta.(*Owner).v3client + conn := meta.v3client + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) repoName := fmt.Sprintf("tf-acc-test-%s", randomID) - individualConfig := fmt.Sprintf(` + config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" auto_init = true @@ -211,9 +204,9 @@ func TestAccGithubRepositoryCollaborators(t *testing.T) { permission = "push" } } - `, repoName, inOrgUser) + `, repoName, testAccConf.testExternalUser) - individualConfigUpdate := fmt.Sprintf(` + configUpdate := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" auto_init = true @@ -228,9 +221,67 @@ func TestAccGithubRepositoryCollaborators(t *testing.T) { permission = "pull" } } - `, repoName, inOrgUser2) + `, repoName, testAccConf.testExternalUser2) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + }, + { + Config: configUpdate, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "user.#"), + resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "team.#"), + resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "user.#", "1"), + resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "team.#", "0"), + func(state *terraform.State) error { + owner := testAccConf.owner + + collaborators := state.RootModule().Resources["github_repository_collaborators.test_repo_collaborators"].Primary + for name, val := range collaborators.Attributes { + if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".username") && val != testAccConf.testExternalUser2 { + return fmt.Errorf("expected user.*.username to be set to %s, was %s", testAccConf.testExternalUser, val) + } + if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".permission") && val != "pull" { + return fmt.Errorf("expected user.*.permission to be set to pull, was %s", val) + } + } + + invites, _, err := conn.Repositories.ListInvitations(context.TODO(), owner, repoName, nil) + if err != nil { + return err + } + if len(invites) != 1 { + return fmt.Errorf("expected an invite for %s but not found", testAccConf.testExternalUser) + } + if invites[0].GetInvitee().GetLogin() != testAccConf.testExternalUser2 { + return fmt.Errorf("expected an invite for %s for repo %s/%s", testAccConf.testExternalUser, owner, repoName) + } + perm := getPermission(invites[0].GetPermissions()) + if perm != "pull" { + return fmt.Errorf("expected the invite for %s to have pull perms for for %s/%s, found %s", testAccConf.testExternalUser, owner, repoName, perm) + } + return nil + }, + ), + }, + }, + }) + }) + + t.Run("updates team collaborators without error", func(t *testing.T) { + if len(testAccConf.testExternalUser2) == 0 { + t.Skip("No additional external user provided") + } - orgConfig := fmt.Sprintf(` + conn := meta.v3client + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repoName := fmt.Sprintf("tf-acc-test-%s", randomID) + + config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" auto_init = true @@ -265,9 +316,9 @@ func TestAccGithubRepositoryCollaborators(t *testing.T) { permission = "pull" } } - `, repoName, inOrgUser, inOrgUser2) + `, repoName, testAccConf.testExternalUser, testAccConf.testExternalUser2) - orgConfigUpdate := fmt.Sprintf(` + configUpdate := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" auto_init = true @@ -294,128 +345,78 @@ func TestAccGithubRepositoryCollaborators(t *testing.T) { permission = "push" } } - `, repoName, inOrgUser) - - testCase := func(t *testing.T, mode, config, configUpdate string, testCheck func(state *terraform.State) error) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - Config: configUpdate, - Check: testCheck, - }, + `, repoName, testAccConf.testExternalUser) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "user.#"), - resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "team.#"), - resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "user.#", "1"), - resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "team.#", "0"), - func(state *terraform.State) error { - owner := meta.(*Owner).name - - collaborators := state.RootModule().Resources["github_repository_collaborators.test_repo_collaborators"].Primary - for name, val := range collaborators.Attributes { - if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".username") && val != inOrgUser2 { - return fmt.Errorf("expected user.*.username to be set to %s, was %s", inOrgUser, val) - } - if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".permission") && val != "pull" { - return fmt.Errorf("expected user.*.permission to be set to pull, was %s", val) - } - } - - invites, _, err := conn.Repositories.ListInvitations(context.TODO(), owner, repoName, nil) - if err != nil { - return err - } - if len(invites) != 1 { - return fmt.Errorf("expected an invite for %s but not found", inOrgUser) - } - if invites[0].GetInvitee().GetLogin() != inOrgUser2 { - return fmt.Errorf("expected an invite for %s for repo %s/%s", inOrgUser, owner, repoName) - } - perm := getPermission(invites[0].GetPermissions()) - if perm != "pull" { - return fmt.Errorf("expected the invite for %s to have pull perms for for %s/%s, found %s", inOrgUser, owner, repoName, perm) - } - return nil + { + Config: configUpdate, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "user.#"), + resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "team.#"), + resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "user.#", "1"), + resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "team.#", "1"), + func(state *terraform.State) error { + owner := testAccConf.owner + + teamAttrs := state.RootModule().Resources["github_team.test"].Primary.Attributes + collaborators := state.RootModule().Resources["github_repository_collaborators.test_repo_collaborators"].Primary + for name, val := range collaborators.Attributes { + if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".username") && val != testAccConf.testExternalUser { + return fmt.Errorf("expected user.*.username to be set to %s, was %s", testAccConf.testExternalUser, val) + } + if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".permission") && val != "push" { + return fmt.Errorf("expected user.*.permission to be set to push, was %s", val) + } + if strings.HasPrefix(name, "team.") && strings.HasSuffix(name, ".team_id") && val != teamAttrs["id"] { + return fmt.Errorf("expected team.*.team_id to be set to %s, was %s", teamAttrs["id"], val) + } + if strings.HasPrefix(name, "team.") && strings.HasSuffix(name, ".permission") && val != "push" { + return fmt.Errorf("expected team.*.permission to be set to push, was %s", val) + } + } + + users, _, err := conn.Repositories.ListCollaborators(context.TODO(), owner, repoName, &github.ListCollaboratorsOptions{Affiliation: "direct"}) + if err != nil { + return err + } + if len(users) != 1 { + return fmt.Errorf("expected %s to be a collaborator for repo %s/%s", testAccConf.testExternalUser, owner, repoName) + } + perm := getPermission(users[0].GetRoleName()) + if perm != "push" { + return fmt.Errorf("expected %s to have push perms for repo %s/%s, found %s", testAccConf.testExternalUser, owner, repoName, perm) + } + teams, _, err := conn.Repositories.ListTeams(context.TODO(), owner, repoName, nil) + if err != nil { + return err + } + if len(teams) != 1 { + return fmt.Errorf("expected team %s to be a collaborator for %s/%s", repoName, owner, repoName) + } + perm = getPermission(teams[0].GetPermission()) + if perm != "push" { + return fmt.Errorf("expected team %s to have push perms for repo %s/%s, found %s", repoName, owner, repoName, perm) + } + return nil + }, + ), }, - ) - testCase(t, individual, individualConfig, individualConfigUpdate, check) - }) - - t.Run("with an organization account", func(t *testing.T) { - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "user.#"), - resource.TestCheckResourceAttrSet("github_repository_collaborators.test_repo_collaborators", "team.#"), - resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "user.#", "1"), - resource.TestCheckResourceAttr("github_repository_collaborators.test_repo_collaborators", "team.#", "1"), - func(state *terraform.State) error { - owner := testOrganizationFunc() - - teamAttrs := state.RootModule().Resources["github_team.test"].Primary.Attributes - collaborators := state.RootModule().Resources["github_repository_collaborators.test_repo_collaborators"].Primary - for name, val := range collaborators.Attributes { - if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".username") && val != inOrgUser { - return fmt.Errorf("expected user.*.username to be set to %s, was %s", inOrgUser, val) - } - if strings.HasPrefix(name, "user.") && strings.HasSuffix(name, ".permission") && val != "push" { - return fmt.Errorf("expected user.*.permission to be set to push, was %s", val) - } - if strings.HasPrefix(name, "team.") && strings.HasSuffix(name, ".team_id") && val != teamAttrs["id"] { - return fmt.Errorf("expected team.*.team_id to be set to %s, was %s", teamAttrs["id"], val) - } - if strings.HasPrefix(name, "team.") && strings.HasSuffix(name, ".permission") && val != "push" { - return fmt.Errorf("expected team.*.permission to be set to push, was %s", val) - } - } - - users, _, err := conn.Repositories.ListCollaborators(context.TODO(), owner, repoName, &github.ListCollaboratorsOptions{Affiliation: "direct"}) - if err != nil { - return err - } - if len(users) != 1 { - return fmt.Errorf("expected %s to be a collaborator for repo %s/%s", inOrgUser, owner, repoName) - } - perm := getPermission(users[0].GetRoleName()) - if perm != "push" { - return fmt.Errorf("expected %s to have push perms for repo %s/%s, found %s", inOrgUser, owner, repoName, perm) - } - teams, _, err := conn.Repositories.ListTeams(context.TODO(), owner, repoName, nil) - if err != nil { - return err - } - if len(teams) != 1 { - return fmt.Errorf("expected team %s to be a collaborator for %s/%s", repoName, owner, repoName) - } - perm = getPermission(teams[0].GetPermission()) - if perm != "push" { - return fmt.Errorf("expected team %s to have push perms for repo %s/%s, found %s", repoName, owner, repoName, perm) - } - return nil - }, - ) - testCase(t, organization, orgConfig, orgConfigUpdate, check) + }, }) }) - t.Run("removes collaborators without error", func(t *testing.T) { - - conn := meta.(*Owner).v3client + t.Run("removes user collaborators without error", func(t *testing.T) { + conn := meta.v3client + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) repoName := fmt.Sprintf("tf-acc-test-%s", randomID) - individualConfig := fmt.Sprintf(` + config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" auto_init = true @@ -430,9 +431,9 @@ func TestAccGithubRepositoryCollaborators(t *testing.T) { permission = "push" } } - `, repoName, inOrgUser) + `, repoName, testAccConf.testExternalUser) - individualConfigUpdate := fmt.Sprintf(` + configUpdate := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" auto_init = true @@ -440,7 +441,44 @@ func TestAccGithubRepositoryCollaborators(t *testing.T) { } `, repoName) - orgConfig := fmt.Sprintf(` + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + }, + { + Config: configUpdate, + Check: resource.ComposeTestCheckFunc( + func(state *terraform.State) error { + owner := testAccConf.owner + + invites, _, err := conn.Repositories.ListInvitations(context.TODO(), owner, repoName, nil) + if err != nil { + return err + } + if len(invites) != 0 { + return fmt.Errorf("expected no invites but not found %d", len(invites)) + } + return nil + }, + ), + }, + }, + }) + }) + + t.Run("removes team collaborators without error", func(t *testing.T) { + if len(testAccConf.testExternalUser2) == 0 { + t.Skip("No additional external user provided") + } + + conn := meta.v3client + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repoName := fmt.Sprintf("tf-acc-test-%s", randomID) + + config := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" auto_init = true @@ -467,9 +505,9 @@ func TestAccGithubRepositoryCollaborators(t *testing.T) { permission = "pull" } } - `, repoName, inOrgUser, inOrgUser2) + `, repoName, testAccConf.testExternalUser, testAccConf.testExternalUser2) - orgConfigUpdate := fmt.Sprintf(` + configUpdate := fmt.Sprintf(` resource "github_repository" "test" { name = "%s" auto_init = true @@ -481,67 +519,38 @@ func TestAccGithubRepositoryCollaborators(t *testing.T) { } `, repoName) - testCase := func(t *testing.T, mode, config, configUpdate string, testCheck func(state *terraform.State) error) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - }, - { - Config: configUpdate, - Check: testCheck, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - check := resource.ComposeTestCheckFunc( - func(state *terraform.State) error { - owner := meta.(*Owner).name - - invites, _, err := conn.Repositories.ListInvitations(context.TODO(), owner, repoName, nil) - if err != nil { - return err - } - if len(invites) != 0 { - return fmt.Errorf("expected no invites but not found %d", len(invites)) - } - return nil - }, - ) - testCase(t, individual, individualConfig, individualConfigUpdate, check) - }) - - t.Run("with an organization account", func(t *testing.T) { - check := resource.ComposeTestCheckFunc( - func(state *terraform.State) error { - owner := testOrganizationFunc() - - users, _, err := conn.Repositories.ListCollaborators(context.TODO(), owner, repoName, &github.ListCollaboratorsOptions{Affiliation: "direct"}) - if err != nil { - return err - } - if len(users) != 0 { - return fmt.Errorf("expected no collaborators for repo %s/%s but found %d", owner, repoName, len(users)) - } - teams, _, err := conn.Repositories.ListTeams(context.TODO(), owner, repoName, nil) - if err != nil { - return err - } - if len(teams) != 0 { - return fmt.Errorf("expected no teams to be a collaborator for %s/%s but found %d", owner, repoName, len(teams)) - } - return nil + { + Config: configUpdate, + Check: resource.ComposeTestCheckFunc( + func(state *terraform.State) error { + owner := testAccConf.owner + + users, _, err := conn.Repositories.ListCollaborators(context.TODO(), owner, repoName, &github.ListCollaboratorsOptions{Affiliation: "direct"}) + if err != nil { + return err + } + if len(users) != 0 { + return fmt.Errorf("expected no collaborators for repo %s/%s but found %d", owner, repoName, len(users)) + } + teams, _, err := conn.Repositories.ListTeams(context.TODO(), owner, repoName, nil) + if err != nil { + return err + } + if len(teams) != 0 { + return fmt.Errorf("expected no teams to be a collaborator for %s/%s but found %d", owner, repoName, len(teams)) + } + return nil + }, + ), }, - ) - testCase(t, organization, orgConfig, orgConfigUpdate, check) + }, }) }) } diff --git a/github/resource_github_repository_automated_security_fixes.go b/github/resource_github_repository_dependabot_security_updates.go similarity index 100% rename from github/resource_github_repository_automated_security_fixes.go rename to github/resource_github_repository_dependabot_security_updates.go diff --git a/github/resource_github_repository_automated_security_fixes_test.go b/github/resource_github_repository_dependabot_security_updates_test.go similarity index 54% rename from github/resource_github_repository_automated_security_fixes_test.go rename to github/resource_github_repository_dependabot_security_updates_test.go index 2d7e5322fd..7308473b97 100644 --- a/github/resource_github_repository_automated_security_fixes_test.go +++ b/github/resource_github_repository_dependabot_security_updates_test.go @@ -9,12 +9,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -func TestAccGithubAutomatedSecurityFixes(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - +func TestAccGithubRepositoryDependabotSecurityUpdates(t *testing.T) { t.Run("enables automated security fixes without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) enabled := "enabled = false" updatedEnabled := "enabled = true" config := fmt.Sprintf(` @@ -48,40 +45,26 @@ func TestAccGithubAutomatedSecurityFixes(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - enabled, - updatedEnabled, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + enabled, + updatedEnabled, 1), + Check: checks["after"], + }, + }, }) }) t.Run("disables automated security fixes without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) enabled := "enabled = true" updatedEnabled := "enabled = false" @@ -116,39 +99,26 @@ func TestAccGithubAutomatedSecurityFixes(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - enabled, - updatedEnabled, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + enabled, + updatedEnabled, 1), + Check: checks["after"], + }, + }, }) }) t.Run("imports automated security fixes without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -167,34 +137,20 @@ func TestAccGithubAutomatedSecurityFixes(t *testing.T) { resource.TestCheckResourceAttr("github_repository_dependabot_security_updates.test", "enabled", "false"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_repository_dependabot_security_updates.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_repository_dependabot_security_updates.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) } diff --git a/github/resource_github_repository_deploy_key_test.go b/github/resource_github_repository_deploy_key_test.go index 68a40b5199..a368e2c441 100644 --- a/github/resource_github_repository_deploy_key_test.go +++ b/github/resource_github_repository_deploy_key_test.go @@ -3,11 +3,11 @@ package github import ( "context" "fmt" - "os" "os/exec" "path/filepath" "regexp" "strconv" + "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" @@ -49,57 +49,53 @@ func TestSuppressDeployKeyDiff(t *testing.T) { i+1, tcCount, tc.OldValue, tc.NewValue) } } - } func TestAccGithubRepositoryDeployKey_basic(t *testing.T) { - testUserEmail := os.Getenv("GITHUB_TEST_USER_EMAIL") - if testUserEmail == "" { - t.Skip("Skipping because `GITHUB_TEST_USER_EMAIL` is not set") - } - cmd := exec.Command("bash", "-c", fmt.Sprintf("ssh-keygen -t rsa -b 4096 -C %s -N '' -f test-fixtures/id_rsa>/dev/null <<< y >/dev/null", testUserEmail)) - if err := cmd.Run(); err != nil { - t.Fatal(err) - } + t.Run("creates repository deploy key without error", func(t *testing.T) { + cmd := exec.Command("bash", "-c", "ssh-keygen -t rsa -b 4096 -C test@example.com -N '' -f test-fixtures/id_rsa>/dev/null <<< y >/dev/null") + if err := cmd.Run(); err != nil { + t.Fatal(err) + } - rn := "github_repository_deploy_key.test_repo_deploy_key" - rs := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) - repositoryName := fmt.Sprintf("acctest-%s", rs) - keyPath := filepath.Join("test-fixtures", "id_rsa.pub") - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubRepositoryDeployKeyDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubRepositoryDeployKeyConfig(repositoryName, keyPath), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubRepositoryDeployKeyExists(rn), - resource.TestCheckResourceAttr(rn, "read_only", "false"), - resource.TestCheckResourceAttr(rn, "repository", repositoryName), - resource.TestMatchResourceAttr(rn, "key", regexp.MustCompile(`^ssh-rsa [^\s]+$`)), - resource.TestCheckResourceAttr(rn, "title", "title"), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateVerify: true, + rn := "github_repository_deploy_key.test_repo_deploy_key" + rs := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) + repositoryName := fmt.Sprintf("acctest-%s", rs) + keyPath := strings.ReplaceAll(filepath.Join("test-fixtures", "id_rsa.pub"), "\\", "/") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubRepositoryDeployKeyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubRepositoryDeployKeyConfig(repositoryName, keyPath), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubRepositoryDeployKeyExists(rn), + resource.TestCheckResourceAttr(rn, "read_only", "false"), + resource.TestCheckResourceAttr(rn, "repository", repositoryName), + resource.TestMatchResourceAttr(rn, "key", regexp.MustCompile(`^ssh-rsa [^\s]+$`)), + resource.TestCheckResourceAttr(rn, "title", "title"), + ), + }, }, - }, + }) }) } func testAccCheckGithubRepositoryDeployKeyDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*Owner).v3client + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client for _, rs := range s.RootModule().Resources { if rs.Type != "github_repository_deploy_key" { continue } - owner := testAccProvider.Meta().(*Owner).name + owner := meta.name repoName, idString, err := parseTwoPartID(rs.Primary.ID, "repository", "ID") if err != nil { return err @@ -132,8 +128,12 @@ func testAccCheckGithubRepositoryDeployKeyExists(n string) resource.TestCheckFun return fmt.Errorf("no membership ID is set") } - conn := testAccProvider.Meta().(*Owner).v3client - owner := testAccProvider.Meta().(*Owner).name + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + owner := meta.name repoName, idString, err := parseTwoPartID(rs.Primary.ID, "repository", "ID") if err != nil { return err diff --git a/github/resource_github_repository_deployment_branch_policy_test.go b/github/resource_github_repository_deployment_branch_policy_test.go index 083529336f..bdbd013be2 100644 --- a/github/resource_github_repository_deployment_branch_policy_test.go +++ b/github/resource_github_repository_deployment_branch_policy_test.go @@ -9,11 +9,8 @@ import ( ) func TestAccGithubRepositoryDeploymentBranchPolicy(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates deployment branch policy", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -62,34 +59,19 @@ func TestAccGithubRepositoryDeploymentBranchPolicy(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config + config1, - Check: check1, - }, - { - Config: config + config2, - Check: check2, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config + config1, + Check: check1, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config + config2, + Check: check2, + }, + }, }) - }) } diff --git a/github/resource_github_repository_environment_deployment_policy_test.go b/github/resource_github_repository_environment_deployment_policy_test.go index 61d8554703..98c3910885 100644 --- a/github/resource_github_repository_environment_deployment_policy_test.go +++ b/github/resource_github_repository_environment_deployment_policy_test.go @@ -9,12 +9,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -func TestAccGithubRepositoryEnvironmentDeploymentPolicyBranch(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - +func TestAccGithubRepositoryEnvironmentDeploymentPolicy(t *testing.T) { t.Run("creates a repository environment with branch-based deployment policy", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` data "github_user" "current" { @@ -65,39 +62,20 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicyBranch(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) -} - -func TestAccGithubRepositoryEnvironmentDeploymentPolicyBranchUpdate(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("updates the pattern for a branch-based deployment policy", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) var deploymentPolicyId string config1 := fmt.Sprintf(` @@ -205,44 +183,24 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicyBranchUpdate(t *testing.T ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config1, - Check: check1, - }, - { - Config: config2, - Check: check2, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config1, + Check: check1, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check2, + }, + }, }) - }) -} - -func TestAccGithubRepositoryEnvironmentDeploymentPolicyTag(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("creates a repository environment with tag-based deployment policy", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` data "github_user" "current" { @@ -293,40 +251,21 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicyTag(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) -} - -func TestAccGithubRepositoryEnvironmentDeploymentPolicyTagUpdate(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("updates the pattern for a tag-based deployment policy", func(t *testing.T) { var deploymentPolicyId string + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config1 := fmt.Sprintf(` @@ -433,44 +372,25 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicyTagUpdate(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config1, - Check: check1, - }, - { - Config: config2, - Check: check2, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config1, + Check: check1, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check2, + }, + }, }) - }) -} - -func TestAccGithubRepositoryEnvironmentDeploymentPolicyBranchToTagUpdate(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("recreates deployment policy when pattern type changes from branch to tag", func(t *testing.T) { var deploymentPolicyId string + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config1 := fmt.Sprintf(` @@ -577,44 +497,25 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicyBranchToTagUpdate(t *test ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config1, - Check: check1, - }, - { - Config: config2, - Check: check2, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config1, + Check: check1, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check2, + }, + }, }) - }) -} - -func TestAccGithubRepositoryEnvironmentDeploymentPolicyTagToBranchUpdate(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("recreates deployment policy when pattern type changes from tag to branch", func(t *testing.T) { var deploymentPolicyId string + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config1 := fmt.Sprintf(` @@ -721,35 +622,20 @@ func TestAccGithubRepositoryEnvironmentDeploymentPolicyTagToBranchUpdate(t *test ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config1, - Check: check1, - }, - { - Config: config2, - Check: check2, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config1, + Check: check1, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check2, + }, + }, }) - }) } diff --git a/github/resource_github_repository_environment_test.go b/github/resource_github_repository_environment_test.go index 6198b6b102..addcfeb42d 100644 --- a/github/resource_github_repository_environment_test.go +++ b/github/resource_github_repository_environment_test.go @@ -9,11 +9,8 @@ import ( ) func TestAccGithubRepositoryEnvironment(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates a repository environment", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` data "github_user" "current" { @@ -49,30 +46,15 @@ func TestAccGithubRepositoryEnvironment(t *testing.T) { resource.TestCheckResourceAttr("github_repository_environment.test", "wait_timer", "10000"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/resource_github_repository_file_test.go b/github/resource_github_repository_file_test.go index 0a2da9aa5a..c0d0033dfe 100644 --- a/github/resource_github_repository_file_test.go +++ b/github/resource_github_repository_file_test.go @@ -11,19 +11,16 @@ import ( ) func TestAccGithubRepositoryFile(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates and manages files", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - + resource "github_repository" "test" { name = "tf-acc-test-%s" auto_init = true vulnerability_alerts = true } - + resource "github_repository_file" "test" { repository = github_repository.test.name branch = "main" @@ -64,35 +61,20 @@ func TestAccGithubRepositoryFile(t *testing.T) { resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("can be configured to overwrite files on create", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -139,41 +121,26 @@ func TestAccGithubRepositoryFile(t *testing.T) { resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile(`refusing to overwrite existing file`), - }, - { - Config: strings.Replace(config, - "overwrite_on_create = false", - "overwrite_on_create = true", 1), - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile(`refusing to overwrite existing file`), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + "overwrite_on_create = false", + "overwrite_on_create = true", 1), + Check: check, + }, + }, }) - }) t.Run("creates and manages files on default branch if branch is omitted", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -234,42 +201,27 @@ func TestAccGithubRepositoryFile(t *testing.T) { resource.TestCheckNoResourceAttr("github_repository_file.test", "autocreate_branch_source_sha"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("creates and manages files on auto created branch if branch does not exist", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" auto_init = true vulnerability_alerts = true } - + resource "github_repository_file" "test" { repository = github_repository.test.name branch = "does/not/exist" @@ -312,36 +264,21 @@ func TestAccGithubRepositoryFile(t *testing.T) { resource.TestCheckResourceAttrSet("github_repository_file.test", "autocreate_branch_source_sha"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - ExpectError: regexp.MustCompile(`unexpected status code: 404 Not Found`), - }, - { - Config: strings.Replace(config, - "autocreate_branch = false", - "autocreate_branch = true", 1), - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + ExpectError: regexp.MustCompile(`unexpected status code: 404 Not Found`), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + "autocreate_branch = false", + "autocreate_branch = true", 1), + Check: check, + }, + }, }) - }) } diff --git a/github/resource_github_repository_milestone_test.go b/github/resource_github_repository_milestone_test.go index ae4bfe2abc..ebede46eed 100644 --- a/github/resource_github_repository_milestone_test.go +++ b/github/resource_github_repository_milestone_test.go @@ -9,11 +9,8 @@ import ( ) func TestAccGithubRepositoryMilestone(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates a repository milestone", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -38,30 +35,15 @@ func TestAccGithubRepositoryMilestone(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) } diff --git a/github/resource_github_repository_project.go b/github/resource_github_repository_project.go index 278900c7fe..61976b15cd 100644 --- a/github/resource_github_repository_project.go +++ b/github/resource_github_repository_project.go @@ -14,6 +14,8 @@ import ( func resourceGithubRepositoryProject() *schema.Resource { return &schema.Resource{ + DeprecationMessage: "This resource is deprecated as classic projects have been removed and this resource will no longer work. This resource will be removed in the next major version.", + Create: resourceGithubRepositoryProjectCreate, Read: resourceGithubRepositoryProjectRead, Update: resourceGithubRepositoryProjectUpdate, diff --git a/github/resource_github_repository_project_test.go b/github/resource_github_repository_project_test.go index 4257011495..3c77fc0122 100644 --- a/github/resource_github_repository_project_test.go +++ b/github/resource_github_repository_project_test.go @@ -10,11 +10,10 @@ import ( ) func TestAccGithubRepositoryProject(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + t.Skip("Skipping test as the GitHub API no longer supports classic projects") t.Run("creates a repository project", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -36,30 +35,15 @@ func TestAccGithubRepositoryProject(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) } diff --git a/github/resource_github_repository_pull_request.go b/github/resource_github_repository_pull_request.go index 385824a5cd..46ad1f0477 100644 --- a/github/resource_github_repository_pull_request.go +++ b/github/resource_github_repository_pull_request.go @@ -165,7 +165,6 @@ func resourceGithubRepositoryPullRequestCreate(d *schema.ResourceData, meta inte Body: github.String(d.Get("body").(string)), MaintainerCanModify: github.Bool(d.Get("maintainer_can_modify").(bool)), }) - if err != nil { return err } @@ -296,7 +295,7 @@ func resourceGithubRepositoryPullRequestUpdate(d *schema.ResourceData, meta inte errors = append(errors, fmt.Sprintf("could not read the Pull Request after the failed update: %v", err)) } - return fmt.Errorf(strings.Join(errors, ", ")) + return fmt.Errorf("%s", strings.Join(errors, ", ")) } func resourceGithubRepositoryPullRequestDelete(d *schema.ResourceData, meta interface{}) error { diff --git a/github/resource_github_repository_pull_request_test.go b/github/resource_github_repository_pull_request_test.go index c76a1a99f7..b1f964c7c5 100644 --- a/github/resource_github_repository_pull_request_test.go +++ b/github/resource_github_repository_pull_request_test.go @@ -63,34 +63,20 @@ func TestAccGithubRepositoryPullRequest(t *testing.T) { resource.TestCheckResourceAttrSet(resourceName, "updated_at"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) } diff --git a/github/resource_github_repository_ruleset_test.go b/github/resource_github_repository_ruleset_test.go index a51682507f..74787207d5 100644 --- a/github/resource_github_repository_ruleset_test.go +++ b/github/resource_github_repository_ruleset_test.go @@ -12,11 +12,8 @@ import ( ) func TestGithubRepositoryRulesets(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("Creates and updates repository rulesets without errors", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -88,42 +85,20 @@ func TestGithubRepositoryRulesets(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("Creates and updates repository rulesets with enterprise features without errors", func(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - - if testEnterprise == "" { - t.Skip("Skipping because `ENTERPRISE_SLUG` is not set") - } - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-%s" @@ -170,27 +145,20 @@ func TestGithubRepositoryRulesets(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an enterprise account", func(t *testing.T) { - testCase(t, enterprise) + }, }) - }) t.Run("Updates a ruleset name without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) repoName := fmt.Sprintf(`tf-acc-test-rename-%[1]s`, randomID) oldRSName := fmt.Sprintf(`ruleset-%[1]s`, randomID) newRSName := fmt.Sprintf(`%[1]s-renamed`, randomID) @@ -228,43 +196,28 @@ func TestGithubRepositoryRulesets(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - // Rename the ruleset to something else - Config: strings.Replace( - config, - oldRSName, - newRSName, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + // Rename the ruleset to something else + Config: strings.Replace( + config, + oldRSName, + newRSName, 1), + Check: checks["after"], + }, + }, }) - }) t.Run("Imports rulesets without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-import-%[1]s" @@ -330,40 +283,24 @@ func TestGithubRepositoryRulesets(t *testing.T) { resource.TestCheckResourceAttrSet("github_repository_ruleset.test", "name"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_repository_ruleset.test", - ImportState: true, - ImportStateVerify: true, - ImportStateIdFunc: importRepositoryRulesetByResourcePaths( - "github_repository.test", "github_repository_ruleset.test"), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_repository_ruleset.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdFunc: importRepositoryRulesetByResourcePaths( + "github_repository.test", "github_repository_ruleset.test"), + }, + }, }) - }) - } func importRepositoryRulesetByResourcePaths(repoLogicalName, rulesetLogicalName string) resource.ImportStateIdFunc { diff --git a/github/resource_github_repository_test.go b/github/resource_github_repository_test.go index e09505dac9..8fa2dedd4e 100644 --- a/github/resource_github_repository_test.go +++ b/github/resource_github_repository_test.go @@ -1,9 +1,7 @@ package github import ( - "context" "fmt" - "log" "regexp" "strings" "testing" @@ -18,11 +16,8 @@ import ( ) func TestAccGithubRepositories(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates and updates repositories without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { @@ -66,35 +61,20 @@ func TestAccGithubRepositories(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("updates a repositories name without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) oldName := fmt.Sprintf(`tf-acc-test-rename-%[1]s`, randomID) newName := fmt.Sprintf(`%[1]s-renamed`, oldName) @@ -128,43 +108,28 @@ func TestAccGithubRepositories(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - // Rename the repo to something else - Config: strings.Replace( - config, - oldName, - newName, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + // Rename the repo to something else + Config: strings.Replace( + config, + oldName, + newName, 1), + Check: checks["after"], + }, + }, }) - }) t.Run("imports repositories without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-import-%[1]s" @@ -177,40 +142,25 @@ func TestAccGithubRepositories(t *testing.T) { resource.TestCheckResourceAttrSet("github_repository.test", "name"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_repository.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_repository.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) - }) t.Run("archives repositories without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-archive-%[1]s" @@ -234,41 +184,26 @@ func TestAccGithubRepositories(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - `archived = false`, - `archived = true`, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + `archived = false`, + `archived = true`, 1), + Check: checks["after"], + }, + }, }) - }) t.Run("manages the project feature for a repository", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-project-%[1]s" @@ -292,41 +227,26 @@ func TestAccGithubRepositories(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - `has_projects = false`, - `has_projects = true`, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + `has_projects = false`, + `has_projects = true`, 1), + Check: checks["after"], + }, + }, }) - }) t.Run("manages the default branch feature for a repository", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-branch-%[1]s" @@ -356,50 +276,35 @@ func TestAccGithubRepositories(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - // Test changing default_branch - { - Config: strings.Replace(config, - `default_branch = "main"`, - `default_branch = "default"`, 1), - Check: checks["after"], - }, - // Test changing default_branch back to main again - { - Config: config, - Check: checks["before"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + // Test changing default_branch + { + Config: strings.Replace(config, + `default_branch = "main"`, + `default_branch = "default"`, 1), + Check: checks["after"], + }, + // Test changing default_branch back to main again + { + Config: config, + Check: checks["before"], + }, + }, }) - }) t.Run("allows setting default_branch on an empty repository", func(t *testing.T) { - // Although default_branch is deprecated, for backwards compatibility // we allow setting it to "main". + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-empty-%[1]s" @@ -415,44 +320,29 @@ func TestAccGithubRepositories(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - // Test creation with default_branch set - { - Config: config, - Check: check, - }, - // Test that changing another property does not try to set - // default_branch (which would crash). - { - Config: strings.Replace(config, - `acceptance tests`, - `acceptance test`, 1), - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + // Test creation with default_branch set + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + // Test that changing another property does not try to set + // default_branch (which would crash). + { + Config: strings.Replace(config, + `acceptance tests`, + `acceptance test`, 1), + Check: check, + }, + }, }) - }) t.Run("manages the license and gitignore feature for a repository", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-license-%[1]s" @@ -473,35 +363,20 @@ func TestAccGithubRepositories(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) t.Run("configures topics for a repository", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-topic-%[1]s" @@ -517,35 +392,20 @@ func TestAccGithubRepositories(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - t.Run("creates a repository using a template", func(t *testing.T) { - + t.Run("creates a repository using a public template", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-template-%s" @@ -557,7 +417,7 @@ func TestAccGithubRepositories(t *testing.T) { } } - `, randomID, testOrganization, "terraform-template-module") + `, randomID, testAccConf.testPublicTemplateRepositoryOwner, testAccConf.testPublicTemplateRepository) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -566,35 +426,54 @@ func TestAccGithubRepositories(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) + }) - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) + t.Run("creates a repository using an org template", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "tf-acc-test-template-%s" + description = "Terraform acceptance tests %[1]s" - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) + template { + owner = "%s" + repository = "%s" + } + + } + `, randomID, testAccConf.owner, testAccConf.testOrgTemplateRepository) + + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.test", "is_template", + "false", + ), + ) + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, + }) }) t.Run("archives repositories on destroy", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-destroy-%[1]s" @@ -619,164 +498,110 @@ func TestAccGithubRepositories(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - `archived = false`, - `archived = true`, 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + `archived = false`, + `archived = true`, 1), + Check: checks["after"], + }, + }, }) - }) - t.Run("configures vulnerability alerts", func(t *testing.T) { + t.Run("configures vulnerability alerts for a public repository", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repoName := fmt.Sprintf("tf-acc-test-pub-vuln-%s", randomID) - t.Run("for a public repository", func(t *testing.T) { + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "%s" + visibility = "public" + } + `, repoName) - config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "tf-acc-test-pub-vuln-%s" - visibility = "public" - } - `, randomID) + configUpdate := fmt.Sprintf(` + resource "github_repository" "test" { + name = "%s" + visibility = "public" - checks := map[string]resource.TestCheckFunc{ - "before": resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_repository.test", "vulnerability_alerts", - "false", - ), - ), - "after": resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_repository.test", "vulnerability_alerts", - "true", + vulnerability_alerts = false + } + `, repoName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_repository.test", "visibility", "public"), + resource.TestCheckResourceAttr("github_repository.test", "vulnerability_alerts", "true"), ), - resource.TestCheckResourceAttr( - "github_repository.test", "visibility", - "public", + }, + { + Config: configUpdate, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_repository.test", "visibility", "public"), + resource.TestCheckResourceAttr("github_repository.test", "vulnerability_alerts", "false"), ), - ), - } - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - `}`, - "vulnerability_alerts = true\n}", 1), - Check: checks["after"], - }, - }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) + }, + }, }) + }) - t.Run("for a private repository", func(t *testing.T) { + t.Run("configures vulnerability alerts for a private repository", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + repoName := fmt.Sprintf("tf-acc-test-prv-vuln-%s", randomID) - config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "tf-acc-test-prv-vuln-%s" - visibility = "private" - } - `, randomID) + config := fmt.Sprintf(` + resource "github_repository" "test" { + name = "%s" + visibility = "private" + } + `, repoName) - checks := map[string]resource.TestCheckFunc{ - "before": resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_repository.test", "vulnerability_alerts", - "false", - ), - ), - "after": resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_repository.test", "vulnerability_alerts", - "true", + configUpdate := fmt.Sprintf(` + resource "github_repository" "test" { + name = "%s" + visibility = "private" + + vulnerability_alerts = true + } + `, repoName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_repository.test", "visibility", "private"), + resource.TestCheckResourceAttr("github_repository.test", "vulnerability_alerts", "false"), ), - resource.TestCheckResourceAttr( - "github_repository.test", "visibility", - "private", + }, + { + Config: configUpdate, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_repository.test", "visibility", "private"), + resource.TestCheckResourceAttr("github_repository.test", "vulnerability_alerts", "true"), ), - ), - } - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - `}`, - "vulnerability_alerts = true\n}", 1), - Check: checks["after"], - }, - }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) + }, + }, }) - }) t.Run("create and modify merge commit strategy without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) mergeCommitTitle := "PR_TITLE" mergeCommitMessage := "BLANK" updatedMergeCommitTitle := "MERGE_MESSAGE" @@ -825,37 +650,24 @@ func TestAccGithubRepositories(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: configs["before"], - Check: checks["before"], - }, - { - Config: configs["after"], - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: configs["before"], + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configs["after"], + Check: checks["after"], + }, + }, }) }) t.Run("create and modify squash merge commit strategy without error", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) squashMergeCommitTitle := "PR_TITLE" squashMergeCommitMessage := "PR_BODY" updatedSquashMergeCommitTitle := "COMMIT_OR_PR_TITLE" @@ -903,37 +715,24 @@ func TestAccGithubRepositories(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: configs["before"], - Check: checks["before"], - }, - { - Config: configs["after"], - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: configs["before"], + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configs["after"], + Check: checks["after"], + }, + }, }) }) t.Run("create a repository with go as primary_language", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-%s" @@ -953,50 +752,32 @@ func TestAccGithubRepositories(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - // Not doing any checks since the file needs to be created before the language can be updated - Config: config, - }, - { - // Re-running the terraform will refresh the language since the go-file has been created - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + // Not doing any checks since the file needs to be created before the language can be updated + Config: config, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + // Re-running the terraform will refresh the language since the go-file has been created + Config: config, + Check: check, + }, + }, }) - }) -} -func TestAccGithubRepositoryPages(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("manages the legacy pages feature for a repository", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-%s" auto_init = true pages { + build_type = "legacy" + source { branch = "main" } @@ -1004,139 +785,54 @@ func TestAccGithubRepositoryPages(t *testing.T) { } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_repository.test", "pages.0.source.0.branch", - "main", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_repository.test", "pages.0.source.0.branch", "main"), + resource.TestCheckResourceAttr("github_repository.test", "pages.0.source.0.path", "/"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("manages the pages from workflow feature for a repository", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` - resource "github_repository" "test" { - name = "tf-acc-%s" - auto_init = true - pages { - build_type = "workflow" - } + resource "github_repository" "test" { + name = "tf-acc-%s" + auto_init = true + pages { + build_type = "workflow" } + } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_repository.test", "pages.0.source.0.branch", - "main", - ), - ) - - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckNoResourceAttr("github_repository.test", "pages.0.source.#"), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) - t.Run("expand Pages configuration with workflow", func(t *testing.T) { - input := []interface{}{map[string]interface{}{ - "build_type": "workflow", - "source": []interface{}{map[string]interface{}{}}, - }} - - pages := expandPages(input) - if pages == nil { - t.Fatal("pages is nil") - } - if pages.GetBuildType() != "workflow" { - t.Errorf("got %q; want %q", pages.GetBuildType(), "workflow") - } - if pages.GetSource().GetBranch() != "main" { - t.Errorf("got %q; want %q", pages.GetSource().GetBranch(), "main") + t.Run("manages the security feature for a private repository", func(t *testing.T) { + if !testAccConf.testAdvancedSecurity { + t.Skip("Advanced Security is not enabled for this account") } - }) - t.Run("expand Pages configuration with source", func(t *testing.T) { - input := []interface{}{map[string]interface{}{ - "build_type": "legacy", - "source": []interface{}{map[string]interface{}{ - "branch": "main", - "path": "/docs", - }}, - }} - - pages := expandPages(input) - if pages == nil { - t.Fatal("pages is nil") - } - if pages.GetBuildType() != "legacy" { - t.Errorf("got %q; want %q", pages.GetBuildType(), "legacy") - } - if pages.GetSource().GetBranch() != "main" { - t.Errorf("got %q; want %q", pages.GetSource().GetBranch(), "main") - } - if pages.GetSource().GetPath() != "/docs" { - t.Errorf("got %q; want %q", pages.GetSource().GetPath(), "/docs") - } - }) -} - -func TestAccGithubRepositorySecurity(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - - t.Run("manages the security feature for a repository", func(t *testing.T) { - - t.Run("for a private repository", func(t *testing.T) { - t.Skip("organization/individual must have purchased Advanced Security in order to enable it") - - config := fmt.Sprintf(` + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-%s" description = "A repository created by Terraform to test security features" @@ -1155,48 +851,35 @@ func TestAccGithubRepositorySecurity(t *testing.T) { } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.advanced_security.0.status", - "enabled", - ), - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.secret_scanning.0.status", - "enabled", - ), - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.secret_scanning_push_protection.0.status", - "disabled", - ), - ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - }, - }) - } - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.test", "security_and_analysis.0.advanced_security.0.status", + "enabled", + ), + resource.TestCheckResourceAttr( + "github_repository.test", "security_and_analysis.0.secret_scanning.0.status", + "enabled", + ), + resource.TestCheckResourceAttr( + "github_repository.test", "security_and_analysis.0.secret_scanning_push_protection.0.status", + "disabled", + ), + ) + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, }) + }) - t.Run("for a public repository", func(t *testing.T) { - - config := fmt.Sprintf(` + t.Run("manages the security feature for a public repository", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-%s" description = "A repository created by Terraform to test security features" @@ -1213,50 +896,30 @@ func TestAccGithubRepositorySecurity(t *testing.T) { } `, randomID) - check := resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.secret_scanning.0.status", - "enabled", - ), - resource.TestCheckResourceAttr( - "github_repository.test", "security_and_analysis.0.secret_scanning_push_protection.0.status", - "disabled", - ), - ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) + check := resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "github_repository.test", "security_and_analysis.0.secret_scanning.0.status", + "enabled", + ), + resource.TestCheckResourceAttr( + "github_repository.test", "security_and_analysis.0.secret_scanning_push_protection.0.status", + "disabled", + ), + ) + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, + }, + }, }) }) -} - -func TestAccGithubRepositoryVisibility(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) t.Run("creates repos with private visibility", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "private" { name = "tf-acc-test-visibility-private-%s" @@ -1271,35 +934,20 @@ func TestAccGithubRepositoryVisibility(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("creates repos with internal visibility", func(t *testing.T) { - t.Skip("organization used in automated tests does not support internal repositories") - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "internal" { name = "tf-acc-test-visibility-internal-%s" @@ -1314,34 +962,20 @@ func TestAccGithubRepositoryVisibility(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) }) t.Run("updates repos to private visibility", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "public" { name = "tf-acc-test-visibility-public-%s" @@ -1365,38 +999,24 @@ func TestAccGithubRepositoryVisibility(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: reconfigureVisibility(config, "private"), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: reconfigureVisibility(config, "private"), + Check: checks["after"], + }, + }, }) }) t.Run("updates repos to public visibility", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-prv-vuln-%s" @@ -1423,40 +1043,26 @@ func TestAccGithubRepositoryVisibility(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - `}`, - "vulnerability_alerts = true\n}", 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + `}`, + "vulnerability_alerts = true\n}", 1), + Check: checks["after"], + }, + }, }) }) t.Run("updates repos to internal visibility", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "tf-acc-test-prv-vuln-%s" @@ -1483,40 +1089,26 @@ func TestAccGithubRepositoryVisibility(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["before"], - }, - { - Config: strings.Replace(config, - `}`, - "vulnerability_alerts = true\n}", 1), - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + `}`, + "vulnerability_alerts = true\n}", 1), + Check: checks["after"], + }, + }, }) }) t.Run("sets private visibility for repositories created by a template", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "private" { name = "tf-acc-test-visibility-private-%s" @@ -1526,7 +1118,7 @@ func TestAccGithubRepositoryVisibility(t *testing.T) { repository = "%s" } } - `, randomID, testOrganization, "terraform-template-module") + `, randomID, testAccConf.testPublicTemplateRepositoryOwner, testAccConf.testPublicTemplateRepository) check := resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr( @@ -1539,32 +1131,61 @@ func TestAccGithubRepositoryVisibility(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) + }) +} - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) +func Test_expandPages(t *testing.T) { + t.Run("expand Pages configuration with workflow", func(t *testing.T) { + input := []interface{}{map[string]interface{}{ + "build_type": "workflow", + "source": []interface{}{map[string]interface{}{}}, + }} - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) + pages := expandPages(input) + if pages == nil { + t.Fatal("pages is nil") + } + if pages.GetBuildType() != "workflow" { + t.Errorf("got %q; want %q", pages.GetBuildType(), "workflow") + } + if pages.GetSource().GetBranch() != "main" { + t.Errorf("got %q; want %q", pages.GetSource().GetBranch(), "main") + } }) + t.Run("expand Pages configuration with source", func(t *testing.T) { + input := []interface{}{map[string]interface{}{ + "build_type": "legacy", + "source": []interface{}{map[string]interface{}{ + "branch": "main", + "path": "/docs", + }}, + }} + + pages := expandPages(input) + if pages == nil { + t.Fatal("pages is nil") + } + if pages.GetBuildType() != "legacy" { + t.Errorf("got %q; want %q", pages.GetBuildType(), "legacy") + } + if pages.GetSource().GetBranch() != "main" { + t.Errorf("got %q; want %q", pages.GetSource().GetBranch(), "main") + } + if pages.GetSource().GetPath() != "/docs" { + t.Errorf("got %q; want %q", pages.GetSource().GetPath(), "/docs") + } + }) } func TestGithubRepositoryTopicPassesValidation(t *testing.T) { @@ -1591,39 +1212,6 @@ func TestGithubRepositoryTopicFailsValidationWhenOverMaxCharacters(t *testing.T) } } -func testSweepRepositories(region string) error { - meta, err := sharedConfigForRegion(region) - if err != nil { - return err - } - - client := meta.(*Owner).v3client - - repos, _, err := client.Repositories.ListByUser(context.TODO(), meta.(*Owner).name, nil) - if err != nil { - return err - } - - for _, r := range repos { - if name := r.GetName(); strings.HasPrefix(name, "tf-acc-") || strings.HasPrefix(name, "foo-") { - log.Printf("[DEBUG] Destroying Repository %s", name) - - if _, err := client.Repositories.Delete(context.TODO(), meta.(*Owner).name, name); err != nil { - return err - } - } - } - - return nil -} - -func init() { - resource.AddTestSweepers("github_repository", &resource.Sweeper{ - Name: "github_repository", - F: testSweepRepositories, - }) -} - func reconfigureVisibility(config, visibility string) string { re := regexp.MustCompile(`visibility = "(.*)"`) newConfig := re.ReplaceAllString( diff --git a/github/resource_github_repository_topics_test.go b/github/resource_github_repository_topics_test.go index d5f571e202..1548a49a35 100644 --- a/github/resource_github_repository_topics_test.go +++ b/github/resource_github_repository_topics_test.go @@ -30,34 +30,20 @@ func TestAccGithubRepositoryTopics(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "topics.#", "2"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, }) }) @@ -97,33 +83,19 @@ func TestAccGithubRepositoryTopics(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "topics.#", "3"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: configBefore, - Check: checkBefore, - }, - { - Config: configAfter, - Check: checkAfter, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: configBefore, + Check: checkBefore, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configAfter, + Check: checkAfter, + }, + }, }) }) } diff --git a/github/resource_github_repository_webhook_test.go b/github/resource_github_repository_webhook_test.go index 1ba5ab8c8e..89a4669f3e 100644 --- a/github/resource_github_repository_webhook_test.go +++ b/github/resource_github_repository_webhook_test.go @@ -9,11 +9,8 @@ import ( ) func TestAccGithubRepositoryWebhook(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates repository webhooks without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "test-%[1]s" @@ -43,35 +40,20 @@ func TestAccGithubRepositoryWebhook(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("imports repository webhooks without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_repository" "test" { name = "test-%[1]s" @@ -92,41 +74,26 @@ func TestAccGithubRepositoryWebhook(t *testing.T) { check := resource.ComposeTestCheckFunc() - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_repository_webhook.test", - ImportState: true, - ImportStateVerify: true, - ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_repository_webhook.test", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf("test-%s/", randomID), + }, + }, }) - }) t.Run("updates repository webhooks without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) configs := map[string]string{ "before": fmt.Sprintf(` resource "github_repository" "test" { @@ -178,33 +145,19 @@ func TestAccGithubRepositoryWebhook(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: configs["before"], - Check: checks["before"], - }, - { - Config: configs["after"], - Check: checks["after"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: configs["before"], + Check: checks["before"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configs["after"], + Check: checks["after"], + }, + }, }) }) } diff --git a/github/resource_github_team_members_test.go b/github/resource_github_team_members_test.go index fc9a1ff568..8d1d029d1d 100644 --- a/github/resource_github_team_members_test.go +++ b/github/resource_github_team_members_test.go @@ -3,74 +3,87 @@ package github import ( "context" "fmt" - "strconv" "testing" - "github.com/google/go-github/v66/github" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func TestAccGithubTeamMembers(t *testing.T) { - if testCollaborator == "" { - t.Skip("Skipping because `GITHUB_TEST_COLLABORATOR` is not set") + if len(testAccConf.testOrgUser) == 0 { + t.Skip("No test user provided") } - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - resourceName := "github_team_members.test_team_members" + t.Run("creates a team & members configured with defaults", func(t *testing.T) { + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + teamName := fmt.Sprintf("tf-acc-test-team-%s", randomID) - var membership github.Membership + config := fmt.Sprintf(` +resource "github_team" "test" { + name = "%s" +} - t.Run("creates a team & members configured with defaults", func(t *testing.T) { - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubTeamMembersDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubTeamMembersConfig(randomID, testCollaborator, "member"), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubTeamMembersExists(resourceName, &membership), - testAccCheckGithubTeamMembersRoleState(resourceName, "member", &membership), - ), - }, - { - Config: testAccGithubTeamMembersConfig(randomID, testCollaborator, "maintainer"), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubTeamMembersExists(resourceName, &membership), - testAccCheckGithubTeamMembersRoleState(resourceName, "maintainer", &membership), - ), - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - }, - }, - }) - } +resource "github_team_members" "test" { + team_id = github_team.test.id - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) + members { + username = "%s" + role = "member" + } +} +`, teamName, testAccConf.testOrgUser) - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) + configUpdated := fmt.Sprintf(` +resource "github_team" "test" { + name = "%s" +} - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) +resource "github_team_members" "test" { + team_id = github_team.test.id + members { + username = "%s" + role = "maintainer" + } +} +`, teamName, testAccConf.testOrgUser) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubTeamMembersDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_team_members.test", "members.#"), + resource.TestCheckResourceAttr("github_team_members.test", "members.#", "1"), + resource.TestCheckResourceAttr("github_team_members.test", "members.0.username", testAccConf.testOrgUser), + resource.TestCheckResourceAttr("github_team_members.test", "members.0.role", "member"), + ), + }, + { + Config: configUpdated, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet("github_team_members.test", "members.#"), + resource.TestCheckResourceAttr("github_team_members.test", "members.#", "1"), + resource.TestCheckResourceAttr("github_team_members.test", "members.0.username", testAccConf.testOrgUser), + resource.TestCheckResourceAttr("github_team_members.test", "members.0.role", "member"), + ), + }, + }, + }) }) - } func testAccCheckGithubTeamMembersDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*Owner).v3client - orgId := testAccProvider.Meta().(*Owner).id + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + orgId := meta.id for _, rs := range s.RootModule().Resources { if rs.Type != "github_team_members" { @@ -79,7 +92,7 @@ func testAccCheckGithubTeamMembersDestroy(s *terraform.State) error { teamIdString := rs.Primary.ID - teamId, err := strconv.ParseInt(teamIdString, 10, 64) + teamId, err := getTeamID(teamIdString, meta) if err != nil { return unconvertibleIdErr(teamIdString, err) } @@ -98,115 +111,3 @@ func testAccCheckGithubTeamMembersDestroy(s *terraform.State) error { } return nil } - -func testAccCheckGithubTeamMembersExists(n string, membership *github.Membership) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("no team ID is set") - } - - conn := testAccProvider.Meta().(*Owner).v3client - orgId := testAccProvider.Meta().(*Owner).id - teamIdString := rs.Primary.ID - - teamId, err := strconv.ParseInt(teamIdString, 10, 64) - if err != nil { - return unconvertibleIdErr(teamIdString, err) - } - - members, _, err := conn.Teams.ListTeamMembersByID(context.TODO(), orgId, teamId, nil) - if err != nil { - return err - } - - if len(members) != 1 { - return fmt.Errorf("team has not one member: %d", len(members)) - } - - TeamMembership, _, err := conn.Teams.GetTeamMembershipByID(context.TODO(), orgId, teamId, *members[0].Login) - - if err != nil { - return err - } - *membership = *TeamMembership - return nil - } -} - -func testAccCheckGithubTeamMembersRoleState(n, expected string, membership *github.Membership) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("no team ID is set") - } - - conn := testAccProvider.Meta().(*Owner).v3client - orgId := testAccProvider.Meta().(*Owner).id - teamIdString := rs.Primary.ID - - teamId, err := strconv.ParseInt(teamIdString, 10, 64) - if err != nil { - return unconvertibleIdErr(teamIdString, err) - } - - members, _, err := conn.Teams.ListTeamMembersByID(context.TODO(), orgId, teamId, nil) - if err != nil { - return err - } - - if len(members) != 1 { - return fmt.Errorf("team has not one member: %d", len(members)) - } - - TeamMembers, _, err := conn.Teams.GetTeamMembershipByID(context.TODO(), - orgId, teamId, *members[0].Login) - if err != nil { - return err - } - - resourceRole := membership.GetRole() - actualRole := TeamMembers.GetRole() - - if resourceRole != expected { - return fmt.Errorf("team membership role %v in resource does match expected state of %v", resourceRole, expected) - } - - if resourceRole != actualRole { - return fmt.Errorf("team membership role %v in resource does match actual state of %v", resourceRole, actualRole) - } - return nil - } -} - -func testAccGithubTeamMembersConfig(randString, username, role string) string { - return fmt.Sprintf(` -resource "github_membership" "test_org_membership" { - username = "%s" - role = "member" -} - -resource "github_team" "test_team" { - name = "tf-acc-test-team-membership-%s" - description = "Terraform acc test group" -} - -resource "github_team_members" "test_team_members" { - team_id = "${github_team.test_team.id}" - members { - username = "%s" - role = "%s" - } - - depends_on = [github_membership.test_org_membership] -} -`, username, randString, username, role) -} diff --git a/github/resource_github_team_membership_test.go b/github/resource_github_team_membership_test.go index 13c113722e..e61747503b 100644 --- a/github/resource_github_team_membership_test.go +++ b/github/resource_github_team_membership_test.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "strconv" "testing" "github.com/google/go-github/v66/github" @@ -13,107 +12,81 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -func TestAccGithubTeamMembership_basic(t *testing.T) { - if testCollaborator == "" { - t.Skip("Skipping because `GITHUB_TEST_COLLABORATOR` is not set") - } - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) +func TestAccGithubTeamMembership(t *testing.T) { + if len(testAccConf.testOrgUser) == 0 { + t.Skip("No test user provided") } - var membership github.Membership - - rn := "github_team_membership.test_team_membership" - rns := "github_team_membership.test_team_membership_slug" - randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubTeamMembershipDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubTeamMembershipConfig(randString, testCollaborator, "member"), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubTeamMembershipExists(rn, &membership), - testAccCheckGithubTeamMembershipRoleState(rn, "member", &membership), - testAccCheckGithubTeamMembershipExists(rns, &membership), - testAccCheckGithubTeamMembershipRoleState(rns, "member", &membership), - ), - }, - { - Config: testAccGithubTeamMembershipConfig(randString, testCollaborator, "maintainer"), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubTeamMembershipExists(rn, &membership), - testAccCheckGithubTeamMembershipRoleState(rn, "maintainer", &membership), - testAccCheckGithubTeamMembershipExists(rns, &membership), - testAccCheckGithubTeamMembershipRoleState(rns, "maintainer", &membership), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateVerify: true, - }, - { - ResourceName: rns, - ImportState: true, - ImportStateVerify: true, + t.Run("creates a team membership", func(t *testing.T) { + var membership github.Membership + randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubTeamMembershipDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubTeamMembershipConfig(randString, testAccConf.testOrgUser, "member"), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubTeamMembershipExists("github_team_membership.test_team_membership", &membership), + testAccCheckGithubTeamMembershipRoleState("github_team_membership.test_team_membership", "member", &membership), + testAccCheckGithubTeamMembershipExists("github_team_membership.test_team_membership_slug", &membership), + testAccCheckGithubTeamMembershipRoleState("github_team_membership.test_team_membership_slug", "member", &membership), + ), + }, + { + Config: testAccGithubTeamMembershipConfig(randString, testAccConf.testOrgUser, "maintainer"), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubTeamMembershipExists("github_team_membership.test_team_membership", &membership), + testAccCheckGithubTeamMembershipRoleState("github_team_membership.test_team_membership", "maintainer", &membership), + testAccCheckGithubTeamMembershipExists("github_team_membership.test_team_membership_slug", &membership), + testAccCheckGithubTeamMembershipRoleState("github_team_membership.test_team_membership_slug", "maintainer", &membership), + ), + }, }, - }, + }) }) -} - -func TestAccGithubTeamMembership_caseInsensitive(t *testing.T) { - if testCollaborator == "" { - t.Skip("Skipping because `GITHUB_TEST_COLLABORATOR` is not set") - } - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) - } - - var membership github.Membership - var otherMembership github.Membership - - rn := "github_team_membership.test_team_membership" - randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) - - otherCase := flipUsernameCase(testCollaborator) - - if testCollaborator == otherCase { - t.Skip("Skipping because `GITHUB_TEST_COLLABORATOR` has no letters to flip case") - } - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubTeamMembershipDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubTeamMembershipConfig(randString, testCollaborator, "member"), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubTeamMembershipExists(rn, &membership), - ), + t.Run("is case insensitive", func(t *testing.T) { + var membership github.Membership + var otherMembership github.Membership + + rn := "github_team_membership.test_team_membership" + randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum) + + otherCase := flipUsernameCase(testAccConf.testOrgUser) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubTeamMembershipDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubTeamMembershipConfig(randString, testAccConf.testOrgUser, "member"), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubTeamMembershipExists(rn, &membership), + ), + }, + { + Config: testAccGithubTeamMembershipConfig(randString, otherCase, "member"), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubTeamMembershipExists(rn, &otherMembership), + testAccGithubTeamMembershipTheSame(&membership, &otherMembership), + ), + }, }, - { - Config: testAccGithubTeamMembershipConfig(randString, otherCase, "member"), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubTeamMembershipExists(rn, &otherMembership), - testAccGithubTeamMembershipTheSame(&membership, &otherMembership), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateVerify: true, - }, - }, + }) }) } func testAccCheckGithubTeamMembershipDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*Owner).v3client - orgId := testAccProvider.Meta().(*Owner).id + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + orgId := meta.id for _, rs := range s.RootModule().Resources { if rs.Type != "github_team_membership" { @@ -125,7 +98,7 @@ func testAccCheckGithubTeamMembershipDestroy(s *terraform.State) error { return err } - teamId, err := strconv.ParseInt(teamIdString, 10, 64) + teamId, err := getTeamID(teamIdString, meta) if err != nil { return unconvertibleIdErr(teamIdString, err) } @@ -156,20 +129,23 @@ func testAccCheckGithubTeamMembershipExists(n string, membership *github.Members return fmt.Errorf("no team membership ID is set") } - conn := testAccProvider.Meta().(*Owner).v3client - orgId := testAccProvider.Meta().(*Owner).id + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + orgId := meta.id teamIdString, username, err := parseTwoPartID(rs.Primary.ID, "team_id", "username") if err != nil { return err } - teamId, err := strconv.ParseInt(teamIdString, 10, 64) + teamId, err := getTeamID(teamIdString, meta) if err != nil { return unconvertibleIdErr(teamIdString, err) } teamMembership, _, err := conn.Teams.GetTeamMembershipByID(context.TODO(), orgId, teamId, username) - if err != nil { return err } @@ -189,13 +165,17 @@ func testAccCheckGithubTeamMembershipRoleState(n, expected string, membership *g return fmt.Errorf("no team membership ID is set") } - conn := testAccProvider.Meta().(*Owner).v3client - orgId := testAccProvider.Meta().(*Owner).id + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + orgId := meta.id teamIdString, username, err := parseTwoPartID(rs.Primary.ID, "team_id", "username") if err != nil { return err } - teamId, err := strconv.ParseInt(teamIdString, 10, 64) + teamId, err := getTeamID(teamIdString, meta) if err != nil { return unconvertibleIdErr(teamIdString, err) } @@ -222,11 +202,6 @@ func testAccCheckGithubTeamMembershipRoleState(n, expected string, membership *g func testAccGithubTeamMembershipConfig(randString, username, role string) string { return fmt.Sprintf(` -resource "github_membership" "test_org_membership" { - username = "%s" - role = "member" -} - resource "github_team" "test_team" { name = "tf-acc-test-team-membership-%s" description = "Terraform acc test group" @@ -238,17 +213,17 @@ resource "github_team" "test_team_slug" { } resource "github_team_membership" "test_team_membership" { - team_id = "${github_team.test_team.id}" + team_id = github_team.test_team.id username = "%s" role = "%s" } resource "github_team_membership" "test_team_membership_slug" { - team_id = "${github_team.test_team_slug.slug}" + team_id = github_team.test_team_slug.slug username = "%s" role = "%s" } -`, username, randString, randString, username, role, username, role) +`, randString, randString, username, role, username, role) } func testAccGithubTeamMembershipTheSame(orig, other *github.Membership) resource.TestCheckFunc { diff --git a/github/resource_github_team_repository_test.go b/github/resource_github_team_repository_test.go index d97ad2c2ca..9e15a74afa 100644 --- a/github/resource_github_team_repository_test.go +++ b/github/resource_github_team_repository_test.go @@ -10,11 +10,8 @@ import ( ) func TestAccGithubTeamRepository(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("manages team permissions to a repository", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-test-team-repo-%s" @@ -65,59 +62,44 @@ func TestAccGithubTeamRepository(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["pull"], - }, - { - Config: strings.Replace(config, - `permission = "pull"`, - `permission = "triage"`, 1), - Check: checks["triage"], - }, - { - Config: strings.Replace(config, - `permission = "pull"`, - `permission = "push"`, 1), - Check: checks["push"], - }, - { - Config: strings.Replace(config, - `permission = "pull"`, - `permission = "maintain"`, 1), - Check: checks["maintain"], - }, - { - Config: strings.Replace(config, - `permission = "pull"`, - `permission = "admin"`, 1), - Check: checks["admin"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["pull"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + `permission = "pull"`, + `permission = "triage"`, 1), + Check: checks["triage"], + }, + { + Config: strings.Replace(config, + `permission = "pull"`, + `permission = "push"`, 1), + Check: checks["push"], + }, + { + Config: strings.Replace(config, + `permission = "pull"`, + `permission = "maintain"`, 1), + Check: checks["maintain"], + }, + { + Config: strings.Replace(config, + `permission = "pull"`, + `permission = "admin"`, 1), + Check: checks["admin"], + }, + }, }) - }) t.Run("accepts both team slug and team ID for `team_id`", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-test-team-repo-%s" @@ -139,35 +121,21 @@ func TestAccGithubTeamRepository(t *testing.T) { resource.TestCheckResourceAttrSet("github_team_repository.test", "team_id"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - Config: strings.Replace(config, - `github_team.test.id`, - `github_team.test.slug`, 1), - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + `github_team.test.id`, + `github_team.test.slug`, 1), + Check: check, + }, + }, }) }) } diff --git a/github/resource_github_team_settings_test.go b/github/resource_github_team_settings_test.go index 323cbe72e6..cb9544c481 100644 --- a/github/resource_github_team_settings_test.go +++ b/github/resource_github_team_settings_test.go @@ -10,12 +10,9 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) -func TestCanUseIDOrSlugForTeamIDWhenChangingSettings(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - +func TestAccGithubTeamSettings(t *testing.T) { t.Run("manages team settings can use team_id id and slug", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-test-team-repo-%s" @@ -31,47 +28,26 @@ func TestCanUseIDOrSlugForTeamIDWhenChangingSettings(t *testing.T) { resource.TestCheckResourceAttrSet("github_team_settings.test", "team_id"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - Config: strings.Replace(config, - `github_team.test.id`, - `github_team.test.slug`, 1), - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + `github_team.test.id`, + `github_team.test.slug`, 1), + Check: check, + }, + }, }) - }) -} - -func TestCanUpdateTeamSettings(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("manages team code review settings", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-test-team-repo-%s" @@ -115,59 +91,38 @@ func TestCanUpdateTeamSettings(t *testing.T) { ), } - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: checks["round_robin"], - }, - { - Config: strings.Replace(config, - `algorithm = "ROUND_ROBIN"`, - `algorithm = "LOAD_BALANCE"`, 1), - Check: checks["load_balance"], - }, - { - Config: strings.Replace(config, - `member_count = 1`, - `member_count = 3`, 1), - Check: checks["review_count"], - }, - { - Config: strings.Replace(config, - `notify = true`, - `notify = false`, 1), - Check: checks["notify"], - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: checks["round_robin"], }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: strings.Replace(config, + `algorithm = "ROUND_ROBIN"`, + `algorithm = "LOAD_BALANCE"`, 1), + Check: checks["load_balance"], + }, + { + Config: strings.Replace(config, + `member_count = 1`, + `member_count = 3`, 1), + Check: checks["review_count"], + }, + { + Config: strings.Replace(config, + `notify = true`, + `notify = false`, 1), + Check: checks["notify"], + }, + }, }) - }) -} - -func TestCannotUseReviewSettingsIfDisabled(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("cannot manage team code review settings if disabled", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-test-team-repo-%s" @@ -184,33 +139,17 @@ func TestCannotUseReviewSettingsIfDisabled(t *testing.T) { } `, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: strings.Replace(config, - `algorithm = "ROUND_ROBIN"`, - `algorithm = "invalid"`, 1), - ExpectError: regexp.MustCompile(`review request delegation algorithm must be one of \[.*\]`), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: strings.Replace(config, + `algorithm = "ROUND_ROBIN"`, + `algorithm = "invalid"`, 1), + ExpectError: regexp.MustCompile(`review request delegation algorithm must be one of \[.*\]`), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) - } diff --git a/github/resource_github_team_sync_group_mapping_test.go b/github/resource_github_team_sync_group_mapping_test.go index a0488f2080..3be9eec5dc 100644 --- a/github/resource_github_team_sync_group_mapping_test.go +++ b/github/resource_github_team_sync_group_mapping_test.go @@ -13,140 +13,134 @@ import ( ) func TestAccGithubTeamSyncGroupMapping_basic(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - teamName := acctest.RandomWithPrefix("tf-acc-test-%s") - rn := "github_team_sync_group_mapping.test_mapping" + t.Run("creates a team sync group mapping", func(t *testing.T) { + teamName := acctest.RandomWithPrefix("tf-acc-test-%s") + rn := "github_team_sync_group_mapping.test_mapping" - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubTeamSyncGroupMappingConfig(teamName), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(rn, "group.#", "3"), - resource.TestCheckResourceAttrSet(rn, "group.3924494127.group_id"), - resource.TestCheckResourceAttrSet(rn, "group.3924494127.group_name"), - resource.TestCheckResourceAttrSet(rn, "group.4283356133.group_id"), - resource.TestCheckResourceAttrSet(rn, "group.4283356133.group_name"), - resource.TestCheckResourceAttrSet(rn, "group.451718421.group_id"), - resource.TestCheckResourceAttrSet(rn, "group.451718421.group_name"), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateIdFunc: testAccGithubTeamSyncGroupMappingImportStateIdFunc(rn), - ImportStateVerify: true, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubTeamSyncGroupMappingConfig(teamName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(rn, "group.#", "3"), + resource.TestCheckResourceAttrSet(rn, "group.3924494127.group_id"), + resource.TestCheckResourceAttrSet(rn, "group.3924494127.group_name"), + resource.TestCheckResourceAttrSet(rn, "group.4283356133.group_id"), + resource.TestCheckResourceAttrSet(rn, "group.4283356133.group_name"), + resource.TestCheckResourceAttrSet(rn, "group.451718421.group_id"), + resource.TestCheckResourceAttrSet(rn, "group.451718421.group_name"), + ), + }, + { + ResourceName: rn, + ImportState: true, + ImportStateIdFunc: testAccGithubTeamSyncGroupMappingImportStateIdFunc(rn), + ImportStateVerify: true, + }, }, - }, + }) }) -} -func TestAccGithubTeamSyncGroupMapping_disappears(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - teamName := acctest.RandomWithPrefix("tf-acc-test-%s") - rn := "github_team_sync_group_mapping.test_mapping" + t.Run("creates a team sync group mapping and then deletes it", func(t *testing.T) { + teamName := acctest.RandomWithPrefix("tf-acc-test-%s") + rn := "github_team_sync_group_mapping.test_mapping" - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubTeamSyncGroupMappingConfig(teamName), - Check: resource.ComposeTestCheckFunc( - testAccCheckGithubTeamSyncGroupMappingDisappears(rn), - ), - ExpectNonEmptyPlan: true, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubTeamSyncGroupMappingConfig(teamName), + Check: resource.ComposeTestCheckFunc( + testAccCheckGithubTeamSyncGroupMappingDisappears(rn), + ), + ExpectNonEmptyPlan: true, + }, }, - }, + }) }) -} -func TestAccGithubTeamSyncGroupMapping_update(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - teamName := acctest.RandomWithPrefix("tf-acc-test-%s") - description := "tf-acc-group-description-update" - rn := "github_team_sync_group_mapping.test_mapping" + t.Run("creates a team sync group mapping and then updates it", func(t *testing.T) { + teamName := acctest.RandomWithPrefix("tf-acc-test-%s") + description := "tf-acc-group-description-update" + rn := "github_team_sync_group_mapping.test_mapping" - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubTeamSyncGroupMappingConfig(teamName), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateIdFunc: testAccGithubTeamSyncGroupMappingImportStateIdFunc(rn), - ImportStateVerify: true, - }, - { - Config: testAccGithubTeamSyncGroupMappingEmptyConfig(teamName), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(rn, "group.#", "0"), - ), - }, - { - Config: testAccGithubTeamSyncGroupMappingConfig(teamName), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(rn, "group.#", "3"), - ), + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubTeamSyncGroupMappingConfig(teamName), + }, + { + ResourceName: rn, + ImportState: true, + ImportStateIdFunc: testAccGithubTeamSyncGroupMappingImportStateIdFunc(rn), + ImportStateVerify: true, + }, + { + Config: testAccGithubTeamSyncGroupMappingEmptyConfig(teamName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(rn, "group.#", "0"), + ), + }, + { + Config: testAccGithubTeamSyncGroupMappingConfig(teamName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(rn, "group.#", "3"), + ), + }, + { + Config: testAccGithubTeamSyncGroupMappingAddGroupAndUpdateConfig(teamName, description), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(rn, "group.#", "3"), + resource.TestCheckResourceAttr(rn, "group.1385744695.group_description", description), + resource.TestCheckResourceAttr(rn, "group.2749525965.group_description", description), + resource.TestCheckResourceAttr(rn, "group.3830341445.group_description", description), + ), + }, }, - { - Config: testAccGithubTeamSyncGroupMappingAddGroupAndUpdateConfig(teamName, description), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(rn, "group.#", "3"), - resource.TestCheckResourceAttr(rn, "group.1385744695.group_description", description), - resource.TestCheckResourceAttr(rn, "group.2749525965.group_description", description), - resource.TestCheckResourceAttr(rn, "group.3830341445.group_description", description), - ), - }, - }, + }) }) -} -func TestAccGithubTeamSyncGroupMapping_empty(t *testing.T) { - if isEnterprise != "true" { - t.Skip("Skipping because `ENTERPRISE_ACCOUNT` is not set or set to false") - } - teamName := acctest.RandomWithPrefix("tf-acc-test-%s") - rn := "github_team_sync_group_mapping.test_mapping" + t.Run("creates empty team sync group mapping", func(t *testing.T) { + teamName := acctest.RandomWithPrefix("tf-acc-test-%s") + rn := "github_team_sync_group_mapping.test_mapping" - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubTeamSyncGroupMappingEmptyConfig(teamName), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(rn, "group.#", "0"), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateIdFunc: testAccGithubTeamSyncGroupMappingImportStateIdFunc(rn), - ImportStateVerify: true, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessMode(t, enterprise) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubTeamSyncGroupMappingDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubTeamSyncGroupMappingEmptyConfig(teamName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(rn, "group.#", "0"), + ), + }, + { + ResourceName: rn, + ImportState: true, + ImportStateIdFunc: testAccGithubTeamSyncGroupMappingImportStateIdFunc(rn), + ImportStateVerify: true, + }, }, - }, + }) }) } func testAccCheckGithubTeamSyncGroupMappingDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*Owner).v3client - orgName := testAccProvider.Meta().(*Owner).name + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + orgName := meta.name ctx := context.TODO() for _, rs := range s.RootModule().Resources { if rs.Type != "github_team_sync_group_mapping" { @@ -173,12 +167,16 @@ func testAccCheckGithubTeamSyncGroupMappingDisappears(resourceName string) resou if !ok { return fmt.Errorf("not found: %s", resourceName) } - conn := testAccProvider.Meta().(*Owner).v3client - orgName := testAccProvider.Meta().(*Owner).name + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + orgName := meta.name slug := rs.Primary.Attributes["team_slug"] emptyGroupList := github.IDPGroupList{Groups: []*github.IDPGroup{}} - _, _, err := conn.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(context.TODO(), orgName, slug, emptyGroupList) + _, _, err = conn.Teams.CreateOrUpdateIDPGroupConnectionsBySlug(context.TODO(), orgName, slug, emptyGroupList) return err } diff --git a/github/resource_github_team_test.go b/github/resource_github_team_test.go index 93ed1f4c68..1bf8c62baa 100644 --- a/github/resource_github_team_test.go +++ b/github/resource_github_team_test.go @@ -9,11 +9,8 @@ import ( ) func TestAccGithubTeam(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates a team configured with defaults", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-%s" @@ -24,41 +21,20 @@ func TestAccGithubTeam(t *testing.T) { resource.TestCheckResourceAttrSet("github_team.test", "slug"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") + }, }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) -} - -func TestAccGithubTeamHierarchical(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates a hierarchy of teams", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "team01" { name = "tf-acc-team01-%s" @@ -115,44 +91,24 @@ func TestAccGithubTeamHierarchical(t *testing.T) { resource.TestCheckResourceAttr("github_team.team03", "parent_team_read_slug", ""), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - Config: config2, - Check: check2, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: config2, + Check: check2, + }, + }, }) - }) -} -func TestAccGithubTeamRemovesDefaultMaintainer(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("creates a team and removes the default maintainer", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-%s" @@ -164,40 +120,20 @@ func TestAccGithubTeamRemovesDefaultMaintainer(t *testing.T) { resource.TestCheckResourceAttr("github_team.test", "members_count", "0"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) -} - -func TestAccGithubTeamUpdateName(t *testing.T) { - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - t.Run("marks the slug as computed when the name changes", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) config := fmt.Sprintf(` resource "github_team" "test" { name = "tf-acc-%s" @@ -215,38 +151,23 @@ func TestAccGithubTeamUpdateName(t *testing.T) { } `, randomID, randomID) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("github_team.test", "slug", fmt.Sprintf("tf-acc-%s", randomID)), - ), - }, - { - Config: configUpdated, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr("github_team.other", "description", fmt.Sprintf("tf-acc-updated-%s", randomID)), - ), - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_team.test", "slug", fmt.Sprintf("tf-acc-%s", randomID)), + ), }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - t.Skip("individual account not supported for this operation") - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + Config: configUpdated, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("github_team.other", "description", fmt.Sprintf("tf-acc-updated-%s", randomID)), + ), + }, + }, }) - }) } diff --git a/github/resource_github_user_gpg_key_test.go b/github/resource_github_user_gpg_key_test.go index 5e00ae7a30..08b6feeaed 100644 --- a/github/resource_github_user_gpg_key_test.go +++ b/github/resource_github_user_gpg_key_test.go @@ -4,20 +4,21 @@ import ( "fmt" "path/filepath" "regexp" + "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" ) func TestAccGithubUserGpgKey(t *testing.T) { - t.Run("creates a GPG key without error", func(t *testing.T) { + keyPath := strings.ReplaceAll(filepath.Join("test-fixtures", "gpg-pubkey.asc"), "\\", "/") config := fmt.Sprintf(` - resource "github_user_gpg_key" "test" { - armored_public_key = "${file("%s")}" - } - `, filepath.Join("test-fixtures", "gpg-pubkey.asc")) + resource "github_user_gpg_key" "test" { + armored_public_key = "${file("%s")}" + } + `, keyPath) check := resource.ComposeTestCheckFunc( resource.TestMatchResourceAttr( @@ -32,31 +33,15 @@ func TestAccGithubUserGpgKey(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + }, }) - }) - } diff --git a/github/resource_github_user_invitation_accepter_test.go b/github/resource_github_user_invitation_accepter_test.go index ae15429873..5b4772fcd6 100644 --- a/github/resource_github_user_invitation_accepter_test.go +++ b/github/resource_github_user_invitation_accepter_test.go @@ -2,61 +2,60 @@ package github import ( "fmt" - "os" "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -func TestAccGithubUserInvitationAccepter_basic(t *testing.T) { - rn := "github_repository_collaborator.test" - repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5)) +func TestAccGithubUserInvitationAccepter(t *testing.T) { + if len(testAccConf.testExternalUser) == 0 { + t.Skip("No external user provided") + } - inviteeToken := os.Getenv("GITHUB_TEST_COLLABORATOR_TOKEN") - if inviteeToken == "" { - t.Skip("GITHUB_TEST_COLLABORATOR_TOKEN was not provided, skipping test") + if len(testAccConf.testExternalUserToken) == 0 { + t.Skip("No external user token provided") } - var providers []*schema.Provider - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), - CheckDestroy: testAccCheckGithubUserInvitationAccepterDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubUserInvitationAccepterConfig(inviteeToken, repoName), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(rn, "permission", "push"), - resource.TestMatchResourceAttr(rn, "invitation_id", regexp.MustCompile(`^[0-9]+$`)), - ), + t.Run("accepts an invitation", func(t *testing.T) { + rn := "github_repository_collaborator.test" + repoName := fmt.Sprintf("tf-acc-test-collab-%s", acctest.RandString(5)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubUserInvitationAccepterDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubUserInvitationAccepterConfig(testAccConf.testExternalUserToken, repoName, testAccConf.testExternalUser), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(rn, "permission", "push"), + resource.TestMatchResourceAttr(rn, "invitation_id", regexp.MustCompile(`^[0-9]+$`)), + ), + }, }, - }, + }) }) -} -func TestAccGithubUserInvitationAccepterAllowEmptyId(t *testing.T) { - rn := "github_user_invitation_accepter.test" - - var providers []*schema.Provider - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - ProviderFactories: testAccProviderFactories(&providers), - CheckDestroy: testAccCheckGithubUserInvitationAccepterDestroy, - Steps: []resource.TestStep{ - { - Config: testAccGithubUserInvitationAccepterAllowEmptyId(), - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(rn, "invitation_id", ""), - resource.TestCheckResourceAttr(rn, "allow_empty_id", "true"), - ), + t.Run("accepts an invitation with an empty invitation_id", func(t *testing.T) { + rn := "github_user_invitation_accepter.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccCheckGithubUserInvitationAccepterDestroy, + Steps: []resource.TestStep{ + { + Config: testAccGithubUserInvitationAccepterAllowEmptyId(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(rn, "invitation_id", ""), + resource.TestCheckResourceAttr(rn, "allow_empty_id", "true"), + ), + }, }, - }, + }) }) } @@ -64,7 +63,7 @@ func testAccCheckGithubUserInvitationAccepterDestroy(s *terraform.State) error { return nil } -func testAccGithubUserInvitationAccepterConfig(inviteeToken, repoName string) string { +func testAccGithubUserInvitationAccepterConfig(inviteeToken, repoName, collaborator string) string { return fmt.Sprintf(` provider "github" { alias = "main" @@ -91,7 +90,7 @@ resource "github_user_invitation_accepter" "test" { provider = "github.invitee" invitation_id = "${github_repository_collaborator.test.invitation_id}" } -`, inviteeToken, repoName, testCollaborator) +`, inviteeToken, repoName, collaborator) } func testAccGithubUserInvitationAccepterAllowEmptyId() string { diff --git a/github/resource_github_user_ssh_key_test.go b/github/resource_github_user_ssh_key_test.go index b26d5b9524..80ab315b79 100644 --- a/github/resource_github_user_ssh_key_test.go +++ b/github/resource_github_user_ssh_key_test.go @@ -14,12 +14,9 @@ import ( ) func TestAccGithubUserSshKey(t *testing.T) { - - randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) - testKey := newTestKey() - t.Run("creates and destroys a user SSH key without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + testKey := newTestKey() config := fmt.Sprintf(` resource "github_user_ssh_key" "test" { title = "tf-acc-test-%s" @@ -42,35 +39,21 @@ func TestAccGithubUserSshKey(t *testing.T) { ), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") + }, }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) - }) - }) t.Run("imports an individual account SSH key without error", func(t *testing.T) { - + randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) + testKey := newTestKey() config := fmt.Sprintf(` resource "github_user_ssh_key" "test" { title = "tf-acc-test-%s" @@ -83,36 +66,21 @@ func TestAccGithubUserSshKey(t *testing.T) { resource.TestCheckResourceAttrSet("github_user_ssh_key.test", "key"), ) - testCase := func(t *testing.T, mode string) { - resource.Test(t, resource.TestCase{ - PreCheck: func() { skipUnlessMode(t, mode) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: config, - Check: check, - }, - { - ResourceName: "github_user_ssh_key.test", - ImportState: true, - ImportStateVerify: true, - }, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnauthenticated(t) }, + ProviderFactories: providerFactories, + Steps: []resource.TestStep{ + { + Config: config, + Check: check, }, - }) - } - - t.Run("with an anonymous account", func(t *testing.T) { - t.Skip("anonymous account not supported for this operation") - }) - - t.Run("with an individual account", func(t *testing.T) { - testCase(t, individual) - }) - - t.Run("with an organization account", func(t *testing.T) { - testCase(t, organization) + { + ResourceName: "github_user_ssh_key.test", + ImportState: true, + ImportStateVerify: true, + }, + }, }) - }) } diff --git a/github/resource_organization_block_test.go b/github/resource_organization_block_test.go index f4bfb03a62..555affcebd 100644 --- a/github/resource_organization_block_test.go +++ b/github/resource_organization_block_test.go @@ -10,35 +10,43 @@ import ( ) func TestAccOrganizationBlock_basic(t *testing.T) { - if err := testAccCheckOrganization(); err != nil { - t.Skipf("Skipping because %s.", err.Error()) - } + t.Run("creates organization block", func(t *testing.T) { + config := ` +resource "github_organization_block" "test" { + username = "cgriggs01" +} +` - rn := "github_organization_block.test" + rn := "github_organization_block.test" - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccOrganizationBlockDestroy, - Steps: []resource.TestStep{ - { - Config: testAccOrganizationBlockConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckOrganizationBlockExists(rn), - ), - }, - { - ResourceName: rn, - ImportState: true, - ImportStateVerify: true, + resource.Test(t, resource.TestCase{ + PreCheck: func() { skipUnlessHasOrgs(t) }, + ProviderFactories: providerFactories, + CheckDestroy: testAccOrganizationBlockDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testAccCheckOrganizationBlockExists(rn), + ), + }, + { + ResourceName: rn, + ImportState: true, + ImportStateVerify: true, + }, }, - }, + }) }) } func testAccOrganizationBlockDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*Owner).v3client - orgName := testAccProvider.Meta().(*Owner).name + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + orgName := meta.name for _, rs := range s.RootModule().Resources { if rs.Type != "github_organization_block" { @@ -64,8 +72,12 @@ func testAccCheckOrganizationBlockExists(n string) resource.TestCheckFunc { } username := rs.Primary.ID - conn := testAccProvider.Meta().(*Owner).v3client - orgName := testAccProvider.Meta().(*Owner).name + meta, err := getTestMeta() + if err != nil { + return err + } + conn := meta.v3client + orgName := meta.name blocked, _, err := conn.Organizations.IsBlocked(context.TODO(), orgName, username) if err != nil { @@ -77,9 +89,3 @@ func testAccCheckOrganizationBlockExists(n string) resource.TestCheckFunc { return nil } } - -const testAccOrganizationBlockConfig = ` -resource "github_organization_block" "test" { - username = "cgriggs01" -} -` diff --git a/github/sweeper_test.go b/github/sweeper_test.go deleted file mode 100644 index 92f3f14a1c..0000000000 --- a/github/sweeper_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package github - -import ( - "fmt" - "os" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" -) - -func TestMain(m *testing.M) { - resource.TestMain(m) -} - -func sharedConfigForRegion(region string) (interface{}, error) { - if os.Getenv("GITHUB_TOKEN") == "" { - return nil, fmt.Errorf("empty GITHUB_TOKEN") - } - - if os.Getenv("GITHUB_OWNER") == "" { - return nil, fmt.Errorf("empty GITHUB_OWNER") - } - - config := Config{ - Token: os.Getenv("GITHUB_TOKEN"), - Owner: os.Getenv("GITHUB_OWNER"), - BaseURL: "", - } - - meta, err := config.Meta() - if err != nil { - return nil, fmt.Errorf("error getting GitHub meta parameter") - } - - return meta, nil -} diff --git a/github/util_test.go b/github/util_test.go index 67bca149e4..677a9d7693 100644 --- a/github/util_test.go +++ b/github/util_test.go @@ -7,7 +7,7 @@ import ( "github.com/hashicorp/go-cty/cty" ) -func TestAccValidateTeamIDFunc(t *testing.T) { +func TestValidateTeamIDFunc(t *testing.T) { // warnings, errors := validateTeamIDFunc(interface{"1234567"}) cases := []struct { @@ -15,7 +15,6 @@ func TestAccValidateTeamIDFunc(t *testing.T) { ErrCount int }{ { - TeamID: "1234567", ErrCount: 0, }, @@ -38,7 +37,7 @@ func TestAccValidateTeamIDFunc(t *testing.T) { } } -func TestAccGithubUtilRole_validation(t *testing.T) { +func TestGithubUtilRole_validation(t *testing.T) { cases := []struct { Value string ErrCount int @@ -68,7 +67,7 @@ func TestAccGithubUtilRole_validation(t *testing.T) { } } -func TestAccGithubUtilTwoPartID(t *testing.T) { +func TestGithubUtilTwoPartID(t *testing.T) { partOne, partTwo := "foo", "bar" id := buildTwoPartID(partOne, partTwo) @@ -104,12 +103,11 @@ func flipUsernameCase(username string) string { } break } - } return string(oc) } -func TestAccGithubUtilValidateSecretName(t *testing.T) { +func TestGithubUtilValidateSecretName(t *testing.T) { cases := []struct { Name string Error bool