def test_start_pod_suspend(self, _run, mock_update): user = self.user url = self.item_url(self.user.id) ippool = IPPool(network='192.168.1.252/30') ippool.block_ip([u'192.168.1.252', u'192.168.1.255']) ippool.save() min_pod = { 'restartPolicy': 'Always', 'kube_type': 0, 'containers': [{ 'image': 'nginx', 'name': 'fk8i0gai', 'args': ['nginx', '-g', 'daemon off;'], 'ports': [{ 'protocol': 'tcp', 'isPublic': True, 'containerPort': 80 }], }] } # pod res = PodCollection(user).add(dict(min_pod, name='pod-1'), skip_check=False) data = {'command': 'start'} pod_url = '/'.join(['podapi', res['id']]) response = self.open(url=pod_url, method='PUT', auth=self.userauth, json=data) self.assert200(response) check_data = check_change_pod_data(data) mock_update.assert_called_once_with(res['id'], check_data) data = {'suspended': True} response = self.admin_open(url=url, method='PUT', json=data) self.assert200(response) mock_update.called = False response = self.open(url=pod_url, method='PUT', auth=self.userauth, json={'command': 'start'}) self.assertAPIError(response, 403, "PermissionDenied") assert mock_update.not_called
def test_suspend(self, _run): """AC-1608 In case of unsuspend, return all public IPs""" # Disable as otherwise test breaks. Was created before introducing # this feature current_app.config['FIXED_IP_POOLS'] = False user = self.user url = self.item_url(self.user.id) ippool = IPPool(network='192.168.1.252/30') ippool.block_ip([u'192.168.1.252', u'192.168.1.255']) ippool.save() min_pod = { 'restartPolicy': 'Always', 'kube_type': 0, 'containers': [{ 'image': 'nginx', 'name': 'fk8i0gai', 'args': ['nginx', '-g', 'daemon off;'], 'ports': [{ 'protocol': 'tcp', 'isPublic': True, 'containerPort': 80 }], }] } # pod-1 res = PodCollection(user).add(dict(min_pod, name='pod-1'), skip_check=False) pod_1 = Pod.query.get(res['id']) pod_1.with_ip_conf = { 'public_ip': res['public_ip'], 'containers': [{ 'ports': [{ 'isPublic': True }] }], } pod_1.without_ip_conf = { 'public_ip_before_freed': res['public_ip'], 'containers': [{ 'ports': [{ 'isPublic_before_freed': True }] }], } # pod-2 res = PodCollection(user).add(dict(min_pod, name='pod-2'), skip_check=False) pod_2 = Pod.query.get(res['id']) pod_2.with_ip_conf = { 'public_ip': res['public_ip'], 'containers': [{ 'ports': [{ 'isPublic': True }] }], } pod_2.without_ip_conf = { 'public_ip_before_freed': res['public_ip'], 'containers': [{ 'ports': [{ 'isPublic_before_freed': True }] }], } # helpers def _has_public_ip(pod): podip = PodIP.query.filter_by(pod_id=pod.id).first() conf = pod.get_dbconfig() port = conf['containers'][0]['ports'][0] if podip is None: self.assertFalse(port.get('isPublic')) self.assertFalse(conf.get('public_ip')) self.assertTrue(port.get('isPublic_before_freed')) self.assertTrue(conf.get('public_ip_before_freed')) return False self.assertFalse(port.get('isPublic_before_freed')) self.assertFalse(conf.get('public_ip_before_freed')) self.assertTrue(port.get('isPublic')) self.assertEqual(conf.get('public_ip'), unicode(ip_address(podip.ip_address))) return True def _count_pods_with_public_ip(): return _has_public_ip(pod_1) + _has_public_ip(pod_2) # suspend user. Both ip must be freed self.assertEqual(_count_pods_with_public_ip(), 2, 'all pods must have public ip in the beginning') data = {'suspended': True} response = self.admin_open(url=url, method='PUT', json=data) self.assert200(response) self.assertEqual(_count_pods_with_public_ip(), 0, 'all pods must lose public ip') # unsuspend must be atomic, so if one pod cannot get public ip, # all won't ippool.block_ip(ippool.free_hosts(as_int=True)[0]) db.session.commit() data = {'suspended': False} response = self.admin_open(url=url, method='PUT', json=data) self.assertAPIError(response, 400, 'NoFreeIPs') self.assertEqual( _count_pods_with_public_ip(), 0, "operation must be atomic, so if one pod can't get " "public ip, all won't") # unblock ip in ippool to be able to unsuspend user ippool.unblock_ip(ippool.get_blocked_set(as_int=True).pop()) db.session.commit() data = {'suspended': False} response = self.admin_open(url=url, method='PUT', json=data) self.assert200(response) self.assertEqual(_count_pods_with_public_ip(), 2, 'all pods must get their ip back')