-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from malletgaetan/tests
feat: unit tests
- Loading branch information
Showing
13 changed files
with
377 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.