Skip to content

Commit

Permalink
Add a few tests and a CI pipeline for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nateinaction committed Jul 16, 2024
1 parent 4d689aa commit 08a16ed
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: golangci-lint
name: lint

on:
pull_request:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: goreleaser
name: release

on:
push:
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -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 ./...
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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/ ./...
18 changes: 18 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -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))
}
39 changes: 39 additions & 0 deletions internal/helpers/helpers_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
17 changes: 3 additions & 14 deletions internal/pikvm/sslconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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))
}
42 changes: 42 additions & 0 deletions internal/sslpaths/sslpaths_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit 08a16ed

Please sign in to comment.