Skip to content

Commit 2cf59b8

Browse files
committed
Add src documentation
1 parent 5b92176 commit 2cf59b8

5 files changed

Lines changed: 55 additions & 11 deletions

File tree

pkg/runtime/executor.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package runtime
22

3+
// Executor interface which will be implemented by all available executors, like ssh or local
34
type Executor interface {
45
Execute(test TestCase) TestResult
56
}

pkg/runtime/local_executor.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,16 @@ import (
66
"strings"
77
)
88

9+
// LocalExecutor will be used to execute tests on the local host
910
type LocalExecutor struct {
1011
}
1112

13+
// NewLocalExecutor creates a new local executor
14+
func NewLocalExecutor() Executor {
15+
return LocalExecutor{}
16+
}
17+
18+
// Execute will execute the given test on the current node
1219
func (e LocalExecutor) Execute(test TestCase) TestResult {
1320
timeoutOpt, err := createTimeoutOption(test.Command.Timeout)
1421
if err != nil {

pkg/runtime/runtime.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828
Skipped
2929
)
3030

31+
// NewRuntime creates a new runtime and inits default nodes
3132
func NewRuntime(nodes ...Node) Runtime {
3233
local := Node{
3334
Name: "local",
@@ -41,6 +42,7 @@ func NewRuntime(nodes ...Node) Runtime {
4142
}
4243
}
4344

45+
// Runtime represents the current runtime, please use NewRuntime() instead of createing an instance directly
4446
type Runtime struct {
4547
Nodes []Node
4648
}
@@ -54,6 +56,9 @@ type TestCase struct {
5456
Nodes []string
5557
}
5658

59+
// Node represents a configured node with everything needed to connect to the given host
60+
// which is defined in the type property
61+
// If the type is not available the test will fail and stop its execution
5762
type Node struct {
5863
Name string
5964
Type string
@@ -147,7 +152,7 @@ func (r *Runtime) Start(tests []TestCase) <-chan TestResult {
147152
defer wg.Done()
148153

149154
for t := range tests {
150-
// If no node was set use local mode
155+
// If no node was set use local mode as default
151156
if len(t.Nodes) == 0 {
152157
t.Nodes = []string{"local"}
153158
}
@@ -183,23 +188,18 @@ func (r *Runtime) Start(tests []TestCase) <-chan TestResult {
183188

184189
func (r *Runtime) getExecutor(node string) Executor {
185190
if len(r.Nodes) == 0 {
186-
return LocalExecutor{}
191+
return NewLocalExecutor()
187192
}
188193

189194
for _, n := range r.Nodes {
190195
if n.Name == node {
191196
switch n.Type {
192197
case "ssh":
193-
return SSHExecutor{
194-
Password: n.Pass,
195-
IdentityFile: n.IdentityFile,
196-
User: n.User,
197-
Host: n.Addr,
198-
}
198+
return NewSSHExecutor(n.Addr, n.User, WithIdentityFile(n.IdentityFile), WithPassword(n.Pass))
199199
case "local":
200-
return LocalExecutor{}
200+
return NewLocalExecutor()
201201
case "":
202-
return LocalExecutor{}
202+
return NewLocalExecutor()
203203
default:
204204
log.Fatal(fmt.Sprintf("Node type %s not found for node %s", n.Type, n.Name))
205205
}

pkg/runtime/ssh_executor.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,34 @@ type SSHExecutor struct {
1818
IdentityFile string
1919
}
2020

21+
// WithIdentityFile sets the identity file option for the ssh executor
22+
func WithIdentityFile(identityFile string) func(e *SSHExecutor) {
23+
return func(e *SSHExecutor) {
24+
e.IdentityFile = identityFile
25+
}
26+
}
27+
28+
// WithPassword sets the identity file option for the ssh executor
29+
func WithPassword(pass string) func(e *SSHExecutor) {
30+
return func(e *SSHExecutor) {
31+
e.Password = pass
32+
}
33+
}
34+
35+
// NewSSHExecutor creates a new executor
36+
func NewSSHExecutor(host string, user string, opts ...func(e *SSHExecutor)) Executor {
37+
e := SSHExecutor{
38+
Host: host,
39+
User: user,
40+
}
41+
42+
for _, o := range opts {
43+
o(&e)
44+
}
45+
46+
return e
47+
}
48+
2149
// Execute executes a command on a remote host viá SSH
2250
func (e SSHExecutor) Execute(test TestCase) TestResult {
2351
if test.Command.InheritEnv {

pkg/suite/suite.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ import (
55
"github.com/SimonBaeumer/commander/pkg/runtime"
66
)
77

8-
// Suite represents the current tests and configs
8+
// Suite represents the current tests, nodes and configs.
9+
// It is used by the runtime to execute all tests and is an abstraction for the given source.
10+
// In example it could be possible to add more formats like XML or a custom DSL implementation.
911
type Suite struct {
1012
TestCases []runtime.TestCase
1113
Config runtime.TestConfig
1214
Nodes []runtime.Node
1315
}
1416

17+
// GetNodes returns all nodes defined in the suite
1518
func (s Suite) GetNodes() []runtime.Node {
1619
return s.Nodes
1720
}
1821

22+
// GetNodeByName returns a node by the given name
1923
func (s Suite) GetNodeByName(name string) (runtime.Node, error) {
2024
for _, n := range s.Nodes {
2125
if n.Name == name {
@@ -25,6 +29,8 @@ func (s Suite) GetNodeByName(name string) (runtime.Node, error) {
2529
return runtime.Node{}, fmt.Errorf("could not find node with name %s", name)
2630
}
2731

32+
// AddTest pushes a new test to the suite
33+
// if the test was already added it will panic
2834
func (s Suite) AddTest(t runtime.TestCase) {
2935
if _, err := s.GetTestByTitle(t.Title); err == nil {
3036
panic(fmt.Sprintf("Tests %s was already added to the suite", t.Title))
@@ -37,6 +43,7 @@ func (s Suite) GetTests() []runtime.TestCase {
3743
return s.TestCases
3844
}
3945

46+
// GetTestByTitle returns a test by title, if the test was not found an error is returned
4047
func (s Suite) GetTestByTitle(title string) (runtime.TestCase, error) {
4148
for _, t := range s.GetTests() {
4249
if t.Title == title {
@@ -46,6 +53,7 @@ func (s Suite) GetTestByTitle(title string) (runtime.TestCase, error) {
4653
return runtime.TestCase{}, fmt.Errorf("could not find test %s", title)
4754
}
4855

56+
// GetGlobalConfig returns the global configuration which applies to the complete suite
4957
func (s Suite) GetGlobalConfig() runtime.TestConfig {
5058
return s.Config
5159
}

0 commit comments

Comments
 (0)