/
module_test.go
126 lines (110 loc) · 3.03 KB
/
module_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package neat_test
import (
"testing"
"github.com/jsternberg/neat"
)
type MockFactory struct{}
func (*MockFactory) New(args ...interface{}) (neat.Module, error) {
var v interface{}
if len(args) >= 1 {
v = args[0]
}
return &Mock{v}, nil
}
type Mock struct {
v interface{}
}
func (m *Mock) Execute(p *neat.Playbook) (interface{}, neat.ModuleStatus, error) {
if m.v != nil {
switch v := m.v.(type) {
case error:
me := neat.ModuleE{Module: m}
return me.Fail(v.Error())
case neat.ModuleStatus:
return nil, v, nil
default:
panic(v)
}
}
return nil, neat.ModuleOk, nil
}
func TestModuleFunc(t *testing.T) {
registry := &neat.ModuleRegistry{}
registry.Register("mock", neat.ModuleFunc(
func(args ...interface{}) (neat.Module, error) {
return &Mock{}, nil
},
))
mf := registry.Lookup("mock")
_, err := mf.New()
if err != nil {
t.Fatalf("unable to lookup mock module")
}
}
func TestModuleE_Ok(t *testing.T) {
registry := &neat.ModuleRegistry{}
registry.Register("mock", &MockFactory{})
m := neat.ModuleE{Module: registry.MustCreate("mock")}
result, status, err := m.Ok("module succeeded")
actual, ok := result.(string)
if !ok || actual != "module succeeded" {
t.Errorf("expected result to be 'module succeded', got '%v'", result)
}
if status != neat.ModuleOk {
t.Errorf("expected status to be %s, got %s", neat.ModuleOk, status)
}
if err != nil {
t.Error(err)
}
}
func TestModuleE_Changed(t *testing.T) {
registry := &neat.ModuleRegistry{}
registry.Register("mock", &MockFactory{})
m := neat.ModuleE{Module: registry.MustCreate("mock")}
m.Changed = true
result, status, err := m.Ok("module succeeded")
actual, ok := result.(string)
if !ok || actual != "module succeeded" {
t.Errorf("expected result to be 'module succeded', got '%v'", result)
}
if status != neat.ModuleChanged {
t.Errorf("expected status to be %s, got %s", neat.ModuleChanged, status)
}
if err != nil {
t.Error(err)
}
}
func TestModuleE_Fail(t *testing.T) {
registry := &neat.ModuleRegistry{}
registry.Register("mock", &MockFactory{})
m := neat.ModuleE{Module: registry.MustCreate("mock")}
result, status, err := m.Fail("module failed to run")
if result != nil {
t.Errorf("expected result to be nil, got %v", result)
}
if status != neat.ModuleFailed {
t.Errorf("expected status to be %s, got %s", neat.ModuleFailed, status)
}
if err == nil || err.Error() != "module failed to run" {
t.Errorf("expected error to be 'module failed to run', got '%s'", err)
}
}
func TestModuleStatus_String(t *testing.T) {
tests := []struct {
Expected string
Status neat.ModuleStatus
}{
{Expected: "ok", Status: neat.ModuleOk},
{Expected: "changed", Status: neat.ModuleChanged},
{Expected: "deferred", Status: neat.ModuleDeferred},
{Expected: "skipped", Status: neat.ModuleSkipped},
{Expected: "failed", Status: neat.ModuleFailed},
{Expected: "unknown", Status: -1},
}
for _, test := range tests {
actual := test.Status.String()
if actual != test.Expected {
t.Errorf("expected '%s', got '%s'", test.Expected, actual)
}
}
}