def test_check_node_is_locked(self):
        """Test LocalStorage.check_node_is_locked method."""
        kube_type = Kube.get_default_kube_type()
        node1 = Node(ip='192.168.1.2', hostname='host1', kube_id=kube_type)
        node2 = Node(ip='192.168.1.3', hostname='host2', kube_id=kube_type)
        db.session.add_all([node1, node2])
        db.session.commit()
        user, _ = self.fixtures.user_fixtures()
        pd = PersistentDisk(name='q', owner_id=user.id, size=1)
        db.session.add(pd)
        db.session.commit()

        flag, reason = pstorage.LocalStorage.check_node_is_locked(node1.id)
        self.assertFalse(flag)
        self.assertIsNone(reason)
        flag, reason = pstorage.LocalStorage.check_node_is_locked(node2.id)
        self.assertFalse(flag)

        pd = PersistentDisk(name='w',
                            owner_id=user.id,
                            size=1,
                            node_id=node1.id)
        db.session.add(pd)
        db.session.commit()

        flag, reason = pstorage.LocalStorage.check_node_is_locked(node1.id)
        self.assertTrue(flag)
        self.assertIsNotNone(reason)
Example #2
0
    def run(self, hostname):
        node = db.session.query(Node).filter(Node.hostname == hostname).first()
        if node is None:
            raise InvalidCommand(u'Node "{0}" not found'.format(hostname))

        PersistentDisk.get_by_node_id(node.id).delete(
            synchronize_session=False)
        delete_node(node=node, force=True)
    def test_delete_persistent_drives(self, sc_mock):
        """Test for pstorage.delete_persistent_drives function"""
        user, _ = self.fixtures.user_fixtures()
        pd = PersistentDisk(name='q', owner_id=user.id, size=1)
        db.session.add(pd)
        db.session.commit()

        ps_delete_by_id_mock = sc_mock.return_value.delete_by_id

        ps_delete_by_id_mock.return_value = 1

        pstorage.delete_persistent_drives([pd.id])
        pds = db.session.query(PersistentDisk).all()
        self.assertEqual(len(pds), 1)
        self.assertEqual(pds[0].state, pd.state)

        ps_delete_by_id_mock.return_value = 0
        pstorage.delete_persistent_drives([pd.id], mark_only=True)
        pds = db.session.query(PersistentDisk).all()
        self.assertEqual(len(pds), 1)
        self.assertEqual(pds[0].state, PersistentDiskStatuses.DELETED)

        ps_delete_by_id_mock.return_value = 0
        pstorage.delete_persistent_drives([pd.id])
        pds = db.session.query(PersistentDisk).all()
        self.assertEqual(pds, [])
    def test_delete_drive_by_id(self, delete_pd_mock, update_pod_mock):
        """Test pstorage.delete_drive_by_id function."""
        user, _ = self.fixtures.user_fixtures()
        pd = PersistentDisk(name='q', owner_id=user.id, size=1)
        db.session.add(pd)
        db.session.commit()
        old_id = pd.id
        old_drive_name = pd.drive_name
        old_name = pd.name

        pstorage.delete_drive_by_id(pd.id)

        new_pd = db.session.query(PersistentDisk).filter(
            PersistentDisk.state == PersistentDiskStatuses.DELETED).first()
        self.assertIsNotNone(new_pd)
        update_pod_mock.assert_called_once_with(new_pd)
        delete_pd_mock.delay.assert_called_once_with([old_id], mark_only=False)

        updated_pd = db.session.query(PersistentDisk).filter(
            PersistentDisk.id == old_id).first()
        self.assertIsNotNone(updated_pd)
        self.assertNotEqual(updated_pd.name, old_name)
        self.assertEqual(updated_pd.state, PersistentDiskStatuses.TODELETE)
        self.assertEqual(updated_pd.drive_name, old_drive_name)
        self.assertEqual(updated_pd.drive_name + '_1', new_pd.drive_name)
        self.assertEqual(new_pd.name, old_name)
        self.assertEqual(new_pd.owner_id, updated_pd.owner_id)
def upgrade(upd, with_testing, *args, **kwargs):
    upd.print_log('Add PersistentDisk model.')
    upd.print_log('Upgrading db...')
    helpers.upgrade_db(revision='56f9182bf415')

    upd.print_log('Populate db...')
    drives = []
    if CEPH:
        drives.extend(CephStorage().get())
    if AWS:
        drives.extend(AmazonStorage().get())
    if not drives:
        return

    pods_by_drives = get_pods_by_drives()
    for drive in drives:
        owner = User.filter_by(username=drive['owner']).one()
        pod = pods_by_drives.get(drive['drive_name'])
        pd = PersistentDisk(id=drive['id'],
                            drive_name=drive['drive_name'],
                            name=drive['name'],
                            owner=owner,
                            size=drive['size'],
                            pod=pod)
        db.session.add(pd)
    db.session.commit()
 def test_already_exists(self):
     existed = PersistentDisk.create(owner=self.user,
                                     name=self.devices[0]['name'],
                                     size=1)
     existed.save()
     res = self.user_open(url, 'POST', self.devices[0])
     self.assertAPIError(res, 406, 'DuplicateName')
    def test_drive_can_be_deleted(self):
        """Test LocalStorage.drive_can_be_deleted method."""
        user, _ = self.fixtures.user_fixtures()
        pd = PersistentDisk(name='q', owner_id=user.id, size=1)
        db.session.add(pd)
        db.session.commit()

        flag, reason = pstorage.LocalStorage.drive_can_be_deleted(pd.id)
        self.assertTrue(flag)
        self.assertIsNone(reason)

        pod_id = str(uuid.uuid4())
        pdname = 'somename1'
        pod = Pod(id=pod_id,
                  name='somename',
                  owner_id=user.id,
                  kube_id=Kube.get_default_kube_type(),
                  config=json.dumps({
                      "volumes_public": [{
                          "persistentDisk": {
                              "pdSize": 1,
                              "pdName": pdname
                          },
                      }]
                  }))
        db.session.add(pod)
        db.session.commit()

        flag, reason = pstorage.LocalStorage.drive_can_be_deleted(pd.id)
        self.assertTrue(flag)
        self.assertIsNone(reason)

        pd = PersistentDisk(name=pdname, owner_id=user.id, size=1)
        db.session.add(pd)
        db.session.commit()
        flag, reason = pstorage.LocalStorage.drive_can_be_deleted(pd.id)
        self.assertFalse(flag)
        self.assertIsNotNone(reason)

        # delete pod, drive must became deletable
        pod.status = 'deleted'
        db.session.query(Pod).update({Pod.status: 'deleted'},
                                     synchronize_session=False)
        db.session.commit()
        flag, reason = pstorage.LocalStorage.drive_can_be_deleted(pd.id)
        self.assertTrue(flag)
        self.assertIsNone(reason)
Example #8
0
 def _check_volume_name(self, volume_name):
     persistent_disk = PersistentDisk.filter(
         PersistentDisk.owner_id == self.owner.id,
         PersistentDisk.name == volume_name,
         PersistentDisk.state.in_([
             PersistentDiskStatuses.PENDING, PersistentDiskStatuses.CREATED
         ])).first()
     if persistent_disk:
         return VolumeExists(persistent_disk.name, persistent_disk.id)
 def test_update_pods_volumes(self):
     """Test pstorage.update_pods_volumes function"""
     user, _ = self.fixtures.user_fixtures()
     old_drive_name = 'qq11'
     new_drive_name = 'ww22'
     pdname = 'qwerty1243'
     pod_id = str(uuid.uuid4())
     storage_prefix = pstorage.NODE_LOCAL_STORAGE_PREFIX
     pod = Pod(id=pod_id,
               name='somename',
               owner_id=user.id,
               kube_id=Kube.get_default_kube_type(),
               config=json.dumps({
                   "volumes": [{
                       "hostPath": {
                           "path": storage_prefix + '/' + old_drive_name
                       },
                       "name": "var-qqq7824431125",
                       "annotation": {
                           "localStorage": {
                               "path":
                               storage_prefix + '/' + old_drive_name,
                               "size": 1
                           }
                       }
                   }],
                   "volumes_public": [{
                       "persistentDisk": {
                           "pdSize": 1,
                           "pdName": pdname
                       },
                       "name": "var-qqq7824431125"
                   }]
               }))
     db.session.add(pod)
     new_pd = PersistentDisk(name=pdname,
                             drive_name=new_drive_name,
                             owner_id=user.id,
                             size=1)
     db.session.add(new_pd)
     db.session.commit()
     pstorage.update_pods_volumes(new_pd)
     pods = db.session.query(Pod).all()
     self.assertTrue(len(pods), 1)
     new_pod = pods[0]
     config = new_pod.get_dbconfig()
     self.assertEqual(len(config['volumes']), len(config['volumes_public']))
     self.assertEqual(len(config['volumes']), 1)
     new_drive_path = storage_prefix + '/' + new_drive_name
     self.assertEqual(config['volumes'][0]['hostPath']['path'],
                      new_drive_path)
     self.assertEqual(
         config['volumes'][0]['annotation']['localStorage']['path'],
         new_drive_path)
     self.assertEqual(
         config['volumes_public'][0]['persistentDisk']['pdName'], pdname)
 def setUp(self):
     self.pod = self.fixtures.pod(owner_id=self.user.id,
                                  config=json.dumps({
                                      "volumes_public": [{
                                          "persistentDisk": {
                                              "pdSize": 1,
                                              "pdName": 'q'
                                          },
                                      }]
                                  }))
     self.free_pd = PersistentDisk.create(size=3,
                                          owner=self.user,
                                          name='free-pd')
     self.non_free_pd = PersistentDisk.create(size=2,
                                              owner=self.user,
                                              name='non-free-pd',
                                              pod_id=self.pod.id)
     self.free_pd.save()
     self.non_free_pd.save()
def get_persistent_disk(upd, internal_volume):
    if 'rbd' in internal_volume:
        drive_name = internal_volume['rbd']['image']
        size = internal_volume['rbd'].get('size', 1)
    elif 'awsElasticBlockStore' in internal_volume:
        drive_name = internal_volume['awsElasticBlockStore']['drive']
        size = internal_volume['awsElasticBlockStore'].get('size', 1)
    else:
        raise ValueError('Incorrect volume! {0}'.format(internal_volume))
    pd = PersistentDisk.query.filter_by(drive_name=drive_name).first()
    if pd is None:
        upd.print_log('PD was not found {0}'.format(internal_volume))
        pd = PersistentDisk(drive_name=drive_name, size=size).save()
    return pd
    def test_get_drives_from_db(self):
        """Test PersistentStorage._get_drives_from_db"""
        user, _ = self.fixtures.user_fixtures()
        pd1 = PersistentDisk(name='q', owner_id=user.id, size=1)
        pd2 = PersistentDisk(name='q1', owner_id=user.id, size=2)
        pd3 = PersistentDisk(name='q1', owner_id=1, size=2)
        db.session.add_all([pd1, pd2, pd3])
        db.session.commit()

        ps = pstorage.PersistentStorage()
        drives = ps._get_drives_from_db()
        self.assertEqual(len(drives), 3)
        self.assertEqual([[], [], []], [item['linkedPods'] for item in drives])

        ps = pstorage.PersistentStorage()
        drives = ps._get_drives_from_db(user_id=user.id)
        self.assertEqual(len(drives), 2)
        self.assertEqual([[], []], [item['linkedPods'] for item in drives])
        self.assertEqual({user.id: user.username},
                         {item['owner_id']: item['owner']
                          for item in drives})
        self.assertEqual({'q1', 'q'}, {item['name'] for item in drives})
        self.assertEqual([False, False], [item['in_use'] for item in drives])
        self.assertEqual([False, False],
                         [item['forbidDeletion'] for item in drives])

        pod_id = str(uuid.uuid4())
        pod_name = 'somename1'
        pod = Pod(id=pod_id,
                  name=pod_name,
                  owner_id=user.id,
                  kube_id=Kube.get_default_kube_type(),
                  config=json.dumps({
                      "volumes_public": [{
                          "persistentDisk": {
                              "pdSize": 1,
                              "pdName": 'q'
                          },
                      }]
                  }))
        db.session.add(pod)
        db.session.commit()
        ps = pstorage.PersistentStorage()
        drives = ps._get_drives_from_db(user_id=user.id)
        self.assertEqual(len(drives), 2)
        with_pods = None
        without_pods = None
        for drive in drives:
            if drive['name'] == 'q':
                with_pods = drive
            else:
                without_pods = drive
        self.assertEqual(with_pods['linkedPods'], [{
            'podId': pod_id,
            'name': pod_name
        }])
        self.assertEqual(without_pods['linkedPods'], [])
        self.assertEqual([False, False], [item['in_use'] for item in drives])
        self.assertEqual([False, False],
                         [item['forbidDeletion'] for item in drives])

        pd1.pod_id = pod.id
        db.session.commit()
        drives = ps._get_drives_from_db(user_id=user.id)
        pd1_data = (item for item in drives if item['id'] == pd1.id).next()
        pd2_data = (item for item in drives if item['id'] == pd2.id).next()
        self.assertEqual(True, pd1_data['in_use'])
        self.assertEqual(False, pd2_data['in_use'])
        self.assertEqual(True, pd1_data['forbidDeletion'])
        self.assertEqual(False, pd2_data['forbidDeletion'])
Example #13
0
def persistent_disk(**kwargs):
    if 'owner_id' not in kwargs and 'owner' not in kwargs:
        kwargs['owner'], _ = user_fixtures()
    return PersistentDisk(**kwargs).save()