|
| 1 | +package darwin |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/golang/mock/gomock" |
| 9 | + "github.com/metrue/go-ssh-client" |
| 10 | + sshMocks "github.com/metrue/go-ssh-client/mocks" |
| 11 | +) |
| 12 | + |
| 13 | +func TestDriverProvision(t *testing.T) { |
| 14 | + t.Run("SSHConnectError", func(t *testing.T) { |
| 15 | + ctrl := gomock.NewController(t) |
| 16 | + defer ctrl.Finish() |
| 17 | + |
| 18 | + sshClient := sshMocks.NewMockClienter(ctrl) |
| 19 | + n := &Docker{sshClient: sshClient} |
| 20 | + err := errors.New("could not connect to host") |
| 21 | + sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(false, err).AnyTimes() |
| 22 | + if err := n.Provision(context.Background(), true); err == nil { |
| 23 | + t.Fatalf("should get error when SSH connection not ok") |
| 24 | + } |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("SSHConnectionNotOK", func(t *testing.T) { |
| 28 | + ctrl := gomock.NewController(t) |
| 29 | + defer ctrl.Finish() |
| 30 | + |
| 31 | + sshClient := sshMocks.NewMockClienter(ctrl) |
| 32 | + n := New(sshClient) |
| 33 | + sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(false, nil).AnyTimes() |
| 34 | + if err := n.Provision(context.Background(), true); err == nil { |
| 35 | + t.Fatalf("should get error when SSH connection not ok") |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + t.Run("DockerAndFxAgentOK", func(t *testing.T) { |
| 40 | + ctrl := gomock.NewController(t) |
| 41 | + defer ctrl.Finish() |
| 42 | + |
| 43 | + sshClient := sshMocks.NewMockClienter(ctrl) |
| 44 | + n := New(sshClient) |
| 45 | + sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes() |
| 46 | + sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil) |
| 47 | + sshClient.EXPECT().RunCommand(scripts["check_fx_agent"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil) |
| 48 | + if err := n.Provision(context.Background(), true); err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + t.Run("DockerNotReady", func(t *testing.T) { |
| 54 | + ctrl := gomock.NewController(t) |
| 55 | + defer ctrl.Finish() |
| 56 | + |
| 57 | + sshClient := sshMocks.NewMockClienter(ctrl) |
| 58 | + n := New(sshClient) |
| 59 | + sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes() |
| 60 | + err := errors.New("docker command not found") |
| 61 | + sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err) |
| 62 | + sshClient.EXPECT().RunCommand(scripts["has_docker"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err) |
| 63 | + if err := n.Provision(context.Background(), true); err == nil { |
| 64 | + t.Fatal("should tell user to install docker first") |
| 65 | + } |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("FxAgentNotReady", func(t *testing.T) { |
| 69 | + ctrl := gomock.NewController(t) |
| 70 | + defer ctrl.Finish() |
| 71 | + |
| 72 | + sshClient := sshMocks.NewMockClienter(ctrl) |
| 73 | + n := New(sshClient) |
| 74 | + sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes() |
| 75 | + err := errors.New("fx agent not found") |
| 76 | + sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil) |
| 77 | + sshClient.EXPECT().RunCommand(scripts["check_fx_agent"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(err) |
| 78 | + sshClient.EXPECT().RunCommand(scripts["start_fx_agent"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil) |
| 79 | + if err := n.Provision(context.Background(), true); err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + t.Run("DockerAndFxAgentReady", func(t *testing.T) { |
| 85 | + ctrl := gomock.NewController(t) |
| 86 | + defer ctrl.Finish() |
| 87 | + |
| 88 | + sshClient := sshMocks.NewMockClienter(ctrl) |
| 89 | + n := New(sshClient) |
| 90 | + |
| 91 | + sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil).AnyTimes() |
| 92 | + sshClient.EXPECT().RunCommand(scripts["docker_version"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil) |
| 93 | + sshClient.EXPECT().RunCommand(scripts["check_fx_agent"], ssh.CommandOptions{Timeout: sshConnectionTimeout}).Return(nil) |
| 94 | + if err := n.Provision(context.Background(), true); err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | + }) |
| 98 | +} |
| 99 | + |
| 100 | +func TestRunCommand(t *testing.T) { |
| 101 | + ctrl := gomock.NewController(t) |
| 102 | + defer ctrl.Finish() |
| 103 | + |
| 104 | + sshClient := sshMocks.NewMockClienter(ctrl) |
| 105 | + n := &Docker{ |
| 106 | + sshClient: sshClient, |
| 107 | + } |
| 108 | + script := "script" |
| 109 | + option := ssh.CommandOptions{ |
| 110 | + Timeout: sshConnectionTimeout, |
| 111 | + } |
| 112 | + sshClient.EXPECT().Connectable(sshConnectionTimeout).Return(true, nil) |
| 113 | + sshClient.EXPECT().RunCommand(script, option).Return(nil) |
| 114 | + if err := n.runCmd(script, true, option); err != nil { |
| 115 | + t.Fatal(err) |
| 116 | + } |
| 117 | +} |
0 commit comments