|
| 1 | +// Copyright (C) 2024 The Dagu Authors |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +package executor |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/dagu-org/dagu/internal/dag" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestSSHExecutor(t *testing.T) { |
| 29 | + t.Parallel() |
| 30 | + |
| 31 | + t.Run("Basic", func(t *testing.T) { |
| 32 | + step := dag.Step{ |
| 33 | + Name: "ssh-exec", |
| 34 | + ExecutorConfig: dag.ExecutorConfig{ |
| 35 | + Type: "ssh", |
| 36 | + Config: map[string]any{ |
| 37 | + "User": "testuser", |
| 38 | + "IP": "testip", |
| 39 | + "Port": 25, |
| 40 | + "Password": "testpassword", |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + ctx := context.Background() |
| 45 | + exec, err := newSSHExec(ctx, step) |
| 46 | + require.NoError(t, err) |
| 47 | + |
| 48 | + sshExec, ok := exec.(*sshExec) |
| 49 | + require.True(t, ok) |
| 50 | + |
| 51 | + assert.Equal(t, "testuser", sshExec.config.User) |
| 52 | + assert.Equal(t, "testip", sshExec.config.IP) |
| 53 | + assert.Equal(t, "25", sshExec.config.Port) |
| 54 | + assert.Equal(t, "testpassword", sshExec.config.Password) |
| 55 | + }) |
| 56 | + |
| 57 | + t.Run("ExpandEnv", func(t *testing.T) { |
| 58 | + os.Setenv("TEST_SSH_EXEC_USER", "testuser") |
| 59 | + os.Setenv("TEST_SSH_EXEC_IP", "testip") |
| 60 | + os.Setenv("TEST_SSH_EXEC_PORT", "23") |
| 61 | + os.Setenv("TEST_SSH_EXEC_PASSWORD", "testpassword") |
| 62 | + |
| 63 | + step := dag.Step{ |
| 64 | + Name: "ssh-exec", |
| 65 | + ExecutorConfig: dag.ExecutorConfig{ |
| 66 | + Type: "ssh", |
| 67 | + Config: map[string]any{ |
| 68 | + "User": "${TEST_SSH_EXEC_USER}", |
| 69 | + "IP": "${TEST_SSH_EXEC_IP}", |
| 70 | + "Port": "${TEST_SSH_EXEC_PORT}", |
| 71 | + "Password": "${TEST_SSH_EXEC_PASSWORD}", |
| 72 | + }, |
| 73 | + }, |
| 74 | + } |
| 75 | + ctx := context.Background() |
| 76 | + exec, err := newSSHExec(ctx, step) |
| 77 | + require.NoError(t, err) |
| 78 | + |
| 79 | + sshExec, ok := exec.(*sshExec) |
| 80 | + require.True(t, ok) |
| 81 | + |
| 82 | + assert.Equal(t, "testuser", sshExec.config.User) |
| 83 | + assert.Equal(t, "testip", sshExec.config.IP) |
| 84 | + assert.Equal(t, "23", sshExec.config.Port) |
| 85 | + assert.Equal(t, "testpassword", sshExec.config.Password) |
| 86 | + }) |
| 87 | +} |
0 commit comments