Browse Source

test/remote (#12)

test: Remote dockerization 8

test: Remote dockerization 7

test: Remote dockerization 6

test: Remote dockerization 5

test: Remote dockerization 4

test: Remote dockerization 3

test: Remote dockerization 2

test: Remote dockerization

Co-authored-by: mco-system <michael.costa@mcos.nc>
Reviewed-on: https://git.mcos.nc/scrippy/scrippy-remote/pulls/12
Co-Authored-By: Michaël Costa <michael.costa@mcos.nc>
Co-Committed-By: Michaël Costa <michael.costa@mcos.nc>
pull/13/head
Michaël Costa 5 months ago
parent
commit
e7e2eb530b
14 changed files with 201 additions and 56 deletions
  1. +8
    -1
      .drone.yml
  2. +1
    -3
      scrippy_remote/remote/ssh.py
  3. +19
    -0
      tests/docker/Makefile
  4. +6
    -0
      tests/docker/docker-compose.yml
  5. +1
    -0
      tests/docker/scrippy/.ssh/authorized_keys
  6. +1
    -0
      tests/docker/scrippy/inquisition.txt
  7. +14
    -0
      tests/docker/sshd.dockerfile
  8. +14
    -0
      tests/docker/start.sh
  9. +1
    -0
      tests/parrot.txt
  10. +38
    -0
      tests/ssh/scrippy.rsa
  11. +36
    -0
      tests/test_get_files.py
  12. +36
    -0
      tests/test_put_files.py
  13. +26
    -0
      tests/test_remote_exec.py
  14. +0
    -52
      tests/test_remote_ssh.py

+ 8
- 1
.drone.yml View File

@ -62,8 +62,15 @@ steps:
exclude:
- tag
- pull_request
services:
- name: sshd
pull: never
image: sshd:latest
ports:
- 2200
---
kind: signature
hmac: eb6bf91ba9756c5dd47e19acd8b3fb2367163817f024addd896dbc84fd4a5c91
hmac: f12443faf3b1eeb6ed2d7a66ff9039c508861c54ccc6fb956c3fe953869d5e58
...

+ 1
- 3
scrippy_remote/remote/ssh.py View File

@ -172,9 +172,7 @@ class Ssh:
exit_code = channel.recv_exit_status()
break
res = {
'exit_code': exit_code
}
res = {'exit_code': exit_code}
if return_stdout:
res['stdout'] = stdout_content
return res


+ 19
- 0
tests/docker/Makefile View File

@ -0,0 +1,19 @@
.RECIPEPREFIX +=
.PHONY: run kill build delete prune stop rebuild
stop:
docker-compose down --rmi local --remove-orphans
prune:
docker container prune -f
delete:
docker rmi sshd
build:
docker build -t sshd -f sshd.dockerfile .
run:
docker-compose up -d
rebuild: | stop delete build run

+ 6
- 0
tests/docker/docker-compose.yml View File

@ -0,0 +1,6 @@
services:
sshd:
image: sshd:latest
container_name: sshd
ports:
- "2200:2200"

+ 1
- 0
tests/docker/scrippy/.ssh/authorized_keys View File

@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC3Mqb2Ndo/bBzJAs9wpJs/0pd3S3SU33KNu16uGs0jY2tT9FT80bsIOdnmVmXtZnHGIbQC1e5d+RYQoym/VKuD+wIh0QbkDAfV1ssMyGmhGETkyAkmzVHATwHOwVbWtuuv+Yd/aJ+KI9Vu7wrhZuhd6ht190LmjbQ4KZ42NGeN3KSEb+qL6DCxqF+9yqljRPlw+ox4Oyk0Is+4HC7SS9cFDe28kTNLRDZTDoZsJ46Y0Y9BMtjmyK6bRO+agrNzX+u/4MA874QxVYUjBB31w1p24jok8C+xjz4GEotlfG4hUkbASnpAL1gCGXDzW032UQ3yWbrgXwjuCoqVhI3d5qv07+D515HfMBoQuq6/Ky7TlWAJt+wvEK+Q8SycySv5edlPjRHyW+TUj9ndSjorPBaZ2HA7lk2EIUBgIxdoBippm4B29aqSjqTOLYd2c8R/g2K47QePIbtUwIkYZoaYkMC5OyFb9Vgy+NQmJ/nGciBkT9rR4UB5ICsDxcVygV4zzkE= scrippy@scrangourou

+ 1
- 0
tests/docker/scrippy/inquisition.txt View File

@ -0,0 +1 @@
Nobody expects the Spanish Inquisition!

+ 14
- 0
tests/docker/sshd.dockerfile View File

@ -0,0 +1,14 @@
FROM alpine:latest
RUN apk add openssh openssl shadow bash
COPY start.sh .
RUN chmod +x ./start.sh
RUN ./start.sh init
RUN useradd -m -d /home/scrippy -s /bin/bash scrippy
RUN usermod --password $(echo 0123456789 | openssl passwd -1 -stdin) scrippy
COPY ./scrippy/ /home/scrippy/
RUN chmod 700 /home/scrippy/.ssh
RUN chmod 640 /home/scrippy/.ssh/authorized_keys
RUN chown -R scrippy: /home/scrippy
USER root
ENTRYPOINT ["./start.sh", "start"]
EXPOSE 2200

+ 14
- 0
tests/docker/start.sh View File

@ -0,0 +1,14 @@
#!/bin/bash
case $1 in
init)
/usr/bin/ssh-keygen -v -t dsa -f /etc/ssh/ssh_host_dsa_key -N ''
/usr/bin/ssh-keygen -v -t rsa -f /etc/ssh/ssh_host_rsa_key -N ''
/usr/bin/ssh-keygen -v -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
/usr/bin/ssh-keygen -v -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ''
/usr/bin/ssh-keygen -v -A
;;
start)
echo "Starting ssh daemon..."
/usr/sbin/sshd -D -p 2200
;;
esac

+ 1
- 0
tests/parrot.txt View File

@ -0,0 +1 @@
It’s not pinin’! It’s passed on! This parrot is no more!

+ 38
- 0
tests/ssh/scrippy.rsa View File

@ -0,0 +1,38 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAYEAtzKm9jXaP2wcyQLPcKSbP9KXd0t0lN9yjbterhrNI2NrU/RU/NG7
CDnZ5lZl7WZxxiG0AtXuXfkWEKMpv1Srg/sCIdEG5AwH1dbLDMhpoRhE5MgJJs1RwE8Bzs
FW1rbrr/mHf2ifiiPVbu8K4WboXeobdfdC5o20OCmeNjRnjdykhG/qi+gwsahfvcqpY0T5
cPqMeDspNCLPuBwu0kvXBQ3tvJEzS0Q2Uw6GbCeOmNGPQTLY5sium0TvmoKzc1/rv+DAPO
+EMVWFIwQd9cNaduI6JPAvsY8+BhKLZXxuIVJGwEp6QC9YAhlw81tN9lEN8lm64F8I7gqK
lYSN3ear9O/g+deR3zAaELquvysu05VgCbfsLxCvkPEsnMkr+XnZT40R8lvk1I/Z3Uo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scrippy_remote.remote.Ssh."""
import os
import logging
from scrippy_remote.remote import Ssh
from scrippy_remote import ScrippyRemoteError
remote_host = "sshd"
remote_port = 2200
remote_user = "scrippy"
remote_path = "/home/scrippy"
key_filename = "{}/ssh/scrippy.rsa".format(os.path.dirname(os.path.realpath(__file__)))
local_path = "/tmp"
test_filename = "inquisition.txt"
pattern = ".*{}".format(test_filename)
def test_get_file():
"""Test de récuperation de fichier."""
recursive = False
delete = False
exit_on_error = True
with Ssh(username=remote_user,
hostname=remote_host,
port=remote_port,
key_filename=key_filename) as host:
try:
err = host.sftp_get(remote_path=remote_path,
local_path=local_path,
pattern=pattern,
recursive=recursive,
delete=delete,
exit_on_error=exit_on_error)
logging.info("Errors: {}".format(err))
assert os.path.isfile(os.path.join(local_path, test_filename))
except ScrippyRemoteError as e:
logging.critical("{}".format(e))

+ 36
- 0
tests/test_put_files.py View File

@ -0,0 +1,36 @@
"""Module de test scrippy_remote.remote.Ssh."""
import os
import logging
from scrippy_remote.remote import Ssh
from scrippy_remote import ScrippyRemoteError
remote_host = "sshd"
remote_port = 2200
remote_user = "scrippy"
remote_path = "/home/scrippy"
key_filename = "{}/ssh/scrippy.rsa".format(os.path.dirname(os.path.realpath(__file__)))
local_path = "/tmp"
test_filename = "parrot.txt"
pattern = ".*{}".format(test_filename)
def test_put_file():
"""Test d'envoi de fichier."""
recursive = False
delete = False
exit_on_error = True
local_path = "{}".format(os.path.dirname(os.path.realpath(__file__)))
with Ssh(username=remote_user,
hostname=remote_host,
port=remote_port,
key_filename=key_filename) as host:
try:
err = host.sftp_put(remote_path=remote_path,
local_path=local_path,
pattern=pattern,
recursive=recursive,
delete=delete,
exit_on_error=exit_on_error)
logging.info("Errors: {}".format(err))
except ScrippyRemoteError as e:
logging.critical("{}".format(e))

+ 26
- 0
tests/test_remote_exec.py View File

@ -0,0 +1,26 @@
"""Module de test scrippy_remote.remote.Ssh."""
import os
import re
from scrippy_remote.remote import Ssh
remote_host = "sshd"
remote_port = 2200
remote_user = "scrippy"
remote_path = "/home/scrippy"
key_filename = "{}/ssh/scrippy.rsa".format(os.path.dirname(os.path.realpath(__file__)))
local_path = "/tmp"
test_filename = "inquisition.txt"
pattern = ".*{}".format(test_filename)
def test_remote_exec():
with Ssh(username=remote_user,
hostname=remote_host,
port=remote_port,
key_filename=key_filename) as host:
stdout = host.exec_command("ls {}".format(remote_path), return_stdout=True)
assert stdout["exit_code"] == 0
for line in stdout["stdout"]:
if re.match(pattern, line):
return True
return False

+ 0
- 52
tests/test_remote_ssh.py View File

@ -1,52 +0,0 @@
"""Module de test scrippy_remote.remote.Ssh."""
import os
import logging
from scrippy_remote.remote import Ssh
from scrippy_remote import ScrippyRemoteError
def test_remote_cmd():
"""Test d'exécution de commande."""
assert True
# remote_host = "srv-trinity-01.preprod-ref.gnc"
# remote_port = 22
# remote_user = "jenkins"
# key_filename = "/var/lib/jenkins/.ssh/id_rsa"
#
# with Ssh(username=remote_user,
# hostname=remote_host,
# port=remote_port,
# key_filename=key_filename) as host:
# stdout = host.exec_command("ls /", return_stdout=True)
# assert stdout["exit_code"] == 0
#
#
# def test_get_file():
# """Test de récuperation de fichier."""
# remote_host = "srv-trinity-01.preprod-ref.gnc"
# remote_port = 22
# remote_user = "jenkins"
# key_filename = "/var/lib/jenkins/.ssh/id_rsa"
# remote_path = "/var/lib/jenkins"
# local_path = "/var/lib/jenkins/.ssh/known_hosts"
# test_filename = "jenkins.CLI.xml"
# pattern = ".*{}".format(test_filename)
# recursive = False
# delete = False
# exit_on_error = True
#
# with Ssh(username=remote_user,
# hostname=remote_host,
# port=remote_port,
# key_filename=key_filename) as host:
# try:
# err = host.sftp_get(remote_path=remote_path,
# local_path=local_path,
# pattern=pattern,
# recursive=recursive,
# delete=delete,
# exit_on_error=exit_on_error)
# logging.info("Errors: {}".format(err))
# assert os.path.isfile(os.path.join(local_path, test_filename))
# except ScrippyRemoteError as e:
# logging.critical("{}".format(e))

Loading…
Cancel
Save