Beispiel #1
0
 def test_dump_as_file(self):
     parsed = load(open(util.get_data_filename('nginx.conf')))
     parsed[-1][-1].append([['server'],
                            [['listen', '443 ssl'],
                             ['server_name', 'localhost'],
                             ['ssl_certificate', 'cert.pem'],
                             ['ssl_certificate_key', 'cert.key'],
                             ['ssl_session_cache', 'shared:SSL:1m'],
                             ['ssl_session_timeout', '5m'],
                             ['ssl_ciphers', 'HIGH:!aNULL:!MD5'],
                             [['location', '/'],
                              [['root', 'html'],
                               ['index', 'index.html index.htm']]]]])
     _file = open(util.get_data_filename('nginx.new.conf'), 'w')
     dump(parsed, _file)
     _file.close()
     parsed_new = load(open(util.get_data_filename('nginx.new.conf')))
     self.assertEquals(parsed, parsed_new)
 def test_dump_as_file(self):
     parsed = load(open(util.get_data_filename('nginx.conf')))
     parsed[-1][-1].append([['server'],
                            [['listen', '443 ssl'],
                             ['server_name', 'localhost'],
                             ['ssl_certificate', 'cert.pem'],
                             ['ssl_certificate_key', 'cert.key'],
                             ['ssl_session_cache', 'shared:SSL:1m'],
                             ['ssl_session_timeout', '5m'],
                             ['ssl_ciphers', 'HIGH:!aNULL:!MD5'],
                             [['location', '/'],
                              [['root', 'html'],
                               ['index', 'index.html index.htm']]]]])
     _file = open(util.get_data_filename('nginx.new.conf'), 'w')
     dump(parsed, _file)
     _file.close()
     parsed_new = load(open(util.get_data_filename('nginx.new.conf')))
     self.assertEquals(parsed, parsed_new)
    def test_comments(self):
        with open(util.get_data_filename('minimalistic_comments.conf')) as handle:
            parsed = load(handle)

        with open(util.get_data_filename('minimalistic_comments.new.conf'), 'w') as handle:
            dump(parsed, handle)

        with open(util.get_data_filename('minimalistic_comments.new.conf')) as handle:
            parsed_new = load(handle)

        self.assertEquals(parsed, parsed_new)

        self.assertEqual(parsed_new, [
            ['#', " Use bar.conf when it's a full moon!"],
            ['include', 'foo.conf'],
            ['#', ' Kilroy was here'],
            ['check_status', None],
            [['server'],
             [['#', ''],
              ['#', " Don't forget to open up your firewall!"],
              ['#', ''],
              ['listen', '1234'],
              ['#', ' listen 80;']]],
        ])
Beispiel #4
0
    def test_comments(self):
        with open(util.get_data_filename(
                'minimalistic_comments.conf')) as handle:
            parsed = load(handle)

        with open(util.get_data_filename('minimalistic_comments.new.conf'),
                  'w') as handle:
            dump(parsed, handle)

        with open(util.get_data_filename(
                'minimalistic_comments.new.conf')) as handle:
            parsed_new = load(handle)

        self.assertEquals(parsed, parsed_new)

        self.assertEqual(parsed_new, [
            ['#', " Use bar.conf when it's a full moon!"],
            ['include', 'foo.conf'],
            ['#', ' Kilroy was here'],
            ['check_status', None],
            [['server'],
             [['#', ''], ['#', " Don't forget to open up your firewall!"],
              ['#', ''], ['listen', '1234'], ['#', ' listen 80;']]],
        ])
 def test_parse_from_file2(self):
     parsed = load(open(util.get_data_filename('edge_cases.conf')))
     self.assertEqual(
         parsed,
         [[['server'], [['server_name', 'simple']]],
          [['server'],
           [['server_name', 'with.if'],
            [['location', '~', '^/services/.+$'],
             [[['if', '($request_filename ~* \\.(ttf|woff)$)'],
               [['add_header', 'Access-Control-Allow-Origin "*"']]]]]]],
          [['server'],
           [['server_name', 'with.complicated.headers'],
            [['location', '~*', '\\.(?:gif|jpe?g|png)$'],
             [['add_header', 'Pragma public'],
              ['add_header',
               'Cache-Control  \'public, must-revalidate, proxy-revalidate\''
               ' "test,;{}" foo'],
              ['blah', '"hello;world"'],
              ['try_files', '$uri @rewrites']]]]]])
Beispiel #6
0
 def test_parse_from_file2(self):
     parsed = load(open(util.get_data_filename('edge_cases.conf')))
     self.assertEqual(parsed, [
         [['server'], [['server_name', 'simple']]],
         [['server'],
          [['server_name', 'with.if'],
           [['location', '~', '^/services/.+$'],
            [[['if', '($request_filename ~* \\.(ttf|woff)$)'],
              [['add_header', 'Access-Control-Allow-Origin "*"']]]]]]],
         [['server'],
          [['server_name', 'with.complicated.headers'],
           [['location', '~*', '\\.(?:gif|jpe?g|png)$'],
            [['add_header', 'Pragma public'],
             [
                 'add_header',
                 'Cache-Control  \'public, must-revalidate, proxy-revalidate\''
                 ' "test,;{}" foo'
             ], ['blah', '"hello;world"'], ['try_files', '$uri @rewrites']]]
           ]]
     ])
Beispiel #7
0
 def test_parse_from_file(self):
     parsed = load(open(util.get_data_filename('foo.conf')))
     self.assertEqual(
         parsed,
         [['user', 'www-data'],
          [['http'],
           [[['server'],
             [['listen', '*:80 default_server ssl'],
              ['server_name', '*.www.foo.com *.www.example.com'],
              ['root', '/home/ubuntu/sites/foo/'],
              [['location', '/status'],
               [
                   [['types'], [['image/jpeg', 'jpg']]],
               ]],
              [['location', '~', r'case_sensitive\.php$'],
               [
                   ['index', 'index.php'],
                   ['root', '/var/root'],
               ]], [['location', '~*', r'case_insensitive\.php$'], []],
              [['location', '=', r'exact_match\.php$'], []],
              [['location', '^~', r'ignore_regex\.php$'], []]]]]]])
 def test_parse_from_file(self):
     parsed = load(open(util.get_data_filename('foo.conf')))
     self.assertEqual(
         parsed,
         [['user', 'www-data'],
          [['http'],
           [[['server'], [
               ['listen', '*:80 default_server ssl'],
               ['server_name', '*.www.foo.com *.www.example.com'],
               ['root', '/home/ubuntu/sites/foo/'],
               [['location', '/status'], [
                   [['types'], [['image/jpeg', 'jpg']]],
               ]],
               [['location', '~', r'case_sensitive\.php$'], [
                   ['index', 'index.php'],
                   ['root', '/var/root'],
               ]],
               [['location', '~*', r'case_insensitive\.php$'], []],
               [['location', '=', r'exact_match\.php$'], []],
               [['location', '^~', r'ignore_regex\.php$'], []]
           ]]]]]
     )
    def _parse_files(self, filepath, override=False):
        """Parse files from a glob

        :param str filepath: Nginx config file path
        :param bool override: Whether to parse a file that has been parsed
        :returns: list of parsed tree structures
        :rtype: list

        """
        files = glob.glob(filepath)
        trees = []
        for item in files:
            if item in self.parsed and not override:
                continue
            try:
                with open(item) as _file:
                    parsed = nginxparser.load(_file)
                    self.parsed[item] = parsed
                    trees.append(parsed)
            except IOError:
                logging.warn("Could not open file: %s", item)
            except pyparsing.ParseException:
                logging.debug("Could not parse file: %s", item)
        return trees
    def _parse_files(self, filepath, override=False):
        """Parse files from a glob

        :param str filepath: Nginx config file path
        :param bool override: Whether to parse a file that has been parsed
        :returns: list of parsed tree structures
        :rtype: list

        """
        files = glob.glob(filepath)
        trees = []
        for item in files:
            if item in self.parsed and not override:
                continue
            try:
                with open(item) as _file:
                    parsed = nginxparser.load(_file)
                    self.parsed[item] = parsed
                    trees.append(parsed)
            except IOError:
                logger.warn("Could not open file: %s", item)
            except pyparsing.ParseException:
                logger.debug("Could not parse file: %s", item)
        return trees