From 83cabb31b2ddf06cafb8cd75869a8d09fabc122e Mon Sep 17 00:00:00 2001 From: Rory Z <16801068+Rory-Z@users.noreply.github.com> Date: Tue, 17 Oct 2023 15:59:38 +0800 Subject: [PATCH] style: format makefile Signed-off-by: Rory Z <16801068+Rory-Z@users.noreply.github.com> --- .github/workflows/build.yaml | 2 ++ .github/workflows/golangci-lint.yml | 2 -- .github/workflows/test.yaml | 3 +- Makefile | 28 ++++++++--------- main_test.go | 47 ++++++----------------------- 5 files changed, 24 insertions(+), 58 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 1973787..8fee6e8 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -30,6 +30,8 @@ jobs: flavor: | latest=true tags: | + type=sha,short=true + type=ref,event=pr type=ref,event=branch type=ref,event=tag type=semver,pattern={{version}} diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index e411f1f..365abe2 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -5,7 +5,6 @@ on: - "go.sum" - "go.mod" - "**.go" - - "scripts/errcheck_excludes.txt" - ".github/workflows/golangci-lint.yml" - ".golangci.yml" pull_request: @@ -13,7 +12,6 @@ on: - "go.sum" - "go.mod" - "**.go" - - "scripts/errcheck_excludes.txt" - ".github/workflows/golangci-lint.yml" - ".golangci.yml" diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 8d11b19..f07eae2 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -16,7 +16,7 @@ concurrency: cancel-in-progress: true jobs: - lint: + test: runs-on: ubuntu-latest steps: @@ -27,4 +27,3 @@ jobs: - run: go install github.com/google/go-licenses@latest - run: $(go env GOPATH)/bin/go-licenses check . --disallowed_types forbidden,restricted,unknown - run: make test - diff --git a/Makefile b/Makefile index c1d3130..dd48474 100644 --- a/Makefile +++ b/Makefile @@ -11,26 +11,22 @@ # See the License for the specific language governing permissions and # limitations under the License. -# Ensure that 'all' is the default target otherwise it will be the first target from Makefile.common. -all:: +all: build -ARCH = $(shell go env GOARCH) -OS = $(shell go env GOOS) - -.PHONY: build LOCALBIN +.PHONY: build build: - CGO_ENABLED=0 GOOS=${OS} GOARCH=${ARCH} go build -o .build/${OS}-${ARCH}/emqx-exporter + go build -o $(LOCALBIN)/$(PROJECT_NAME) .PHONY: test -test: - go test -race --cover -covermode=atomic -coverpkg=./... -coverprofile=cover.out ./... +test: build + go test -v -race --cover -covermode=atomic -coverpkg=./... -coverprofile=cover.out ./... -DOCKER_IMAGE_NAME ?= emqx-exporter -.PHONY: docker-build -docker-build: - docker build -t ${DOCKER_IMAGE_NAME} . +.PHONY: docker +docker: + docker build -t $(PROJECT_NAME) . -## Location to install dependencies to -LOCALBIN ?= $(shell pwd)/.build/${OS}-${ARCH} +PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) +PROJECT_NAME := emqx-exporter +LOCALBIN ?= $(PROJECT_DIR)/bin $(LOCALBIN): - mkdir -p $(LOCALBIN) \ No newline at end of file + mkdir -p $(LOCALBIN) diff --git a/main_test.go b/main_test.go index 400f39b..fc64555 100644 --- a/main_test.go +++ b/main_test.go @@ -19,34 +19,34 @@ import ( "net/http" "os" "os/exec" - "path/filepath" "testing" "time" "github.com/prometheus/procfs" ) -var ( - binary = filepath.Join(os.Getenv("GOPATH"), "bin/emqx-exporter") -) - const ( - address = "localhost:19100" + address = "localhost:19100" + binary = "bin/emqx-exporter" + configFile = "config/example/config.yaml" ) func TestFileDescriptorLeak(t *testing.T) { if _, err := os.Stat(binary); err != nil { - t.Skipf("emqx-exporter binary not available, try to run `make build` first: %s", err) + // t.Skipf("emqx-exporter binary not available, try to run `make build` first: %s", err) + t.Errorf("emqx-exporter binary not available, try to run `make build` first: %s", err) } + fs, err := procfs.NewDefaultFS() if err != nil { - t.Skipf("proc filesystem is not available, but currently required to read number of open file descriptors: %s", err) + // t.Skipf("proc filesystem is not available, but currently required to read number of open file descriptors: %s", err) + t.Errorf("proc filesystem is not available, but currently required to read number of open file descriptors: %s", err) } if _, err := fs.Stat(); err != nil { t.Errorf("unable to read process stats: %s", err) } - exporter := exec.Command(binary, "--web.listen-address", address) + exporter := exec.Command(binary, "--web.listen-address", address, "--config.file", configFile) test := func(pid int) error { if err := queryExporter(address); err != nil { return err @@ -79,35 +79,6 @@ func TestFileDescriptorLeak(t *testing.T) { } } -func TestHandlingOfDuplicatedMetrics(t *testing.T) { - if _, err := os.Stat(binary); err != nil { - t.Skipf("emqx-exporter binary not available, try to run `make build` first: %s", err) - } - - dir, err := os.MkdirTemp("", "node-exporter") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(dir) - - content := []byte("dummy_metric 1\n") - if err := os.WriteFile(filepath.Join(dir, "a.prom"), content, 0600); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(filepath.Join(dir, "b.prom"), content, 0600); err != nil { - t.Fatal(err) - } - - exporter := exec.Command(binary, "--web.listen-address", address, "--collector.textfile.directory", dir) - test := func(_ int) error { - return queryExporter(address) - } - - if err := runCommandAndTests(exporter, address, test); err != nil { - t.Error(err) - } -} - func queryExporter(address string) error { resp, err := http.Get(fmt.Sprintf("http://%s/metrics", address)) if err != nil {