@@ -6,10 +6,12 @@ package handlers
6
6
7
7
import (
8
8
"context"
9
+ "errors"
9
10
"testing"
10
11
11
12
"github.com/stretchr/testify/require"
12
13
14
+ "github.com/elastic/elastic-agent-libs/logp"
13
15
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
14
16
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
15
17
"github.com/elastic/elastic-agent/internal/pkg/agent/application/reexec"
@@ -25,8 +27,9 @@ import (
25
27
)
26
28
27
29
type mockUpgradeManager struct {
28
- msgChan chan string
29
- completedChan chan struct {}
30
+ msgChan chan string
31
+ successChan chan string
32
+ errChan chan error
30
33
}
31
34
32
35
func (u * mockUpgradeManager ) Upgradeable () bool {
@@ -39,11 +42,13 @@ func (u *mockUpgradeManager) Reload(rawConfig *config.Config) error {
39
42
40
43
func (u * mockUpgradeManager ) Upgrade (ctx context.Context , version string , sourceURI string , action * fleetapi.ActionUpgrade , details * details.Details , skipVerifyOverride bool , skipDefaultPgp bool , pgpBytes ... string ) (_ reexec.ShutdownCallbackFn , err error ) {
41
44
select {
42
- case <- u .completedChan :
43
- u .msgChan <- "completed " + version
45
+ case msg := <- u .successChan :
46
+ u .msgChan <- msg
44
47
return nil , nil
48
+ case err := <- u .errChan :
49
+ u .msgChan <- err .Error ()
50
+ return nil , ctx .Err ()
45
51
case <- ctx .Done ():
46
- u .msgChan <- "canceled " + version
47
52
return nil , ctx .Err ()
48
53
}
49
54
}
@@ -66,7 +71,7 @@ func TestUpgradeHandler(t *testing.T) {
66
71
67
72
agentInfo := & info.AgentInfo {}
68
73
msgChan := make (chan string )
69
- completedChan := make (chan struct {} )
74
+ completedChan := make (chan string )
70
75
71
76
// Create and start the coordinator
72
77
c := coordinator .New (
@@ -76,7 +81,7 @@ func TestUpgradeHandler(t *testing.T) {
76
81
agentInfo ,
77
82
component.RuntimeSpecs {},
78
83
nil ,
79
- & mockUpgradeManager {msgChan : msgChan , completedChan : completedChan },
84
+ & mockUpgradeManager {msgChan : msgChan , successChan : completedChan },
80
85
nil , nil , nil , nil , nil , false )
81
86
//nolint:errcheck // We don't need the termination state of the Coordinator
82
87
go c .Run (ctx )
@@ -96,14 +101,17 @@ func TestUpgradeHandler(t *testing.T) {
96
101
func TestUpgradeHandlerSameVersion (t * testing.T ) {
97
102
// Create a cancellable context that will shut down the coordinator after
98
103
// the test.
104
+ logp .DevelopmentSetup ()
105
+ logger .SetLevel (logp .DebugLevel )
99
106
ctx , cancel := context .WithCancel (context .Background ())
100
107
defer cancel ()
101
108
102
109
log , _ := logger .New ("" , false )
103
110
104
111
agentInfo := & info.AgentInfo {}
105
112
msgChan := make (chan string )
106
- completedChan := make (chan struct {})
113
+ successChan := make (chan string )
114
+ errChan := make (chan error )
107
115
108
116
// Create and start the Coordinator
109
117
c := coordinator .New (
@@ -113,7 +121,7 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
113
121
agentInfo ,
114
122
component.RuntimeSpecs {},
115
123
nil ,
116
- & mockUpgradeManager {msgChan : msgChan , completedChan : completedChan },
124
+ & mockUpgradeManager {msgChan : msgChan , successChan : successChan , errChan : errChan },
117
125
nil , nil , nil , nil , nil , false )
118
126
//nolint:errcheck // We don't need the termination state of the Coordinator
119
127
go c .Run (ctx )
@@ -126,10 +134,12 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
126
134
err2 := u .Handle (ctx , & a , ack )
127
135
require .NoError (t , err1 )
128
136
require .NoError (t , err2 )
129
- // indicate that upgrade is completed
130
- close (completedChan )
131
- msg := <- msgChan
132
- require .Equal (t , "completed 8.3.0" , msg )
137
+
138
+ successChan <- "completed 8.3.0"
139
+ require .Equal (t , "completed 8.3.0" , <- msgChan )
140
+ errChan <- errors .New ("duplicated update, not finishing it?" )
141
+ require .Equal (t , "duplicated update, not finishing it?" , <- msgChan )
142
+
133
143
}
134
144
135
145
func TestUpgradeHandlerNewVersion (t * testing.T ) {
@@ -142,7 +152,8 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
142
152
143
153
agentInfo := & info.AgentInfo {}
144
154
msgChan := make (chan string )
145
- completedChan := make (chan struct {})
155
+ completedChan := make (chan string )
156
+ errorChan := make (chan error )
146
157
147
158
// Create and start the Coordinator
148
159
c := coordinator .New (
@@ -152,7 +163,7 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
152
163
agentInfo ,
153
164
component.RuntimeSpecs {},
154
165
nil ,
155
- & mockUpgradeManager {msgChan : msgChan , completedChan : completedChan },
166
+ & mockUpgradeManager {msgChan : msgChan , successChan : completedChan , errChan : errorChan },
156
167
nil , nil , nil , nil , nil , false )
157
168
//nolint:errcheck // We don't need the termination state of the Coordinator
158
169
go c .Run (ctx )
@@ -163,14 +174,19 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
163
174
a2 := fleetapi.ActionUpgrade {Data : fleetapi.ActionUpgradeData {
164
175
Version : "8.5.0" , SourceURI : "http://localhost" }}
165
176
ack := noopacker .New ()
177
+
178
+ // Send both upgrade actions, a1 will error before a2 succeeds
166
179
err1 := u .Handle (ctx , & a1 , ack )
167
180
require .NoError (t , err1 )
168
181
err2 := u .Handle (ctx , & a2 , ack )
169
182
require .NoError (t , err2 )
170
- msg1 := <- msgChan
171
- require .Equal (t , "canceled 8.2.0" , msg1 )
172
- // indicate that upgrade is completed
173
- close (completedChan )
174
- msg2 := <- msgChan
175
- require .Equal (t , "completed 8.5.0" , msg2 )
183
+
184
+ // Send an error so the first action is "cancelled"
185
+ errorChan <- errors .New ("cancelled 8.2.0" )
186
+ // Wait for the mockUpgradeHandler to receive and "process" the error
187
+ require .Equal (t , "cancelled 8.2.0" , <- msgChan , "mockUpgradeHandler.Upgrade did not receive the expected error" )
188
+
189
+ // Send a success so the second action succeeds
190
+ completedChan <- "completed 8.5.0"
191
+ require .Equal (t , "completed 8.5.0" , <- msgChan , "mockUpgradeHandler.Upgrade did not receive the success signal" )
176
192
}
0 commit comments