Skip to content

Commit f9dfb4d

Browse files
committed
Add tests in CI for the project
1 parent 0533988 commit f9dfb4d

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

.github/workflows/ci.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
name: CI
3+
4+
on:
5+
push:
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
env:
12+
NGINX_VERSION: latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- uses: actions/setup-go@v4
16+
with:
17+
go-version: '1.22'
18+
- name: Go Tests
19+
run: go test ./...
20+
- name: Cache nginx image
21+
id: cache-nginx
22+
uses: actions/cache@v3
23+
with:
24+
path: ~/.cache/nginx.tar
25+
key: ${{ runner.os }}-nginx-${{ env.NGINX_VERSION }}
26+
- name: Load nginx image from cache
27+
if: steps.cache-nginx.outputs.cache-hit == 'true'
28+
run: docker load -i ~/.cache/nginx.tar
29+
- name: Pull nginx image
30+
if: steps.cache-nginx.outputs.cache-hit != 'true'
31+
run: |
32+
docker pull "nginx:$NGINX_VERSION"
33+
mkdir -p ~/.cache
34+
docker save "nginx:$NGINX_VERSION" -o ~/.cache/nginx.tar
35+
- name: Validate generated nginx config
36+
run: |
37+
go run ./cmd/sea/main.go > nginx.conf
38+
docker run --rm \
39+
-v "$PWD/nginx.conf:/etc/nginx/conf.d/default.conf:ro" \
40+
"nginx:$NGINX_VERSION" \
41+
nginx -t

.yamllint.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
extends: default
3+
rules:
4+
# Allow `foo: "on"` and other commonly used values.
5+
truthy: disable

cmd/sea/main_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
)
9+
10+
func TestEscapeSpace(t *testing.T) {
11+
got := escapeSpace("hello world")
12+
if got != "hello\\ world" {
13+
t.Errorf("expected 'hello\\\\ world', got %q", got)
14+
}
15+
}
16+
17+
func TestLoadConfig(t *testing.T) {
18+
dir := t.TempDir()
19+
path := filepath.Join(dir, "cfg.toml")
20+
data := []byte(`listen = 8080
21+
server_name = "example.com"
22+
23+
[[custom_keywords]]
24+
phrase = "foo bar"
25+
dest = "google"`)
26+
if err := os.WriteFile(path, data, 0644); err != nil {
27+
t.Fatalf("failed to write config: %v", err)
28+
}
29+
cfg, err := loadConfig(path)
30+
if err != nil {
31+
t.Fatalf("loadConfig returned error: %v", err)
32+
}
33+
if cfg.Listen != 8080 {
34+
t.Errorf("expected listen 8080, got %d", cfg.Listen)
35+
}
36+
if cfg.ServerName != "example.com" {
37+
t.Errorf("expected server name example.com, got %s", cfg.ServerName)
38+
}
39+
if len(cfg.CustomKeywords) != 1 {
40+
t.Fatalf("expected 1 custom keyword, got %d", len(cfg.CustomKeywords))
41+
}
42+
if cfg.CustomKeywords[0].Phrase != "foo bar" || cfg.CustomKeywords[0].Dest != "google" {
43+
t.Errorf("unexpected custom keyword %+v", cfg.CustomKeywords[0])
44+
}
45+
}
46+
47+
func TestGenerateNginx(t *testing.T) {
48+
cfg := Config{Listen: 8080, ServerName: "example.com", CustomKeywords: []KeywordRule{{Phrase: "foo", Dest: "google"}}}
49+
out, err := generateNginx(cfg)
50+
if err != nil {
51+
t.Fatalf("generateNginx returned error: %v", err)
52+
}
53+
if !strings.Contains(out, "server_name example.com;") {
54+
t.Errorf("generated config missing server name: %s", out)
55+
}
56+
if !strings.Contains(out, "~*(?i)^foo$") {
57+
t.Errorf("generated config missing custom rule: %s", out)
58+
}
59+
}

0 commit comments

Comments
 (0)