From 979c8ea6ca035aa818f7965887af2ca415b7b67c Mon Sep 17 00:00:00 2001 From: Victor Yves Crispim Date: Fri, 29 Sep 2023 15:17:01 -0300 Subject: [PATCH] ci: add Go-related workflows and steps --- .github/workflows/build.yml | 9 ++++ .github/workflows/go-code-quality.yml | 34 +++++++++++++ .github/workflows/rust-code-quality.yml | 2 +- internal/pkg/services/graphql-service.go | 5 +- internal/pkg/services/graphql-service_test.go | 49 +++++++++++++++++++ 5 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/go-code-quality.yml create mode 100644 internal/pkg/services/graphql-service_test.go diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8a5bd075d..5fa29a7dc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -78,6 +78,15 @@ jobs: path: offchain/schema.graphql if-no-files-found: error + - name: Install Go + uses: actions/setup-go@v4 + with: + go-version-file: 'go.mod' + + - name: Run Go tests + working-directory: ${{ github.workspace }} + run: go test ./internal/pkg/services + build_docker: runs-on: ubuntu-22.04 needs: diff --git a/.github/workflows/go-code-quality.yml b/.github/workflows/go-code-quality.yml new file mode 100644 index 000000000..191263839 --- /dev/null +++ b/.github/workflows/go-code-quality.yml @@ -0,0 +1,34 @@ +name: Assess Go code quality + +on: + push: + paths: + - ".github/workflows/go-code-quality.yml" + - "cmd/**" + - "internal/**" + +jobs: + assess-go-code-quality: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + with: + submodules: recursive + + - uses: actions/setup-go@v4 + with: + go-version-file: "go.mod" + + - name: Check code format + run: | + output=$(gofmt -s -l .) + if [ -n "$output" ]; then + echo "$output" + exit 1; + fi + + - name: Run linter + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54.2 diff --git a/.github/workflows/rust-code-quality.yml b/.github/workflows/rust-code-quality.yml index bbfde7e25..6f9d843cd 100644 --- a/.github/workflows/rust-code-quality.yml +++ b/.github/workflows/rust-code-quality.yml @@ -4,7 +4,7 @@ name: Assess Rust code quality on: push: paths: - - '.github/workflows/code-quality.yml' + - '.github/workflows/rust-code-quality.yml' - 'offchain/**' jobs: diff --git a/internal/pkg/services/graphql-service.go b/internal/pkg/services/graphql-service.go index 3f8808105..8dccd87da 100644 --- a/internal/pkg/services/graphql-service.go +++ b/internal/pkg/services/graphql-service.go @@ -30,7 +30,10 @@ func (g GraphQLService) Start(ctx context.Context) error { go func() { <-ctx.Done() fmt.Printf("%v: %v\n", g.String(), ctx.Err()) - cmd.Process.Signal(syscall.SIGTERM) + if err := cmd.Process.Signal(syscall.SIGTERM); err != nil { + msg := "%v: failed to send SIGTERM to %v\n" + fmt.Printf(msg, g.String(), binaryName) + } }() err := cmd.Wait() diff --git a/internal/pkg/services/graphql-service_test.go b/internal/pkg/services/graphql-service_test.go new file mode 100644 index 000000000..417f0c6f4 --- /dev/null +++ b/internal/pkg/services/graphql-service_test.go @@ -0,0 +1,49 @@ +package services + +import ( + "context" + "fmt" + "os" + "os/exec" + "path/filepath" + "syscall" + "testing" + "time" +) + +func TestGraphQLService(t *testing.T) { + t.Run("it stops when the context is cancelled", func(t *testing.T) { + service := GraphQLService{} + setupEnvVars() + ctx, cancel := context.WithCancel(context.Background()) + exit := make(chan error) + + go func() { + <-time.After(100 * time.Millisecond) + if err := service.Start(ctx); err != nil { + exit <- err + } + }() + + <-time.After(200 * time.Millisecond) + cancel() + + err := <-exit + exitError, ok := err.(*exec.ExitError) + if !ok || !assertExitErrorWasCausedBy(exitError, syscall.SIGTERM) { + fmt.Printf("service exited for the wrong reason: %v", err) + t.FailNow() + } + }) +} + +func setupEnvVars() { + abs, _ := filepath.Abs("../../../offchain/target/debug") + os.Setenv("PATH", abs) + os.Setenv("POSTGRES_PASSWORD", "pw") +} + +func assertExitErrorWasCausedBy(err *exec.ExitError, signal syscall.Signal) bool { + status := err.Sys().(syscall.WaitStatus) + return status.Signal() == signal +}