Exemple #1
0
    def compare(self, expected, path=None, files_only=False, recursive=True):
        """
        Compare the expected contents with the actual contents of the temporary
        directory.

        :param expected: A sequence of strings containing the paths
                         expected in the directory. These paths should
                         be forward-slash separated and relative to
                         the root of the temporary directory.

        :param path: The path to use as the root for the comparison,
                     relative to the root of the temporary directory.
                     This can either be:

                     * A tuple of strings, making up the relative path.

                     * A forward-slash separated string.

                     If it is not provided, the root of the temporary
                     directory will be used.

        :param files_only: If specified, directories will be excluded from
                           the list of actual paths used in the comparison.

        :param recursive: If passed as ``False``, only the direct contents of
                          the directory specified by ``path`` will be included
                          in the actual contents used for comparison.
        """
        compare(expected,
                tuple(self.actual(path, recursive, files_only)),
                recursive=False)
Exemple #2
0
 def compare(self, expected='', stdout='', stderr=''):
     for prefix, _expected, captured in ((None, expected, self.captured),
                                         ('stdout', stdout,
                                          self.stdout.getvalue()),
                                         ('stderr', stderr,
                                          self.stderr.getvalue())):
         compare(_expected.strip(), actual=captured.strip(), prefix=prefix)
Exemple #3
0
    def check_dir(self,dir,*expected):
        """
        Compare the contents of the specified subdirectory of the
        temporary directory with the expected contents supplied.

        This method will only check the contents of the subdirectory
        specified and will not recursively check subdirectories.
        
        :param dir: The subdirectory to check, which can be:

                     * A tuple of strings, indicating that the
                       elements of the tuple should be used as directory
                       names to traverse from the root of the
                       temporary directory to find the directory to be
                       checked.

                     * A forward-slash separated string, indicating
                       the directory or subdirectory that should be
                       traversed to from the temporary directory and
                       checked.

        :param expected: A sequence of strings containing the names
                         expected in the directory.
        """
        compare(expected,tuple(self.actual(dir)))
Exemple #4
0
    def check_all(self,dir,*expected):
        """
        Recursively compare the contents of the specified directory
        with the expected contents supplied.

        :param dir: The directory to check, which can be:

                     * A tuple of strings, indicating that the
                       elements of the tuple should be used as directory
                       names to traverse from the root of the
                       temporary directory to find the directory to be
                       checked.

                     * A forward-slash separated string, indicating
                       the directory or subdirectory that should be
                       traversed to from the temporary directory and
                       checked.

                     * An empty string, indicating that the whole
                       temporary directory should be checked.

        :param expected: A sequence of strings containing the paths
                         expected in the directory. These paths should
                         be forward-slash separated and relative to
                         the root of the temporary directory.
        """
        compare(expected,tuple(self.actual(dir,recursive=True)))
Exemple #5
0
    def check_dir(self, dir, *expected):
        """
        .. deprecated:: 4.3.0

        Compare the contents of the specified subdirectory of the
        temporary directory with the expected contents supplied.

        This method will only check the contents of the subdirectory
        specified and will not recursively check subdirectories.

        :param dir: The subdirectory to check, which can be:

                     * A tuple of strings, indicating that the
                       elements of the tuple should be used as directory
                       names to traverse from the root of the
                       temporary directory to find the directory to be
                       checked.

                     * A forward-slash separated string, indicating
                       the directory or subdirectory that should be
                       traversed to from the temporary directory and
                       checked.

        :param expected: A sequence of strings containing the names
                         expected in the directory.
        """
        compare(expected, tuple(self.actual(dir)), recursive=False)
Exemple #6
0
    def check_all(self, dir, *expected):
        """
        .. deprecated:: 4.3.0

        Recursively compare the contents of the specified directory
        with the expected contents supplied.

        :param dir: The directory to check, which can be:

                     * A tuple of strings, indicating that the
                       elements of the tuple should be used as directory
                       names to traverse from the root of the
                       temporary directory to find the directory to be
                       checked.

                     * A forward-slash separated string, indicating
                       the directory or subdirectory that should be
                       traversed to from the temporary directory and
                       checked.

                     * An empty string, indicating that the whole
                       temporary directory should be checked.

        :param expected: A sequence of strings containing the paths
                         expected in the directory. These paths should
                         be forward-slash separated and relative to
                         the root of the temporary directory.
        """
        compare(expected,
                tuple(self.actual(dir, recursive=True)),
                recursive=False)
Exemple #7
0
    def compare(self, expected=u'', stdout=u'', stderr=u''):
        """
        Compare the captured output to that expected. If the output is
        not the same, an :class:`AssertionError` will be raised.

        :param expected: A string containing the expected combined output
                         of ``stdout`` and ``stderr``.

        :param stdout: A string containing the expected output to ``stdout``.

        :param stderr: A string containing the expected output to ``stderr``.
        """
        expected_mapping = {}
        actual_mapping = {}
        for prefix, _expected, captured in (
                ('captured', expected, self.captured),
                ('stdout', stdout, self._read(self.stdout)),
                ('stderr', stderr, self._read(self.stderr)),
        ):
            if self.fd and isinstance(_expected, Unicode):
                _expected = _expected.encode()
            if self.strip_whitespace:
                _expected = _expected.strip()
                captured = captured.strip()
            if _expected != captured:
                expected_mapping[prefix] = _expected
                actual_mapping[prefix] = captured
        if len(expected_mapping) == 1:
            compare(expected=tuple(expected_mapping.values())[0],
                    actual=tuple(actual_mapping.values())[0])
        compare(expected=expected_mapping, actual=actual_mapping)
Exemple #8
0
    def compare(self, expected, path=None, files_only=False, recursive=True):
        """
        Compare the expected contents with the actual contents of the temporary
        directory.

        :param expected: A sequence of strings containing the paths
                         expected in the directory. These paths should
                         be forward-slash separated and relative to
                         the root of the temporary directory.

        :param path: The path to use as the root for the comparison,
                     relative to the root of the temporary directory.
                     This can either be:

                     * A tuple of strings, making up the relative path.

                     * A forward-slash separated string.

                     If it is not provided, the root of the temporary
                     directory will be used.

        :param files_only: If specified, directories will be excluded from
                           the list of actual paths used in the comparison.

        :param recursive: If passed as ``False``, only the direct contents of
                          the directory specified by ``path`` will be included
                          in the actual contents used for comparison.
        """
        compare(expected,
                actual=tuple(self.actual(path, recursive, files_only)),
                recursive=False)
    def compare(self, expected):
        """
        Compare the captured output to that expected. If the output is
        not the same, an :class:`AssertionError` will be raised.

        :param expected: A string containing the expected output.
        """
        compare(expected.strip(), self.captured.strip())
Exemple #10
0
    def compare(self,expected):
        """
        Compare the captured output to that expected. If the output is
        not the same, an :class:`AssertionError` will be raised.

        :param expected: A string containing the expected output.
        """
        compare(expected.strip(),self.captured.strip())
Exemple #11
0
 def test_write_board_to_text_file_padding(self):
     board = [
         [0, 1, 2],
         [3, 4],
         [6, 7, 8],
         [9],
     ]
     with TempDirectory() as d:
         hg.write_board_to_text_file(board, os.path.join(d.path, 'test.txt'))
         compare(d.read('test.txt', 'utf-8'), '0 3 6 9\n1 4 7 0\n2 0 8 0\n')
Exemple #12
0
    def check(self,*expected):
        """
        Compare the contents of the temporary directory with the
        expected contents supplied.

        This method only checks the root of the temporary directory.

        :param expected: A sequence of strings containing the names
                         expected in the directory.
        """
        compare(expected,tuple(self.actual()))
Exemple #13
0
    def check(self, *expected):
        """
        Compare the contents of the temporary directory with the
        expected contents supplied.

        This method only checks the root of the temporary directory.

        :param expected: A sequence of strings containing the names
                         expected in the directory.
        """
        compare(expected, tuple(self.actual()))
Exemple #14
0
    def compare(self,
                expected,
                path=None,
                files_only=False,
                recursive=True,
                followlinks=False):
        """
        Compare the expected contents with the actual contents of the temporary
        directory. An :class:`AssertionError` will be raised if they are not the
        same.

        :param expected: A sequence of strings containing the paths
                         expected in the directory. These paths should
                         be forward-slash separated and relative to
                         the root of the temporary directory.

        :param path: The path to use as the root for the comparison,
                     relative to the root of the temporary directory.
                     This can either be:

                     * A tuple of strings, making up the relative path.

                     * A forward-slash separated string.

                     If it is not provided, the root of the temporary
                     directory will be used.

        :param files_only: If specified, directories will be excluded from
                           the list of actual paths used in the comparison.

        :param recursive: If passed as ``False``, only the direct contents of
                          the directory specified by ``path`` will be included
                          in the actual contents used for comparison.

        :param followlinks: If passed as ``True``, symlinks and hard links
                            will be followed when recursively building up
                            the actual list of directory contents.
        """

        __tracebackhide__ = True

        compare(expected=sorted(expected),
                actual=tuple(
                    self.actual(path, recursive, files_only, followlinks)),
                recursive=False)
    def compare(self, expected='', stdout='', stderr=''):
        """
        Compare the captured output to that expected. If the output is
        not the same, an :class:`AssertionError` will be raised.

        :param expected: A string containing the expected combined output
                         of ``stdout`` and ``stderr``.

        :param stdout: A string containing the expected output to ``stdout``.

        :param stderr: A string containing the expected output to ``stderr``.
        """
        for prefix, _expected, captured in (
                (None, expected, self.captured),
                ('stdout', stdout, self.stdout.getvalue()),
                ('stderr', stderr, self.stderr.getvalue()),
        ):
            compare(_expected.strip(), actual=captured.strip(), prefix=prefix)
Exemple #16
0
    def compare(self, expected='', stdout='', stderr=''):
        """
        Compare the captured output to that expected. If the output is
        not the same, an :class:`AssertionError` will be raised.

        :param expected: A string containing the expected combined output
                         of ``stdout`` and ``stderr``.

        :param stdout: A string containing the expected output to ``stdout``.

        :param stderr: A string containing the expected output to ``stderr``.
        """
        for prefix, _expected, captured in (
            (None, expected, self.captured),
            ('stdout', stdout, self.stdout.getvalue()),
            ('stderr', stderr, self.stderr.getvalue()),
        ):
            compare(_expected.strip(), actual=captured.strip(), prefix=prefix)
    def compare(self, expected=u'', stdout=u'', stderr=u''):
        """
        Compare the captured output to that expected. If the output is
        not the same, an :class:`AssertionError` will be raised.

        :param expected: A string containing the expected combined output
                         of ``stdout`` and ``stderr``.

        :param stdout: A string containing the expected output to ``stdout``.

        :param stderr: A string containing the expected output to ``stderr``.
        """
        for prefix, _expected, captured in (
                (None, expected, self.captured),
                ('stdout', stdout, self._read(self.stdout)),
                ('stderr', stderr, self._read(self.stderr)),
        ):
            if self.fd and isinstance(_expected, Unicode):
                _expected = _expected.encode()
            compare(_expected.strip(), actual=captured.strip(), prefix=prefix)
    def check(self, *expected):
        """
        This will compare the captured entries with the expected
        entries provided and raise an :class:`AssertionError` if they
        do not match.

        :param expected: A sequence of 3-tuples containing the
                         expected log entries. Each tuple should be of
                         the form (logger_name, string_level, message)
        """
        return compare(expected, tuple(self.actual()))
Exemple #19
0
    def check(self, *expected):
        """
        This will compare the captured entries with the expected
        entries provided and raise an :class:`AssertionError` if they
        do not match.

        :param expected: A sequence of 3-tuples containing the
                         expected log entries. Each tuple should be of
                         the form (logger_name, string_level, message)
        """
        return compare(expected, tuple(self.actual()), recursive=False)
Exemple #20
0
    def compare(self, expected, path=None, files_only=False, recursive=True,
                followlinks=False):
        """
        Compare the expected contents with the actual contents of the temporary
        directory. An :class:`AssertionError` will be raised if they are not the
        same.

        :param expected: A sequence of strings containing the paths
                         expected in the directory. These paths should
                         be forward-slash separated and relative to
                         the root of the temporary directory.

        :param path: The path to use as the root for the comparison,
                     relative to the root of the temporary directory.
                     This can either be:

                     * A tuple of strings, making up the relative path.

                     * A forward-slash separated string.

                     If it is not provided, the root of the temporary
                     directory will be used.

        :param files_only: If specified, directories will be excluded from
                           the list of actual paths used in the comparison.

        :param recursive: If passed as ``False``, only the direct contents of
                          the directory specified by ``path`` will be included
                          in the actual contents used for comparison.

        :param followlinks: If passed as ``True``, symlinks and hard links
                            will be followed when recursively building up
                            the actual list of directory contents.
        """
        compare(expected=sorted(expected),
                actual=tuple(self.actual(
                    path, recursive, files_only, followlinks
                )),
                recursive=False)
Exemple #21
0
    def check(self, *expected):
        """
        This will compare the captured entries with the expected
        entries provided and raise an :class:`AssertionError` if they
        do not match.

        :param expected:

          A sequence of values returns as specified in the ``attributes``
          passed to the constructor.
        """
        return compare(expected,
                       actual=tuple(self.actual()),
                       recursive=self.recursive_check)
Exemple #22
0
    def check(self, *expected):
        """
        This will compare the captured entries with the expected
        entries provided and raise an :class:`AssertionError` if they
        do not match.

        :param expected:

          A sequence of entries of the structure specified by the ``attributes``
          passed to the constructor.
        """
        return compare(
            expected,
            actual=self.actual(),
            recursive=self.recursive_check
            )
Exemple #23
0
    def check(self, *expected):
        """
        This will compare the captured entries with the expected
        entries provided and raise an :class:`AssertionError` if they
        do not match.

        :param expected:

          A sequence of entries of the structure specified by the ``attributes``
          passed to the constructor.
        """
        result = compare(expected,
                         actual=self.actual(),
                         recursive=self.recursive_check)
        self.mark_all_checked()
        return result
    def test_init_cidr_block_wildcards(self,
                                       mock_get_stack_name_by_match,
                                       mock_get_stack_vpc_id,
                                       mock_get_vpc_cidr_blocks,
                                       mock_get_vpc_route_table_ids):
        """
        TestVPC::test_init_cidr_block_wildcards: Test that we generate the correct config when we have a wildcard on the cidr blocks
        """
        mock_get_stack_name_by_match.return_value = [
            {"StackName": "peer_stack_1-abc"}
        ]
        mock_get_stack_vpc_id.side_effect = [
            "vpc_123",
            "peervpc_xyz"
        ]
        mock_get_vpc_cidr_blocks.side_effect = [
            ["1.0.0.0/8"],  # First Peer VPC cidr block
            ["2.0.0.0/8"],  # Self VPC cidr block
        ]
        mock_get_vpc_route_table_ids.side_effect = [
            ["rt_def"],
            ["rt_abc"],
            ["rt_456"],
            ["rt_123"],
            None
        ]
        # Peer all source/target cidr blocks to specific tables
        config = {
            "vpc": {
                "peering": {
                    "peer_stack_1": {
                        "source_routes": {
                            "rt_abc": {
                                "cidr_blocks": ["1.2.3.4/16", "5.6.7.8/16"],
                            },

                            "rt_def": {
                                "cidr_blocks": "*"
                            }
                        },
                        "target_routes": {
                            "rt_123": {
                                "cidr_blocks": ["2.2.3.4/16"],
                            },
                            "rt_456": {
                                "cidr_blocks": "*"
                            }
                        }
                    }
                }
            }
        }

        stack_name = "test_stack"

        test_vpc = vpc.VPC(config, stack_name)

        expected_result = {
            "peer_stack_1":
            {
                "source_routes": {
                    "rt_abc": {
                        "cidr_blocks": ["1.2.3.4/16", "5.6.7.8/16"],
                        "route_table_id": "rt_abc"
                    },
                    "rt_def": {
                        "cidr_blocks": ["1.0.0.0/8"],
                        "route_table_id": "rt_def"
                    }
                },
                "target_routes": {
                    "rt_123": {
                        "cidr_blocks": ["2.2.3.4/16"],
                        "route_table_id": "rt_123"
                    },
                    "rt_456": {
                        "cidr_blocks": ["2.0.0.0/8"],
                        "route_table_id": "rt_456"
                    }
                },
                "vpc_id": "peervpc_xyz",
                "stack_name": "peer_stack_1-abc"
            }
        }
        actual_result = test_vpc.peering_config

        self.assertDictEqual(expected_result,
                             actual_result,
                             "TestVPC::test_init_stack_wildcard: "
                             "TODO: dicts not equal %s"
                             % (compare(expected_result, actual_result)))
Exemple #25
0
    def check_present(self, *expected, **kw):
        """
        This will check if the captured entries contain all of the expected
        entries provided and raise an :class:`AssertionError` if not.
        This will ignore entries that have been captured but that do not
        match those in ``expected``.

        :param expected:

          A sequence of entries of the structure specified by the ``attributes``
          passed to the constructor.

        :param order_matters:

          A keyword-only parameter that controls whether the order of the
          captured entries is required to match those of the expected entries.
          Defaults to ``True``.
        """
        order_matters = kw.pop('order_matters', True)
        assert not kw, 'order_matters is the only keyword parameter'
        actual = self.actual()
        if order_matters:
            matched_indices = [0]
            matched = []
            for entry in expected:
                try:
                    index = actual.index(entry, matched_indices[-1])
                except ValueError:
                    if len(matched_indices) > 1:
                        matched_indices.pop()
                        matched.pop()
                    break
                else:
                    matched_indices.append(index + 1)
                    matched.append(entry)
            else:
                return

            compare(expected,
                    actual=matched + actual[matched_indices[-1]:],
                    recursive=self.recursive_check)
        else:
            expected = list(expected)
            matched = []
            unmatched = []
            for entry in actual:
                try:
                    index = expected.index(entry)
                except ValueError:
                    unmatched.append(entry)
                else:
                    matched.append(expected.pop(index))
                if not expected:
                    break
            if expected:
                raise AssertionError(
                    ('entries not as expected:\n\n'
                     'expected and found:\n%s\n\n'
                     'expected but not found:\n%s\n\n'
                     'other entries:\n%s') %
                    (pformat(matched), pformat(expected), pformat(unmatched)))
Exemple #26
0
 def check_dir(self, dir, *expected):
     compare(expected, tuple(self.actual(dir)), recursive=False)
Exemple #27
0
 def check(self, *expected):
     compare(expected, tuple(self.actual()), recursive=False)
Exemple #28
0
    def test_init_source_route_table_wildcard(self,
                                              mock_get_stack_name_by_match,
                                              mock_get_stack_vpc_id,
                                              mock_get_vpc_cidr_blocks,
                                              mock_get_vpc_route_table_ids):
        """
        TestVPC::test_init_source_route_table_wildcard: Test that we generate the correct config when we have a wildcard on the source routes
        """
        # Peer all source route tables to the full target stacks cidr block
        config = {
            "vpc": {
                "peering": {
                    "peer_stack_1": {
                        "source_routes": "*",
                        "target_routes": {
                            "rt_456": {
                                "cidr_blocks": ["66.66.66.66/24"]
                            }
                        }
                    }
                }
            }
        }

        mock_get_stack_name_by_match.return_value = [{
            "StackName":
            "peer_stack_1-abc"
        }]
        mock_get_stack_vpc_id.side_effect = ["vpc_123", "peervpc_xyz"]
        mock_get_vpc_cidr_blocks.side_effect = [
            ["1.2.3.4/8"],  # Self VPC cidr block
            ["10.11.12.13/24"]  # Peer VPC cidr block
        ]
        mock_get_vpc_route_table_ids.side_effect = [
            ["rt_abc", "rt_def"],  # All self vpc route tables
            ["rt_456"],  # Peering vpc route tables matched to rt_456
            ["rt_abc", "rt_def"],  # All self vpc route tables
            ["rt_456"],  # Peering vpc route tables matched to rt_456
            None
        ]

        stack_name = "test_stack"

        test_vpc = vpc.VPC(config, stack_name)

        expected_result = {
            "peer_stack_1": {
                "source_routes": {
                    "rt_abc": {
                        "cidr_blocks": ["1.2.3.4/8"],
                        "route_table_id": "rt_abc"
                    },
                    "rt_def": {
                        "cidr_blocks": ["1.2.3.4/8"],
                        "route_table_id": "rt_def"
                    }
                },
                "target_routes": {
                    "rt_456": {
                        "cidr_blocks": ["66.66.66.66/24"],
                        "route_table_id": "rt_456"
                    }
                },
                "vpc_id": "peervpc_xyz",
                "stack_name": "peer_stack_1-abc"
            }
        }
        actual_result = test_vpc.peering_config

        self.assertDictEqual(
            expected_result, actual_result,
            "TestVPC::test_init_source_route_table_wildcard: "
            "TODO: dicts not equal %s" %
            (compare(expected_result, actual_result)))
Exemple #29
0
    def test_init_stack_wildcard(self, mock_get_stack_name_by_match,
                                 mock_get_stack_vpc_id,
                                 mock_get_vpc_cidr_blocks,
                                 mock_get_vpc_route_table_ids):
        """
        TestVPC::test_init_stack_wildcard: Test that we generate the correct config when we have a wildcard on the entire stack
        """
        # Peer the fill stack cidr block between all routes
        config = {"vpc": {"peering": {"peer_stack_1": "*"}}}

        mock_get_stack_name_by_match.return_value = [{
            "StackName":
            "peer_stack_1-abc"
        }]
        mock_get_stack_vpc_id.side_effect = ["vpc_123", "peervpc_xyz"]
        mock_get_vpc_cidr_blocks.side_effect = [
            ["1.2.3.4/8"],  # Self VPC cidr block
            ["10.11.12.13/24"]  # Peer VPC cidr block
        ]
        mock_get_vpc_route_table_ids.side_effect = [
            ["rt_abc", "rt_def"],  # All self vpc route tables
            ["rt_123", "rt_456", "rt_789"],  # All peering vpc route tables
            ["rt_abc", "rt_def"],  # All self vpc route tables
            ["rt_123", "rt_456", "rt_789"],  # All peering vpc route tables
            None
        ]

        stack_name = "test_stack"

        test_vpc = vpc.VPC(config, stack_name)

        expected_result = {
            "peer_stack_1": {
                "source_routes": {
                    "rt_abc": {
                        "cidr_blocks": ["1.2.3.4/8"],
                        "route_table_id": "rt_abc"
                    },
                    "rt_def": {
                        "cidr_blocks": ["1.2.3.4/8"],
                        "route_table_id": "rt_def"
                    }
                },
                "target_routes": {
                    "rt_123": {
                        "cidr_blocks": ["10.11.12.13/24"],
                        "route_table_id": "rt_123"
                    },
                    "rt_456": {
                        "cidr_blocks": ["10.11.12.13/24"],
                        "route_table_id": "rt_456"
                    },
                    "rt_789": {
                        "cidr_blocks": ["10.11.12.13/24"],
                        "route_table_id": "rt_789"
                    }
                },
                "vpc_id": "peervpc_xyz",
                "stack_name": "peer_stack_1-abc"
            }
        }
        actual_result = test_vpc.peering_config

        # Test our calling counts
        mock_get_stack_name_by_match.assert_called_with('peer_stack_1',
                                                        min_results=1,
                                                        max_results=1)
        mock_get_stack_vpc_id.assert_called_with('peer_stack_1-abc')
        # Getting the cidr block for self as its wildcarded
        mock_get_vpc_cidr_blocks.assert_called_with('vpc_123')
        # Getting the route tables ids for one route on the peer only
        mock_get_vpc_route_table_ids.assert_called_with('peervpc_xyz')
        self.assertDictEqual(
            expected_result, actual_result,
            "TestVPC::test_init_stack_wildcard: "
            "TODO: dicts not equal %s" %
            (compare(expected_result, actual_result)))
    def test_init_target_route_table_wildcard(self,
                                              mock_get_stack_name_by_match,
                                              mock_get_stack_vpc_id,
                                              mock_get_vpc_cidr_blocks,
                                              mock_get_vpc_route_table_ids):
        """
         TestVPC::test_init_target_route_table_wildcard: Test that we generate the correct config when we have a wildcard on the target routes
        """
        # Peer all target route tables to the full source stacks cidr block
        config = {
            "vpc": {
                "peering": {
                    "peer_stack_1": {
                        "source_routes": {
                            "rt_def": {
                                "cidr_blocks": ["66.66.66.66/24"]
                            }
                        },
                        "target_routes": "*"
                    }
                }
            }
        }

        mock_get_stack_name_by_match.return_value = [
            {"StackName": "peer_stack_1-abc"}
        ]
        mock_get_stack_vpc_id.side_effect = [
            "vpc_123",
            "peervpc_xyz"
        ]
        mock_get_vpc_cidr_blocks.side_effect = [
            ["1.2.3.4/8"],  # Self VPC cidr block
        ]
        mock_get_vpc_route_table_ids.side_effect = [
            ["rt_def"],  # Self vpc route tables matched to rt_def
            ["rt_123", "rt_456", "rt_789"],  # All peering vpc route tables
            None
        ]

        stack_name = "test_stack"

        test_vpc = vpc.VPC(config, stack_name)

        expected_result = {
            "peer_stack_1":
            {
                "source_routes": {
                    "rt_def": {
                        "cidr_blocks": ["66.66.66.66/24"],
                        "route_table_id": "rt_def"
                    }
                },
                "target_routes": {
                    "rt_123": {
                        "cidr_blocks": ["1.2.3.4/8"],
                        "route_table_id": "rt_123"
                    },
                    "rt_456": {
                        "cidr_blocks": ["1.2.3.4/8"],
                        "route_table_id": "rt_456"
                    },
                    "rt_789": {
                        "cidr_blocks": ["1.2.3.4/8"],
                        "route_table_id": "rt_789"
                    }
                },
                "vpc_id": "peervpc_xyz",
                "stack_name": "peer_stack_1-abc"
            }
        }
        actual_result = test_vpc.peering_config

        self.assertDictEqual(expected_result,
                             actual_result,
                             "TestVPC::test_init_target_route_table_wildcard: "
                             "TODO: dicts not equal %s"
                             % (compare(expected_result, actual_result)))
    def test_init_stack_wildcard(self,
                                 mock_get_stack_name_by_match,
                                 mock_get_stack_vpc_id,
                                 mock_get_vpc_cidr_blocks,
                                 mock_get_vpc_route_table_ids):
        """
        TestVPC::test_init_stack_wildcard: Test that we generate the correct config when we have a wildcard on the entire stack
        """
        # Peer the fill stack cidr block between all routes
        config = {
            "vpc": {
                "peering": {
                    "peer_stack_1": "*"
                }
            }
        }

        mock_get_stack_name_by_match.return_value = [
            {"StackName": "peer_stack_1-abc"}
        ]
        mock_get_stack_vpc_id.side_effect = [
            "vpc_123",
            "peervpc_xyz"
        ]
        mock_get_vpc_cidr_blocks.side_effect = [
            ["1.2.3.4/8"],  # Self VPC cidr block
            ["10.11.12.13/24"]  # Peer VPC cidr block
        ]
        mock_get_vpc_route_table_ids.side_effect = [
            ["rt_abc", "rt_def"],  # All self vpc route tables
            ["rt_123", "rt_456", "rt_789"],  # All peering vpc route tables
            ["rt_abc", "rt_def"],  # All self vpc route tables
            ["rt_123", "rt_456", "rt_789"],  # All peering vpc route tables
            None
        ]

        stack_name = "test_stack"

        test_vpc = vpc.VPC(config, stack_name)

        expected_result = {
            "peer_stack_1":
            {
                "source_routes": {
                    "rt_abc": {
                        "cidr_blocks": ["1.2.3.4/8"],
                        "route_table_id": "rt_abc"
                    },
                    "rt_def": {
                        "cidr_blocks": ["1.2.3.4/8"],
                        "route_table_id": "rt_def"
                    }
                },
                "target_routes": {
                    "rt_123": {
                        "cidr_blocks": ["10.11.12.13/24"],
                        "route_table_id": "rt_123"
                    },
                    "rt_456": {
                        "cidr_blocks": ["10.11.12.13/24"],
                        "route_table_id": "rt_456"
                    },
                    "rt_789": {
                        "cidr_blocks": ["10.11.12.13/24"],
                        "route_table_id": "rt_789"
                    }
                },
                "vpc_id": "peervpc_xyz",
                "stack_name": "peer_stack_1-abc"
            }
        }
        actual_result = test_vpc.peering_config

        # Test our calling counts
        mock_get_stack_name_by_match.assert_called_with('peer_stack_1', min_results=1, max_results=1)
        mock_get_stack_vpc_id.assert_called_with('peer_stack_1-abc')
        # Getting the cidr block for self as its wildcarded
        mock_get_vpc_cidr_blocks.assert_called_with('vpc_123')
        # Getting the route tables ids for one route on the peer only
        mock_get_vpc_route_table_ids.assert_called_with('peervpc_xyz')
        self.assertDictEqual(expected_result,
                             actual_result,
                             "TestVPC::test_init_stack_wildcard: "
                             "TODO: dicts not equal %s"
                             % (compare(expected_result, actual_result)))
Exemple #32
0
    def check_present(self, *expected, **kw):
        """
        This will check if the captured entries contain all of the expected
        entries provided and raise an :class:`AssertionError` if not.
        This will ignore entries that have been captured but that do not
        match those in ``expected``.

        :param expected:

          A sequence of entries of the structure specified by the ``attributes``
          passed to the constructor.

        :param order_matters:

          A keyword-only parameter that controls whether the order of the
          captured entries is required to match those of the expected entries.
          Defaults to ``True``.
        """
        order_matters = kw.pop('order_matters', True)
        assert not kw, 'order_matters is the only keyword parameter'
        actual = self.actual()
        if order_matters:
            matched_indices = [0]
            matched = []
            for entry in expected:
                try:
                    index = actual.index(entry, matched_indices[-1])
                except ValueError:
                    if len(matched_indices) > 1:
                        matched_indices.pop()
                        matched.pop()
                    break
                else:
                    matched_indices.append(index+1)
                    matched.append(entry)
            else:
                return

            compare(expected,
                    actual=matched+actual[matched_indices[-1]:],
                    recursive=self.recursive_check)
        else:
            expected = list(expected)
            matched = []
            unmatched = []
            for entry in actual:
                try:
                    index = expected.index(entry)
                except ValueError:
                    unmatched.append(entry)
                else:
                    matched.append(expected.pop(index))
                if not expected:
                    break
            if expected:
                raise AssertionError((
                    'entries not as expected:\n\n'
                    'expected and found:\n%s\n\n'
                    'expected but not found:\n%s\n\n'
                    'other entries:\n%s'
                ) % (pformat(matched), pformat(expected), pformat(unmatched)))
Exemple #33
0
    def test_init_cidr_block_wildcards(self, mock_get_stack_name_by_match,
                                       mock_get_stack_vpc_id,
                                       mock_get_vpc_cidr_blocks,
                                       mock_get_vpc_route_table_ids):
        """
        TestVPC::test_init_cidr_block_wildcards: Test that we generate the correct config when we have a wildcard on the cidr blocks
        """
        mock_get_stack_name_by_match.return_value = [{
            "StackName":
            "peer_stack_1-abc"
        }]
        mock_get_stack_vpc_id.side_effect = ["vpc_123", "peervpc_xyz"]
        mock_get_vpc_cidr_blocks.side_effect = [
            ["1.0.0.0/8"],  # First Peer VPC cidr block
            ["2.0.0.0/8"],  # Self VPC cidr block
        ]
        mock_get_vpc_route_table_ids.side_effect = [["rt_def"], ["rt_abc"],
                                                    ["rt_456"], ["rt_123"],
                                                    None]
        # Peer all source/target cidr blocks to specific tables
        config = {
            "vpc": {
                "peering": {
                    "peer_stack_1": {
                        "source_routes": {
                            "rt_abc": {
                                "cidr_blocks": ["1.2.3.4/16", "5.6.7.8/16"],
                            },
                            "rt_def": {
                                "cidr_blocks": "*"
                            }
                        },
                        "target_routes": {
                            "rt_123": {
                                "cidr_blocks": ["2.2.3.4/16"],
                            },
                            "rt_456": {
                                "cidr_blocks": "*"
                            }
                        }
                    }
                }
            }
        }

        stack_name = "test_stack"

        test_vpc = vpc.VPC(config, stack_name)

        expected_result = {
            "peer_stack_1": {
                "source_routes": {
                    "rt_abc": {
                        "cidr_blocks": ["1.2.3.4/16", "5.6.7.8/16"],
                        "route_table_id": "rt_abc"
                    },
                    "rt_def": {
                        "cidr_blocks": ["1.0.0.0/8"],
                        "route_table_id": "rt_def"
                    }
                },
                "target_routes": {
                    "rt_123": {
                        "cidr_blocks": ["2.2.3.4/16"],
                        "route_table_id": "rt_123"
                    },
                    "rt_456": {
                        "cidr_blocks": ["2.0.0.0/8"],
                        "route_table_id": "rt_456"
                    }
                },
                "vpc_id": "peervpc_xyz",
                "stack_name": "peer_stack_1-abc"
            }
        }
        actual_result = test_vpc.peering_config

        self.assertDictEqual(
            expected_result, actual_result,
            "TestVPC::test_init_stack_wildcard: "
            "TODO: dicts not equal %s" %
            (compare(expected_result, actual_result)))
Exemple #34
0
 def check_all(self, dir, *expected):
     compare(expected, tuple(self.actual(dir, recursive=True)), recursive=False)
Exemple #35
0
 def compare(self, expected, path = None, files_only = False, recursive = True, followlinks = False):
     compare(expected, actual=tuple(self.actual(path, recursive, files_only, followlinks)), recursive=False)
Exemple #36
0
 def check(self, *expected):
     return compare(expected, actual=tuple(self.actual()), recursive=False)