def setUp(self):
     self.a = ["\n    ", "things", " ", "quirk"]
     self.b = ["y", " "]
     self.l = self.a[:]
     self.l2 = self.b[:]
     self.ul = UnspacedList(self.l)
     self.ul2 = UnspacedList(self.l2)
Exemple #2
0
 def setUp(self):
     self.a = ["\n    ", "things", " ", "quirk"]
     self.b = ["y", " "]
     self.l = self.a[:]
     self.l2 = self.b[:]
     self.ul = UnspacedList(self.l)
     self.ul2 = UnspacedList(self.l2)
Exemple #3
0
    def test_double_redirect(self):
        # Test that we add one redirect for each domain
        example_conf = self.config.parser.abs_path('sites-enabled/example.com')
        self.config.enhance("example.com", "redirect")
        self.config.enhance("example.org", "redirect")

        expected1 = UnspacedList(_redirect_block_for_domain("example.com"))[0]
        expected2 = UnspacedList(_redirect_block_for_domain("example.org"))[0]

        generated_conf = self.config.parser.parsed[example_conf]
        self.assertTrue(util.contains_at_depth(generated_conf, expected1, 2))
        self.assertTrue(util.contains_at_depth(generated_conf, expected2, 2))
 def test_is_dirty(self):
     self.assertEqual(False, self.ul2.is_dirty())
     ul3 = UnspacedList([])
     ul3.append(self.ul)
     self.assertEqual(False, self.ul.is_dirty())
     self.assertEqual(True, ul3.is_dirty())
     ul4 = UnspacedList([[1], [2, 3, 4]])
     self.assertEqual(False, ul4.is_dirty())
     ul4[1][2] = 5
     self.assertEqual(True, ul4.is_dirty())
Exemple #5
0
    def test_deploy_no_match_add_redirect(self):
        default_conf = self.config.parser.abs_path('sites-enabled/default')
        foo_conf = self.config.parser.abs_path('foo.conf')
        del self.config.parser.parsed[foo_conf][2][1][0][1][
            0]  # remove default_server
        self.config.version = (1, 3, 1)

        self.config.deploy_cert("www.nomatch.com", "example/cert.pem",
                                "example/key.pem", "example/chain.pem",
                                "example/fullchain.pem")

        self.config.deploy_cert("nomatch.com", "example/cert.pem",
                                "example/key.pem", "example/chain.pem",
                                "example/fullchain.pem")

        self.config.enhance("www.nomatch.com", "redirect")

        self.config.save()

        self.config.parser.load()

        expected = UnspacedList(
            _redirect_block_for_domain("www.nomatch.com"))[0]

        generated_conf = self.config.parser.parsed[default_conf]
        self.assertTrue(util.contains_at_depth(generated_conf, expected, 2))
    def test_dump_as_string(self):
        dumped = dumps(
            UnspacedList(
                [['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']]],
                    ]]]]]))

        self.assertEqual(
            dumped.split('\n'), '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;}}}'.split('\n'))
 def test_set(self):
     ul3 = copy.deepcopy(self.ul)
     ul3[0] = "zither"
     l = ["\n ", "zather", "zest"]
     ul3[1] = UnspacedList(l)
     self.assertEqual(ul3, ["zither", ["zather", "zest"]])
     self.assertEqual(ul3.spaced, [self.a[0], "zither", " ", l])
    def test_dump_as_file(self):
        with open(util.get_data_filename('nginx.conf')) as handle:
            parsed = load(handle)
        parsed[-1][-1].append(UnspacedList([['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']]]]]))

        with open(util.get_data_filename('nginx.new.conf'), 'w') as handle:
            dump(parsed, handle)
        with open(util.get_data_filename('nginx.new.conf')) as handle:
            parsed_new = load(handle)
        try:
            self.maxDiff = None
            self.assertEqual(parsed[0], parsed_new[0])
            self.assertEqual(parsed[1:], parsed_new[1:])
        finally:
            os.unlink(util.get_data_filename('nginx.new.conf'))
 def test_insert(self):
     x = UnspacedList(
         [['\n    ', 'listen', '       ', '69.50.225.155:9000'],
          ['\n    ', 'listen', '       ', '127.0.0.1'],
          ['\n    ', 'server_name', ' ', '.example.com'],
          ['\n    ', 'server_name', ' ', 'example.*'], '\n',
          ['listen', ' ', '5001 ssl']])
     x.insert(5, "FROGZ")
     self.assertEqual(
         x, [['listen', '69.50.225.155:9000'], ['listen', '127.0.0.1'],
             ['server_name', '.example.com'], ['server_name', 'example.*'],
             ['listen', '5001 ssl'], 'FROGZ'])
     self.assertEqual(
         x.spaced, [['\n    ', 'listen', '       ', '69.50.225.155:9000'],
                    ['\n    ', 'listen', '       ', '127.0.0.1'],
                    ['\n    ', 'server_name', ' ', '.example.com'],
                    ['\n    ', 'server_name', ' ', 'example.*'], '\n',
                    ['listen', ' ', '5001 ssl'], 'FROGZ'])
Exemple #10
0
 def test_insert(self):
     x = UnspacedList(
             [['\n    ', 'listen', '       ', '69.50.225.155:9000'],
             ['\n    ', 'listen', '       ', '127.0.0.1'],
             ['\n    ', 'server_name', ' ', '.example.com'],
             ['\n    ', 'server_name', ' ', 'example.*'], '\n',
             ['listen', ' ', '5001 ssl']])
     x.insert(5, "FROGZ")
     self.assertEqual(x,
         [['listen', '69.50.225.155:9000'], ['listen', '127.0.0.1'],
         ['server_name', '.example.com'], ['server_name', 'example.*'],
         ['listen', '5001 ssl'], 'FROGZ'])
     self.assertEqual(x.spaced,
         [['\n    ', 'listen', '       ', '69.50.225.155:9000'],
         ['\n    ', 'listen', '       ', '127.0.0.1'],
         ['\n    ', 'server_name', ' ', '.example.com'],
         ['\n    ', 'server_name', ' ', 'example.*'], '\n',
         ['listen', ' ', '5001 ssl'],
         'FROGZ'])
Exemple #11
0
    def test_redirect_enhance(self):
        # Test that we successfully add a redirect when there is
        # a listen directive
        expected = UnspacedList(_redirect_block_for_domain("www.example.com"))[0]

        example_conf = self.config.parser.abs_path('sites-enabled/example.com')
        self.config.enhance("www.example.com", "redirect")

        generated_conf = self.config.parser.parsed[example_conf]
        self.assertTrue(util.contains_at_depth(generated_conf, expected, 2))

        # Test that we successfully add a redirect when there is
        # no listen directive
        migration_conf = self.config.parser.abs_path('sites-enabled/migration.com')
        self.config.enhance("migration.com", "redirect")

        expected = UnspacedList(_redirect_block_for_domain("migration.com"))[0]

        generated_conf = self.config.parser.parsed[migration_conf]
        self.assertTrue(util.contains_at_depth(generated_conf, expected, 2))
Exemple #12
0
 def test_is_dirty(self):
     self.assertEqual(False, self.ul2.is_dirty())
     ul3 = UnspacedList([])
     ul3.append(self.ul)
     self.assertEqual(False, self.ul.is_dirty())
     self.assertEqual(True, ul3.is_dirty())
     ul4 = UnspacedList([[1], [2, 3, 4]])
     self.assertEqual(False, ul4.is_dirty())
     ul4[1][2] = 5
     self.assertEqual(True, ul4.is_dirty())
Exemple #13
0
    def test_dump_as_file(self):
        with open(util.get_data_filename('nginx.conf')) as handle:
            parsed = load(handle)
        parsed[-1][-1].append(UnspacedList([['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']]]]]))

        with tempfile.TemporaryFile(mode='w+t') as f:
            dump(parsed, f)
            f.seek(0)
            parsed_new = load(f)
        self.assertEqual(parsed, parsed_new)
class TestUnspacedList(unittest.TestCase):
    """Test the UnspacedList data structure"""
    def setUp(self):
        self.a = ["\n    ", "things", " ", "quirk"]
        self.b = ["y", " "]
        self.l = self.a[:]
        self.l2 = self.b[:]
        self.ul = UnspacedList(self.l)
        self.ul2 = UnspacedList(self.l2)

    def test_construction(self):
        self.assertEqual(self.ul, ["things", "quirk"])
        self.assertEqual(self.ul2, ["y"])

    def test_append(self):
        ul3 = copy.deepcopy(self.ul)
        ul3.append("wise")
        self.assertEqual(ul3, ["things", "quirk", "wise"])
        self.assertEqual(ul3.spaced, self.a + ["wise"])

    def test_add(self):
        ul3 = self.ul + self.ul2
        self.assertEqual(ul3, ["things", "quirk", "y"])
        self.assertEqual(ul3.spaced, self.a + self.b)
        self.assertEqual(self.ul.spaced, self.a)
        ul3 = self.ul + self.l2
        self.assertEqual(ul3, ["things", "quirk", "y"])
        self.assertEqual(ul3.spaced, self.a + self.b)

    def test_extend(self):
        ul3 = copy.deepcopy(self.ul)
        ul3.extend(self.ul2)
        self.assertEqual(ul3, ["things", "quirk", "y"])
        self.assertEqual(ul3.spaced, self.a + self.b)
        self.assertEqual(self.ul.spaced, self.a)

    def test_set(self):
        ul3 = copy.deepcopy(self.ul)
        ul3[0] = "zither"
        l = ["\n ", "zather", "zest"]
        ul3[1] = UnspacedList(l)
        self.assertEqual(ul3, ["zither", ["zather", "zest"]])
        self.assertEqual(ul3.spaced, [self.a[0], "zither", " ", l])

    def test_get(self):
        self.assertRaises(IndexError, self.ul2.__getitem__, 2)
        self.assertRaises(IndexError, self.ul2.__getitem__, -3)

    def test_insert(self):
        x = UnspacedList(
            [['\n    ', 'listen', '       ', '69.50.225.155:9000'],
             ['\n    ', 'listen', '       ', '127.0.0.1'],
             ['\n    ', 'server_name', ' ', '.example.com'],
             ['\n    ', 'server_name', ' ', 'example.*'], '\n',
             ['listen', ' ', '5001 ssl']])
        x.insert(5, "FROGZ")
        self.assertEqual(
            x, [['listen', '69.50.225.155:9000'], ['listen', '127.0.0.1'],
                ['server_name', '.example.com'], ['server_name', 'example.*'],
                ['listen', '5001 ssl'], 'FROGZ'])
        self.assertEqual(
            x.spaced, [['\n    ', 'listen', '       ', '69.50.225.155:9000'],
                       ['\n    ', 'listen', '       ', '127.0.0.1'],
                       ['\n    ', 'server_name', ' ', '.example.com'],
                       ['\n    ', 'server_name', ' ', 'example.*'], '\n',
                       ['listen', ' ', '5001 ssl'], 'FROGZ'])

    def test_rawlists(self):
        ul3 = copy.deepcopy(self.ul)
        ul3.insert(0, "some")
        ul3.append("why")
        ul3.extend(["did", "whether"])
        del ul3[2]
        self.assertEqual(ul3, ["some", "things", "why", "did", "whether"])

    def test_is_dirty(self):
        self.assertEqual(False, self.ul2.is_dirty())
        ul3 = UnspacedList([])
        ul3.append(self.ul)
        self.assertEqual(False, self.ul.is_dirty())
        self.assertEqual(True, ul3.is_dirty())
        ul4 = UnspacedList([[1], [2, 3, 4]])
        self.assertEqual(False, ul4.is_dirty())
        ul4[1][2] = 5
        self.assertEqual(True, ul4.is_dirty())
Exemple #15
0
class TestUnspacedList(unittest.TestCase):
    """Test the UnspacedList data structure"""
    def setUp(self):
        self.a = ["\n    ", "things", " ", "quirk"]
        self.b = ["y", " "]
        self.l = self.a[:]
        self.l2 = self.b[:]
        self.ul = UnspacedList(self.l)
        self.ul2 = UnspacedList(self.l2)

    def test_construction(self):
        self.assertEqual(self.ul, ["things", "quirk"])
        self.assertEqual(self.ul2, ["y"])

    def test_append(self):
        ul3 = copy.deepcopy(self.ul)
        ul3.append("wise")
        self.assertEqual(ul3, ["things", "quirk", "wise"])
        self.assertEqual(ul3.spaced, self.a + ["wise"])

    def test_add(self):
        ul3 = self.ul + self.ul2
        self.assertEqual(ul3, ["things", "quirk", "y"])
        self.assertEqual(ul3.spaced, self.a + self.b)
        self.assertEqual(self.ul.spaced, self.a)
        ul3 = self.ul + self.l2
        self.assertEqual(ul3, ["things", "quirk", "y"])
        self.assertEqual(ul3.spaced, self.a + self.b)

    def test_extend(self):
        ul3 = copy.deepcopy(self.ul)
        ul3.extend(self.ul2)
        self.assertEqual(ul3, ["things", "quirk", "y"])
        self.assertEqual(ul3.spaced, self.a + self.b)
        self.assertEqual(self.ul.spaced, self.a)

    def test_set(self):
        ul3 = copy.deepcopy(self.ul)
        ul3[0] = "zither"
        l = ["\n ", "zather", "zest"]
        ul3[1] = UnspacedList(l)
        self.assertEqual(ul3, ["zither", ["zather", "zest"]])
        self.assertEqual(ul3.spaced, [self.a[0], "zither", " ", l])

    def test_get(self):
        self.assertRaises(IndexError, self.ul2.__getitem__, 2)
        self.assertRaises(IndexError, self.ul2.__getitem__, -3)

    def test_insert(self):
        x = UnspacedList(
                [['\n    ', 'listen', '       ', '69.50.225.155:9000'],
                ['\n    ', 'listen', '       ', '127.0.0.1'],
                ['\n    ', 'server_name', ' ', '.example.com'],
                ['\n    ', 'server_name', ' ', 'example.*'], '\n',
                ['listen', ' ', '5001 ssl']])
        x.insert(5, "FROGZ")
        self.assertEqual(x,
            [['listen', '69.50.225.155:9000'], ['listen', '127.0.0.1'],
            ['server_name', '.example.com'], ['server_name', 'example.*'],
            ['listen', '5001 ssl'], 'FROGZ'])
        self.assertEqual(x.spaced,
            [['\n    ', 'listen', '       ', '69.50.225.155:9000'],
            ['\n    ', 'listen', '       ', '127.0.0.1'],
            ['\n    ', 'server_name', ' ', '.example.com'],
            ['\n    ', 'server_name', ' ', 'example.*'], '\n',
            ['listen', ' ', '5001 ssl'],
            'FROGZ'])

    def test_rawlists(self):
        ul3 = copy.deepcopy(self.ul)
        ul3.insert(0, "some")
        ul3.append("why")
        ul3.extend(["did", "whether"])
        del ul3[2]
        self.assertEqual(ul3, ["some", "things", "why", "did", "whether"])

    def test_is_dirty(self):
        self.assertEqual(False, self.ul2.is_dirty())
        ul3 = UnspacedList([])
        ul3.append(self.ul)
        self.assertEqual(False, self.ul.is_dirty())
        self.assertEqual(True, ul3.is_dirty())
        ul4 = UnspacedList([[1], [2, 3, 4]])
        self.assertEqual(False, ul4.is_dirty())
        ul4[1][2] = 5
        self.assertEqual(True, ul4.is_dirty())
Exemple #16
0
class TestUnspacedList(unittest.TestCase):
    """Test the UnspacedList data structure"""
    def setUp(self):
        self.a = ["\n    ", "things", " ", "quirk"]
        self.b = ["y", " "]
        self.l = self.a[:]
        self.l2 = self.b[:]
        self.ul = UnspacedList(self.l)
        self.ul2 = UnspacedList(self.l2)

    def test_construction(self):
        self.assertEqual(self.ul, ["things", "quirk"])
        self.assertEqual(self.ul2, ["y"])

    def test_append(self):
        ul3 = copy.deepcopy(self.ul)
        ul3.append("wise")
        self.assertEqual(ul3, ["things", "quirk", "wise"])
        self.assertEqual(ul3.spaced, self.a + ["wise"])

    def test_add(self):
        ul3 = self.ul + self.ul2
        self.assertEqual(ul3, ["things", "quirk", "y"])
        self.assertEqual(ul3.spaced, self.a + self.b)
        self.assertEqual(self.ul.spaced, self.a)
        ul3 = self.ul + self.l2
        self.assertEqual(ul3, ["things", "quirk", "y"])
        self.assertEqual(ul3.spaced, self.a + self.b)

    def test_extend(self):
        ul3 = copy.deepcopy(self.ul)
        ul3.extend(self.ul2)
        self.assertEqual(ul3, ["things", "quirk", "y"])
        self.assertEqual(ul3.spaced, self.a + self.b)
        self.assertEqual(self.ul.spaced, self.a)

    def test_set(self):
        ul3 = copy.deepcopy(self.ul)
        ul3[0] = "zither"
        l = ["\n ", "zather", "zest"]
        ul3[1] = UnspacedList(l)
        self.assertEqual(ul3, ["zither", ["zather", "zest"]])
        self.assertEqual(ul3.spaced, [self.a[0], "zither", " ", l])

    def test_get(self):
        self.assertRaises(IndexError, self.ul2.__getitem__, 2)
        self.assertRaises(IndexError, self.ul2.__getitem__, -3)

    def test_rawlists(self):
        ul3 = copy.deepcopy(self.ul)
        ul3.insert(0, "some")
        ul3.append("why")
        ul3.extend(["did", "whether"])
        del ul3[2]
        self.assertEqual(ul3, ["some", "things", "why", "did", "whether"])

    def test_is_dirty(self):
        self.assertEqual(False, self.ul2.is_dirty())
        ul3 = UnspacedList([])
        ul3.append(self.ul)
        self.assertEqual(False, self.ul.is_dirty())
        self.assertEqual(True, ul3.is_dirty())
        ul4 = UnspacedList([[1], [2, 3, 4]])
        self.assertEqual(False, ul4.is_dirty())
        ul4[1][2] = 5
        self.assertEqual(True, ul4.is_dirty())