-
Notifications
You must be signed in to change notification settings - Fork 1
/
coyote_test.go
153 lines (128 loc) · 4.49 KB
/
coyote_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"strings"
"testing"
"github.com/andreyvit/diff"
"github.com/cucumber/godog"
)
type ctxKey struct{}
func TestFeatures(t *testing.T) {
suite := godog.TestSuite{
TestSuiteInitializer: InitializeTestSuite,
ScenarioInitializer: InitializeScenario,
Options: &godog.Options{
Format: "pretty",
Paths: []string{"features"},
TestingT: t,
},
}
if suite.Run() != 0 {
t.Fatal("non-zero status returned, failed to run feature tests")
}
}
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
cmd := exec.Command("go", "build", ".")
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
ctx.AfterSuite(func() {
err := os.Remove("coyote")
if err != nil {
log.Fatal(err)
}
})
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Given(`^coyote is present locally$`, coyoteIsPresentLocally)
ctx.Given(`^brew is present$`, brewIsPresent)
ctx.When(`^coyote is run with help option$`, coyoteIsRunWithHelpOption)
ctx.When(`^coyote is run with version option$`, coyoteIsRunWithVersionOption)
ctx.When(`^"(.+)" is installed using brew$`, formulaIsInstalledUsingBrew)
ctx.Then(`^help message is printed$`, helpMessageIsPrinted)
ctx.Then(`^version message is printed$`, versionMessageIsPrinted)
ctx.Then(`^coyote is installed$`, coyoteIsInstalled)
}
func coyoteIsPresentLocally() error {
_, err := os.Stat("coyote")
return err
}
func brewIsPresent() error {
_, err := exec.LookPath("brew")
return err
}
func coyoteIsRunWithHelpOption(ctx context.Context) (context.Context, error) {
out, err := exec.Command("./coyote", "-h").Output()
if err != nil {
return ctx, err
}
return context.WithValue(ctx, ctxKey{}, string(out)), nil
}
func coyoteIsRunWithVersionOption(ctx context.Context) (context.Context, error) {
out, err := exec.Command("./coyote", "-v").Output()
if err != nil {
return ctx, err
}
return context.WithValue(ctx, ctxKey{}, string(out)), nil
}
func formulaIsInstalledUsingBrew(formula string) error {
cmd := exec.Command("brew", "install", formula)
if err := cmd.Run(); err != nil {
return err
}
return nil
}
func helpMessageIsPrinted(ctx context.Context) error {
actualOutput := strings.TrimRight(ctx.Value(ctxKey{}).(string), "\n")
if actualOutput == expectedHelpOutput {
return nil
}
return fmt.Errorf("Result not as expected:\n%v", diff.LineDiff(expectedHelpOutput, actualOutput))
}
func versionMessageIsPrinted(ctx context.Context) error {
actualOutput := strings.TrimRight(ctx.Value(ctxKey{}).(string), "\n")
if actualOutput == expectedVersionOutput {
return nil
}
return fmt.Errorf("Result not as expected:\n%v", diff.LineDiff(expectedVersionOutput, actualOutput))
}
func coyoteIsInstalled() error {
_, err := exec.LookPath("coyote")
return err
}
const (
expectedVersionOutput = `coyote version development`
expectedHelpOutput = `NAME:
coyote - Coyote is a RabbitMQ message sink.
USAGE:
coyote [global options]
Examples:
coyote --url amqps://user@myurl --exchange myexchange --store events.sqlite
coyote --url amqps://user:password@myurl --noprompt --exchange myexchange --store events.sqlite
coyote --url amqps://user:password@myurl --noprompt --insecure --exchange myexchange
Exchange binding formats:
--exchange myexchange # All messages in single exchange
--exchange myexchange1=mykey1 # Messages with routing key in a single exchange
--exchange myexchange1=mykey1,myexchange1=mykey2 # Messages with routing keys in a single exchange
--exchange myexchange1,myexchange2 # All messages in multiple exchanges
--exchange myexchange1=mykey1,myexchange2=mykey2 # Messages with routing keys in multiple exchanges
--exchange myexchange1,myexchange2=mykey2 # Messages with or without routing keys in multiple exchanges
VERSION:
development
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--url value RabbitMQ url, must start with amqps:// or amqp://.
--exchange value Exchange & routing key combinations to listen messages.
--queue value Interceptor queue name. If provided, interceptor queue will not be auto deleted.
--store value SQLite filename to store events.
--insecure Skips certificate verification. (default: false)
--noprompt Disables password prompt. (default: false)
--silent Disables terminal print. (default: false)
--help, -h show help
--version, -v print the version`
)