Skip to content

Commit

Permalink
Merge pull request #1 from malletgaetan/tests
Browse files Browse the repository at this point in the history
feat: unit tests
  • Loading branch information
malletgaetan authored Dec 29, 2024
2 parents fdc8e79 + 302b12a commit ce510ef
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 21 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Test Pipeline

on:
pull_request:
branches: [ main ]
push:
branches: [ main ]

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'
cache: true

- name: Get dependencies
run: go mod download

- name: Run tests
run: make test

- name: Run linter
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --timeout=1m
8 changes: 0 additions & 8 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ jobs:
run: |
GOOS=linux GOARCH=amd64 go build -o dockermon-linux-amd64
# - name: Build for macOS (amd64)
# run: |
# GOOS=darwin GOARCH=amd64 go build ./cmd/dockermon -o dockermon-darwin-amd64

# - name: Build for macOS (arm64)
# run: |
# GOOS=darwin GOARCH=arm64 go build ./cmd/dockermon -o dockermon-darwin-arm64

- name: Create Release
id: create_release
uses: softprops/action-gh-release@v1
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ vet:
test:
go test ./...

lint:
golangci-lint run

fuzz:
go test -fuzz=FuzzParseConfig -fuzztime=5m ./internal/config

re: clean all

.PHONY: all clean re fmt
.PHONY: all clean re fmt lint
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [x] Build docker event filter
- [x] Resilience on events listening fail
- [ ] Add more tests
- [ ] CI/CD Pipeline, no push on main (tests, fmt and static analysis)
- [ ] One file installer
- [ ] Event Type wildcard
- [ ] Windows compatibility
1 change: 1 addition & 0 deletions configs/dockermon.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ container::*::5::'/usr/bin/slack_notify','info'
container::die::::'/usr/bin/slack_notify','error'
# timeout can be unset
network::*::::'/usr/bin/stuff'
container:start::5::'/usr/bin/cmd'
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require github.com/docker/docker v27.4.1+incompatible
require (
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
Expand All @@ -22,6 +23,8 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/testify v1.10.0 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.58.0 // indirect
go.opentelemetry.io/otel v1.33.0 // indirect
Expand Down
5 changes: 2 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,15 @@ func (c *Config) Dump() {
var t uint = 0
if cmd.Timeout != 0 {
t = cmd.Timeout
} else if cmd.Timeout != 0 {
t = cmd.Timeout
} else if c.timeout != 0 {
t = c.timeout
}
if t != 0 {
timeout += strconv.FormatUint(uint64(t), 10)
}
fmt.Printf("%s%s%s%s%s%s%s\n", eventType, delimiter, action, delimiter, timeout, delimiter, strings.Join(cmd.Args, ","))
}
}
return
}

func (c *Config) SetCmd(typ string, action string, comd *cmd.Cmd) {
Expand Down
159 changes: 159 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package config

import (
"testing"

"github.com/malletgaetan/dockermon/internal/cmd"
"github.com/stretchr/testify/assert"
)

func TestSetCmd(t *testing.T) {
tests := []struct {
name string
typ string
action string
cmd *cmd.Cmd
validate func(*testing.T, *Config)
}{
{
name: "set new type and action",
typ: "container",
action: "start",
cmd: &cmd.Cmd{
Args: []string{"/usr/bin/notify"},
Timeout: 5,
},
validate: func(t *testing.T, c *Config) {
assert.Len(t, c.map_, 1)
assert.Len(t, c.map_["container"], 1)
assert.Equal(t, uint(5), c.map_["container"]["start"].Timeout)
assert.Equal(t, []string{"/usr/bin/notify"}, c.map_["container"]["start"].Args)
},
},
{
name: "override existing command",
typ: "container",
action: "start",
cmd: &cmd.Cmd{
Args: []string{"/usr/bin/new-notify"},
Timeout: 10,
},
validate: func(t *testing.T, c *Config) {
assert.Len(t, c.map_, 1)
assert.Len(t, c.map_["container"], 1)
assert.Equal(t, uint(10), c.map_["container"]["start"].Timeout)
assert.Equal(t, []string{"/usr/bin/new-notify"}, c.map_["container"]["start"].Args)
},
},
{
name: "add new action to existing type",
typ: "container",
action: "stop",
cmd: &cmd.Cmd{
Args: []string{"/usr/bin/stop-notify"},
Timeout: 3,
},
validate: func(t *testing.T, c *Config) {
assert.Len(t, c.map_, 1)
assert.Len(t, c.map_["container"], 2)
assert.Equal(t, uint(3), c.map_["container"]["stop"].Timeout)
assert.Equal(t, []string{"/usr/bin/stop-notify"}, c.map_["container"]["stop"].Args)
},
},
{
name: "set wildcard action",
typ: "network",
action: "*",
cmd: &cmd.Cmd{
Args: []string{"/usr/bin/network-monitor"},
Timeout: 0,
},
validate: func(t *testing.T, c *Config) {
assert.Contains(t, c.map_, "network")
assert.Contains(t, c.map_["network"], "*")
assert.Equal(t, uint(0), c.map_["network"]["*"].Timeout)
assert.Equal(t, []string{"/usr/bin/network-monitor"}, c.map_["network"]["*"].Args)
},
},
}

config := &Config{
map_: make(map[string]map[string]*cmd.Cmd),
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config.SetCmd(tt.typ, tt.action, tt.cmd)
tt.validate(t, config)
})
}
}

func TestGetCmd(t *testing.T) {
config := &Config{
map_: make(map[string]map[string]*cmd.Cmd),
}

specificCmd := &cmd.Cmd{Args: []string{"/usr/bin/specific"}, Timeout: 5}
wildcardActionCmd := &cmd.Cmd{Args: []string{"/usr/bin/wildcard-action"}, Timeout: 3}

config.SetCmd("container", "start", specificCmd)
config.SetCmd("container", "*", wildcardActionCmd)

tests := []struct {
name string
typ string
action string
expectedCmd *cmd.Cmd
expectError bool
errorType error
}{
{
name: "get specific command",
typ: "container",
action: "start",
expectedCmd: specificCmd,
expectError: false,
},
{
name: "fallback to wildcard action",
typ: "container",
action: "stop",
expectedCmd: wildcardActionCmd,
expectError: false,
},
{
name: "unknown type",
typ: "unknown",
action: "start",
expectedCmd: nil,
expectError: true,
errorType: ErrUnimplemented,
},
{
name: "unknown action without wildcard",
typ: "network",
action: "connect",
expectedCmd: nil,
expectError: true,
errorType: ErrUnimplemented,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd, err := config.GetCmd(tt.typ, tt.action)

if tt.expectError {
assert.Error(t, err)
if tt.errorType != nil {
assert.ErrorIs(t, err, tt.errorType)
}
assert.Nil(t, cmd)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expectedCmd, cmd)
}
})
}
}
1 change: 0 additions & 1 deletion internal/config/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type Error struct {
}

func (e *Error) Error() string {
return e.context
return e.err.Error() + ": " + e.context
}

Expand Down
2 changes: 1 addition & 1 deletion internal/config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func ParseConfig(scanner *bufio.Scanner, hints map[string][]string) (*Config, er
}

parser := &parser{
pos: Position{row: 1, col: 1},
pos: Position{row: 1},
config: config,
scanner: scanner,
hints: hints,
Expand Down
Loading

0 comments on commit ce510ef

Please sign in to comment.