Skip to content

Commit 3d9b7de

Browse files
authored
test(cli): add resiliency to TestAlertClose (#1254)
GROW-1544
1 parent feb5cbe commit 3d9b7de

File tree

7 files changed

+654
-39
lines changed

7 files changed

+654
-39
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ require (
5858
github.com/aws/smithy-go v1.13.5
5959
github.com/go-git/go-git/v5 v5.5.2
6060
github.com/google/uuid v1.3.0
61+
github.com/hashicorp/consul/sdk v0.13.1
6162
github.com/mattn/go-isatty v0.0.18
6263
github.com/mitchellh/hashstructure/v2 v2.0.2
6364
github.com/pmezard/go-difflib v1.0.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
271271
github.com/googleapis/gax-go/v2 v2.7.0 h1:IcsPKeInNvYi7eqSaDjiZqDDKu5rsmunY0Y1YupQSSQ=
272272
github.com/googleapis/gax-go/v2 v2.7.0/go.mod h1:TEop28CZZQ2y+c0VxMUmu1lV+fQx57QpBWsYpwqHJx8=
273273
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
274+
github.com/hashicorp/consul/sdk v0.13.1 h1:EygWVWWMczTzXGpO93awkHFzfUka6hLYJ0qhETd+6lY=
275+
github.com/hashicorp/consul/sdk v0.13.1/go.mod h1:SW/mM4LbKfqmMvcFu8v+eiQQ7oitXEFeiBe9StxERb0=
274276
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
275277
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
276278
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=

integration/alert_close_test.go

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,25 @@
2020
package integration
2121

2222
import (
23+
"strings"
2324
"testing"
25+
"time"
2426

27+
"github.com/hashicorp/consul/sdk/testutil/retry"
2528
"github.com/stretchr/testify/assert"
2629
)
2730

31+
func threeTimesWaitOneSecond() *retry.Counter {
32+
// ThreeTimes repeats an operation three times and waits 1s in between.
33+
return &retry.Counter{Count: 3, Wait: 1 * time.Second}
34+
}
35+
36+
func alreadyClosedRetry(r *retry.R, s string) {
37+
if strings.Contains(s, "The action on this alert is not allowed") {
38+
r.Error("popAlert returned a closed alert")
39+
}
40+
}
41+
2842
func TestAlertCloseMissingArg(t *testing.T) {
2943
out, err, exitcode := LaceworkCLIWithTOMLConfig("alert", "close")
3044
assert.Empty(t, out.String(), "STDOUT should be empty")
@@ -40,52 +54,61 @@ func TestAlertCloseBadID(t *testing.T) {
4054
}
4155

4256
func TestAlertCloseReasonSurvey(t *testing.T) {
43-
id, err := popAlert()
44-
if err != nil {
45-
assert.FailNow(t, err.Error())
46-
}
47-
out, stderr, exitcode := LaceworkCLIWithTOMLConfig("alert", "close", id)
48-
assert.Contains(t, out.String(), "[Use arrows to move, type to filter]")
49-
assert.Contains(t, stderr.String(), "unable to process alert close reason: EOF")
50-
assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one")
57+
retry.RunWith(threeTimesWaitOneSecond(), t, func(r *retry.R) {
58+
id, err := popAlert()
59+
if err != nil {
60+
assert.FailNow(t, err.Error())
61+
}
62+
out, stderr, exitcode := LaceworkCLIWithTOMLConfig("alert", "close", id)
63+
alreadyClosedRetry(r, stderr.String())
64+
assert.Contains(t, out.String(), "[Use arrows to move, type to filter]")
65+
assert.Contains(t, stderr.String(), "unable to process alert close reason: EOF")
66+
assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one")
67+
})
5168
}
5269

5370
func TestAlertCloseReasonInline(t *testing.T) {
54-
id, err := popAlert()
55-
if err != nil {
56-
assert.FailNow(t, err.Error())
57-
}
58-
out, stderr, exitcode := LaceworkCLIWithTOMLConfig("alert", "close", id, "-r", "1")
59-
assert.Contains(t, out.String(), "Type a comment")
60-
assert.Contains(t, out.String(), "[Enter to launch editor]")
61-
assert.Contains(t, stderr.String(), "unable to process alert close comment: EOF")
62-
assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one")
71+
retry.RunWith(threeTimesWaitOneSecond(), t, func(r *retry.R) {
72+
id, err := popAlert()
73+
if err != nil {
74+
assert.FailNow(t, err.Error())
75+
}
76+
out, stderr, exitcode := LaceworkCLIWithTOMLConfig("alert", "close", id, "-r", "1")
77+
alreadyClosedRetry(r, stderr.String())
78+
assert.Contains(t, out.String(), "Type a comment")
79+
assert.Contains(t, out.String(), "[Enter to launch editor]")
80+
assert.Contains(t, stderr.String(), "unable to process alert close comment: EOF")
81+
assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one")
82+
})
6383
}
6484

6585
func TestAlertCloseInline(t *testing.T) {
66-
id, err := popAlert()
67-
if err != nil {
68-
assert.FailNow(t, err.Error())
69-
}
70-
// verify
71-
out, stderr, exitcode := LaceworkCLIWithTOMLConfig(
72-
"alert", "close", id, "-r", "1", "-c", "everything is awesome")
73-
assert.Contains(t, out.String(), "Are you sure you want to close alert")
74-
assert.Contains(t, stderr.String(), "unable to confirm alert close attempt: EOF")
75-
assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one")
86+
retry.RunWith(threeTimesWaitOneSecond(), t, func(r *retry.R) {
87+
id, err := popAlert()
88+
if err != nil {
89+
assert.FailNow(t, err.Error())
90+
}
91+
// verify
92+
out, stderr, exitcode := LaceworkCLIWithTOMLConfig(
93+
"alert", "close", id, "-r", "1", "-c", "everything is awesome")
94+
assert.Contains(t, out.String(), "Are you sure you want to close alert")
95+
assert.Contains(t, stderr.String(), "unable to confirm alert close attempt: EOF")
96+
assert.Equal(t, 1, exitcode, "EXITCODE is not the expected one")
7697

77-
// close
78-
out, stderr, exitcode = LaceworkCLIWithTOMLConfig(
79-
"alert", "close", id, "-r", "1", "-c", "everything is awesome", "--noninteractive")
80-
assert.Contains(t, out.String(), "was successfully closed.")
81-
assert.Empty(t, stderr.String(), "STDERR should be empty")
82-
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")
98+
// close
99+
out, stderr, exitcode = LaceworkCLIWithTOMLConfig(
100+
"alert", "close", id, "-r", "1", "-c", "everything is awesome", "--noninteractive")
101+
alreadyClosedRetry(r, stderr.String())
102+
assert.Contains(t, out.String(), "was successfully closed.")
103+
assert.Empty(t, stderr.String(), "STDERR should be empty")
104+
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")
83105

84-
// list closed
85-
out, stderr, exitcode = LaceworkCLIWithTOMLConfig("alert", "list", "--status", "Closed")
86-
assert.Contains(t, out.String(), id)
87-
assert.Empty(t, stderr.String(), "STDERR should be empty")
88-
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")
106+
// list closed
107+
out, stderr, exitcode = LaceworkCLIWithTOMLConfig("alert", "list", "--status", "Closed")
108+
assert.Contains(t, out.String(), id)
109+
assert.Empty(t, stderr.String(), "STDERR should be empty")
110+
assert.Equal(t, 0, exitcode, "EXITCODE is not the expected one")
111+
})
89112
}
90113

91114
func TestAlertCloseDoesNotExist(t *testing.T) {

integration/alert_list_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func popAlert() (string, error) {
3333
var alerts api.Alerts
3434

3535
out, stderr, exitcode := LaceworkCLIWithTOMLConfig(
36-
"alert", "list", "--status", "Open", "--json", "--range", "last 7 days",
36+
"alert", "list", "--status", "Open", "--json", "--range", "last 7 days", "--nocache",
3737
)
3838
if stderr.String() != "" {
3939
return "-1", errors.New(stderr.String())

0 commit comments

Comments
 (0)