From 4d43a70a3893272aeac0bcf8bf8d42a9504f0dd8 Mon Sep 17 00:00:00 2001 From: Areg Harutyunyan Date: Tue, 4 May 2021 00:58:48 +0100 Subject: [PATCH] Setup a Github action for checking the cloudflared build --- .github/workflows/check.yaml | 20 ++++++++ cmd/cloudflared/tunnel/signal_test.go | 2 + cmd/cloudflared/tunnel/subcommands_test.go | 3 +- .../updater/workers_service_test.go | 13 ++--- metrics/timer.go | 48 ------------------- metrics/timer_test.go | 24 ---------- sshgen/sshgen_test.go | 2 + watcher/file_test.go | 2 + 8 files changed, 32 insertions(+), 82 deletions(-) create mode 100644 .github/workflows/check.yaml delete mode 100644 metrics/timer.go delete mode 100644 metrics/timer_test.go diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml new file mode 100644 index 00000000..f1ee99d7 --- /dev/null +++ b/.github/workflows/check.yaml @@ -0,0 +1,20 @@ +on: [push, pull_request] +name: Check +jobs: + check: + strategy: + matrix: + go-version: [1.15.x] + os: [ubuntu-latest, macos-latest, windows-latest] + runs-on: ${{ matrix.os }} + steps: + - name: Install Go + uses: actions/setup-go@v2 + with: + go-version: ${{ matrix.go-version }} + - name: Install go-sumtype + run: go get github.com/BurntSushi/go-sumtype + - name: Checkout code + uses: actions/checkout@v2 + - name: Test + run: make test diff --git a/cmd/cloudflared/tunnel/signal_test.go b/cmd/cloudflared/tunnel/signal_test.go index a6aa0059..43921c6f 100644 --- a/cmd/cloudflared/tunnel/signal_test.go +++ b/cmd/cloudflared/tunnel/signal_test.go @@ -1,3 +1,5 @@ +// +build !windows + package tunnel import ( diff --git a/cmd/cloudflared/tunnel/subcommands_test.go b/cmd/cloudflared/tunnel/subcommands_test.go index 02b6b4ff..67aad6ea 100644 --- a/cmd/cloudflared/tunnel/subcommands_test.go +++ b/cmd/cloudflared/tunnel/subcommands_test.go @@ -1,7 +1,6 @@ package tunnel import ( - "fmt" "path/filepath" "testing" @@ -95,7 +94,7 @@ func TestTunnelfilePath(t *testing.T) { assert.NoError(t, err) homeDir, err := homedir.Dir() assert.NoError(t, err) - expected := fmt.Sprintf("%s/.cloudflared/%v.json", homeDir, tunnelID) + expected := filepath.Join(homeDir, ".cloudflared", tunnelID.String()+".json") assert.Equal(t, expected, actual) } diff --git a/cmd/cloudflared/updater/workers_service_test.go b/cmd/cloudflared/updater/workers_service_test.go index ccf95c89..94cf8a7e 100644 --- a/cmd/cloudflared/updater/workers_service_test.go +++ b/cmd/cloudflared/updater/workers_service_test.go @@ -1,3 +1,5 @@ +// +build !windows + package updater import ( @@ -13,6 +15,7 @@ import ( "net/http" "net/http/httptest" "os" + "path/filepath" "runtime" "strconv" "strings" @@ -21,6 +24,8 @@ import ( "github.com/stretchr/testify/require" ) +var testFilePath = filepath.Join(os.TempDir(), "test") + func respondWithJSON(w http.ResponseWriter, v interface{}, status int) { data, _ := json.Marshal(v) @@ -208,7 +213,6 @@ func TestUpdateService(t *testing.T) { ts := createServer() defer ts.Close() - testFilePath := "tmpfile" createTestFile(t, testFilePath) defer os.Remove(testFilePath) log.Println("server url: ", ts.URL) @@ -229,7 +233,6 @@ func TestBetaUpdateService(t *testing.T) { ts := createServer() defer ts.Close() - testFilePath := "tmpfile" createTestFile(t, testFilePath) defer os.Remove(testFilePath) @@ -249,7 +252,6 @@ func TestFailUpdateService(t *testing.T) { ts := createServer() defer ts.Close() - testFilePath := "tmpfile" createTestFile(t, testFilePath) defer os.Remove(testFilePath) @@ -263,7 +265,6 @@ func TestNoUpdateService(t *testing.T) { ts := createServer() defer ts.Close() - testFilePath := "tmpfile" createTestFile(t, testFilePath) defer os.Remove(testFilePath) @@ -278,7 +279,6 @@ func TestForcedUpdateService(t *testing.T) { ts := createServer() defer ts.Close() - testFilePath := "tmpfile" createTestFile(t, testFilePath) defer os.Remove(testFilePath) @@ -298,7 +298,6 @@ func TestUpdateSpecificVersionService(t *testing.T) { ts := createServer() defer ts.Close() - testFilePath := "tmpfile" createTestFile(t, testFilePath) defer os.Remove(testFilePath) reqVersion := "2020.9.1" @@ -319,7 +318,6 @@ func TestCompressedUpdateService(t *testing.T) { ts := createServer() defer ts.Close() - testFilePath := "tmpfile" createTestFile(t, testFilePath) defer os.Remove(testFilePath) @@ -339,7 +337,6 @@ func TestUpdateWhenRunningKnownBuggyVersion(t *testing.T) { ts := createServer() defer ts.Close() - testFilePath := "tmpfile" createTestFile(t, testFilePath) defer os.Remove(testFilePath) diff --git a/metrics/timer.go b/metrics/timer.go deleted file mode 100644 index b4f0ae85..00000000 --- a/metrics/timer.go +++ /dev/null @@ -1,48 +0,0 @@ -package metrics - -import ( - "time" - - "github.com/prometheus/client_golang/prometheus" -) - -// Timer assumes the metrics is partitioned by one label -type Timer struct { - startTime map[string]time.Time - metrics *prometheus.HistogramVec - measureUnit time.Duration - labelKey string -} - -func NewTimer(metrics *prometheus.HistogramVec, unit time.Duration, labelKey string) *Timer { - return &Timer{ - startTime: make(map[string]time.Time), - measureUnit: unit, - metrics: metrics, - labelKey: labelKey, - } -} - -func (i *Timer) Start(labelVal string) { - i.startTime[labelVal] = time.Now() -} - -func (i *Timer) End(labelVal string) time.Duration { - if start, ok := i.startTime[labelVal]; ok { - return Latency(start, time.Now()) - } - return 0 -} - -func (i *Timer) Observe(measurement time.Duration, labelVal string) { - metricsLabels := prometheus.Labels{i.labelKey: labelVal} - i.metrics.With(metricsLabels).Observe(float64(measurement / i.measureUnit)) -} - -func (i *Timer) EndAndObserve(labelVal string) { - i.Observe(i.End(labelVal), labelVal) -} - -func Latency(startTime, endTime time.Time) time.Duration { - return endTime.Sub(startTime) -} diff --git a/metrics/timer_test.go b/metrics/timer_test.go deleted file mode 100644 index 18c8ef6a..00000000 --- a/metrics/timer_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package metrics - -import ( - "testing" - "time" - - "github.com/prometheus/client_golang/prometheus" - "github.com/stretchr/testify/assert" -) - -func TestEnd(t *testing.T) { - m := prometheus.NewHistogramVec( - prometheus.HistogramOpts{ - Namespace: "TestCallLatencyWithoutMeasurement", - Name: "Latency", - Buckets: prometheus.LinearBuckets(0, 50, 100), - }, - []string{"key"}, - ) - timer := NewTimer(m, time.Millisecond, "key") - assert.Equal(t, time.Duration(0), timer.End("dne")) - timer.Start("test") - assert.NotEqual(t, time.Duration(0), timer.End("test")) -} diff --git a/sshgen/sshgen_test.go b/sshgen/sshgen_test.go index cb8af2ff..a42345ba 100644 --- a/sshgen/sshgen_test.go +++ b/sshgen/sshgen_test.go @@ -1,3 +1,5 @@ +// +build !windows + package sshgen import ( diff --git a/watcher/file_test.go b/watcher/file_test.go index 93f57769..1e99fe30 100644 --- a/watcher/file_test.go +++ b/watcher/file_test.go @@ -1,3 +1,5 @@ +// +build !windows + package watcher import (