Skip to content

Commit

Permalink
Tests being run against dockerized sshd
Browse files Browse the repository at this point in the history
That enables running SshClientSpec on CI and also make it easier to run this test locally as it does not require
any local setup any more.

Solves sirthias#42, at least on code side, @sirthias would need to enable travis on
project settings to make it effective

Related to sirthias#38 - while this PR does not fix exactly this issue it may enable
what was the motivation behind sirthias#38
  • Loading branch information
note committed Oct 24, 2019
1 parent 6eba027 commit e265f3a
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 5 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
language: scala
jdk:
- oraclejdk8
- openjdk8
- openjdk11
scala:
- 2.12.8
- 2.13.0
script:
# TODO https://github.com/sirthias/scala-ssh/issues/38
- sbt scalafmt::test test:scalafmt::test sbt:scalafmt::test +test:compile "+ testOnly com.decodified.scalassh.HostFileConfigSpec"
script: bash scripts/ci/test.sh
cache:
directories:
- "$HOME/.ivy2/cache"
Expand Down
14 changes: 14 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ encounter exceptions like this:
at net.schmizz.sshj.common.KeyType$3.readPubKeyFromBuffer(KeyType.java:144) ~[sshj-0.12.0.jar:na]
... 7 common frames omitted

Running tests locally
---------------------

Some of the tests needs a running SSH daemon and expects particular structure of `~/.scala-ssh`. In case you want to run
those tests locally there is prepared structure to run dockerized sbt and sshd. Running dockerized structure should
work just by itself:

```
scripts/local/dockerized-sbt.sh
# and then in sbt:
test
```

Having sbt as a separate step make it easier to run changes iteratively in dockerized sbt.

License
-------
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ scalafmtOnCompile := true
scalafmtVersion := "1.4.0"

libraryDependencies ++= Seq(
"com.hierynomus" % "sshj" % "0.26.0",
"com.hierynomus" % "sshj" % "0.27.0",
"org.slf4j" % "slf4j-api" % "1.7.25",
"com.jcraft" % "jsch.agentproxy.sshj" % "0.0.9" % "provided",
"com.jcraft" % "jsch.agentproxy.connector-factory" % "0.0.9" % "provided",
Expand Down
37 changes: 37 additions & 0 deletions scripts/ci/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

source scripts/common/common.sh

# Check is source worked
echo "Common definitions loaded: $PUBLIC_KEY_FILENAME"

ssh-keygen -t ed25519 -f "$PRIVATE_KEY_FILENAME" -N "" -q

docker pull "$DOCKER_IMAGE_NAME"
docker run -d -P --name test_sshd "$DOCKER_IMAGE_NAME"

# Ensure that sshd started
RETRIES_LEFT=15
COMMAND_STATUS=1
until { [ $COMMAND_STATUS -eq 0 ] || [ $RETRIES_LEFT -eq 0 ]; }; do
echo "checking if sshd is up: $RETRIES_LEFT"
docker ps -a | grep test_sshd
COMMAND_STATUS=$?
sleep 2
let RETRIES_LEFT=RETRIES_LEFT-1
done

docker cp "$PUBLIC_KEY_FILENAME" test_sshd:/root/.ssh/authorized_keys
docker exec test_sshd chown root:root /root/.ssh/authorized_keys

# returns e.g. 0.0.0.0:32875
SSHD_HOST_PORT=`docker port test_sshd 22`

# returns e.g. 32875, uses https://stackoverflow.com/a/3162500/429311
SSHD_PORT=${SSHD_HOST_PORT##*:}
echo "sshd ephemeral port detected: $SSHD_PORT"
write_scala_ssh_config "localhost" "$SSHD_PORT"

ssh-keyscan -t ed25519 -p "$SSHD_PORT" localhost >>~/.ssh/known_hosts

sbt test
24 changes: 24 additions & 0 deletions scripts/common/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

# enable job control
set -m

PRIVATE_KEY_FILENAME="id_ed25519"
PUBLIC_KEY_FILENAME="id_ed25519.pub"
DOCKER_IMAGE_NAME="rastasheep/ubuntu-sshd:16.04"

function write_scala_ssh_config() {
local SSHD_HOST="$1"
local SSHD_PORT="$2"

mkdir -p ~/.scala-ssh
echo $SSHD_HOST > ~/.scala-ssh/.testhost
FULLPATH=`realpath $PRIVATE_KEY_FILENAME`

cat <<EOF > ~/.scala-ssh/$SSHD_HOST
login-type = keyfile
username = root
keyfile = $FULLPATH
port = $SSHD_PORT
EOF
}
22 changes: 22 additions & 0 deletions scripts/local/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: '2.4'

services:
sbt:
image: hseeberger/scala-sbt:8u222_1.3.3_2.12.10
command: bash -c "/root/workdir/scripts/local/sbt-shell.sh"
ports:
- "5005:5005"
volumes:
- $HOME/.ivy2:/root/.ivy2:cached
- $HOME/.sbt:/root/.sbt:cached
- $HOME/.coursier:/root/.coursier:cached
- ../..:/root/workdir
working_dir: /root/workdir
links:
- sshd

sshd:
container_name: test_sshd
image: rastasheep/ubuntu-sshd:16.04
ports:
- 22
18 changes: 18 additions & 0 deletions scripts/local/dockerized-sbt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

source scripts/common/common.sh

# Check is source worked
echo "Common definitions loaded: $PUBLIC_KEY_FILENAME"

# there may exist files from previous run - remove them
rm -f id_ed25519 id_ed25519.pub

ssh-keygen -t ed25519 -f "$PRIVATE_KEY_FILENAME" -N "" -q
# docker-compose -f scripts/local/docker-compose.yml pull --include-deps sbt
docker-compose -f scripts/local/docker-compose.yml up -d sshd

docker cp "$PUBLIC_KEY_FILENAME" test_sshd:/root/.ssh/authorized_keys
docker exec test_sshd chown root:root /root/.ssh/authorized_keys

docker-compose -f scripts/local/docker-compose.yml run --service-ports sbt
14 changes: 14 additions & 0 deletions scripts/local/sbt-shell.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

source scripts/common/common.sh

# Check is source worked
echo "Common definitions loaded: $PUBLIC_KEY_FILENAME"

# hostname "sshd" as in docker-compose
write_scala_ssh_config sshd 22

mkdir ~/.ssh
ssh-keyscan -t ed25519 -p 22 sshd >>~/.ssh/known_hosts

sbt shell

0 comments on commit e265f3a

Please sign in to comment.