Skip to content

Commit

Permalink
ci: remove IaC for integration tests in favor of Github workflow service
Browse files Browse the repository at this point in the history
  • Loading branch information
pandatix committed Jan 5, 2024
1 parent d20430a commit c683ef9
Show file tree
Hide file tree
Showing 18 changed files with 412 additions and 3,829 deletions.
36 changes: 30 additions & 6 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: CI

on: [push, pull_request]

jobs:
Expand All @@ -11,7 +10,7 @@ jobs:
with:
access_token: ${{ github.token }}

unit-tests:
tests:
strategy:
matrix:
go-version: [1.x, 1.21.x]
Expand All @@ -22,6 +21,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 @@ -38,14 +42,34 @@ 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: Run go tests
run: make tests
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: functional.out

go-lint:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ tests:
go test ./api -run=^Test_U_ -json | tee -a gotest.json

@echo "--- Functional tests ---"
go test ./deploy/integration -run=^Test_F_ -json -coverpkg "github.com/ctfer-io/go-ctfd/api" -coverprofile=functional.out | tee -a gotest.json
go test ./api -run=^Test_F_ -coverprofile=functional.out -json | tee -a gotest.json

.PHONY: clean
clean:
rm gotest.json unitary.out functional.out
rm gotest.json functional.out
26 changes: 26 additions & 0 deletions api/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package api_test

import (
"fmt"
"os"
"testing"
)

var (
CTFD_URL = ""
)

func TestMain(m *testing.M) {
u, ok := os.LookupEnv("CTFD_URL")
if !ok {
fmt.Println("Environment variable CTFD is not set, please indicate the domain name/IP address to reach out the cluster.")
os.Exit(1)
}
CTFD_URL = u

os.Exit(m.Run())
}

func ptr[T any](t T) *T {
return &t
}
30 changes: 18 additions & 12 deletions api/reset.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package api

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"

"github.com/gorilla/schema"
"github.com/pkg/errors"
)

type ResetParams struct {
Accounts *string `json:"accounts,omitempty"`
Submissions *string `json:"submissions,omitempty"`
Challenges *string `json:"challenges,omitempty"`
Pages *string `json:"pages,omitempty"`
Notifications *string `json:"notifications,omitempty"`
Accounts *string `schema:"accounts,omitempty"`
Submissions *string `schema:"submissions,omitempty"`
Challenges *string `schema:"challenges,omitempty"`
Pages *string `schema:"pages,omitempty"`
Notifications *string `schema:"notifications,omitempty"`
// Nonce is autofilled by the API wrapper.
// XXX the "nonce" should not be part of the API call but rather be extracted from HTTP headers.
Nonce string `json:"nonce"`
Nonce string `schema:"nonce"`
}

func (client *Client) Reset(params *ResetParams, opts ...Option) error {
Expand All @@ -27,11 +28,16 @@ func (client *Client) Reset(params *ResetParams, opts ...Option) error {
}

// Build request
body, err := json.Marshal(params)
if err != nil {
return errors.Wrap(err, "during JSON marshalling")
str := ""
if params != nil {
val := url.Values{}
if err := schema.NewEncoder().Encode(params, val); err != nil {
return err
}
str = val.Encode()
}
req, _ := http.NewRequest(http.MethodPost, "/admin/reset", bytes.NewBuffer(body))
req, _ := http.NewRequest(http.MethodPost, "/admin/reset", strings.NewReader(str))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

res, err := client.Do(req)
if err != nil {
Expand Down

0 comments on commit c683ef9

Please sign in to comment.