Ejemplo n.º 1
0
    def test_add_server_directives(self):
        nparser = parser.NginxParser(self.config_path, self.ssl_options)
        nparser.add_server_directives(nparser.abs_path('nginx.conf'),
                                      set(['localhost',
                                           r'~^(www\.)?(example|bar)\.']),
                                      [['foo', 'bar'], ['ssl_certificate',
                                                        '/etc/ssl/cert.pem']],
                                      replace=False)
        ssl_re = re.compile(r'\n\s+ssl_certificate /etc/ssl/cert.pem')
        dump = nginxparser.dumps(nparser.parsed[nparser.abs_path('nginx.conf')])
        self.assertEqual(1, len(re.findall(ssl_re, dump)))

        server_conf = nparser.abs_path('server.conf')
        names = set(['alias', 'another.alias', 'somename'])
        nparser.add_server_directives(server_conf, names,
                                      [['foo', 'bar'], ['ssl_certificate',
                                                        '/etc/ssl/cert2.pem']],
                                      replace=False)
        nparser.add_server_directives(server_conf, names, [['foo', 'bar']],
                                      replace=False)
        self.assertEqual(nparser.parsed[server_conf],
                         [['server_name', 'somename  alias  another.alias'],
                          ['foo', 'bar'],
                          ['ssl_certificate', '/etc/ssl/cert2.pem']
                          ])
    def test_dump_as_string(self):
        dumped = dumps([
            ['user', 'www-data'],
            [['server'], [
                ['listen', '80'],
                ['server_name', 'foo.com'],
                ['root', '/home/ubuntu/sites/foo/'],
                [['location', '/status'], [
                    ['check_status'],
                    [['types'], [['image/jpeg', 'jpg']]],
                ]]
            ]]])

        self.assertEqual(dumped,
                         'user www-data;\n'
                         'server {\n'
                         '    listen 80;\n'
                         '    server_name foo.com;\n'
                         '    root /home/ubuntu/sites/foo/;\n \n'
                         '    location /status {\n'
                         '        check_status;\n \n'
                         '        types {\n'
                         '            image/jpeg jpg;\n'
                         '        }\n'
                         '    }\n'
                         '}')
Ejemplo n.º 3
0
    def test_add_server_directives(self):
        nparser = parser.NginxParser(self.config_path, self.ssl_options)
        nparser.add_server_directives(
            nparser.abs_path('nginx.conf'),
            set(['localhost', r'~^(www\.)?(example|bar)\.']),
            [['foo', 'bar'], ['ssl_certificate', '/etc/ssl/cert.pem']],
            replace=False)
        ssl_re = re.compile(r'\n\s+ssl_certificate /etc/ssl/cert.pem')
        dump = nginxparser.dumps(
            nparser.parsed[nparser.abs_path('nginx.conf')])
        self.assertEqual(1, len(re.findall(ssl_re, dump)))

        server_conf = nparser.abs_path('server.conf')
        names = set(['alias', 'another.alias', 'somename'])
        nparser.add_server_directives(
            server_conf,
            names, [['foo', 'bar'], ['ssl_certificate', '/etc/ssl/cert2.pem']],
            replace=False)
        nparser.add_server_directives(server_conf,
                                      names, [['foo', 'bar']],
                                      replace=False)
        self.assertEqual(
            nparser.parsed[server_conf],
            [['server_name', 'somename  alias  another.alias'], ['foo', 'bar'],
             ['ssl_certificate', '/etc/ssl/cert2.pem']])
Ejemplo n.º 4
0
    def test_dump_as_string(self):
        dumped = dumps([['user', 'www-data'],
                        [['server'],
                         [['listen', '80'], ['server_name', 'foo.com'],
                          ['root', '/home/ubuntu/sites/foo/'],
                          [['location', '/status'],
                           [
                               ['check_status', None],
                               [['types'], [['image/jpeg', 'jpg']]],
                           ]]]]])

        self.assertEqual(
            dumped, 'user www-data;\n'
            'server {\n'
            '    listen 80;\n'
            '    server_name foo.com;\n'
            '    root /home/ubuntu/sites/foo/;\n'
            '\n'
            '    location /status {\n'
            '        check_status;\n'
            '\n'
            '        types {\n'
            '            image/jpeg jpg;\n'
            '        }\n'
            '    }\n'
            '}\n')
Ejemplo n.º 5
0
    def filedump(self, ext='tmp'):
        """Dumps parsed configurations into files.

        :param str ext: The file extension to use for the dumped files. If
            empty, this overrides the existing conf files.

        """
        for filename in self.parsed:
            tree = self.parsed[filename]
            if ext:
                filename = filename + os.path.extsep + ext
            try:
                logger.debug('Dumping to %s:\n%s', filename, nginxparser.dumps(tree))
                with open(filename, 'w') as _file:
                    nginxparser.dump(tree, _file)
            except IOError:
                logger.error("Could not open file for writing: %s", filename)