예제 #1
0
    def test_client_script_host_ssh(self):
        """
        Verify use cases for the client_script ssh requests.
        """
        sys.argv = ['', 'host', 'ssh']
        with mock.patch('requests.Session.get') as _get, \
                mock.patch('os.path.realpath') as _realpath, \
                mock.patch('subprocess.call') as _call, \
                mock.patch('tempfile.mkstemp') as _mkstemp, \
                mock.patch('os.close') as _close:
            _realpath.return_value = self.conf
            _mkstemp.return_value = (1, '/tmp/test_key_file')
            for cmd in (
                    ['127.0.0.1'],
                    ['127.0.0.1', '-p 22'],
                    ['127.0.0.1', '-p 22 -v']):
                content = six.b(
                    '{"ssh_priv_key": "dGVzdAo=", "remote_user": "******"}')
                mock_return = requests.Response()
                mock_return._content = content
                mock_return.status_code = 200
                _get.return_value = mock_return

                sys.argv[3:] = cmd
                expected = 'ssh -i /tmp/test_key_file -l root {0} {1}'.format(
                    ' '.join(cmd[1:]), cmd[0])
                print(sys.argv)
                client_script.main()
                _get.assert_called_once_with(mock.ANY)
                _call.assert_called_once_with(expected, shell=True)
                _call.reset_mock()
                _get.reset_mock()
예제 #2
0
    def test_client_script_create(self):
        """
        Verify use cases for the client_script create command.
        """
        sys.argv = ['', 'create']
        with contextlib.nested(
                mock.patch('requests.Session.put'),
                mock.patch('os.path.realpath'),
                mock.patch('argparse.FileType.__call__',
                           mock.mock_open(read_data='1234567890'),
                           create=True)
                ) as (_put, _realpath, _filetype):
            _realpath.return_value = self.conf
            for subcmd in (
                    ['cluster'],
                    ['host', '-c', 'honeynut', '1.2.3.4'],
                    ['restart'],
                    ['upgrade', '-u', '1']):
                mock_return = requests.Response()
                mock_return._content = '{}'
                mock_return.status_code = 201
                _put.return_value = mock_return

                sys.argv[2:] = subcmd + ['test']
                print sys.argv
                client_script.main()
                self.assertEquals(1, _put.call_count)
                _put.reset_mock()
예제 #3
0
    def test_client_script_create(self):
        """
        Verify use cases for the client_script create command.
        """
        sys.argv = ['', 'create']
        with contextlib.nested(
                mock.patch('requests.Session.put'),
                mock.patch('os.path.realpath'),
                mock.patch('argparse.FileType.__call__',
                           mock.mock_open(read_data='1234567890'),
                           create=True)
                ) as (_put, _realpath, _filetype):
            _realpath.return_value = self.conf
            for subcmd in (
                    ['cluster'],
                    ['host', '-c', 'honeynut', '1.2.3.4']):
                mock_return = requests.Response()
                mock_return._content = '{}'
                mock_return.status_code = 201
                _put.return_value = mock_return

                sys.argv[2:] = subcmd + ['test']
                print sys.argv
                client_script.main()
                self.assertEquals(1, _put.call_count)
                _put.reset_mock()
예제 #4
0
    def test_client_script_delete(self):
        """
        Verify use cases for the client_script delete requests.
        """
        sys.argv = ['', 'delete']
        with mock.patch('requests.Session.delete') as _delete, \
                mock.patch('os.path.realpath') as  _realpath:
            _realpath.return_value = self.conf
            for cmd in (['cluster', 'delete',
                         'test'], ['cluster', 'delete', 'test1',
                                   'test2'], ['host', 'delete', 'localhost'],
                        ['host', 'delete', '10.0.0.1', '10.0.0.2',
                         '10.0.0.3'], ['network', 'delete', 'test'],
                        ['network', 'delete', 'test', 'test2', 'test3']):
                mock_return = requests.Response()
                mock_return._content = '{}'
                mock_return.status_code = 200
                _delete.return_value = mock_return

                sys.argv[1:] = cmd
                print sys.argv
                client_script.main()
                num_things = len(cmd) - 2
                self.assertEquals(num_things, _delete.call_count)
                _delete.reset_mock()
예제 #5
0
    def test_client_script_get(self):
        """
        Verify use cases for the client_script get requests.
        """
        sys.argv = ['']
        with mock.patch('requests.Session.get') as _get, \
                mock.patch('os.path.realpath') as _realpath:
            _realpath.return_value = self.conf
            for cmd, content in (
                    (['cluster', 'get', 'test'], '{}'),
                    (['cluster', 'list'], '[]'),
                    (['cluster', 'restart', 'status', 'test'], '{}'),
                    (['cluster', 'upgrade', 'status', 'test'], '{}'),
                    (['cluster', 'restart', 'status', 'test'], '{}'),
                    (['container_manager', 'get', 'test'], '{}'),
                    (['container_manager', 'list'], '[]'),
                    (['host', 'get', 'localhost'], '{}'),
                    (['host', 'list'], '[]'),
                    (['host', 'list', 'test'], '[]'),
                    (['host', 'status', 'localhost'], '{}'),
                    (['network', 'get', 'test'], '{}'),
                    (['network', 'list'], '[]'),
                    ):
                mock_return = requests.Response()
                mock_return._content = content
                mock_return.status_code = 200
                _get.return_value = mock_return

                sys.argv[1:] = cmd
                print(sys.argv)
                client_script.main()
                self.assertEquals(1, _get.call_count)
                _get.reset_mock()
예제 #6
0
    def test_client_script_host_ssh(self):
        """
        Verify use cases for the client_script ssh requests.
        """
        sys.argv = ['', 'host', 'ssh']
        with mock.patch('requests.Session.get') as _get, \
                mock.patch('os.path.realpath') as _realpath, \
                mock.patch('subprocess.call') as _call, \
                mock.patch('tempfile.mkstemp') as _mkstemp, \
                mock.patch('os.close') as _close:
            _realpath.return_value = self.conf
            _mkstemp.return_value = (1, '/tmp/test_key_file')
            for cmd in (['127.0.0.1'], ['127.0.0.1',
                                        '-p 22'], ['127.0.0.1', '-p 22 -v']):
                mock_return = requests.Response()
                mock_return._content = (
                    '{"ssh_priv_key": "dGVzdAo=", "remote_user": "******"}')
                mock_return.status_code = 200
                _get.return_value = mock_return

                sys.argv[3:] = cmd
                expected = 'ssh -i /tmp/test_key_file -l root {0} {1}'.format(
                    ' '.join(cmd[1:]), cmd[0])
                print sys.argv
                client_script.main()
                _get.assert_called_once()
                _call.assert_called_once_with(expected, shell=True)
                _call.reset_mock()
                _get.reset_mock()
예제 #7
0
    def test_client_script_get(self):
        """
        Verify use cases for the client_script get requests.
        """
        sys.argv = ['']
        with mock.patch('requests.Session.get') as _get, \
                mock.patch('os.path.realpath') as _realpath:
            _realpath.return_value = self.conf
            for cmd, content in (
                (['cluster', 'get', 'test'], '{}'),
                (['cluster', 'list'], '[]'),
                (['cluster', 'restart', 'status', 'test'], '{}'),
                (['cluster', 'upgrade', 'status', 'test'], '{}'),
                (['cluster', 'restart', 'status', 'test'], '{}'),
                (['host', 'get', 'localhost'], '{}'),
                (['host', 'list'], '[]'),
                (['host', 'list', 'test'], '[]'),
                (['host', 'status', 'localhost'], '{}'),
                (['network', 'get', 'test'], '{}'),
                (['network', 'list'], '[]'),
            ):
                mock_return = requests.Response()
                mock_return._content = content
                mock_return.status_code = 200
                _get.return_value = mock_return

                sys.argv[1:] = cmd
                print sys.argv
                client_script.main()
                self.assertEquals(1, _get.call_count)
                _get.reset_mock()
예제 #8
0
    def test_client_script_put(self):
        """
        Verify use cases for the client_script put requests.
        """
        sys.argv = ['']
        with mock.patch('requests.Session.put') as _put, \
                mock.patch('os.path.realpath') as _realpath, \
                mock.patch('argparse.FileType.__call__', mock.mock_open(
                    read_data='1234567890'), create=True) as _filetype:
            _realpath.return_value = self.conf
            for cmd in (['cluster', 'create'], [
                    'cluster', 'create', '-n', 'default'
            ], ['cluster', 'create', '-t', 'kubernetes'
                ], ['cluster', 'create', '-t', 'kubernetes', '-n',
                    'default'], ['cluster', 'deploy',
                                 'start'], ['cluster', 'restart', 'start'
                                            ], ['cluster', 'upgrade', 'start'],
                        ['host', 'create', '-c', 'honeynut', '1.2.3.4']):
                mock_return = requests.Response()
                mock_return._content = '{}'
                mock_return.status_code = 201
                mock_return.request = mock.MagicMock(path_url='/fake/path')
                _put.return_value = mock_return

                sys.argv[1:] = cmd + ['test']
                if cmd[1] == 'deploy':
                    sys.argv.append('1')  # arbitrary version
                print sys.argv
                client_script.main()
                self.assertEquals(1, _put.call_count)
                _put.reset_mock()
예제 #9
0
    def test_client_script_delete(self):
        """
        Verify use cases for the client_script delete requests.
        """
        sys.argv = ['', 'delete']
        with mock.patch('requests.Session.delete') as _delete, \
                mock.patch('os.path.realpath') as  _realpath:
            _realpath.return_value = self.conf
            for cmd in (
                    ['cluster', 'delete', 'test'],
                    ['cluster', 'delete', 'test1', 'test2'],
                    ['container_manager', 'delete', 'test'],
                    ['container_manager', 'delete', 'test', 'test2'],
                    ['host', 'delete', 'localhost'],
                    ['host', 'delete', '10.0.0.1', '10.0.0.2', '10.0.0.3'],
                    ['network', 'delete', 'test'],
                    ['network', 'delete', 'test', 'test2', 'test3']):
                mock_return = requests.Response()
                mock_return._content = '{}'
                mock_return.status_code = 200
                _delete.return_value = mock_return

                sys.argv[1:] = cmd
                print(sys.argv)
                client_script.main()
                num_things = len(cmd) - 2
                self.assertEquals(num_things, _delete.call_count)
                _delete.reset_mock()
예제 #10
0
 def test_client_script_passhash_with_password(self):
     """
     Verify passhash works via --password
     """
     sys.argv = ['', 'passhash', '--password', 'mypass']
     with mock.patch('sys.stdout', new_callable=StringIO) as _out, \
             mock.patch('os.path.realpath') as _realpath:
         _realpath.return_value = self.conf
         client_script.main()
         hashed = _out.getvalue().strip()
         self.assertEquals(bcrypt.hashpw('mypass', hashed), hashed)
예제 #11
0
 def test_client_script_passhash_with_password(self):
     """
     Verify passhash works via --password
     """
     sys.argv = ['', 'passhash', '--password', 'mypass']
     with mock.patch('sys.stdout', new_callable=six.StringIO) as _out, \
             mock.patch('os.path.realpath') as _realpath:
         _realpath.return_value = self.conf
         client_script.main()
         hashed = _out.getvalue().strip()
         self.assertEquals(bcrypt.hashpw('mypass', hashed), hashed)
예제 #12
0
 def test_client_script_passhash_with_stdin(self):
     """
     Verify passhash works via stdin
     """
     sys.argv = ['', 'create', 'passhash', '--file', '-']
     with contextlib.nested(
             mock.patch('sys.stdout', new_callable=StringIO),
             mock.patch('sys.stdin', StringIO("mypass")),
             mock.patch('os.path.realpath')) as (_out, _in, _realpath):
         _realpath.return_value = self.conf
         client_script.main()
         hashed = _out.getvalue().strip()
         self.assertEquals(bcrypt.hashpw('mypass', hashed), hashed)
예제 #13
0
 def test_client_script_passhash_with_prompt(self):
     """
     Verify passhash works via getpass prompt
     """
     sys.argv = ['', 'passhash']
     with mock.patch('sys.stdout', new_callable=six.StringIO) as _out, \
             mock.patch('getpass.getpass') as _gp, \
             mock.patch('os.path.realpath') as _realpath:
         _realpath.return_value = self.conf
         _gp.return_value = 'mypass'
         client_script.main()
         hashed = _out.getvalue().strip()
         self.assertEquals(bcrypt.hashpw('mypass', hashed), hashed)
예제 #14
0
 def test_client_script_passhash_with_stdin(self):
     """
     Verify passhash works via stdin
     """
     sys.argv = ['', 'create', 'passhash', '--file', '-']
     with contextlib.nested(
             mock.patch('sys.stdout', new_callable=StringIO),
             mock.patch('sys.stdin', StringIO("mypass")),
             mock.patch('os.path.realpath')) as (_out, _in, _realpath):
         _realpath.return_value = self.conf
         client_script.main()
         hashed = _out.getvalue().strip()
         self.assertEquals(bcrypt.hashpw('mypass', hashed), hashed)
예제 #15
0
 def test_client_script_passhash_with_file(self):
     """
     Verify passhash works via --file
     """
     with open('pwdfile', 'w') as f:
         f.write("mypass")
     sys.argv = ['', 'passhash', '--file', 'pwdfile']
     with mock.patch('sys.stdout', new_callable=StringIO) as _out, \
             mock.patch('os.path.realpath') as _realpath:
         _realpath.return_value = self.conf
         client_script.main()
         os.remove('pwdfile')
         hashed = _out.getvalue().strip()
         self.assertEquals(bcrypt.hashpw('mypass', hashed), hashed)
예제 #16
0
 def test_client_script_passhash_with_prompt(self):
     """
     Verify passhash works via getpass prompt
     """
     sys.argv = ['', 'create', 'passhash']
     with contextlib.nested(
             mock.patch('sys.stdout', new_callable=StringIO),
             mock.patch('getpass.getpass'),
             mock.patch('os.path.realpath')) as (_out, _gp, _realpath):
         _realpath.return_value = self.conf
         _gp.return_value = 'mypass'
         client_script.main()
         hashed = _out.getvalue().strip()
         self.assertEquals(bcrypt.hashpw('mypass', hashed), hashed)
예제 #17
0
 def test_client_script_passhash_with_file(self):
     """
     Verify passhash works via --file
     """
     with open('pwdfile', 'w') as f:
         f.write("mypass");
     sys.argv = ['', 'passhash', '--file', 'pwdfile']
     with mock.patch('sys.stdout', new_callable=six.StringIO) as _out, \
             mock.patch('os.path.realpath') as _realpath:
         _realpath.return_value = self.conf
         client_script.main()
         os.remove('pwdfile')
         hashed = _out.getvalue().strip()
         self.assertEquals(bcrypt.hashpw('mypass', hashed), hashed)
예제 #18
0
    def test_client_script_get(self):
        """
        Verify use cases for the client_script get command.
        """
        sys.argv = ['', 'get']
        with contextlib.nested(
                mock.patch('requests.Session.get'),
                mock.patch('os.path.realpath')) as (_get, _realpath):
            _realpath.return_value = self.conf
            for subcmd in ('cluster', 'host', 'restart', 'upgrade'):
                mock_return = requests.Response()
                mock_return._content = '{}'
                mock_return.status_code = 200
                _get.return_value = mock_return

                sys.argv[2:] = [subcmd, 'test']
                client_script.main()
                self.assertEquals(1, _get.call_count)
                _get.reset_mock()
예제 #19
0
    def test_client_script_get(self):
        """
        Verify use cases for the client_script get command.
        """
        sys.argv = ['', 'get']
        with contextlib.nested(
                mock.patch('requests.Session.get'),
                mock.patch('os.path.realpath')) as (_get, _realpath):
            _realpath.return_value = self.conf
            for subcmd in ('cluster', 'host', 'restart', 'upgrade'):
                mock_return = requests.Response()
                mock_return._content = '{}'
                mock_return.status_code = 200
                _get.return_value = mock_return

                sys.argv[2:] = [subcmd, 'test']
                client_script.main()
                self.assertEquals(1, _get.call_count)
                _get.reset_mock()
예제 #20
0
    def test_client_script_delete(self):
        """
        Verify use cases for the client_script delete command.
        """
        sys.argv = ['', 'delete']
        with contextlib.nested(
                mock.patch('requests.Session.delete'),
                mock.patch('os.path.realpath')) as (_delete, _realpath):
            _realpath.return_value = self.conf
            for subcmd in (['cluster'], ['host']):
                mock_return = requests.Response()
                mock_return._content = '{}'
                mock_return.status_code = 200
                _delete.return_value = mock_return

                sys.argv[2:] = subcmd + ['test']
                print sys.argv
                client_script.main()
                self.assertEquals(1, _delete.call_count)
                _delete.reset_mock()
예제 #21
0
    def test_client_script_list(self):
        """
        Verify use cases for the client_script list command.
        """
        sys.argv = ['', 'list']
        with contextlib.nested(mock.patch('requests.Session.get'),
                               mock.patch('os.path.realpath')) as (_get,
                                                                   _realpath):
            _realpath.return_value = self.conf
            for subcmd, content in ((['clusters'], '[]'), (['hosts'], '[]'),
                                    (['hosts', 'test'], '[]')):
                mock_return = requests.Response()
                mock_return._content = content
                mock_return.status_code = 200
                _get.return_value = mock_return

                sys.argv[2:] = subcmd
                client_script.main()
                self.assertEquals(1, _get.call_count)
                _get.reset_mock()
예제 #22
0
    def test_client_script_delete(self):
        """
        Verify use cases for the client_script delete command.
        """
        sys.argv = ['', 'delete']
        with contextlib.nested(
                mock.patch('requests.Session.delete'),
                mock.patch('os.path.realpath')) as (_delete, _realpath):
            _realpath.return_value = self.conf
            for subcmd in (['cluster'], ['host']):
                mock_return = requests.Response()
                mock_return._content = '{}'
                mock_return.status_code = 200
                _delete.return_value = mock_return

                sys.argv[2:] = subcmd + ['test']
                print sys.argv
                client_script.main()
                self.assertEquals(1, _delete.call_count)
                _delete.reset_mock()
예제 #23
0
    def test_client_script_list(self):
        """
        Verify use cases for the client_script list command.
        """
        sys.argv = ['', 'list']
        with contextlib.nested(
                mock.patch('requests.Session.get'),
                mock.patch('os.path.realpath')) as (_get, _realpath):
            _realpath.return_value = self.conf
            for subcmd, content in ((['clusters'], '[]'),
                                    (['hosts'], '[]'),
                                    (['hosts', 'test'], '[]')):
                mock_return = requests.Response()
                mock_return._content = content
                mock_return.status_code = 200
                _get.return_value = mock_return

                sys.argv[2:] = subcmd
                client_script.main()
                self.assertEquals(1, _get.call_count)
                _get.reset_mock()
예제 #24
0
    def test_client_script_put(self):
        """
        Verify use cases for the client_script put requests.
        """
        sys.argv = ['']
        read_data = six.b('1234567890')
        with mock.patch('requests.Session.put') as _put, \
                mock.patch('os.path.realpath') as _realpath, \
                mock.patch(
                    'argparse.FileType.__call__',
                    mock.mock_open(read_data=read_data),
                    create=True) as _filetype:
            _realpath.return_value = self.conf
            for cmd in (
                    ['cluster', 'create'],
                    ['cluster', 'create', '-n', 'default'],
                    ['cluster', 'create', '-t', 'kubernetes'],
                    ['cluster', 'create', '-t', 'kubernetes', '-n', 'default'],
                    ['cluster', 'deploy', 'start'],
                    ['cluster', 'restart', 'start'],
                    ['cluster', 'upgrade', 'start'],
                    ['container_manager', 'create'],
                    ['container_manager', 'create', '-o', '{}'],
                    ['container_manager', 'create'],
                    ['host', 'create', '-c', 'honeynut', '1.2.3.4'],
                    ['host', 'join', '1.2.3.4'],
                    ['network', 'create', '-n', 'test']):
                mock_return = requests.Response()
                mock_return._content = six.b('{}')
                mock_return.status_code = 201
                mock_return.request = mock.MagicMock(path_url='/fake/path')
                _put.return_value = mock_return

                sys.argv[1:] = cmd + ['test']
                if cmd[1] == 'deploy':
                    sys.argv.append('1')  # arbitrary version
                print(sys.argv)
                client_script.main()
                self.assertEquals(1, _put.call_count)
                _put.reset_mock()
예제 #25
0
    def test_client_script_start(self):
        """
        Verify use cases for the client_script start command.
        """
        sys.argv = ['', 'start']
        with contextlib.nested(
                mock.patch('requests.Session.put'),
                mock.patch('os.path.realpath')
                ) as (_put, _realpath):
            _realpath.return_value = self.conf
            for subcmd in (['restart'], ['upgrade']):
                mock_return = requests.Response()
                mock_return._content = '{}'
                mock_return.status_code = 201
                _put.return_value = mock_return

                sys.argv[2:] = subcmd + ['test']
                if subcmd[0] == 'upgrade':
                    sys.argv.append('1')  # arbitrary version
                print sys.argv
                client_script.main()
                self.assertEquals(1, _put.call_count)
                _put.reset_mock()
예제 #26
0
    def test_client_script_user_data(self):
        """
        Verify user-data generates a user-data file.
        """
        from email.mime.multipart import MIMEMultipart

        try:
            output_file = tempfile.mktemp()
            sys.argv = [
                '', 'user-data', '-e', 'https://example.com', '-o', output_file
            ]
            client_script.main()
            with open(output_file, 'r') as f:
                m = MIMEMultipart(f.read())
                self.assertTrue(m.is_multipart())
            filename_count = 0
            for param in m.get_params():
                if param[0] == 'filename':
                    filename_count += 1

            # We should have 3 filenames
            self.assertEquals(3, filename_count)
        finally:
            os.unlink(output_file)
예제 #27
0
    def test_client_script_user_data(self):
        """
        Verify user-data generates a user-data file.
        """
        from email.mime.multipart import MIMEMultipart

        try:
            output_file = tempfile.mktemp()
            sys.argv = [
                '', 'user-data',
                '-e', 'https://example.com', '-o', output_file]
            client_script.main()
            with open(output_file, 'r') as f:
                m = MIMEMultipart(f.read())
                self.assertTrue(m.is_multipart())
            filename_count = 0
            for param in m.get_params():
                if param[0] == 'filename':
                    filename_count += 1

            # We should have 3 filenames
            self.assertEquals(3, filename_count)
        finally:
            os.unlink(output_file)