Beispiel #1
0
def main():
    """
    Main method.

    This method holds what you want to execute when
    the script is run on command line.
    """
    try:
        args = get_arguments()
    except argparse.ArgumentTypeError as exc:
        raise SystemExit(exc.message)
    setup_logging(args)
    try:
        stack = Stack(args.stack, args.naming, args.positioning)
    except (InvalidNaming, InvalidPositioning):
        print(fore.RED + style.BOLD + 'Invalid file provided as argument' +
              style.RESET)  # pylint: disable=superfluous-parens,no-member
        raise SystemExit(1)
    stack.validate()
    if stack.errors:
        for error in stack.errors:
            print(error)  # pylint: disable=superfluous-parens
        raise SystemExit(1)
    else:
        message = 'No naming convention or file positioning errors found!'
        print(fore.GREEN_3A + style.BOLD + message + style.RESET)  # pylint: disable=superfluous-parens,no-member
        raise SystemExit(0)
 def test_naming_errors(self):
     stack = Stack(self.stack_path, self.naming_file, self.positioning_file,
                   self.globals_file)
     stack.validate()
     assert len([
         error for error in stack.errors
         if isinstance(error, ResourceError)
     ]) == 4
 def test_global_positioning_skip(self):
     os.environ['SKIP_POSITIONING'] = "true"
     stack = Stack(self.stack_path, self.naming_file, self.positioning_file,
                   self.globals_file)
     stack.validate()
     for error in stack.errors:
         assert not isinstance(error, FilenameError)
     del os.environ['SKIP_POSITIONING']
 def test_deprecated_warnings(self):
     with warnings.catch_warnings(record=True) as warnings_:
         # Cause all warnings to always be triggered.
         warnings.simplefilter("always")
         self.stack = Stack(self.stack_path, self.naming_file,
                            self.positioning_file, self.globals_file)
         self.stack.validate()
         for wa in warnings_:
             print(wa.message)
         print(warnings_)
         assert len(warnings_) == 3
         for warning_ in warnings_:
             assert issubclass(warning_.category, PendingDeprecationWarning)
class TestLintingFeatures(unittest.TestCase):
    def setUp(self):
        """
        Test set up

        This is where you can setup things that you use throughout the tests. This method is called before every test.
        """
        current_dir = os.path.dirname(os.path.abspath(__file__))
        self.fixtures = os.path.join(current_dir, 'fixtures', 'linting')
        self.stack_path = os.path.join(self.fixtures, 'stack')
        self.naming_file = os.path.join(self.fixtures, 'naming.yaml')
        self.interpolated_naming_file = os.path.join(
            self.fixtures, 'interpolated_naming.yaml')
        self.count_interpolated_naming_file = os.path.join(
            self.fixtures, 'count_interpolated_naming.yaml')
        self.globals_file = os.path.join(self.stack_path, 'global.tfvars')
        self.broken_schema_naming_file = os.path.join(
            self.fixtures, 'broken_schema_naming.yaml')
        self.broken_yaml_naming_file = os.path.join(self.fixtures,
                                                    'broken_yaml_naming.yaml')
        self.positioning_file = os.path.join(self.fixtures, 'positioning.yaml')
        self.broken_schema_positioning_file = os.path.join(
            self.fixtures, 'broken_schema_positioning.yaml')
        self.broken_yaml_positioning_file = os.path.join(
            self.fixtures, 'broken_yaml_positioning.yaml')

    def test_regex_checking(self):
        self.assertFalse(is_valid_regex('['))

    def test_naming_file(self):
        self.assertRaises(InvalidNaming, Stack, self.stack_path, 'random/path',
                          self.positioning_file, self.globals_file)
        self.assertRaises(InvalidNaming, Stack, self.stack_path,
                          self.broken_schema_naming_file,
                          self.positioning_file, self.globals_file)
        self.assertRaises(InvalidNaming, Stack, self.stack_path,
                          self.broken_yaml_naming_file, self.positioning_file,
                          self.globals_file)

    def test_positioning_file(self):
        self.assertRaises(InvalidPositioning, Stack, self.stack_path,
                          self.naming_file, 'random/path', self.globals_file)
        self.assertRaises(InvalidPositioning, Stack, self.stack_path,
                          self.naming_file,
                          self.broken_schema_positioning_file,
                          self.globals_file)
        self.assertRaises(InvalidPositioning, Stack, self.stack_path,
                          self.naming_file, self.broken_yaml_positioning_file,
                          self.globals_file)
        stack = Stack(self.stack_path, self.naming_file, None,
                      self.globals_file)
        assert isinstance(stack, Stack)
        stack.validate()

    def test_global_positioning_skip(self):
        os.environ['SKIP_POSITIONING'] = "true"
        stack = Stack(self.stack_path, self.naming_file, self.positioning_file,
                      self.globals_file)
        stack.validate()
        for error in stack.errors:
            assert not isinstance(error, FilenameError)
        del os.environ['SKIP_POSITIONING']

    def test_positioning_errors(self):
        stack = Stack(self.stack_path, self.naming_file, self.positioning_file,
                      self.globals_file)
        stack.validate()
        assert len([
            error for error in stack.errors
            if isinstance(error, FilenameError)
        ]) == 2

    def test_naming_errors(self):
        stack = Stack(self.stack_path, self.naming_file, self.positioning_file,
                      self.globals_file)
        stack.validate()
        assert len([
            error for error in stack.errors
            if isinstance(error, ResourceError)
        ]) == 4

    def test_deprecated_warnings(self):
        with warnings.catch_warnings(record=True) as warnings_:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")
            self.stack = Stack(self.stack_path, self.naming_file,
                               self.positioning_file, self.globals_file)
            self.stack.validate()
            for wa in warnings_:
                print(wa.message)
            print(warnings_)
            assert len(warnings_) == 3
            for warning_ in warnings_:
                assert issubclass(warning_.category, PendingDeprecationWarning)

    def test_error_messages(self):
        stack = Stack(self.stack_path, self.naming_file, self.positioning_file,
                      self.globals_file)
        stack.validate()
        for error in stack.errors:
            assert 'not followed on file' in str(error)

    def test_variable_interpolation(self):
        stack = Stack(self.stack_path, self.interpolated_naming_file, None,
                      self.globals_file)
        stack.validate()
        for error in stack.errors:
            assert 'test_interpolated_value' in str(error)

    def test_count_interpolation(self):
        stack = Stack(self.stack_path, self.count_interpolated_naming_file,
                      None, self.globals_file)
        stack.validate()
        assert len(stack.errors) == 0

    def test_length_interpolation(self):
        stack = Stack(self.stack_path, self.count_interpolated_naming_file,
                      None, self.globals_file)
        stack.validate()
        assert len(stack.errors) == 0

    def tearDown(self):
        """
        Test tear down

        This is where you should tear down what you've setup in setUp before. This method is called after every test.
        """
        pass
 def test_length_interpolation(self):
     stack = Stack(self.stack_path, self.count_interpolated_naming_file,
                   None, self.globals_file)
     stack.validate()
     assert len(stack.errors) == 0
 def test_variable_interpolation(self):
     stack = Stack(self.stack_path, self.interpolated_naming_file, None,
                   self.globals_file)
     stack.validate()
     for error in stack.errors:
         assert 'test_interpolated_value' in str(error)
 def test_error_messages(self):
     stack = Stack(self.stack_path, self.naming_file, self.positioning_file,
                   self.globals_file)
     stack.validate()
     for error in stack.errors:
         assert 'not followed on file' in str(error)