Skip to content

Commit a7befe4

Browse files
committed
[WIP] Trying to fix TestUpgradeHandler*
1 parent 756b92f commit a7befe4

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

internal/pkg/agent/application/actions/handlers/handler_action_upgrade_test.go

+37-21
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ package handlers
66

77
import (
88
"context"
9+
"errors"
910
"testing"
1011

1112
"github.com/stretchr/testify/require"
1213

14+
"github.com/elastic/elastic-agent-libs/logp"
1315
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
1416
"github.com/elastic/elastic-agent/internal/pkg/agent/application/info"
1517
"github.com/elastic/elastic-agent/internal/pkg/agent/application/reexec"
@@ -25,8 +27,9 @@ import (
2527
)
2628

2729
type mockUpgradeManager struct {
28-
msgChan chan string
29-
completedChan chan struct{}
30+
msgChan chan string
31+
successChan chan string
32+
errChan chan error
3033
}
3134

3235
func (u *mockUpgradeManager) Upgradeable() bool {
@@ -39,11 +42,13 @@ func (u *mockUpgradeManager) Reload(rawConfig *config.Config) error {
3942

4043
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) {
4144
select {
42-
case <-u.completedChan:
43-
u.msgChan <- "completed " + version
45+
case msg := <-u.successChan:
46+
u.msgChan <- msg
4447
return nil, nil
48+
case err := <-u.errChan:
49+
u.msgChan <- err.Error()
50+
return nil, ctx.Err()
4551
case <-ctx.Done():
46-
u.msgChan <- "canceled " + version
4752
return nil, ctx.Err()
4853
}
4954
}
@@ -66,7 +71,7 @@ func TestUpgradeHandler(t *testing.T) {
6671

6772
agentInfo := &info.AgentInfo{}
6873
msgChan := make(chan string)
69-
completedChan := make(chan struct{})
74+
completedChan := make(chan string)
7075

7176
// Create and start the coordinator
7277
c := coordinator.New(
@@ -76,7 +81,7 @@ func TestUpgradeHandler(t *testing.T) {
7681
agentInfo,
7782
component.RuntimeSpecs{},
7883
nil,
79-
&mockUpgradeManager{msgChan: msgChan, completedChan: completedChan},
84+
&mockUpgradeManager{msgChan: msgChan, successChan: completedChan},
8085
nil, nil, nil, nil, nil, false)
8186
//nolint:errcheck // We don't need the termination state of the Coordinator
8287
go c.Run(ctx)
@@ -96,14 +101,17 @@ func TestUpgradeHandler(t *testing.T) {
96101
func TestUpgradeHandlerSameVersion(t *testing.T) {
97102
// Create a cancellable context that will shut down the coordinator after
98103
// the test.
104+
logp.DevelopmentSetup()
105+
logger.SetLevel(logp.DebugLevel)
99106
ctx, cancel := context.WithCancel(context.Background())
100107
defer cancel()
101108

102109
log, _ := logger.New("", false)
103110

104111
agentInfo := &info.AgentInfo{}
105112
msgChan := make(chan string)
106-
completedChan := make(chan struct{})
113+
successChan := make(chan string)
114+
errChan := make(chan error)
107115

108116
// Create and start the Coordinator
109117
c := coordinator.New(
@@ -113,7 +121,7 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
113121
agentInfo,
114122
component.RuntimeSpecs{},
115123
nil,
116-
&mockUpgradeManager{msgChan: msgChan, completedChan: completedChan},
124+
&mockUpgradeManager{msgChan: msgChan, successChan: successChan, errChan: errChan},
117125
nil, nil, nil, nil, nil, false)
118126
//nolint:errcheck // We don't need the termination state of the Coordinator
119127
go c.Run(ctx)
@@ -126,10 +134,12 @@ func TestUpgradeHandlerSameVersion(t *testing.T) {
126134
err2 := u.Handle(ctx, &a, ack)
127135
require.NoError(t, err1)
128136
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+
133143
}
134144

135145
func TestUpgradeHandlerNewVersion(t *testing.T) {
@@ -142,7 +152,8 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
142152

143153
agentInfo := &info.AgentInfo{}
144154
msgChan := make(chan string)
145-
completedChan := make(chan struct{})
155+
completedChan := make(chan string)
156+
errorChan := make(chan error)
146157

147158
// Create and start the Coordinator
148159
c := coordinator.New(
@@ -152,7 +163,7 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
152163
agentInfo,
153164
component.RuntimeSpecs{},
154165
nil,
155-
&mockUpgradeManager{msgChan: msgChan, completedChan: completedChan},
166+
&mockUpgradeManager{msgChan: msgChan, successChan: completedChan, errChan: errorChan},
156167
nil, nil, nil, nil, nil, false)
157168
//nolint:errcheck // We don't need the termination state of the Coordinator
158169
go c.Run(ctx)
@@ -163,14 +174,19 @@ func TestUpgradeHandlerNewVersion(t *testing.T) {
163174
a2 := fleetapi.ActionUpgrade{Data: fleetapi.ActionUpgradeData{
164175
Version: "8.5.0", SourceURI: "http://localhost"}}
165176
ack := noopacker.New()
177+
178+
// Send both upgrade actions, a1 will error before a2 succeeds
166179
err1 := u.Handle(ctx, &a1, ack)
167180
require.NoError(t, err1)
168181
err2 := u.Handle(ctx, &a2, ack)
169182
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")
176192
}

0 commit comments

Comments
 (0)