|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package cmd |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "net" |
| 10 | + "sync/atomic" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | + "github.com/stretchr/testify/require" |
| 16 | + "google.golang.org/grpc" |
| 17 | + |
| 18 | + "github.com/elastic/elastic-agent/internal/pkg/cli" |
| 19 | + "github.com/elastic/elastic-agent/pkg/control/v2/client" |
| 20 | + "github.com/elastic/elastic-agent/pkg/control/v2/cproto" |
| 21 | +) |
| 22 | + |
| 23 | +func TestUpgradeCmd(t *testing.T) { |
| 24 | + t.Run("no error when connection gets interrupted", func(t *testing.T) { |
| 25 | + tcpServer, err := net.Listen("tcp", "127.0.0.1:") |
| 26 | + require.NoError(t, err) |
| 27 | + defer tcpServer.Close() |
| 28 | + |
| 29 | + s := grpc.NewServer() |
| 30 | + defer s.Stop() |
| 31 | + |
| 32 | + upgradeCh := make(chan struct{}) |
| 33 | + mock := &mockServer{upgradeStop: upgradeCh} |
| 34 | + cproto.RegisterElasticAgentControlServer(s, mock) |
| 35 | + go func() { |
| 36 | + err := s.Serve(tcpServer) |
| 37 | + assert.NoError(t, err) |
| 38 | + }() |
| 39 | + |
| 40 | + clientCh := make(chan struct{}) |
| 41 | + // use HTTP prefix for the dialer to use TCP, otherwise it's a unix socket/named pipe |
| 42 | + c := client.New(client.WithAddress("http://" + tcpServer.Addr().String())) |
| 43 | + args := []string{"--skip-verify", "8.13.0"} |
| 44 | + streams := cli.NewIOStreams() |
| 45 | + cmd := newUpgradeCommandWithArgs(args, streams) |
| 46 | + |
| 47 | + // the upgrade command will hang until the server shut down |
| 48 | + go func() { |
| 49 | + err = upgradeCmdWithClient(streams, cmd, args, c) |
| 50 | + assert.NoError(t, err) |
| 51 | + // verify that we actually talked to the server |
| 52 | + counter := atomic.LoadInt32(&mock.upgrades) |
| 53 | + assert.Equal(t, int32(1), counter, "server should have handled one upgrade") |
| 54 | + // unblock the further test execution |
| 55 | + close(clientCh) |
| 56 | + }() |
| 57 | + |
| 58 | + // we will know that the client reached the server watching the `mock.upgrades` counter |
| 59 | + require.Eventually(t, func() bool { |
| 60 | + counter := atomic.LoadInt32(&mock.upgrades) |
| 61 | + return counter > 0 |
| 62 | + }, 5*time.Second, 100*time.Millisecond) |
| 63 | + |
| 64 | + // then we close the tcp server which is supposed to interrupt the connection |
| 65 | + s.Stop() |
| 66 | + // this stops the mock server |
| 67 | + close(upgradeCh) |
| 68 | + // this makes sure all client assertions are done |
| 69 | + <-clientCh |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +type mockServer struct { |
| 74 | + cproto.ElasticAgentControlServer |
| 75 | + upgradeStop <-chan struct{} |
| 76 | + upgrades int32 |
| 77 | +} |
| 78 | + |
| 79 | +func (s *mockServer) Upgrade(ctx context.Context, r *cproto.UpgradeRequest) (resp *cproto.UpgradeResponse, err error) { |
| 80 | + atomic.AddInt32(&s.upgrades, 1) |
| 81 | + <-s.upgradeStop |
| 82 | + return nil, nil |
| 83 | +} |
| 84 | + |
| 85 | +func (s *mockServer) State(ctx context.Context, r *cproto.Empty) (resp *cproto.StateResponse, err error) { |
| 86 | + return &cproto.StateResponse{ |
| 87 | + State: cproto.State_HEALTHY, |
| 88 | + Info: &cproto.StateAgentInfo{}, |
| 89 | + }, nil |
| 90 | +} |
0 commit comments