def test_wait_until_initialization(self, os_system, sleep):
        os_system.side_effect = [65280, 65280, 0]
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.ssh_params.ssh_fonctionnal_params = "a"
        ssh.ssh_params.pem_ssh = "b"
        ssh.ssh_params.instance_user = "******"
        ssh.ssh_params.public_ip = "d"
        ssh.ssh_params.verbose = True
        assert ssh.wait_until_initialization() == 0
        os_system.assert_called_with("ssh a -v b c@d exit && echo $?")
        assert os_system.call_count == 3

        os_system.side_effect = [
            65280, 65280, 65280, 65280, 65280, 65280, 65280, 65280, 65280,
            65280, 65280
        ]
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.ssh_params.ssh_fonctionnal_params = "a"
        ssh.ssh_params.pem_ssh = "b"
        ssh.ssh_params.instance_user = "******"
        ssh.ssh_params.public_ip = "d"
        ssh.ssh_params.verbose = True
        with self.assertRaises(Exception):
            ssh.wait_until_initialization()
        os_system.assert_called_with("ssh a -v b c@d exit && echo $?")

        os_system.side_effect = [1]
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.ssh_params.ssh_fonctionnal_params = "a"
        ssh.ssh_params.pem_ssh = "b"
        ssh.ssh_params.instance_user = "******"
        ssh.ssh_params.public_ip = "d"
        ssh.ssh_params.verbose = True
        assert ssh.wait_until_initialization() == 1
        os_system.assert_called_with("ssh a -v b c@d exit && echo $?")
 def test_init_specific(self, get_region, get_credentials, ec2_node_driver):
     os.environ['IMAGE_NAME'] = 'Fedora'
     ssh = SSHCrossCloud(**self.command_arg)
     ssh.ssh_params.aws.region = None
     get_credentials.return_value = "test1", "test2"
     ssh.spe_driver = get_provider_specific_driver(ssh.ssh_params)
     assert ssh.spe_driver.init_specific() is not None
     assert ssh.ssh_params.instance_user == "fedora"
     get_region.assert_called()
     get_credentials.assert_called()
     ec2_node_driver.assert_called()
    def test_check_parameters(self):
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.ssh_params.leave = True
        ssh.ssh_params.stop = True
        self.assertRaises(Exception)

        ssh = SSHCrossCloud(**self.command_arg)
        ssh.ssh_params.provider = None
        self.assertRaises(Exception)

        ssh = SSHCrossCloud(**self.command_arg)
        ssh.ssh_params.detach = True
        ssh.ssh_params.attach = True
        self.assertRaises(Exception)
Ejemplo n.º 4
0
def main():
    # Module Arguments
    command_args = vars(parser.parse_args())

    # Verbose
    if command_args['v']:
        logging.getLogger().setLevel(logging.INFO)
        coloredlogs.install(level='INFO')

    ssh = SSHCrossCloud(**command_args)
    ssh.execute()
    # ssh.execute('ls')

    return 0
    def test_manage_instance(self, spe_start_instance, ssh_init_instance):
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.init_provider_specifics()
        ssh.manage_instance()
        ssh.ssh_params.sshcrosscloud_instance_id = None
        ssh_init_instance.assert_called_with(with_instance=False)

        ssh.ssh_params.instance_state = "stopped"
        ssh.manage_instance()
        spe_start_instance.assert_called()
        ssh_init_instance.assert_called_with(with_instance=True)

        ssh.ssh_params.instance_state = "unknown"
        with self.assertRaises(Exception):
            ssh.manage_instance()
 def test_create_local_rsa_key_pair_no_key(self, is_path_file, os_chmod,
                                           os_system):
     ssh = SSHCrossCloud(**self.command_arg)
     ssh.ssh_params.rsa_private_key_file_path = "a"
     ssh.spe_driver.create_local_rsa_key_pair()
     os_system.assert_called_with("ssh-keygen -b 2048 -f a")
     os_chmod.assert_called()
     is_path_file.assert_called()
    def test_finish_action(self, spe_terminate_instance, spe_stop_instance):
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.init_provider_specifics()
        ssh.ssh_params.final_state = 'stop'
        ssh.finish_action()
        spe_stop_instance.assert_called()

        ssh.ssh_params.final_state = 'terminate'
        ssh.finish_action()
        spe_terminate_instance.assert_called()
    def test_init_instance(self, spe_driver_create_instance,
                           spe_driver_spe_wait_until_running,
                           spe_driver_get_node):
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.init_provider_specifics()
        ssh.init_instance(with_instance=True)
        spe_driver_get_node.assert_called()
        spe_driver_spe_wait_until_running.assert_called()

        ssh.init_instance(with_instance=False)
        spe_driver_get_node.assert_called()
        spe_driver_create_instance.assert_called()
    def test_get_node_any_arg(self):
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.spe_driver.driver = FakeProviderDriver()
        ssh.spe_driver.driver.node_id = 'foo'
        ssh.ssh_params.sshcrosscloud_instance_id = 'foo'
        assert ssh.spe_driver.get_node_any_arg().id == 'foo'

        ssh.spe_driver.driver.list_nodes_empty = True
        with self.assertRaises(Exception):
            ssh.spe_driver.get_node_any_arg()

        ssh.spe_driver.driver.node_id = 'foo'
        with self.assertRaises(Exception):
            ssh.spe_driver.get_node_any_arg()
    def test_terminate_instance_no_arg(self):
        ssh = SSHCrossCloud(**self.command_arg)

        ssh.spe_driver.driver = FakeProviderDriver()
        ssh.spe_driver.driver.node_id = "b"
        ssh.spe_driver.driver.node_state = "terminated"
        ssh.spe_driver.driver.terminate = True
        ssh.ssh_params.sshcrosscloud_instance_id = "a"
        assert ssh.spe_driver.terminate_instance_no_arg() is None
        assert ssh.spe_driver.driver.list_nodes_called is True
        assert ssh.spe_driver.driver.destroy_node_called is False

        ssh.spe_driver.driver = FakeProviderDriver()
        ssh.spe_driver.driver.node_id = "a"
        ssh.spe_driver.driver.node_state = "not_terminated"
        ssh.spe_driver.driver.terminate = True
        ssh.ssh_params.sshcrosscloud_instance_id = "a"
        assert ssh.spe_driver.terminate_instance_no_arg() is None
        assert ssh.spe_driver.driver.list_nodes_called is True
        assert ssh.spe_driver.driver.destroy_node_called is True

        ssh.spe_driver.driver = FakeProviderDriver()
        ssh.spe_driver.driver.node_id = "b"
        ssh.spe_driver.driver.node_state = "not_terminated"
        ssh.spe_driver.driver.terminate = True
        ssh.ssh_params.sshcrosscloud_instance_id = "a"
        assert ssh.spe_driver.terminate_instance_no_arg() is None
        assert ssh.spe_driver.driver.list_nodes_called is True
        assert ssh.spe_driver.driver.destroy_node_called is False

        ssh.spe_driver.driver = FakeProviderDriver()
        ssh.spe_driver.driver.node_id = "b"
        ssh.spe_driver.driver.node_state = "terminated"
        ssh.spe_driver.driver.terminate = True
        ssh.ssh_params.sshcrosscloud_instance_id = "a"
        assert ssh.spe_driver.terminate_instance_no_arg() is None
        assert ssh.spe_driver.driver.list_nodes_called is True
        assert ssh.spe_driver.driver.destroy_node_called is False

        ssh.spe_driver.driver = FakeProviderDriver()
        ssh.spe_driver.driver.node_id = "a"
        ssh.spe_driver.driver.node_state = "not_terminated"
        ssh.spe_driver.driver.terminate = False
        ssh.ssh_params.sshcrosscloud_instance_id = "a"
        with self.assertRaises(Exception):
            ssh.spe_driver.terminate_instance_no_arg()
    def test_rsync_back_to_local(self, os_system):
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.ssh_params.pem_ssh = "b"
        ssh.ssh_params.instance_user = "******"
        ssh.ssh_params.public_ip = "d"
        ssh.ssh_params.rsync_directory = "e"
        ssh.ssh_params.verbose = False
        ssh.rsync_to_instance()
        os_system.assert_called_with(
            "rsync -Pa -e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o "
            "LogLevel=quiet b' --exclude-from='.rsyncignore' e/* c@d:/home/c")

        ssh.ssh_params.verbose = True
        ssh.rsync_to_instance()
        os_system.assert_called_with(
            "rsync -Pav -e 'ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o "
            "LogLevel=quiet b' --exclude-from='.rsyncignore' e/* c@d:/home/c")
    def test_rsync_to_instance(self, os_system):
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.ssh_params.ssh_fonctionnal_params = "a"
        ssh.ssh_params.pem_ssh = "b"
        ssh.ssh_params.instance_user = "******"
        ssh.ssh_params.public_ip = "d"
        ssh.ssh_params.rsync_directory = "e"
        ssh.ssh_params.verbose = False
        ssh.rsync_to_instance()
        os_system.assert_called_with(
            "rsync -Pa -e 'ssh a b' --exclude-from='.rsyncignore' e/* c@d:/home/c"
        )

        ssh.ssh_params.verbose = True
        ssh.rsync_to_instance()
        os_system.assert_called_with(
            "rsync -Pav -e 'ssh a b' --exclude-from='.rsyncignore' e/* c@d:/home/c"
        )
    def test_attach_to_instance(self, os_system):
        ssh = SSHCrossCloud(**self.command_arg)
        ssh.ssh_params.ssh_fonctionnal_params = "a"
        ssh.ssh_params.pem_ssh = "b"
        ssh.ssh_params.instance_user = "******"
        ssh.ssh_params.public_ip = "d"
        ssh.ssh_params.ssh_params = "e"
        ssh.ssh_params.verbose = True
        ssh.ssh_params.multiplex = False
        ssh.ssh_params.ssh_script = "f"
        ssh.attach_to_instance()
        os_system.assert_called_with("ssh a e -v b c@d f")

        ssh.ssh_params.verbose = False
        ssh.attach_to_instance()
        os_system.assert_called_with("ssh a e b c@d f")

        ssh.ssh_params.ssh_script = None
        ssh.attach_to_instance()
        os_system.assert_called_with("ssh a e b c@d ")

        ssh.ssh_params.multiplex = True
        ssh.ssh_params.instance_name = 'g'
        ssh.ssh_params.ssh_detach = True
        ssh.attach_to_instance()
        os_system.assert_called_with(
            "ssh a e b c@d  -t 'tmux has-session -t g || tmux new-session -s g -d'"
        )

        ssh.ssh_params.ssh_script = 'f'
        ssh.attach_to_instance()
        os_system.assert_called_with(
            'ssh a e b c@d  -t \'tmux has-session -t g || tmux new-session -s g -d "f"\''
        )

        ssh.ssh_params.ssh_script = 'f'
        ssh.ssh_params.ssh_detach = False
        ssh.ssh_params.ssh_attach = True
        ssh.attach_to_instance()
        os_system.assert_called_with(
            'ssh a e b c@d  -t \'tmux attach-session -t g || tmux new-session -s g "f"\''
        )

        ssh.ssh_params.ssh_script = None
        ssh.attach_to_instance()
        os_system.assert_called_with(
            "ssh a e b c@d  -t 'tmux attach-session -t g || tmux new-session -s g'"
        )
 def test_get_credentials(self, config_parser, is_file):
     is_file.return_value = True
     ssh = SSHCrossCloud(**self.command_arg)
     ssh.spe_driver = get_provider_specific_driver(ssh.ssh_params)
     assert len(ssh.spe_driver.get_credentials()) is 4
     config_parser.assert_called()
 def test_get_region(self, config_parser, is_file):
     is_file.return_value = True
     ssh = SSHCrossCloud(**self.command_arg)
     ssh.spe_driver = get_provider_specific_driver(ssh.ssh_params)
     assert ssh.spe_driver.get_region_from_config_file() is not None
     config_parser.assert_called()
 def test_create_local_rsa_key_pair_private_key_and_public_key(
         self, isfile):
     ssh = SSHCrossCloud(**self.command_arg)
     isfile.return_value = True
     ssh.ssh_params.rsa_private_key_file_path = "a"
     assert ssh.spe_driver.create_local_rsa_key_pair() == 0
    def test_manage_commands(self):
        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.leave = True
        ssh.manage_commands()
        assert ssh_params.final_state == 'leave'

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.stop = True
        ssh.manage_commands()
        assert ssh_params.final_state == 'stop'

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.terminate = True
        ssh.manage_commands()
        assert ssh_params.final_state == 'terminate'

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.detach = True
        ssh.manage_commands()
        assert ssh_params.final_state == 'leave'
        assert ssh_params.ssh_detach is True
        assert ssh_params.multiplex is True

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.attach = True
        ssh.manage_commands()
        assert ssh_params.final_state == 'leave'
        assert ssh_params.ssh_attach is True
        assert ssh_params.multiplex is True
        assert ssh_params.no_rsync_begin is True
        assert ssh_params.no_rsync_end is True

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.finish = True
        ssh.manage_commands()
        assert ssh_params.no_rsync_begin is True

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.norsync = True
        ssh.manage_commands()
        assert ssh_params.no_rsync_begin is True
        assert ssh_params.no_rsync_end is True

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.provider = "test"
        ssh.manage_commands()
        assert ssh_params.provider == "test"

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.l = "L_param"
        ssh_params.ssh_params = "a"
        ssh.manage_commands()
        assert ssh_params.ssh_params == "a -L L_param"

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.r = "R_param"
        ssh_params.ssh_params = "a"
        ssh.manage_commands()
        assert ssh_params.ssh_params == "a -R R_param"

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.i = "i_param"
        ssh.manage_commands()
        assert ssh_params.pem_ssh == "-i i_param"

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.debug = True
        ssh.manage_commands()
        assert ssh_params.verbose is True

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.config = True
        ssh.manage_commands()
        assert ssh_params.config is True

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.status = True
        ssh.manage_commands()
        assert ssh_params.status_mode is True
        assert ssh_params.no_rsync_begin is True
        assert ssh_params.no_rsync_end is True
        assert ssh_params.no_attach is True
        assert ssh_params.no_wait_until_init is True

        ssh = SSHCrossCloud(**self.command_arg)
        ssh_params = ssh.ssh_params

        ssh_params.destroy = True
        ssh.manage_commands()
        assert ssh_params.no_rsync_begin is True
        assert ssh_params.no_rsync_end is True
        assert ssh_params.no_attach is True
        assert ssh_params.final_state == 'terminate'