From a1e9315878aa9bd5b743276efa0439024e14e5e3 Mon Sep 17 00:00:00 2001 From: Lucas TESSON Date: Fri, 5 Jan 2024 12:11:12 +0100 Subject: [PATCH] ci: run acceptance tests from github workflow --- .github/workflows/ci.yaml | 36 ++++++++++++++++++-- Makefile | 4 +++ cmd/setup/main.go | 71 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 Makefile create mode 100644 cmd/setup/main.go diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0c38b69..440d3d8 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -25,6 +25,11 @@ jobs: update-coverage: true runs-on: ${{ matrix.platform }} needs: [setup] + services: + ctfd: + image: ctfd/ctfd:3.6.1 + ports: + - 8000:8000 steps: - name: Checkout code uses: actions/checkout@v4 @@ -41,14 +46,39 @@ jobs: key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: ${{ runner.os }}-go- - - name: Run go test - run: go test -v -race -coverprofile coverage.txt ./... + - name: Wait for CTFd server + run: | + max_attempts=60 + base_url="http://localhost:8000" + for ((i=0; i<$max_attempts; i++)); do + if curl --head --fail --silent --show-error "$base_url" >/dev/null; then + echo "Server is up and running!" + break + else + echo "Waiting for the server to respond... (attempt $((i+1)))" + sleep 5 + fi + done + if [ $i -eq $max_attempts ]; then + echo "Server did not respond within the allotted time. Exiting..." + exit 1 + fi + + - name: Setup CTFd + run: go run cmd/setup/main.go + env: + CTFD_URL: http://localhost:8000 + + - name: Run go acceptance tests + run: make test-acc + env: + CTFD_URL: http://localhost:8000 - name: Upload coverage to Coveralls if: ${{ matrix.update-coverage }} uses: shogo82148/actions-goveralls@v1 with: - path-to-profile: coverage.txt + path-to-profile: cov.out go-lint: runs-on: ubuntu-latest diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ff1fe6d --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +.PHONY: test-acc +test-acc: + TF_ACC=1 \ + go test ./... -v -run=^TestAcc_ -count=1 -coverprofile=cov.out -coverpkg "github.com/ctfer-io/terraform-provider-ctfd/internal/provider,github.com/ctfer-io/terraform-provider-ctfd/internal/provider/challenge,github.com/ctfer-io/terraform-provider-ctfd/internal/provider/utils,github.com/ctfer-io/terraform-provider-ctfd/internal/provider/validators" diff --git a/cmd/setup/main.go b/cmd/setup/main.go new file mode 100644 index 0000000..38461cc --- /dev/null +++ b/cmd/setup/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + "log" + "os" + + "github.com/ctfer-io/go-ctfd/api" +) + +// This utility setup a brand new CTFd instance for acceptance testing of the provider, +// and creates an API key ready to work. + +func main() { + url := os.Getenv("CTFD_URL") + + // Note: add /setup so won't have to follow redirect + fmt.Println("[+] Getting initial nonce and session values") + nonce, session, err := api.GetNonceAndSession(url) + if err != nil { + log.Fatalf("Getting nonce and session: %s", err) + } + + // Setup CTFd + fmt.Println("[+] Setting up CTFd") + client := api.NewClient(url, nonce, session, "") + if err := client.Setup(&api.SetupParams{ + CTFName: "TFP-CTFd", + CTFDescription: "Terraform Provider CTFd.", + UserMode: "users", + Name: "ctfer", + Email: "ctfer-io@protonmail.com", + Password: "ctfer", + ChallengeVisibility: "public", + AccountVisibility: "public", + ScoreVisibility: "public", + RegistrationVisibility: "public", + VerifyEmails: false, + TeamSize: nil, + CTFLogo: nil, + CTFBanner: nil, + CTFSmallIcon: nil, + CTFTheme: "core", + ThemeColor: "", + Start: "", + End: "", + Nonce: nonce, + }); err != nil { + log.Fatalf("Setting up CTFd: %s", err) + } + + // Create API Key + fmt.Println("[+] Creating API Token") + token, err := client.PostTokens(&api.PostTokensParams{ + Expiration: "2222-01-01", + Description: "Github Workflow CI API token.", + }) + if err != nil { + log.Fatalf("Creating API token: %s", err) + } + + ghf := os.Getenv("GITHUB_ENV") + f, err := os.OpenFile(ghf, os.O_WRONLY|os.O_APPEND, 0644) + if err != nil { + log.Fatalf("Opening $GITHUB_ENV file (%s): %s", ghf, err) + } + defer f.Close() + if _, err := f.WriteString(fmt.Sprintf("CTFD_API_KEY=%s\n", *token.Value)); err != nil { + log.Fatalf("Writing CTFD_API_KEY to $GITHUB_ENV file (%s): %s", ghf, err) + } +}