def generate_instance_commands(): ''' The Instance client holds the Singularity Instance command group The levels of verbosity (debug and quiet) are passed from the main client via the environment variable MESSAGELEVEL. ''' from spython.instance import Instance from spython.main.base.logger import println from spython.main.instances import list_instances from spython.utils import run_command as run_cmd # run_command uses run_cmd, but wraps to catch error from spython.main.base.command import (init_command, run_command) from spython.main.base.generate import RobotNamer from .start import start from .stop import stop Instance.RobotNamer = RobotNamer() Instance._init_command = init_command Instance.run_command = run_cmd Instance._run_command = run_command Instance._list = list_instances # list command is used to get metadata Instance._println = println Instance.start = start # intended to be called on init, not by user Instance.stop = stop # Give an instance the ability to breed :) Instance.instance = Instance return Instance
def generate_oci_commands(): """The oci command group will allow interaction with an image using OCI commands. """ from spython.oci import OciImage from spython.main.base.logger import println # run_command uses run_cmd, but wraps to catch error from spython.main.base.command import run_command, send_command from spython.main.base.generate import RobotNamer # Oci Command Groups from .mounts import mount, umount from .states import kill, state, start, pause, resume, _state_command from .actions import attach, create, delete, execute, run, _run, update # Oci Commands OciImage.start = start OciImage.mount = mount OciImage.umount = umount OciImage.state = state OciImage.resume = resume OciImage.pause = pause OciImage.attach = attach OciImage.create = create OciImage.delete = delete OciImage.execute = execute OciImage.update = update OciImage.kill = kill OciImage.run = run OciImage._run = _run OciImage._state_command = _state_command OciImage.RobotNamer = RobotNamer() OciImage._send_command = send_command # send and disregard stderr, stdout OciImage._run_command = run_command OciImage._println = println OciImage.OciImage = OciImage return OciImage
def test_oci(sandbox): # pylint: disable=redefined-outer-name image = sandbox container_id = RobotNamer().generate() # A non existing process should not have a state print('...Case 1. Check status of non-existing bundle.') state = Client.oci.state('mycontainer') assert state is None # This will use sudo print("...Case 2: Create OCI image from bundle") result = Client.oci.create(bundle=image, container_id=container_id) print(result) assert result['status'] == 'created' print('...Case 3. Execute command to non running bundle.') result = Client.oci.execute(container_id=container_id, sudo=True, command=['ls', '/']) print(result) print(Client.version_info()) if Client.version_info() >= VersionInfo(3, 2, 0, '1'): assert result['return_code'] == 255 else: assert 'bin' in result print('...Case 4. Start container return value 0.') state = Client.oci.start(container_id, sudo=True) assert state == 0 print('...Case 5. Execute command to running bundle.') result = Client.oci.execute(container_id=container_id, sudo=True, command=['ls', '/']) print(result) assert 'bin' in result print('...Case 6. Check status of existing bundle.') state = Client.oci.state(container_id, sudo=True) assert state['status'] == 'running' print('...Case 7. Pause running container return value 0.') state = Client.oci.pause(container_id, sudo=True) assert state == 0 # State was still reported as running if Client.version_info() >= VersionInfo(3, 2, 0, '1'): print('...check status of paused bundle.') state = Client.oci.state(container_id, sudo=True) assert state['status'] == 'paused' print('...Case 8. Resume paused container return value 0.') state = Client.oci.resume(container_id, sudo=True) assert state == 0 print('...check status of resumed bundle.') state = Client.oci.state(container_id, sudo=True) assert state['status'] == 'running' print('...Case 9. Kill container.') state = Client.oci.kill(container_id, sudo=True) assert state == 0 # Clean up the image (should still use sudo) # Bug in singularity that kill doesn't kill completely - this returns # 255. When testsupdated to 3.1.* add signal=K to run result = Client.oci.delete(container_id, sudo=True) assert result in [0, 255]