Skip to content

Commit

Permalink
ci: run acceptance tests from github workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Jan 5, 2024
1 parent bf67c93 commit a1e9315
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
36 changes: 33 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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"
71 changes: 71 additions & 0 deletions cmd/setup/main.go
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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)
}
}

0 comments on commit a1e9315

Please sign in to comment.