From bcd0cd6ee1b7c6a0f98d200c25e3c60b8a95869c Mon Sep 17 00:00:00 2001 From: sam-at-luther Date: Fri, 30 Jun 2023 10:57:40 -0700 Subject: [PATCH] Add CI checks --- .github/workflows/pushproxng-ci.yml | 15 +++++++ Makefile | 20 +++++++++ client/client.go | 16 ++++---- common.mk | 63 +++++++++++++++++++++++++++++ embeddedclient/embeddedclient.go | 4 +- proxy/proxy.go | 12 +++--- scripts/static-checks.sh | 9 +++++ testmetric/testmetric.go | 2 +- 8 files changed, 124 insertions(+), 17 deletions(-) create mode 100644 .github/workflows/pushproxng-ci.yml create mode 100644 Makefile create mode 100644 common.mk create mode 100755 scripts/static-checks.sh diff --git a/.github/workflows/pushproxng-ci.yml b/.github/workflows/pushproxng-ci.yml new file mode 100644 index 0000000..8504830 --- /dev/null +++ b/.github/workflows/pushproxng-ci.yml @@ -0,0 +1,15 @@ +name: CI Test + +on: + pull_request: + branches: + - main +jobs: + test: + runs-on: ubuntu-latest + container: + image: luthersystems/build-go:v0.0.68 + steps: + - uses: actions/checkout@v3.5.0 + - name: Run CI + run: make citest diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..683a3f0 --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +PROJECT_REL_DIR=./ +include ${PROJECT_REL_DIR}/common.mk +DOCKER_PROJECT_DIR:=$(call DOCKER_DIR, ${PROJECT_REL_DIR}) + +.PHONY: default +default: all + +.PHONY: all + +.PHONY: static-checks +static-checks: + ./scripts/static-checks.sh + +.PHONY: go-test +go-test: + ${GO_TEST_TIMEOUT_10} ./... + +.PHONY: citest +citest: static-checks go-test + @ diff --git a/client/client.go b/client/client.go index 81e8e1c..0dc3808 100644 --- a/client/client.go +++ b/client/client.go @@ -6,7 +6,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "strings" @@ -25,9 +25,9 @@ const ( func run() { viper.SetEnvPrefix("pushproxng_client") viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) - viper.BindEnv("proxy") - viper.BindEnv("fqdn") - viper.BindEnv("target") + _ = viper.BindEnv("proxy") + _ = viper.BindEnv("fqdn") + _ = viper.BindEnv("target") proxy := viper.GetString("proxy") fqdn := viper.GetString("fqdn") @@ -58,7 +58,7 @@ func run() { pause() continue } - respPollBytes, err := ioutil.ReadAll(respPoll.Body) + respPollBytes, err := io.ReadAll(respPoll.Body) respPoll.Body.Close() if err != nil { log(err) @@ -155,13 +155,13 @@ func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") rootCmd.Flags().String("proxy", "invalid", "Proxy address") - viper.BindPFlag("proxy", rootCmd.Flags().Lookup("proxy")) + _ = viper.BindPFlag("proxy", rootCmd.Flags().Lookup("proxy")) rootCmd.Flags().String("fqdn", "invalid", "FQDN") - viper.BindPFlag("fqdn", rootCmd.Flags().Lookup("fqdn")) + _ = viper.BindPFlag("fqdn", rootCmd.Flags().Lookup("fqdn")) rootCmd.Flags().String("target", "invalid", "Target address") - viper.BindPFlag("target", rootCmd.Flags().Lookup("target")) + _ = viper.BindPFlag("target", rootCmd.Flags().Lookup("target")) } func initConfig() { diff --git a/common.mk b/common.mk new file mode 100644 index 0000000..d8b6646 --- /dev/null +++ b/common.mk @@ -0,0 +1,63 @@ +PROJECT=pushproxng +PROJECT_PATH=github.com/luthersystems/${PROJECT} + +BUILDENV_TAG=v0.0.69 +BUILD_IMAGE_GO=luthersystems/build-go:${BUILDENV_TAG} + +TAG_SUFFIX ?= -amd64 +GIT_TAG ?= $(shell git describe --tags --exact-match 2>/dev/null) +GIT_REVISION ?= $(shell git rev-parse --short HEAD) +VERSION ?= $(if $(strip $(GIT_TAG)),$(GIT_TAG),$(GIT_REVISION)) +BUILD_VERSION ?= ${VERSION}${TAG_SUFFIX} + +GO_PKG_VOLUME=${PROJECT}-build-gopath-pkg +GO_PKG_PATH=/go/pkg +GO_PKG_MOUNT=$(if $(CI),-v $(PWD)/build/pkg:${GO_PKG_PATH},--mount='type=volume,source=${GO_PKG_VOLUME},destination=${GO_PKG_PATH}') + +GO_TEST_BASE=go test ${GO_TEST_FLAGS} +GO_TEST_TIMEOUT_10=${GO_TEST_BASE} -timeout 10m + +DOCKER_IN_DOCKER_MOUNT?=-v /var/run/docker.sock:/var/run/docker.sock + +ifeq ($(OS),Windows_NT) + IS_WINDOWS=1 +endif + +CP=cp +RM=rm +DOCKER=docker +DOCKER_SSH_OPTS?=$(shell pinata-ssh-mount || echo "-v $$SSH_AUTH_SOCK:/ssh-agent -v $$HOME/.ssh/known_hosts:/root/.ssh/known_hosts -e SSH_AUTH_SOCK=/ssh-agent") +DOCKER_RUN_OPTS=--rm +DOCKER_RUN=${DOCKER} run ${DOCKER_RUN_OPTS} +CHOWN=$(if $(CIRCLECI),sudo chown,chown) +CHOWN_USR=$(shell id -u) +DOCKER_USER=$(shell id -u):$(shell id -g) +CHOWN_GRP=$(if $(or $(IS_WINDOWS),$(CIRCLECI)),,$(shell id -g)) +DOMAKE=cd $1 && $(MAKE) $2 # NOTE: this is not used for now as it does not work with -j for some versions of Make +MKDIR_P=mkdir -p +TOUCH=touch +GZIP=gzip +GUNZIP=gunzip +TIME_P=time -p +TAR=tar + +# The Makefile determines whether to build a container or not by consulting a +# dummy file that is touched whenever the container is built. The function, +# IMAGE_DUMMY, computes the path to the dummy file. +DUMMY_TARGET=build/$(1)/$(2)/.dummy +IMAGE_DUMMY=$(call DUMMY_TARGET,image,$(1)) +PUSH_DUMMY=$(call DUMMY_TARGET,push,$(1)) +MANIFEST_DUMMY=$(call DUMMY_TARGET,manifest,$(1)) +FQ_DOCKER_IMAGE ?= docker.io/$(1) + +UNAME := $(shell uname) +GIT_LS_FILES=$(shell git ls-files $(1)) + +DOCKER_WIN_DIR=$(shell cygpath -wm $(realpath $(1))) +DOCKER_NIX_DIR=$(realpath $(1)) +DOCKER_DIR=$(if $(IS_WINDOWS),$(call DOCKER_WIN_DIR, $(1)),$(call DOCKER_NIX_DIR, $(1))) + +# print out make variables, e.g.: +# make echo:VERSION +echo\:%: + @echo $($*) diff --git a/embeddedclient/embeddedclient.go b/embeddedclient/embeddedclient.go index 15e35a3..476548f 100644 --- a/embeddedclient/embeddedclient.go +++ b/embeddedclient/embeddedclient.go @@ -6,7 +6,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "net/http/httptest" "time" @@ -48,7 +48,7 @@ func Work(proxy string, fqdn string) { pause() continue } - respPollBytes, err := ioutil.ReadAll(respPoll.Body) + respPollBytes, err := io.ReadAll(respPoll.Body) respPoll.Body.Close() if err != nil { log(err) diff --git a/proxy/proxy.go b/proxy/proxy.go index 748ed07..6d5daea 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -7,7 +7,7 @@ import ( "encoding/hex" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "strings" @@ -202,7 +202,7 @@ func (s *State) InterlockMacroAwaitReply(ctx context.Context, fqdn string, id st func run() { viper.SetEnvPrefix("pushproxng_proxy") viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) - viper.BindEnv("listen") + _ = viper.BindEnv("listen") listenAddress := viper.GetString("listen") @@ -251,7 +251,7 @@ func run() { if err := http.ListenAndServe(listenAddress, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/poll" { - jsonBytes, err := ioutil.ReadAll(r.Body) + jsonBytes, err := io.ReadAll(r.Body) if err != nil { log(err) return @@ -294,7 +294,7 @@ func run() { return } } else if r.URL.Path == "/push" { - jsonBytes, err := ioutil.ReadAll(r.Body) + jsonBytes, err := io.ReadAll(r.Body) if err != nil { log(err) return @@ -373,7 +373,7 @@ func run() { } } else if r.URL.Path == "/probe" { w.WriteHeader(http.StatusOK) - w.Write([]byte("+OK")) + _, _ = w.Write([]byte("+OK")) } else { fmt.Printf("http serve unknown: '%s'\n", r.URL.Path) http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) @@ -407,7 +407,7 @@ func init() { rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file") rootCmd.Flags().String("listen", ":8080", "Listen address") - viper.BindPFlag("listen", rootCmd.Flags().Lookup("listen")) + _ = viper.BindPFlag("listen", rootCmd.Flags().Lookup("listen")) } func initConfig() { diff --git a/scripts/static-checks.sh b/scripts/static-checks.sh new file mode 100755 index 0000000..ae92a2a --- /dev/null +++ b/scripts/static-checks.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash +# Copyright © 2021 Luther Systems, Ltd. All right reserved. + +# This script runs source code checking tools it can be called via a make +# target or can be run automatically during CI testing. + +set -exuo pipefail + +golangci-lint run --timeout=5m -v diff --git a/testmetric/testmetric.go b/testmetric/testmetric.go index 583d8d7..5ac365b 100644 --- a/testmetric/testmetric.go +++ b/testmetric/testmetric.go @@ -8,5 +8,5 @@ import ( func main() { http.Handle("/metrics", promhttp.Handler()) - http.ListenAndServe(":9100", nil) + _ = http.ListenAndServe(":9100", nil) }