Пример #1
0
    def test_get_quota(self):
        """
        Tests cluster.get_quota() method

        Verifies:
            * if no user is passed, return default quota values
            * if user has quota, return values from Quota
            * if user doesn't have quota, return default cluster values
        """
        default_quota = {'default': 1, 'ram': 1,
                         'virtual_cpus': None, 'disk': 3}
        user_quota = {'default': 0, 'ram': 4, 'virtual_cpus': 5, 'disk': None}

        cluster = Cluster(hostname='foo.fake.hostname')
        cluster.__dict__.update(default_quota)
        cluster.save()
        user = User(username='******')
        user.save()

        # default quota
        self.assertEqual(default_quota, cluster.get_quota())

        # user without quota, defaults to default
        self.assertEqual(default_quota, cluster.get_quota(user.get_profile()))

        # user with custom quota
        quota = Quota(cluster=cluster, user=user.get_profile())
        quota.__dict__.update(user_quota)
        quota.save()
        self.assertEqual(user_quota, cluster.get_quota(user.get_profile()))

        quota.delete()
        cluster.delete()
        user.delete()
Пример #2
0
    def test_view_list(self):
        """
        Tests displaying the list of clusters
        """
        url = '/clusters/'

        # create extra user and tests
        cluster1 = Cluster(hostname='cluster1', slug='cluster1')
        cluster1.save()

        # anonymous user
        response = self.c.get(url, follow=True)
        self.assertEqual(200, response.status_code)
        self.assertTemplateUsed(response, 'registration/login.html')

        # unauthorized user
        self.assertTrue(
            self.c.login(username=self.unauthorized.username,
                         password='******'))
        response = self.c.get(url)
        self.assertEqual(200, response.status_code)
        self.assertEquals('text/html; charset=utf-8', response['content-type'])
        self.assertTemplateUsed(response, 'ganeti/cluster/list.html')
        clusters = response.context['cluster_list']
        self.assertFalse(clusters)

        # authorized permissions
        self.assertTrue(
            self.c.login(username=self.cluster_admin.username,
                         password='******'))
        response = self.c.get(url)
        self.assertEqual(200, response.status_code)
        self.assertEquals('text/html; charset=utf-8', response['content-type'])
        self.assertTemplateUsed(response, 'ganeti/cluster/list.html')
        clusters = response.context['cluster_list']
        self.assertTrue(self.cluster in clusters)
        self.assertTrue(cluster1 not in clusters)
        self.assertEqual(1, len(clusters))

        # authorized (superuser)
        self.assertTrue(
            self.c.login(username=self.superuser.username, password='******'))
        response = self.c.get(url)
        self.assertEqual(200, response.status_code)
        self.assertEquals('text/html; charset=utf-8', response['content-type'])
        self.assertTemplateUsed(response, 'ganeti/cluster/list.html')
        clusters = response.context['cluster_list']
        self.assertTrue(self.cluster in clusters)
        self.assertTrue(cluster1 in clusters)
        self.assertEqual(2, len(clusters))

        cluster1.delete()
Пример #3
0
    def test_view_list(self):
        """
        Tests displaying the list of clusters
        """
        url = '/clusters/'

        # create extra user and tests
        cluster1 = Cluster(hostname='cluster1', slug='cluster1')
        cluster1.save()

        # anonymous user
        response = self.c.get(url, follow=True)
        self.assertEqual(200, response.status_code)
        self.assertTemplateUsed(response, 'registration/login.html')

        # unauthorized user
        self.assertTrue(self.c.login(username=self.unauthorized.username,
                                     password='******'))
        response = self.c.get(url)
        self.assertEqual(200, response.status_code)
        self.assertEquals('text/html; charset=utf-8', response['content-type'])
        self.assertTemplateUsed(response, 'ganeti/cluster/list.html')
        clusters = response.context['cluster_list']
        self.assertFalse(clusters)

        # authorized permissions
        self.assertTrue(self.c.login(username=self.cluster_admin.username,
                                     password='******'))
        response = self.c.get(url)
        self.assertEqual(200, response.status_code)
        self.assertEquals('text/html; charset=utf-8', response['content-type'])
        self.assertTemplateUsed(response, 'ganeti/cluster/list.html')
        clusters = response.context['cluster_list']
        self.assertTrue(self.cluster in clusters)
        self.assertTrue(cluster1 not in clusters)
        self.assertEqual(1, len(clusters))

        # authorized (superuser)
        self.assertTrue(self.c.login(username=self.superuser.username,
                                     password='******'))
        response = self.c.get(url)
        self.assertEqual(200, response.status_code)
        self.assertEquals('text/html; charset=utf-8', response['content-type'])
        self.assertTemplateUsed(response, 'ganeti/cluster/list.html')
        clusters = response.context['cluster_list']
        self.assertTrue(self.cluster in clusters)
        self.assertTrue(cluster1 in clusters)
        self.assertEqual(2, len(clusters))

        cluster1.delete()
Пример #4
0
class TestVMWizardAdvancedForm(TestCase):

    def setUp(self):
        # XXX #8895 means we need a cluster here
        self.cluster = Cluster()
        self.cluster.hostname = "cluster.example.com"
        self.cluster.save()

        self.pnode = Node()
        self.pnode.cluster = self.cluster
        self.pnode.hostname = "pnode.example.com"
        self.pnode.save()

        self.snode = Node()
        self.snode.cluster = self.cluster
        self.snode.hostname = "snode.example.com"
        self.snode.save()

        self.valid_data = {
            "pnode": self.pnode.id,
            "snode": self.snode.id,
        }

    def tearDown(self):
        self.pnode.delete()
        self.snode.delete()
        self.cluster.delete()

    def test_trivial(self):
        pass

    def test_validate_valid(self):
        form = VMWizardAdvancedForm(self.valid_data)
        self.assertTrue(form.is_valid(), form.errors)

    def test_validate_ip_check_without_name_check(self):
        data = self.valid_data.copy()
        data["ip_check"] = True
        form = VMWizardAdvancedForm(data)
        self.assertFalse(form.is_valid(),
                         "IP check shouldn't be allowed without name check")

    def test_validate_pnode_equals_snode(self):
        invalid_data = self.valid_data.copy()
        invalid_data["snode"] = invalid_data["pnode"]
        form = VMWizardAdvancedForm(invalid_data)
        self.assertFalse(form.is_valid(),
                         "The secondary node cannot be the primary node.")
Пример #5
0
class TestVMWizardAdvancedForm(TestCase):
    def setUp(self):
        # XXX #8895 means we need a cluster here
        self.cluster = Cluster()
        self.cluster.hostname = "cluster.example.com"
        self.cluster.save()

        self.pnode = Node()
        self.pnode.cluster = self.cluster
        self.pnode.hostname = "pnode.example.com"
        self.pnode.save()

        self.snode = Node()
        self.snode.cluster = self.cluster
        self.snode.hostname = "snode.example.com"
        self.snode.save()

        self.valid_data = {
            "pnode": self.pnode.id,
            "snode": self.snode.id,
        }

    def tearDown(self):
        self.pnode.delete()
        self.snode.delete()
        self.cluster.delete()

    def test_trivial(self):
        pass

    def test_validate_valid(self):
        form = VMWizardAdvancedForm(self.valid_data)
        self.assertTrue(form.is_valid(), form.errors)

    def test_validate_ip_check_without_name_check(self):
        data = self.valid_data.copy()
        data["ip_check"] = True
        form = VMWizardAdvancedForm(data)
        self.assertFalse(form.is_valid(),
                         "IP check shouldn't be allowed without name check")

    def test_validate_pnode_equals_snode(self):
        invalid_data = self.valid_data.copy()
        invalid_data["snode"] = invalid_data["pnode"]
        form = VMWizardAdvancedForm(invalid_data)
        self.assertFalse(form.is_valid(),
                         "The secondary node cannot be the primary node.")
Пример #6
0
    def test_set_quota(self):
        """
        Tests cluster.set_quota()

        Verifies:
            * passing values with no quota, creates a new quota object
            * passing values with an existing quota, updates it.
            * passing a None with an existing quota deletes it
            * passing a None with no quota, does nothing
        """
        default_quota = {
            'default': 1,
            'ram': 1,
            'virtual_cpus': None,
            'disk': 3
        }
        user_quota = {'default': 0, 'ram': 4, 'virtual_cpus': 5, 'disk': None}
        user_quota2 = {'default': 0, 'ram': 7, 'virtual_cpus': 8, 'disk': 9}

        cluster = Cluster(hostname='foo.fake.hostname')
        cluster.__dict__.update(default_quota)
        cluster.save()
        user = User(username='******')
        user.save()

        # create new quota
        cluster.set_quota(user.get_profile(), user_quota)
        query = Quota.objects.filter(cluster=cluster, user=user.get_profile())
        self.assertTrue(query.exists())
        self.assertEqual(user_quota, cluster.get_quota(user.get_profile()))

        # update quota with new values
        cluster.set_quota(user.get_profile(), user_quota2)
        query = Quota.objects.filter(cluster=cluster, user=user.get_profile())
        self.assertEqual(1, query.count())
        self.assertEqual(user_quota2, cluster.get_quota(user.get_profile()))

        # delete quota
        cluster.set_quota(user.get_profile(), None)
        query = Quota.objects.filter(cluster=cluster, user=user.get_profile())
        self.assertFalse(query.exists())
        self.assertEqual(default_quota, cluster.get_quota(user.get_profile()))

        cluster.delete()
        user.delete()
Пример #7
0
    def test_parse_info(self):
        """
        Test parsing values from cached info

        Verifies:
            * mtime and ctime are parsed
            * ram, virtual_cpus, and disksize are parsed
        """
        cluster = Cluster(hostname='foo.fake.hostname')
        cluster.save()
        cluster.info = INFO

        self.assertEqual(cluster.ctime,
                         datetime.fromtimestamp(1270685309.818239))
        self.assertEqual(cluster.mtime,
                         datetime.fromtimestamp(1283552454.2998919))

        cluster.delete()
Пример #8
0
    def test_missing_in_ganeti(self):
        """
        Tests missing_in_ganeti property
        """
        cluster = Cluster(hostname='ganeti.example.test')
        cluster.save()
        vm_current = VirtualMachine(cluster=cluster,
                                    hostname='gimager2.example.bak')
        vm_removed = VirtualMachine(cluster=cluster,
                                    hostname='does.not.exist.org')
        vm_current.save()
        vm_removed.save()

        self.assertEqual([u'does.not.exist.org'], cluster.missing_in_ganeti)

        vm_current.delete()
        vm_removed.delete()
        cluster.delete()
Пример #9
0
    def test_parse_info(self):
        """
        Test parsing values from cached info

        Verifies:
            * mtime and ctime are parsed
            * ram, virtual_cpus, and disksize are parsed
        """
        cluster = Cluster(hostname='foo.fake.hostname')
        cluster.save()
        cluster.info = INFO

        self.assertEqual(cluster.ctime,
                         datetime.fromtimestamp(1270685309.818239))
        self.assertEqual(cluster.mtime,
                         datetime.fromtimestamp(1283552454.2998919))

        cluster.delete()
Пример #10
0
    def test_missing_in_ganeti(self):
        """
        Tests missing_in_ganeti property
        """
        cluster = Cluster(hostname='ganeti.example.test')
        cluster.save()
        vm_current = VirtualMachine(cluster=cluster,
                                    hostname='gimager2.example.bak')
        vm_removed = VirtualMachine(cluster=cluster,
                                    hostname='does.not.exist.org')
        vm_current.save()
        vm_removed.save()

        self.assertEqual([u'does.not.exist.org'], cluster.missing_in_ganeti)

        vm_current.delete()
        vm_removed.delete()
        cluster.delete()
Пример #11
0
    def test_sync_virtual_machines(self):
        """
        Tests synchronizing cached virtuals machines (stored in db) with info
        the ganeti cluster is storing

        Verifies:
            * VMs no longer in ganeti are deleted
            * VMs missing from the database are added
        """
        cluster = Cluster(hostname='ganeti.example.test')
        cluster.save()
        vm_missing = 'gimager.example.bak'
        vm_current = VirtualMachine(cluster=cluster,
                                    hostname='gimager2.example.bak')
        vm_removed = VirtualMachine(cluster=cluster,
                                    hostname='does.not.exist.org')
        vm_current.save()
        vm_removed.save()

        cluster.sync_virtual_machines()
        self.assertTrue(VirtualMachine.objects.get(cluster=cluster,
                                                   hostname=vm_missing),
                        'missing vm was not created')
        self.assertTrue(
            VirtualMachine.objects.get(
                cluster=cluster,
                hostname=vm_current.hostname),
            'previously existing vm was not created')
        self.assertTrue(
            VirtualMachine.objects.filter(
                cluster=cluster,
                hostname=vm_removed.hostname),
            "vm not in ganeti was not removed from db")

        cluster.sync_virtual_machines(True)
        self.assertFalse(
            VirtualMachine.objects.filter(
                cluster=cluster,
                hostname=vm_removed.hostname),
            'vm not present in ganeti was not removed from db')

        vm_removed.delete()
        vm_current.delete()
        cluster.delete()
Пример #12
0
    def test_save(self):
        """
        test saving a cluster object

        Verifies:
            * object is saved and queryable
            * hash is updated
        """

        # XXX any reason why these are in a single test?
        cluster = Cluster()
        cluster.save()
        self.assertTrue(cluster.hash)
        cluster.delete()

        cluster = Cluster(hostname='foo.fake.hostname', slug='different')
        cluster.save()
        self.assertTrue(cluster.hash)
        cluster.delete()
Пример #13
0
    def test_save(self):
        """
        test saving a cluster object

        Verifies:
            * object is saved and queryable
            * hash is updated
        """

        # XXX any reason why these are in a single test?
        cluster = Cluster()
        cluster.save()
        self.assertTrue(cluster.hash)
        cluster.delete()

        cluster = Cluster(hostname='foo.fake.hostname', slug='different')
        cluster.save()
        self.assertTrue(cluster.hash)
        cluster.delete()
Пример #14
0
    def test_set_quota(self):
        """
        Tests cluster.set_quota()

        Verifies:
            * passing values with no quota, creates a new quota object
            * passing values with an existing quota, updates it.
            * passing a None with an existing quota deletes it
            * passing a None with no quota, does nothing
        """
        default_quota = {'default': 1, 'ram': 1,
                         'virtual_cpus': None, 'disk': 3}
        user_quota = {'default': 0, 'ram': 4, 'virtual_cpus': 5, 'disk': None}
        user_quota2 = {'default': 0, 'ram': 7, 'virtual_cpus': 8, 'disk': 9}

        cluster = Cluster(hostname='foo.fake.hostname')
        cluster.__dict__.update(default_quota)
        cluster.save()
        user = User(username='******')
        user.save()

        # create new quota
        cluster.set_quota(user.get_profile(), user_quota)
        query = Quota.objects.filter(cluster=cluster, user=user.get_profile())
        self.assertTrue(query.exists())
        self.assertEqual(user_quota, cluster.get_quota(user.get_profile()))

        # update quota with new values
        cluster.set_quota(user.get_profile(), user_quota2)
        query = Quota.objects.filter(cluster=cluster, user=user.get_profile())
        self.assertEqual(1, query.count())
        self.assertEqual(user_quota2, cluster.get_quota(user.get_profile()))

        # delete quota
        cluster.set_quota(user.get_profile(), None)
        query = Quota.objects.filter(cluster=cluster, user=user.get_profile())
        self.assertFalse(query.exists())
        self.assertEqual(default_quota, cluster.get_quota(user.get_profile()))

        cluster.delete()
        user.delete()
Пример #15
0
    def test_sync_virtual_machines(self):
        """
        Tests synchronizing cached virtuals machines (stored in db) with info
        the ganeti cluster is storing

        Verifies:
            * VMs no longer in ganeti are deleted
            * VMs missing from the database are added
        """
        cluster = Cluster(hostname='ganeti.example.test')
        cluster.save()
        vm_missing = 'gimager.example.bak'
        vm_current = VirtualMachine(cluster=cluster,
                                    hostname='gimager2.example.bak')
        vm_removed = VirtualMachine(cluster=cluster,
                                    hostname='does.not.exist.org')
        vm_current.save()
        vm_removed.save()

        cluster.sync_virtual_machines()
        self.assertTrue(
            VirtualMachine.objects.get(cluster=cluster, hostname=vm_missing),
            'missing vm was not created')
        self.assertTrue(
            VirtualMachine.objects.get(cluster=cluster,
                                       hostname=vm_current.hostname),
            'previously existing vm was not created')
        self.assertTrue(
            VirtualMachine.objects.filter(cluster=cluster,
                                          hostname=vm_removed.hostname),
            "vm not in ganeti was not removed from db")

        cluster.sync_virtual_machines(True)
        self.assertFalse(
            VirtualMachine.objects.filter(cluster=cluster,
                                          hostname=vm_removed.hostname),
            'vm not present in ganeti was not removed from db')

        vm_removed.delete()
        vm_current.delete()
        cluster.delete()
Пример #16
0
    def test_get_quota(self):
        """
        Tests cluster.get_quota() method

        Verifies:
            * if no user is passed, return default quota values
            * if user has quota, return values from Quota
            * if user doesn't have quota, return default cluster values
        """
        default_quota = {
            'default': 1,
            'ram': 1,
            'virtual_cpus': None,
            'disk': 3
        }
        user_quota = {'default': 0, 'ram': 4, 'virtual_cpus': 5, 'disk': None}

        cluster = Cluster(hostname='foo.fake.hostname')
        cluster.__dict__.update(default_quota)
        cluster.save()
        user = User(username='******')
        user.save()

        # default quota
        self.assertEqual(default_quota, cluster.get_quota())

        # user without quota, defaults to default
        self.assertEqual(default_quota, cluster.get_quota(user.get_profile()))

        # user with custom quota
        quota = Quota(cluster=cluster, user=user.get_profile())
        quota.__dict__.update(user_quota)
        quota.save()
        self.assertEqual(user_quota, cluster.get_quota(user.get_profile()))

        quota.delete()
        cluster.delete()
        user.delete()