Skip to content

Commit 69a78a0

Browse files
committed
feature(4890): removed confirmation from upgrade check for when force flag is set
1 parent 565ac16 commit 69a78a0

File tree

2 files changed

+6
-95
lines changed

2 files changed

+6
-95
lines changed

internal/pkg/agent/cmd/upgrade.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const (
3535
flagForce = "force"
3636
)
3737

38-
var upgradeUnconfErr error = errors.New("upgrade not confirmed")
38+
var unsupportedUpgrade error = errors.New("upgrading fleet managed agents is not supported")
3939

4040
func newUpgradeCommandWithArgs(_ []string, streams *cli.IOStreams) *cobra.Command {
4141
cmd := &cobra.Command{
@@ -74,11 +74,8 @@ type upgradeInput struct {
7474
args []string
7575
c client.Client
7676
agentInfo info.Agent
77-
cFunc confirmFunc
7877
}
7978

80-
type confirmFunc func(string, bool) (bool, error)
81-
8279
func upgradeCmd(streams *cli.IOStreams, cmd *cobra.Command, args []string) error {
8380
c := client.New()
8481
agentInfo, err := info.NewAgentInfoWithLog(cmd.Context(), "error", false)
@@ -91,12 +88,11 @@ func upgradeCmd(streams *cli.IOStreams, cmd *cobra.Command, args []string) error
9188
args,
9289
c,
9390
agentInfo,
94-
cli.Confirm,
9591
}
9692
return upgradeCmdWithClient(input)
9793
}
9894

99-
func shouldUpgrade(cmd *cobra.Command, agentInfo info.Agent, cFunc confirmFunc) (bool, error) {
95+
func shouldUpgrade(cmd *cobra.Command, agentInfo info.Agent) (bool, error) {
10096
if agentInfo.IsStandalone() {
10197
return true, nil
10298
}
@@ -111,16 +107,7 @@ func shouldUpgrade(cmd *cobra.Command, agentInfo info.Agent, cFunc confirmFunc)
111107
}
112108

113109
if !force {
114-
return false, fmt.Errorf("upgrading fleet managed agents is not supported")
115-
}
116-
117-
cf, err := cFunc("Upgrading fleet managed agents is not supported. Would you still like to proceed?", false)
118-
if err != nil {
119-
return false, fmt.Errorf("failed while confirming action: %w", err)
120-
}
121-
122-
if !cf {
123-
return false, upgradeUnconfErr
110+
return false, unsupportedUpgrade
124111
}
125112

126113
return true, nil
@@ -132,7 +119,7 @@ func upgradeCmdWithClient(input *upgradeInput) error {
132119
version := input.args[0]
133120
sourceURI, _ := cmd.Flags().GetString(flagSourceURI)
134121

135-
su, err := shouldUpgrade(cmd, input.agentInfo, input.cFunc)
122+
su, err := shouldUpgrade(cmd, input.agentInfo)
136123
if !su {
137124
return fmt.Errorf("aborting upgrade: %w", err)
138125
}

internal/pkg/agent/cmd/upgrade_test.go

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func TestUpgradeCmd(t *testing.T) {
5656
args,
5757
c,
5858
mockAgentInfo,
59-
nil,
6059
}
6160

6261
// the upgrade command will hang until the server shut down
@@ -83,6 +82,7 @@ func TestUpgradeCmd(t *testing.T) {
8382
// this makes sure all client assertions are done
8483
<-clientCh
8584
})
85+
8686
t.Run("fail if fleet managed and unprivileged", func(t *testing.T) {
8787
var wg sync.WaitGroup
8888
// Set up mock TCP server for gRPC connection
@@ -120,7 +120,6 @@ func TestUpgradeCmd(t *testing.T) {
120120
args,
121121
c,
122122
mockAgentInfo,
123-
nil,
124123
}
125124

126125
term := make(chan int)
@@ -186,7 +185,6 @@ func TestUpgradeCmd(t *testing.T) {
186185
args,
187186
c,
188187
mockAgentInfo,
189-
nil,
190188
}
191189

192190
term := make(chan int)
@@ -215,79 +213,8 @@ func TestUpgradeCmd(t *testing.T) {
215213

216214
wg.Wait()
217215
})
218-
t.Run("abort upgrade if fleet managed, privileged, --force is set, and user does not confirm", func(t *testing.T) {
219-
var wg sync.WaitGroup
220-
// Set up mock TCP server for gRPC connection
221-
tcpServer, err := net.Listen("tcp", "127.0.0.1:")
222-
require.NoError(t, err)
223-
defer tcpServer.Close()
224-
225-
s := grpc.NewServer()
226-
227-
// Define mock server and agent information
228-
mock := &mockServer{}
229-
mockAgentInfo := mockinfo.NewAgent(t)
230-
mockAgentInfo.EXPECT().IsStandalone().Return(false) // Simulate fleet-managed agent
231-
mockAgentInfo.EXPECT().Unprivileged().Return(false) // Simulate privileged mode
232-
cproto.RegisterElasticAgentControlServer(s, mock)
233-
234-
wg.Add(1)
235-
go func() {
236-
defer wg.Done()
237-
err := s.Serve(tcpServer)
238-
assert.NoError(t, err)
239-
}()
240-
241-
// Create client and command
242-
c := client.New(client.WithAddress("http://" + tcpServer.Addr().String()))
243-
args := []string{"8.13.0"} // Version argument
244-
streams := cli.NewIOStreams()
245-
cmd := newUpgradeCommandWithArgs(args, streams)
246-
cmd.SetContext(context.Background())
247-
err = cmd.Flags().Set("force", "true")
248-
if err != nil {
249-
log.Fatal(err)
250-
}
251216

252-
commandInput := &upgradeInput{
253-
streams,
254-
cmd,
255-
args,
256-
c,
257-
mockAgentInfo,
258-
func(s string, b bool) (bool, error) {
259-
return false, nil
260-
},
261-
}
262-
263-
term := make(chan int)
264-
wg.Add(1)
265-
// Execute upgrade command and validate shouldUpgrade error
266-
go func() {
267-
defer wg.Done()
268-
err = upgradeCmdWithClient(commandInput)
269-
270-
// Expect an error because user does not confirm the upgrade
271-
assert.Error(t, err)
272-
assert.Contains(t, err.Error(), "upgrade not confirmed")
273-
274-
// Verify counter has not incremented since upgrade should not proceed
275-
counter := atomic.LoadInt32(&mock.upgrades)
276-
assert.Equal(t, int32(0), counter, "server should not have handled any upgrades")
277-
278-
close(term)
279-
}()
280-
281-
wg.Add(1)
282-
go func() {
283-
defer wg.Done()
284-
<-term
285-
s.Stop()
286-
}()
287-
288-
wg.Wait()
289-
})
290-
t.Run("proceed with upgrade if fleet managed, privileged, --force is set, and user confirms upgrade", func(t *testing.T) {
217+
t.Run("proceed with upgrade if fleet managed, privileged, --force is set", func(t *testing.T) {
291218
var wg sync.WaitGroup
292219
// Set up mock TCP server for gRPC connection
293220
tcpServer, err := net.Listen("tcp", "127.0.0.1:")
@@ -328,9 +255,6 @@ func TestUpgradeCmd(t *testing.T) {
328255
args,
329256
c,
330257
mockAgentInfo,
331-
func(s string, b bool) (bool, error) {
332-
return true, nil
333-
},
334258
}
335259

336260
term := make(chan int)

0 commit comments

Comments
 (0)