Exemple #1
0
    def test_delete_linode(self):
        """
        Tests that deleting a Linode creates the correct api request
        """
        with self.mock_delete() as m:
            linode = Linode(self.client, 123)
            linode.delete()

            self.assertEqual(m.call_url, '/linode/instances/123')
Exemple #2
0
    def test_mutate(self):
        """
        Tests that you can submit a correct mutate api request
        """
        linode = Linode(self.client, 123)
        result = {}

        with self.mock_post(result) as m:
            linode.mutate()
            self.assertEqual(m.call_url, '/linode/instances/123/mutate')
Exemple #3
0
    def test_shutdown(self):
        """
        Tests that you can submit a correct shutdown api request
        """
        linode = Linode(self.client, 123)
        result = {}

        with self.mock_post(result) as m:
            linode.shutdown()
            self.assertEqual(m.call_url, '/linode/instances/123/shutdown')
Exemple #4
0
    def test_boot_with_config(self):
        """
        Tests that you can submit a correct boot with a config api request
        """
        linode = Linode(self.client, 123)
        config = linode.configs[0]
        result = {}

        with self.mock_post(result) as m:
            linode.boot(config=config)
            self.assertEqual(m.call_url, '/linode/instances/123/boot')
Exemple #5
0
    def test_rebuild(self):
        """
        Tests that you can rebuild with an image
        """
        linode = Linode(self.client, 123)

        with self.mock_post('/linode/instances/123') as m:
            pw = linode.rebuild('linode/debian9')

            self.assertIsNotNone(pw)
            self.assertTrue(isinstance(pw, str))

            self.assertEqual(m.call_url, '/linode/instances/123/rebuild')

            self.assertEqual(m.call_data, {
                "image": "linode/debian9",
                "root_pass": pw,
            })
Exemple #6
0
    def create_linode(self, service, datacenter, source=None, **kwargs):
        if not 'linode' in service.service_type:
            raise AttributeError("{} is not a linode service!".format(service.label))

        ret_pass = None
        if type(source) is Distribution and not 'root_pass' in kwargs:
            ret_pass = Linode.generate_root_password()
            kwargs['root_pass'] = ret_pass

        if 'root_ssh_key' in kwargs:
            root_ssh_key = kwargs['root_ssh_key']
            accepted_types = ('ssh-dss', 'ssh-rsa', 'ecdsa-sha2-nistp', 'ssh-ed25519')
            if not any([ t for t in accepted_types if root_ssh_key.startswith(t) ]):
                # it doesn't appear to be a key.. is it a path to the key?
                import os
                root_ssh_key = os.path.expanduser(root_ssh_key)
                if os.path.isfile(root_ssh_key):
                    with open(root_ssh_key) as f:
                        kwargs['root_ssh_key'] = "".join([ l.strip() for l in f ])
                else:
                    raise ValueError('root_ssh_key must either be a path to the key file or a '
                                    'raw public key of one of these types: {}'.format(accepted_types))

        params = {
             'service': service.id,
             'datacenter': datacenter.id,
             'source': source.id if source else None,
         }
        params.update(kwargs)

        result = self.post('/linodes', data=params)

        if not 'id' in result:
            return result

        l = Linode(self, result['id'])
        l._populate(result)
        if not ret_pass:
            return l
        else:
            return l, ret_pass
    def test_get_linode(self):
        """
        Tests that a client is loaded correctly by ID
        """
        linode = Linode(self.client, 123)
        self.assertEqual(linode._populated, False)

        self.assertEqual(linode.label, "linode123")
        self.assertEqual(linode.group, "test")

        self.assertTrue(isinstance(linode.image, Image))
        self.assertEqual(linode.image.label, "Ubuntu 17.04")
    def test_rebuild(self):
        """
        Tests that you can rebuild with an image
        """
        linode = Linode(self.client, 123)

        # barebones result of a rebuild
        result = {
            "config": [],
            "disks": []
        }

        with self.mock_post(result) as m:
            pw = linode.rebuild('linode/debian9')

            self.assertIsNotNone(pw)
            self.assertTrue(isinstance(pw, str))

            self.assertEqual(m.call_url, '/linode/instances/123/rebuild')

            self.assertEqual(m.call_data, {
                "image": "linode/debian9",
                "root_pass": pw,
            })
Exemple #9
0
    def test_available_backups(self):
        """
        Tests that a Linode can retrieve its own backups
        """
        linode = Linode(self.client, 123)

        backups = linode.available_backups

        # assert we got the correct number of automatic backups
        self.assertEqual(len(backups.automatic), 3)

        # examine one automatic backup
        b = backups.automatic[0]
        self.assertEqual(b.id, 12345)
        self.assertEqual(b._populated, True)
        self.assertEqual(b.status, 'successful')
        self.assertEqual(b.type, 'auto')
        self.assertEqual(
            b.created,
            datetime(year=2018, month=1, day=9, hour=0, minute=1, second=1))
        self.assertEqual(
            b.updated,
            datetime(year=2018, month=1, day=9, hour=0, minute=1, second=1))
        self.assertEqual(
            b.finished,
            datetime(year=2018, month=1, day=9, hour=0, minute=1, second=1))
        self.assertEqual(b.region.id, 'us-east-1a')
        self.assertEqual(b.label, None)
        self.assertEqual(b.message, None)

        self.assertEqual(len(b.disks), 2)
        self.assertEqual(b.disks[0].size, 1024)
        self.assertEqual(b.disks[0].label, 'Debian 8.1 Disk')
        self.assertEqual(b.disks[0].filesystem, 'ext4')
        self.assertEqual(b.disks[1].size, 0)
        self.assertEqual(b.disks[1].label, '256MB Swap Image')
        self.assertEqual(b.disks[1].filesystem, 'swap')

        self.assertEqual(len(b.configs), 1)
        self.assertEqual(b.configs[0], 'My Debian 8.1 Profile')

        # assert that snapshots came back as expected
        self.assertEqual(backups.snapshot.current, None)
        self.assertEqual(backups.snapshot.in_progress, None)
Exemple #10
0
    def test_update_linode(self):
        """
        Tests that a Linode can be updated
        """
        with self.mock_put('linode/instances/123') as m:
            linode = Linode(self.client, 123)

            linode.label = "NewLinodeLabel"
            linode.group = "new_group"
            linode.save()

            self.assertEqual(m.call_url, '/linode/instances/123')
            self.assertEqual(m.call_data, {
                "label": "NewLinodeLabel",
                "group": "new_group"
            })
Exemple #11
0
 def linode(self):
     if self.entity and self.entity.type == 'linode':
         return Linode(self._client, self.entity.id)
     return None