2020package integration
2121
2222import (
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+
2842func 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
4256func 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
5370func 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
6585func 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
91114func TestAlertCloseDoesNotExist (t * testing.T ) {
0 commit comments