Skip to content

Commit 412193b

Browse files
committed
test: add dynamic OpenSSH server integration tests using Docker
- Add a test that dynamically starts an OpenSSH server in a Docker container for integration testing. - Configure the container with test credentials, allow password and sudo access, and inject the public key. - Update the test to use the actual Docker container's hostname, dynamic port, username, and password. - Improve error handling and test skipping if the Docker environment is not available. Signed-off-by: appleboy <appleboy.tw@gmail.com>
1 parent e899efd commit 412193b

File tree

1 file changed

+52
-3
lines changed

1 file changed

+52
-3
lines changed

plugin_test.go

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,54 @@ func TestAllEnvs(t *testing.T) {
916916
}
917917

918918
func TestSudoCommand(t *testing.T) {
919+
ctx := context.Background()
920+
921+
pubKey, err := os.ReadFile("./tests/.ssh/id_rsa.pub")
922+
if err != nil {
923+
t.Fatalf("Could not read public key file: %v", err)
924+
}
925+
// Define the container request using the linuxserver/openssh-server image
926+
// Configure user 'testuser' with password 'testpass'
927+
req := testcontainers.ContainerRequest{
928+
Image: "linuxserver/openssh-server:latest",
929+
ExposedPorts: []string{"2222/tcp"}, // Default port for this image is 2222
930+
Env: map[string]string{
931+
"USER_NAME": "testuser",
932+
"USER_PASSWORD": "testpass",
933+
"PASSWORD_ACCESS": "true", // Enable password authentication
934+
"SUDO_ACCESS": "true", // Optional: grant sudo access
935+
"PUBLIC_KEY": string(pubKey),
936+
},
937+
// Wait for the SSH port (2222) to be listening
938+
WaitingFor: wait.ForListeningPort("2222/tcp").WithStartupTimeout(180 * time.Second),
939+
}
940+
941+
// Create and start the container
942+
sshContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
943+
ContainerRequest: req,
944+
Started: true,
945+
})
946+
// Skip test if Docker is not available or container fails to start
947+
if err != nil {
948+
// Provide more context on failure
949+
t.Skipf("Could not start container with image %s: %v. Check Docker environment and image availability. Skipping test.", req.Image, err)
950+
}
951+
defer func() {
952+
if err := sshContainer.Terminate(ctx); err != nil {
953+
// Log termination errors but don't fail the test for this
954+
t.Logf("Could not terminate container: %v", err)
955+
}
956+
}()
957+
958+
// Get the mapped host and port for 2222/tcp
959+
host, err := sshContainer.Host(ctx)
960+
if err != nil {
961+
t.Fatalf("Could not get container host: %v", err)
962+
}
963+
port, err := sshContainer.MappedPort(ctx, "2222/tcp")
964+
if err != nil {
965+
t.Fatalf("Could not get container mapped port 2222/tcp: %v", err)
966+
}
919967
var (
920968
buffer bytes.Buffer
921969
expected = `
@@ -925,9 +973,10 @@ func TestSudoCommand(t *testing.T) {
925973

926974
plugin := Plugin{
927975
Config: Config{
928-
Host: []string{"localhost"},
929-
Username: "drone-scp",
930-
Port: 22,
976+
Host: []string{host},
977+
Username: "testuser", // Use the configured username
978+
Port: port.Int(), // Use the mapped port
979+
Password: "testpass", // Use the configured password
931980
KeyPath: "./tests/.ssh/id_rsa",
932981
Script: []string{
933982
`sudo su - -c "whoami"`,

0 commit comments

Comments
 (0)