From 08a16eda87467f05d3b5f34ae2e6b8b8b234154a Mon Sep 17 00:00:00 2001 From: Nate Gay Date: Mon, 15 Jul 2024 22:23:37 -0500 Subject: [PATCH] Add a few tests and a CI pipeline for tests --- .../{golangci-lint.yaml => lint.yaml} | 2 +- .../{goreleaser.yaml => release.yaml} | 2 +- .github/workflows/test.yaml | 16 +++++++ Makefile | 4 ++ internal/helpers/helpers.go | 18 ++++++++ internal/helpers/helpers_test.go | 39 +++++++++++++++++ internal/pikvm/sslconf.go | 17 ++------ internal/sslpaths/sslpaths_test.go | 42 +++++++++++++++++++ 8 files changed, 124 insertions(+), 16 deletions(-) rename .github/workflows/{golangci-lint.yaml => lint.yaml} (94%) rename .github/workflows/{goreleaser.yaml => release.yaml} (97%) create mode 100644 .github/workflows/test.yaml create mode 100644 internal/helpers/helpers.go create mode 100644 internal/helpers/helpers_test.go create mode 100644 internal/sslpaths/sslpaths_test.go diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/lint.yaml similarity index 94% rename from .github/workflows/golangci-lint.yaml rename to .github/workflows/lint.yaml index 51a1a85..82a3827 100644 --- a/.github/workflows/golangci-lint.yaml +++ b/.github/workflows/lint.yaml @@ -1,4 +1,4 @@ -name: golangci-lint +name: lint on: pull_request: diff --git a/.github/workflows/goreleaser.yaml b/.github/workflows/release.yaml similarity index 97% rename from .github/workflows/goreleaser.yaml rename to .github/workflows/release.yaml index ca64e30..d039289 100644 --- a/.github/workflows/goreleaser.yaml +++ b/.github/workflows/release.yaml @@ -1,4 +1,4 @@ -name: goreleaser +name: release on: push: diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..f50c958 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,16 @@ +name: test + +on: + pull_request: + +jobs: + gotest: + name: test + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: stable + - name: go test + run: go test -v -race -cover ./... diff --git a/Makefile b/Makefile index f3b902e..f4d9b78 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,10 @@ fmt: -w /app \ golangci/golangci-lint:$(GOLANGCI_LINT_VERSION) golangci-lint run --fix +.PHONY: test +test: + go test -v -race -cover ./... + .PHONY: build build: GOOS=linux GOARCH=arm CGO_ENABLED=0 go build -o bin/ ./... diff --git a/internal/helpers/helpers.go b/internal/helpers/helpers.go new file mode 100644 index 0000000..37ad8bd --- /dev/null +++ b/internal/helpers/helpers.go @@ -0,0 +1,18 @@ +package helpers + +import ( + "fmt" + "regexp" +) + +// SetLine sets the line in the contents if the regex matches +func SetLine(contents []string, regex *regexp.Regexp, replacementLine string) []string { + for i, line := range contents { + if regex.MatchString(line) { + contents[i] = replacementLine + return contents + } + } + + return append(contents, fmt.Sprintf("%s\n", replacementLine)) +} diff --git a/internal/helpers/helpers_test.go b/internal/helpers/helpers_test.go new file mode 100644 index 0000000..804fcc5 --- /dev/null +++ b/internal/helpers/helpers_test.go @@ -0,0 +1,39 @@ +package helpers_test + +import ( + "regexp" + "testing" + + "github.com/nateinaction/pikvm-tailscale-cert-renewer/internal/helpers" +) + +func TestSetLine(t *testing.T) { + t.Parallel() + + contents := []string{ + "line 1", + "line 2", + "line 3", + } + + regex := regexp.MustCompile(`^line \d$`) + certLine := "new line" + + expected := []string{ + "new line", + "line 2", + "line 3", + } + + result := helpers.SetLine(contents, regex, certLine) + + if len(result) != len(expected) { + t.Errorf("Expected %d lines, but got %d", len(expected), len(result)) + } + + for i, line := range result { + if line != expected[i] { + t.Errorf("Expected line '%s', but got '%s'", expected[i], line) + } + } +} diff --git a/internal/pikvm/sslconf.go b/internal/pikvm/sslconf.go index 21db446..5c2e5b9 100644 --- a/internal/pikvm/sslconf.go +++ b/internal/pikvm/sslconf.go @@ -8,6 +8,7 @@ import ( "slices" "strings" + "github.com/nateinaction/pikvm-tailscale-cert-renewer/internal/helpers" "github.com/nateinaction/pikvm-tailscale-cert-renewer/internal/sslpaths" ) @@ -51,8 +52,8 @@ func WriteNginxConfig(ssl *sslpaths.SSLPaths) error { lines := strings.Split(string(b), "\n") - lines = setLine(lines, certlineRegex, ssl.GetNginxConfigCertLine()) - lines = setLine(lines, keylineRegex, ssl.GetNginxConfigKeyLine()) + lines = helpers.SetLine(lines, certlineRegex, ssl.GetNginxConfigCertLine()) + lines = helpers.SetLine(lines, keylineRegex, ssl.GetNginxConfigKeyLine()) if err := SetFSReadWrite(); err != nil { return fmt.Errorf("failed filesystem mode change: %w", err) @@ -72,15 +73,3 @@ func WriteNginxConfig(ssl *sslpaths.SSLPaths) error { return nil } - -// setLine sets the line in the contents if the regex matches -func setLine(contents []string, regex *regexp.Regexp, certLine string) []string { - for i, line := range contents { - if regex.MatchString(line) { - contents[i] = certLine - return contents - } - } - - return append(contents, fmt.Sprintf("%s\n", certLine)) -} diff --git a/internal/sslpaths/sslpaths_test.go b/internal/sslpaths/sslpaths_test.go new file mode 100644 index 0000000..8986f62 --- /dev/null +++ b/internal/sslpaths/sslpaths_test.go @@ -0,0 +1,42 @@ +package sslpaths_test + +import ( + "testing" + + "github.com/nateinaction/pikvm-tailscale-cert-renewer/internal/sslpaths" +) + +func TestNewSSLPaths(t *testing.T) { + t.Parallel() + + dir := "/path/to/dir" + domain := "example.com" + + sslP := sslpaths.NewSSLPaths(dir, domain) + + if sslP.GetCertPath() != "/path/to/dir/example.com.crt" { + t.Errorf("Expected cert path '/path/to/dir/example.com.crt', but got '%s'", sslP.GetCertPath()) + } + + if sslP.GetKeyPath() != "/path/to/dir/example.com.key" { + t.Errorf("Expected key path '/path/to/dir/example.com.key', but got '%s'", sslP.GetKeyPath()) + } + + if sslP.GetDir() != "/path/to/dir" { + t.Errorf("Expected dir '/path/to/dir', but got '%s'", sslP.GetDir()) + } + + if sslP.GetDomain() != "example.com" { + t.Errorf("Expected domain 'example.com', but got '%s'", sslP.GetDomain()) + } + + if sslP.GetNginxConfigCertLine() != "ssl_certificate /path/to/dir/example.com.crt;" { + t.Errorf("Expected nginx config cert line 'ssl_certificate /path/to/dir/example.com.crt;', but got '%s'", + sslP.GetNginxConfigCertLine()) + } + + if sslP.GetNginxConfigKeyLine() != "ssl_certificate_key /path/to/dir/example.com.key;" { + t.Errorf("Expected nginx config key line 'ssl_certificate_key /path/to/dir/example.com.key;', but got '%s'", + sslP.GetNginxConfigKeyLine()) + } +}