|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package kernel |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "os/exec" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/misha-ssh/kernel/internal/storage" |
| 12 | + "github.com/misha-ssh/kernel/pkg/connect" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "github.com/testcontainers/testcontainers-go" |
| 15 | + "github.com/testcontainers/testcontainers-go/wait" |
| 16 | + "golang.org/x/crypto/ssh" |
| 17 | +) |
| 18 | + |
| 19 | +func TestIntegrationDefaultConnect(t *testing.T) { |
| 20 | + ctx := context.Background() |
| 21 | + |
| 22 | + req := testcontainers.ContainerRequest{ |
| 23 | + FromDockerfile: testcontainers.FromDockerfile{ |
| 24 | + Context: "../../build/ssh/default", |
| 25 | + Dockerfile: "Dockerfile", |
| 26 | + }, |
| 27 | + ExposedPorts: []string{"22/tcp"}, |
| 28 | + WaitingFor: wait.ForListeningPort("22/tcp").WithStartupTimeout(30 * time.Second), |
| 29 | + } |
| 30 | + |
| 31 | + c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 32 | + ContainerRequest: req, |
| 33 | + Started: true, |
| 34 | + }) |
| 35 | + if err != nil { |
| 36 | + t.Fatal(err) |
| 37 | + } |
| 38 | + defer func() { |
| 39 | + if err := c.Terminate(ctx); err != nil { |
| 40 | + t.Logf("failed to terminate container: %s", err) |
| 41 | + } |
| 42 | + }() |
| 43 | + |
| 44 | + host, err := c.Host(ctx) |
| 45 | + if err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + |
| 49 | + port, err := c.MappedPort(ctx, "22/tcp") |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + if c.IsRunning() { |
| 55 | + connection := &connect.Connect{ |
| 56 | + Alias: "test", |
| 57 | + Login: "root", |
| 58 | + Password: "password", |
| 59 | + Address: host, |
| 60 | + Type: connect.TypeSSH, |
| 61 | + CreatedAt: "", |
| 62 | + UpdatedAt: "", |
| 63 | + SshOptions: &connect.SshOptions{ |
| 64 | + Port: port.Int(), |
| 65 | + PrivateKey: "", |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + sshConnector := &connect.Ssh{} |
| 70 | + session, err := sshConnector.Session(connection) |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + |
| 75 | + defer func(session *ssh.Session) { |
| 76 | + err := session.Close() |
| 77 | + if err != nil { |
| 78 | + t.Fatal(err) |
| 79 | + } |
| 80 | + }(session) |
| 81 | + |
| 82 | + err = session.Shell() |
| 83 | + if err != nil { |
| 84 | + t.Fatal(err) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestIntegrationPrivateKeyConnect(t *testing.T) { |
| 90 | + sshKeysName := "../../build/ssh/key/dockerkey" |
| 91 | + |
| 92 | + if !storage.Exists(storage.GetDirectionAndFilename(sshKeysName)) { |
| 93 | + cmdKey := exec.Command("ssh-keygen", "-b", "4096", "-t", "rsa", "-f", sshKeysName) |
| 94 | + require.NoError(t, cmdKey.Run()) |
| 95 | + } |
| 96 | + |
| 97 | + ctx := context.Background() |
| 98 | + |
| 99 | + req := testcontainers.ContainerRequest{ |
| 100 | + FromDockerfile: testcontainers.FromDockerfile{ |
| 101 | + Context: "../../build/ssh/key", |
| 102 | + Dockerfile: "Dockerfile", |
| 103 | + }, |
| 104 | + ExposedPorts: []string{"22/tcp"}, |
| 105 | + WaitingFor: wait.ForListeningPort("22/tcp").WithStartupTimeout(30 * time.Second), |
| 106 | + } |
| 107 | + |
| 108 | + c, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ |
| 109 | + ContainerRequest: req, |
| 110 | + Started: true, |
| 111 | + }) |
| 112 | + if err != nil { |
| 113 | + t.Fatal(err) |
| 114 | + } |
| 115 | + defer func() { |
| 116 | + if err := c.Terminate(ctx); err != nil { |
| 117 | + t.Logf("failed to terminate container: %s", err) |
| 118 | + } |
| 119 | + }() |
| 120 | + |
| 121 | + host, err := c.Host(ctx) |
| 122 | + if err != nil { |
| 123 | + t.Fatal(err) |
| 124 | + } |
| 125 | + |
| 126 | + port, err := c.MappedPort(ctx, "22/tcp") |
| 127 | + if err != nil { |
| 128 | + t.Fatal(err) |
| 129 | + } |
| 130 | + |
| 131 | + if c.IsRunning() { |
| 132 | + connection := &connect.Connect{ |
| 133 | + Alias: "test", |
| 134 | + Login: "root", |
| 135 | + Password: "password", |
| 136 | + Address: host, |
| 137 | + Type: connect.TypeSSH, |
| 138 | + CreatedAt: "", |
| 139 | + UpdatedAt: "", |
| 140 | + SshOptions: &connect.SshOptions{ |
| 141 | + Port: port.Int(), |
| 142 | + PrivateKey: sshKeysName, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + sshConnector := &connect.Ssh{} |
| 147 | + session, err := sshConnector.Session(connection) |
| 148 | + require.NoError(t, err) |
| 149 | + |
| 150 | + defer func(session *ssh.Session) { |
| 151 | + require.NoError(t, session.Close()) |
| 152 | + }(session) |
| 153 | + |
| 154 | + require.NoError(t, session.Shell()) |
| 155 | + } |
| 156 | +} |
0 commit comments