Files
openairinterface5g/ci-scripts/tests/cmd.py
T
Robert Schmidt e541aee49e Implement runScript in cls_cmd for LocalCmd/RemoteCmd, add test
A lot of CI code is python mixed with bash, e.g.,

    ssh = getConnection(host)
    ssh.run('ls')
    ssh.run('echo')

At least some of this CI code would benefit if it was written in a
simple bash script, returning error codes and potentially other
information either through stdout/stderr or files, to the calling Python
code:

    ssh = runScript(host, script)
    # script does: ls; echo

This commit introduces the possibility to run entire scripts. The idea
is that the executor has a script (on localhost), which is either
executed locally or on a remote host. For the remote host, the script is
not copied but piped into a remotely executed bash. In both cases,
output is either returned like the Cmd.run() function with returncode
and mixed stdout/stderr, or optionally redirected into a file on the
(remote) host, which can be treated further by the Python code in later
steps.
2024-09-28 12:52:59 +02:00

100 lines
4.3 KiB
Python

import sys
import logging
logging.basicConfig(
level=logging.DEBUG,
stream=sys.stdout,
format="[%(asctime)s] %(levelname)8s: %(message)s"
)
import os
import unittest
sys.path.append('./') # to find OAI imports below
import cls_cmd
class TestCmd(unittest.TestCase):
def test_local_cmd(self):
with cls_cmd.getConnection("localhost") as cmd:
self.assertTrue(isinstance(cmd, cls_cmd.LocalCmd))
with cls_cmd.getConnection("None") as cmd:
self.assertTrue(isinstance(cmd, cls_cmd.LocalCmd))
with cls_cmd.getConnection("none") as cmd:
self.assertTrue(isinstance(cmd, cls_cmd.LocalCmd))
with cls_cmd.getConnection(None) as cmd:
self.assertTrue(isinstance(cmd, cls_cmd.LocalCmd))
ret = cmd.run("true")
self.assertEqual(ret.args, "true")
self.assertEqual(ret.returncode, 0)
self.assertEqual(ret.stdout, "")
ret = cmd.run("false")
self.assertEqual(ret.args, "false")
self.assertEqual(ret.returncode, 1)
self.assertEqual(ret.stdout, "")
ret = cmd.run("echo test")
self.assertEqual(ret.args, "echo test")
self.assertEqual(ret.returncode, 0)
self.assertEqual(ret.stdout, "test")
def test_local_script(self):
ret = cls_cmd.runScript("localhost", "tests/scripts/hello-world.sh", 1)
self.assertEqual(ret.args, "tests/scripts/hello-world.sh")
self.assertEqual(ret.returncode, 0)
self.assertEqual(ret.stdout, "+ echo hello, world\nhello, world")
ret = cls_cmd.runScript("localhost", "tests/scripts/hello-fail.sh", 1, "TESTFAIL")
self.assertEqual(ret.args, "tests/scripts/hello-fail.sh TESTFAIL")
self.assertEqual(ret.returncode, 1)
self.assertEqual(ret.stdout, "TESTFAIL")
ret = cls_cmd.runScript("localhost", "tests/scripts/hello-fail.sh", 1, "TESTFAIL2", "/tmp/result")
self.assertEqual(ret.args, "tests/scripts/hello-fail.sh TESTFAIL2 &> /tmp/result")
self.assertEqual(ret.returncode, 1)
self.assertEqual(ret.stdout, "")
with cls_cmd.getConnection("localhost") as ssh:
ret = ssh.run("cat /tmp/result")
self.assertEqual(ret.args, "cat /tmp/result")
self.assertEqual(ret.returncode, 0)
self.assertEqual(ret.stdout, "TESTFAIL2")
@unittest.skip("need to be able to passwordlessly SSH to localhost, also disable stty -ixon")
def test_remote_cmd(self):
with cls_cmd.getConnection("127.0.0.1") as cmd:
self.assertTrue(isinstance(cmd, cls_cmd.RemoteCmd))
ret = cmd.run("true")
self.assertEqual(ret.args, "true")
self.assertEqual(ret.returncode, 0)
self.assertEqual(ret.stdout, "")
ret = cmd.run("false")
self.assertEqual(ret.args, "false")
self.assertEqual(ret.returncode, 1)
self.assertEqual(ret.stdout, "")
ret = cmd.run("echo test")
self.assertEqual(ret.args, "echo test")
self.assertEqual(ret.returncode, 0)
self.assertEqual(ret.stdout, "test")
@unittest.skip("need to be able to passwordlessly SSH to localhost, also disable stty -ixon")
def test_remote_script(self):
ret = cls_cmd.runScript("127.0.0.1", "tests/scripts/hello-world.sh", 1)
self.assertEqual(ret.args, "tests/scripts/hello-world.sh")
self.assertEqual(ret.returncode, 0)
self.assertEqual(ret.stdout, "+ echo hello, world\nhello, world")
ret = cls_cmd.runScript("127.0.0.1", "tests/scripts/hello-fail.sh", 1, "TESTFAIL")
self.assertEqual(ret.args, "tests/scripts/hello-fail.sh TESTFAIL")
self.assertEqual(ret.returncode, 1)
self.assertEqual(ret.stdout, "TESTFAIL")
ret = cls_cmd.runScript("127.0.0.1", "tests/scripts/hello-fail.sh", 1, "TESTFAIL2", "/tmp/result")
self.assertEqual(ret.args, "tests/scripts/hello-fail.sh TESTFAIL2 &> /tmp/result")
self.assertEqual(ret.returncode, 1)
self.assertEqual(ret.stdout, "")
with cls_cmd.getConnection("localhost") as ssh:
ret = ssh.run("cat /tmp/result")
self.assertEqual(ret.args, "cat /tmp/result")
self.assertEqual(ret.returncode, 0)
self.assertEqual(ret.stdout, "TESTFAIL2")
if __name__ == '__main__':
unittest.main()