Exemple #1
0
    def prepare(self):
        if self.network:
            dhcp_start = str(self.network.network + 2)
            dhcp_end = str(self.network.network + self.network.size - 2)
            dhcp_range = ",".join([dhcp_start, dhcp_end])
            values = {
                "USE_LXC_BRIDGE": "true",
                "LXC_BRIDGE": self.config.get("lxc_bridge", "lxcbr0"),
                "LXC_ADDR": self.network.network + 1,
                "LXC_NETMASK": self.network.netmask,
                "LXC_NETWORK": self.network,
                "LXC_DHCP_RANGE": dhcp_range,
                "LXC_DHCP_MAX": self.network.size - 3,
            }
            config = moves.StringIO()
            for name, value in six.iteritems(values):
                config.write("%(name)s=\"%(value)s\"\n" % {
                    "name": name,
                    "value": value
                })
            config.seek(0)
            self.server.ssh.run("cat > /tmp/.lxc_default", stdin=config)

        self.server.ssh.run("/bin/sh", stdin=_get_script("lxc-install.sh"))
        self.create_local_tunnels()
        self.create_remote_tunnels()
Exemple #2
0
    def setUp(self):
        self.file = mock.Mock()
        self.dir = mock.Mock()

        self.hello_world_md5sum = 'f36b2652200f5e88edd57963a1109146'
        self.hello_world_sha256sum = ('17b949eb67acf16bbf2605d57a01f7af4ff4b5'
                                      '7e200259de63fcebf20e75bbf5')

        self.fake_file = moves.StringIO(u"hello world\n")
        self.increment_hash_one = self.hello_world_sha256sum
        self.increment_hash_multi = ('1b4bc4ff41172a5f29eaeffb7e9fc24c683c693'
                                     '9ab30132ad5d93a1e4a6b16e8')
        self.increment_hash_emptydir = ("6b6c6a3d7548cc4396b3dacc6c2750c3"
                                        "da53f379d20996cbdd2c18be00c3742c")
        self.fake_dir = [
            ('root', ['d1, .git'], ['a', 'b']),
        ]
        self.dir_files = ['root/a', 'root/b']
        self.exclude = "ro*b"
        self.dir_files_without_excludeds = ['root/a']
        self.dir_hashes = [
            'a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447',
            'a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447'
        ]
        self.dir_compute = self.increment_hash_multi
        self.file_compute = self.hello_world_sha256sum + 'onefile'
Exemple #3
0
    def formatException(self, exc_info, record=None):
        """Format exception output with CONF.logging_exception_prefix."""
        if not record:
            try:
                return logging.Formatter.formatException(self, exc_info)
            except TypeError as type_error:
                # Work around https://bugs.python.org/issue28603
                msg = six.text_type(type_error)
                return '<Unprintable exception due to %s>\n' % msg

        stringbuffer = moves.StringIO()
        try:
            traceback.print_exception(exc_info[0], exc_info[1], exc_info[2],
                                      None, stringbuffer)
        except TypeError as type_error:
            # Work around https://bugs.python.org/issue28603
            msg = six.text_type(type_error)
            stringbuffer.write('<Unprintable exception due to %s>\n' % msg)

        lines = stringbuffer.getvalue().split('\n')
        stringbuffer.close()

        if self.conf.logging_exception_prefix.find('%(asctime)') != -1:
            record.asctime = self.formatTime(record, self.datefmt)

        self._compute_iso_time(record)

        formatted_lines = []
        for line in lines:
            pl = self.conf.logging_exception_prefix % record.__dict__
            fl = '%s%s' % (pl, line)
            formatted_lines.append(fl)
        return '\n'.join(formatted_lines)
Exemple #4
0
def dump(obj, prefix):
    """Transform object to dict with small chunks of jsoned object.


    Example:
        >>> dump({'keypair': 'a' * 260}, prefix='some_prefix_')
        {
            'some_prefix_0':
                '{"keypair": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
                # cut
                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
            'some_prefix_1': 'aaaaaaaaaaaaaaaaaa"}'
        }

    Args:
        obj (object): object to serialize. Should be json-serializable
        prefix (str): prefix to result dict keys
    Returns:
        dict: dict with small chunks of json object representation
    """
    meta = {}
    buf = moves.StringIO(json.dumps(obj))
    i = 0
    while True:
        data = buf.read(255)
        if not data:
            break
        meta['{}{}'.format(prefix, i)] = data
        i += 1
    return meta
Exemple #5
0
 def test_print_list_raises(self):
     out = moves.StringIO()
     self.assertRaisesRegexp(ValueError,
                             "Field labels list.*has different number "
                             "of elements than fields list",
                             cliutils.print_list, [self.TestObj()], ["x"],
                             field_labels=["x", "y"],
                             sortby_index=None,
                             out=out)
Exemple #6
0
 def test_do_not_include_message(self):
     out = moves.StringIO()
     opt = cfg.StrOpt('foo', help='foo option', mutable=False)
     gen = generator._OptFormatter(output_file=out)
     gen.format(opt, 'group1')
     result = out.getvalue()
     self.assertNotIn(
         'This option can be changed without restarting.',
         result,
     )
Exemple #7
0
 def _parse_source(self, src):
     """Parse a source file, extracting directives from comments."""
     f = moves.StringIO(src)
     defs_start = None
     closing_bracket_lines = set()
     whitespace_lines = set()
     for tok, _, start, _, line in tokenize.generate_tokens(f.readline):
         lineno, col = start
         if defs_start is None and _CLASS_OR_FUNC_RE.match(line):
             defs_start = lineno
         if _CLOSING_BRACKETS_RE.match(line):
             closing_bracket_lines.add(lineno)
         elif _WHITESPACE_RE.match(line):
             whitespace_lines.add(lineno)
         elif _DOCSTRING_RE.match(line):
             self._docstrings.add(lineno)
         else:
             if closing_bracket_lines:
                 self._adjust_type_comments(closing_bracket_lines,
                                            whitespace_lines)
             closing_bracket_lines.clear()
             whitespace_lines.clear()
         if tok == tokenize.COMMENT:
             matches = list(_DIRECTIVE_RE.finditer(line[col:]))
             is_nested = bool(matches) and matches[0].start(0) > 0
             for m in matches:
                 code = line[:col].strip()
                 tool, data = m.groups()
                 open_ended = not code
                 data = data.strip()
                 if tool == "type":
                     self._process_type(lineno, code, data, is_nested)
                 elif tool == "pytype":
                     try:
                         self._process_pytype(lineno, data, open_ended)
                     except _DirectiveError as e:
                         self._errorlog.invalid_directive(
                             self._filename, lineno, utils.message(e))
                 else:
                     pass  # ignore comments for other tools
     if closing_bracket_lines:
         self._adjust_type_comments(closing_bracket_lines, whitespace_lines)
     if defs_start is not None:
         disables = list(self._disables.items())
         # Add "# type: ignore" to the list of disables that we check.
         disables.append(("Type checking", self._ignore))
         for name, lineset in disables:
             lineno = lineset.get_disable_after(defs_start)
             if lineno is not None:
                 self._errorlog.late_directive(self._filename, lineno, name)
    def shell(self, argstr):
        orig = sys.stdout
        try:
            sys.stdout = moves.StringIO()
            _shell = shell.OpenStackCinderShell()
            _shell.main(argstr.split())
        except SystemExit:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            self.assertEqual(0, exc_value.code)
        finally:
            out = sys.stdout.getvalue()
            sys.stdout.close()
            sys.stdout = orig

        return out
Exemple #9
0
    def shell(self, argstr):
        orig = sys.stdout
        try:
            sys.stdout = moves.StringIO()
            _shell = automationclient.shell.StackopsAutomationShell()
            _shell.main(argstr.split())
        except SystemExit:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            self.assertEqual(exc_value.code, 0)
        finally:
            out = sys.stdout.getvalue()
            sys.stdout.close()
            sys.stdout = orig

        return out
Exemple #10
0
    def shell(self, argstr):
        orig = sys.stdout
        try:
            sys.stdout = moves.StringIO()
            _shell = shell.OpenStackManilaShell()
            _shell._discover_client = self.shell_discover_client
            _shell.main(argstr.split())
        except SystemExit:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            self.assertEqual(exc_value.code, 0)
        finally:
            out = sys.stdout.getvalue()
            sys.stdout.close()
            sys.stdout = orig

        return out
Exemple #11
0
 def test__gen_ansible_inventory_file(self, *args):
     nodes = [{
         "name": "name",
         "user": "******",
         "password": "******",
         "role": "role",
     }]
     d = tempfile.mkdtemp()
     self.addCleanup(self._delete_tmpdir, d)
     a = ansible_common.AnsibleCommon(nodes)
     a.gen_inventory_ini_dict()
     inv_context = a._gen_ansible_inventory_file(d)
     with inv_context:
         c = moves.StringIO()
         inv_context.write_func(c)
         self.assertIn("ansible_ssh_pass=PASS", c.getvalue())
Exemple #12
0
    def formatException(self, exc_info, record=None):
        """Format exception output with CONF.logging_exception_prefix."""
        if not record:
            return logging.Formatter.formatException(self, exc_info)
        else:
            stringbuffer = moves.StringIO()
            traceback.print_exception(exc_info[0], exc_info[1], exc_info[2], None, stringbuffer)
            lines = stringbuffer.getvalue().split('\n')
            stringbuffer.close()
            if CONF.logging_exception_prefix.find('%(asctime)') != -1:
                record.asctime = self.formatTime(record, self.datefmt)
            formatted_lines = []
            for line in lines:
                pl = CONF.logging_exception_prefix % record.__dict__
                fl = '%s%s' % (pl, line)
                formatted_lines.append(fl)

            return '\n'.join(formatted_lines)
Exemple #13
0
 def hashstring(self, string):
     """
     :return: the hash for a given string
     """
     fd = moves.StringIO(string)
     return self.hashfile(fd)
Exemple #14
0
    def _parse_source(self, src):
        """Parse a source file, extracting directives from comments."""
        f = moves.StringIO(src)
        defs_start = None
        open_type_comment_set = _TypeCommentSet.start(1)
        open_decorator = False
        last_function_definition = None
        open_variable_annotation = None
        for token in tokenize.generate_tokens(f.readline):
            tok = token.exact_type
            line = token.line
            lineno, col = token.start

            # Check for the first line with a top-level class or function definition.
            if defs_start is None and _CLASS_OR_FUNC_RE.match(line):
                defs_start = lineno

            # Process the token for decorators, function definitions, and comments.
            if tok == tokenize.AT:
                if _DECORATOR_RE.match(line):
                    open_decorator = True
            elif tok == tokenize.NAME:
                if open_decorator and token.string in ("class", "def"):
                    self.decorators.add(lineno)
                    open_decorator = False
                if token.string == "def":
                    last_function_definition = _FunctionDefinition.start(
                        lineno)
            elif tok == tokenize.COMMENT:
                self._process_comment(line, lineno, col, open_type_comment_set)
            elif tok == tokenize.LPAR:
                if last_function_definition:
                    last_function_definition.add_lpar(lineno)
            elif tok == tokenize.RPAR:
                if last_function_definition:
                    last_function_definition.add_rpar(lineno)
            elif tok in (tokenize.NEWLINE, tokenize.ENDMARKER):
                if open_type_comment_set.type_comments:
                    open_type_comment_set.end_line = lineno
                    self._type_comments.append(open_type_comment_set)
                open_type_comment_set = _TypeCommentSet.start(lineno + 1)

            # Process the token for variable annotations.
            if last_function_definition and last_function_definition.contains(
                    lineno):
                pass  # ignore function annotations
            elif not open_variable_annotation:
                open_variable_annotation = _VariableAnnotation.start(token)
            elif tok in (tokenize.NEWLINE, tokenize.SEMI):
                # NEWLINE indicates the end of a *logical* line of Python code, allowing
                # us to handle annotations split over multiple lines.
                annotation = open_variable_annotation.annotation
                if annotation and open_variable_annotation.closed:
                    self._variable_annotations[lineno] = annotation
                open_variable_annotation = None
            else:
                open_variable_annotation.add_token(token)

            # Record docstrings.
            if _DOCSTRING_RE.match(line):
                self._docstrings.add(lineno)

        if defs_start is not None:
            disables = list(self._disables.items())
            # Add "# type: ignore" to the list of disables that we check.
            disables.append(("Type checking", self._ignore))
            for name, lineset in disables:
                lineno = lineset.get_disable_after(defs_start)
                if lineno is not None:
                    self._errorlog.late_directive(self._filename, lineno, name)
Exemple #15
0
 def __enter__(self):
     sys.stderr = moves.StringIO()
     return sys.stderr
Exemple #16
0
 def __enter__(self):
     self.real_stdout = sys.stdout
     self.stringio = moves.StringIO()
     sys.stdout = self.stringio
     return self
 def _capture_stdout(self):
     self.useFixture(fixtures.MonkeyPatch('sys.stdout', moves.StringIO()))
     return sys.stdout
Exemple #18
0
 def get_image(self, name):
     if (name not in self._images or
        name not in self._image_data):
         raise Exception("Image not found - %s" % name)
     return moves.StringIO(self._image_data[name])
Exemple #19
0
 def _capture_stream(self, stream_name):
     self.useFixture(
         fixtures.MonkeyPatch("sys.%s" % stream_name, moves.StringIO()))
     return getattr(sys, stream_name)
Exemple #20
0
def _get_script_from_template(template_filename, **kwargs):
    template = _get_script(template_filename).read()
    return moves.StringIO(template.format(**kwargs))
Exemple #21
0
    def test_print_list(self):
        class TestObj(object):
            x = 1
            y = 2
            z = 3.142857142857143
            aOrB = 3  # mixed case field

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["x", "y"],
                            print_header=True,
                            print_border=True,
                            sortby_index=None,
                            out=out)
        self.assertEqual(
            "+---+---+\n"
            "| x | y |\n"
            "+---+---+\n"
            "| 1 | 2 |\n"
            "+---+---+",
            out.getvalue().strip())

        out = moves.StringIO()
        formatter = cliutils.pretty_float_formatter("z", 5)
        cliutils.print_list([TestObj()], ["z"],
                            print_header=True,
                            print_border=True,
                            sortby_index=None,
                            formatters={"z": formatter},
                            out=out)
        self.assertEqual(
            "+---------+\n"
            "| z       |\n"
            "+---------+\n"
            "| 3.14286 |\n"
            "+---------+",
            out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["x"],
                            print_header=True,
                            print_border=True,
                            out=out)
        self.assertEqual("+---+\n"
                         "| x |\n"
                         "+---+\n"
                         "| 1 |\n"
                         "+---+",
                         out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["x", "y"],
                            print_header=True,
                            print_border=True,
                            out=out)
        self.assertEqual(
            "+---+---+\n"
            "| x | y |\n"
            "+---+---+\n"
            "| 1 | 2 |\n"
            "+---+---+",
            out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["x"],
                            print_header=False,
                            print_border=False,
                            out=out)
        self.assertEqual("1", out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["x", "y"],
                            print_header=False,
                            print_border=False,
                            out=out)
        self.assertEqual("1 2", out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["x"],
                            print_header=True,
                            print_border=False,
                            out=out)
        self.assertEqual("x \n1", out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["x", "y"],
                            print_header=True,
                            print_border=False,
                            out=out)
        self.assertEqual("x y \n1 2", out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["x"],
                            print_header=False,
                            print_border=True,
                            out=out)
        self.assertEqual("+--+\n" "|1 |\n" "+--+", out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["x", "y"],
                            print_header=False,
                            print_border=True,
                            out=out)
        self.assertEqual("+--+--+\n"
                         "|1 |2 |\n"
                         "+--+--+",
                         out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["aOrB"],
                            mixed_case_fields=["aOrB"],
                            print_header=True,
                            print_border=True,
                            out=out)
        self.assertEqual(
            "+------+\n"
            "| aOrB |\n"
            "+------+\n"
            "| 3    |\n"
            "+------+",
            out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["aOrB"],
                            mixed_case_fields=["aOrB"],
                            print_header=False,
                            print_border=True,
                            out=out)
        self.assertEqual("+--+\n" "|3 |\n" "+--+", out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["aOrB"],
                            mixed_case_fields=["aOrB"],
                            print_header=True,
                            print_border=False,
                            out=out)
        self.assertEqual("aOrB \n" "3", out.getvalue().strip())

        out = moves.StringIO()
        cliutils.print_list([TestObj()], ["aOrB"],
                            mixed_case_fields=["aOrB"],
                            print_header=False,
                            print_border=False,
                            out=out)
        self.assertEqual("3", out.getvalue().strip())

        out = moves.StringIO()
        self.assertRaisesRegexp(ValueError,
                                "Field labels list.*has different number "
                                "of elements than fields list",
                                cliutils.print_list, [TestObj()], ["x"],
                                field_labels=["x", "y"],
                                sortby_index=None,
                                out=out)
Exemple #22
0
 def test_print_list(self, args, kwargs, expected):
     out = moves.StringIO()
     kwargs["out"] = out
     cliutils.print_list(*args, **kwargs)
     self.assertEqual(expected, out.getvalue().strip())