def convert(self, value, param, ctx): """ Try to find correct disk image regarding version. """ if not self.choices: gandi = GandiContextHelper() gandi.echo("No configuration found, please use 'gandi setup' " "command") sys.exit(1) # Exact match if value in self.choices: return value # Try to find 64 bits version new_value = '%s 64 bits' % value if new_value in self.choices: return new_value # Try to find without specific bits version p = re.compile(' (64|32) bits') new_value = p.sub('', value) if new_value in self.choices: return new_value self.fail('invalid choice: %s. (choose from %s)' % (value, ', '.join(self.choices)), param, ctx)
def convert(self, value, param, ctx): """ Try to find correct disk image regarding version. """ if not self.choices: gandi = GandiContextHelper() gandi.echo("No configuration found, please use 'gandi setup' " "command") sys.exit(1) # Exact match if value in self.choices: return value # Try to find 64 bits version new_value = '%s 64 bits' % value if new_value in self.choices: return new_value # Try to find without specific bits version p = re.compile(' (64|32) bits') new_value = p.sub('', value) if new_value in self.choices: return new_value self.fail( 'invalid choice: %s. (choose from %s)' % (value, ', '.join(self.choices)), param, ctx)
def convert(self, value, param, ctx): """ Return converted value. """ if not self.choices: gandi = GandiContextHelper() gandi.echo("No configuration found, please use 'gandi setup' " "command") sys.exit(1) return click.Choice.convert(self, value, param, ctx)
def choices(self): """ Retrieve choices from API if possible""" if not self._choices: gandi = GandiContextHelper() self._choices = self._get_choices(gandi) if not self._choices: gandi = GandiContextHelper() gandi.echo("No configuration found, please use 'gandi setup' " "command") sys.exit(1) return self._choices
def test_create_name_vhost(self): self.maxDiff = None args = ['--name', '123456', '--vhosts', 'ploki.fr', '--ssl'] with mock.patch('gandi.cli.modules.vhost.os.chdir', create=True) as mock_chdir: mock_chdir.return_value = mock.MagicMock() result = self.invoke_with_exceptions(paas.create, args, obj=GandiContextHelper(), input='ploki\nploki\n') self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ password: \nRepeat for confirmation: \n\ There is no certificate for ploki.fr. Create the certificate with (for exemple) : $ gandi certificate create --cn ploki.fr --type std \n\ Or relaunch the current command with --poll-cert option Creating your PaaS instance. \rProgress: [###] 100.00% 00:00:00 \n\ Your PaaS instance 123456 has been created. Creating a new vhost. \rProgress: [###] 100.00% 00:00:00 \n\ Your vhost ploki.fr has been created.""") self.assertEqual(result.exit_code, 0)
def test_create_vlan_ip_ok(self): args = [ '--hostname', 'server400', '--vlan', 'vlantest', '--ip', '10.50.10.10' ] result = self.invoke_with_exceptions(vm.create, args, obj=GandiContextHelper(), input='plokiploki\nplokiploki\n') self.assertEqual( re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ password: \nRepeat for confirmation: \n* Private only ip vm (can't enable \ emergency web console access). * root user will be created. Creating your iface. \rProgress: [###] 100.00% 00:00:00 \n\ Your iface has been created with the following IP addresses: ip4:\t10.50.10.10 * Configuration used: 1 cores, 256Mb memory, ip private, image Debian 8\ , hostname: server400, datacenter: FR-SD3 Creating your Virtual Machine server400. \rProgress: [###] 100.00% 00:00:00 \n\ Your Virtual Machine server400 has been created.""") self.assertEqual(result.exit_code, 0)
def test_create_image_deprecated(self): args = [ '--image', 'Debian 7 64 bits (HVM)', '--sshkey', 'mysecretkey', '--datacenter', 'FR-SD2' ] result = self.invoke_with_exceptions(vm.create, args, obj=GandiContextHelper()) output = re.sub(r'\[#+\]', '[###]', result.output.strip()) self.assertEqual( re.sub(r'vm\d+', 'vm', output), """\ /!\ Datacenter FR-SD2 will be closed on 25/12/2017, please consider \ using another datacenter. /!\ Image Debian 7 64 bits (HVM) is deprecated and will soon be unavailable. * root user will be created. * SSH key authorization will be used. * No password supplied for vm (required to enable emergency web console \ access). * Configuration used: 1 cores, 256Mb memory, ip v6, image Debian 7 64 bits \ (HVM), hostname: vm, datacenter: FR-SD2 Creating your Virtual Machine vm. \rProgress: [###] 100.00% 00:00:00 \n\ Your Virtual Machine vm has been created.""") self.assertEqual(result.exit_code, 0)
def test_create_datacenter_limited(self): args = ['--datacenter', 'FR-SD2'] result = self.invoke_with_exceptions(paas.create, args, obj=GandiContextHelper(), input='ploki\nploki\n') output = re.sub(r'\[#+\]', '[###]', result.output.strip()) self.assertEqual(re.sub(r'paas\d+', 'paas', output), """\ /!\ Datacenter FR-SD2 will be closed on 25/12/2017, please consider using \ another datacenter. password: \nRepeat for confirmation: \n\ Creating your PaaS instance. \rProgress: [###] 100.00% 00:00:00 \n\ Your PaaS instance paas has been created.""") self.assertEqual(result.exit_code, 0) params = self.api_calls['paas.create'][0][0] self.assertEqual(params['datacenter_id'], 1) self.assertEqual(params['size'], 's') self.assertEqual(params['duration'], '1m') self.assertEqual(params['password'], 'ploki') self.assertTrue(params['name'].startswith('paas')) self.assertEqual(result.exit_code, 0)
def test_update_gateway_vm(self): args = [ 'pouet', '--name', 'chocolat', '--gateway', 'server01', '--create', '--bandwidth', '204800' ] result = self.invoke_with_exceptions(vlan.update, args, obj=GandiContextHelper()) self.assertEqual( re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ Will create a new ip in this vlan for vm server01 Creating your iface. \rProgress: [###] 100.00% 00:00:00 \ \nYour iface has been created with the following IP addresses: ip4:\t95.142.160.181 ip6:\t2001:4b98:dc0:47:216:3eff:feb2:3862 Attaching your iface. \rProgress: [###] 100.00% 00:00:00 \ \nUpdating your vlan.""") self.assertEqual(result.exit_code, 0) params = self.api_calls['hosting.vlan.update'][0][1] self.assertEqual(params['name'], 'chocolat') self.assertEqual(params['gateway'], '95.142.160.181')
def test_create_default(self): args = [] with mock.patch('gandi.cli.modules.paas.Paas.init_conf', create=True) as mock_init_conf: mock_init_conf.return_value = mock.MagicMock() result = self.invoke_with_exceptions(paas.create, args, obj=GandiContextHelper(), input='ploki\nploki\n') output = re.sub(r'\[#+\]', '[###]', result.output.strip()) self.assertEqual( re.sub(r'paas\d+', 'paas', output), """\ password: \nRepeat for confirmation: \n\ Creating your PaaS instance. \rProgress: [###] 100.00% 00:00:00 \n\ Your PaaS instance paas has been created.""") self.assertEqual(result.exit_code, 0) params = self.api_calls['paas.create'][0][0] self.assertEqual(params['datacenter_id'], 3) self.assertEqual(params['size'], 's') self.assertEqual(params['duration'], '1m') self.assertEqual(params['password'], 'ploki') self.assertTrue(params['name'].startswith('paas'))
def test_create_params_snapshot_ko(self): args = [ '--name', 'newdisk', '--size', '5G', '--datacenter', 'FR', '--snapshotprofile', '7' ] result = self.invoke_with_exceptions(disk.create, args, obj=GandiContextHelper()) self.assertEqual(result.exit_code, 2)
def convert(self, value, param, ctx): """ Try to find correct kernel regarding version. """ if not self.choices: gandi = GandiContextHelper() gandi.echo("No configuration found, please use 'gandi setup' " "command") sys.exit(1) # Exact match first if value in self.choices: return value # Also try with x86-64 suffix new_value = '%s-x86_64' % value if new_value in self.choices: return new_value self.fail('invalid choice: %s. (choose from %s)' % (value, ', '.join(self.choices)), param, ctx)
def test_delete_background(self): args = ['intranet', '--force', '--bg'] result = self.invoke_with_exceptions(vlan.delete, args, obj=GandiContextHelper()) self.assertEqual(result.output, """\ id : 200 step : WAIT """) self.assertEqual(result.exit_code, 0)
def test_create_datacenter_closed(self): args = ['--name', 'newdisk', '--size', '5G', '--datacenter', 'US-BA1', '--snapshotprofile', '3'] result = self.invoke_with_exceptions(disk.create, args, obj=GandiContextHelper()) self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ Error: /!\ Datacenter US-BA1 is closed, please choose another datacenter.""") self.assertEqual(result.exit_code, 1)
def test_create_params_ip_ko(self): args = ['--datacenter', 'FR', '--bandwidth', '51200', '--ip-version', '4', '--ip', '10.50.10.10'] result = self.invoke_with_exceptions(ip.create, args, obj=GandiContextHelper()) self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ You must have a --vlan when giving an --ip.""") self.assertEqual(result.exit_code, 0)
def test_delete_refuse(self): args = ['intranet'] result = self.invoke_with_exceptions(vlan.delete, args, input='\n', obj=GandiContextHelper()) self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ Are you sure to delete vlan 'intranet'? [y/N]:""") self.assertEqual(result.exit_code, 0)
def test_create_ip_not_vlan_ko(self): args = ['--hostname', 'server500', '--ip', '10.50.10.10'] result = self.invoke_with_exceptions(vm.create, args, obj=GandiContextHelper(), input='plokiploki\nplokiploki\n') self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ password: \nRepeat for confirmation: \n\ --ip can't be used without --vlan.""") self.assertEqual(result.exit_code, 0)
def test_create_datacenter_closed(self): args = ['--datacenter', 'US-BA1'] result = self.invoke_with_exceptions(vm.create, args, obj=GandiContextHelper(), input='plokiploki\nplokiploki\n') output = re.sub(r'\[#+\]', '[###]', result.output.strip()) self.assertEqual(re.sub(r'vm\d+', 'vm', output), """\ Error: /!\ Datacenter US-BA1 is closed, please choose another datacenter.""") self.assertEqual(result.exit_code, 1)
def test_delete_force(self): args = ['intranet', '--force'] result = self.invoke_with_exceptions(vlan.delete, args, obj=GandiContextHelper()) self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ Deleting your vlan. \rProgress: [###] 100.00% 00:00:00""") self.assertEqual(result.exit_code, 0)
def test_create_params_attach_ko(self): args = ['--datacenter', 'US', '--bandwidth', '51200', '--ip-version', '4', '--attach', 'server01'] result = self.invoke_with_exceptions(ip.create, args, obj=GandiContextHelper()) self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ The datacenter you provided does not match the datacenter of the \ vm you want to attach to.""") self.assertEqual(result.exit_code, 0)
def test_create_datacenter_closed(self): args = ['--name', 'testvlan', '--datacenter', 'US-BA1', '--subnet', '10.7.70.0/24', '--gateway', '10.7.70.254'] result = self.invoke_with_exceptions(vlan.create, args, obj=GandiContextHelper()) self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ Error: /!\ Datacenter US-BA1 is closed, please choose another datacenter.""") self.assertEqual(result.exit_code, 1)
def choices(self): """ Retrieve choices from API if possible""" if not self._choices: gandi = self.gandi or GandiContextHelper() self._choices = self._get_choices(gandi) if not self._choices: gandi.echo("No configuration found, please use 'gandi setup' " "command") sys.exit(1) return self._choices
def test_create_datacenter_closed(self): args = ['--datacenter', 'US-BA1', '--bandwidth', '51200', '--ip-version', '6'] result = self.invoke_with_exceptions(ip.create, args, obj=GandiContextHelper()) self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ Error: /!\ Datacenter US-BA1 is closed, please choose another datacenter.""") self.assertEqual(result.exit_code, 1)
def choices(self): """ Retrieve choices from API if possible""" if not self._choices: gandi = self.gandi or GandiContextHelper() self._choices = self._get_choices(gandi) if not self._choices: api = self.gandi.get_api_connector() gandi.echo('Please check that you are connecting to the good ' "api '%s' and that it's running." % (api.host)) sys.exit(1) return self._choices
def test_create_background(self): args = ['--name', 'testvlanbg', '--bg'] result = self.invoke_with_exceptions(vlan.create, args, obj=GandiContextHelper()) self.assertEqual(result.output.strip(), """\ {'id': 200, 'step': 'WAIT'}""") self.assertEqual(result.exit_code, 0) params = self.api_calls['hosting.vlan.create'][0][0] self.assertEqual(params['datacenter_id'], 3) self.assertEqual(params['name'], 'testvlanbg')
def test_delete(self): args = ['intranet'] result = self.invoke_with_exceptions(vlan.delete, args, input='y\n', obj=GandiContextHelper()) self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ Are you sure to delete vlan 'intranet'? [y/N]: y Deleting your vlan. \rProgress: [###] 100.00% 00:00:00""") self.assertEqual(result.exit_code, 0)
def test_update_password(self): args = ['paas_owncloud', '--password'] result = self.invoke_with_exceptions(paas.update, args, obj=GandiContextHelper(), input='ploki\nploki\n') self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ password: \nRepeat for confirmation: \n\ Updating your PaaS instance. \rProgress: [###] 100.00% 00:00:00""") self.assertEqual(result.exit_code, 0)
def test_update_gateway_vm_unknown(self): args = ['pouet', '--name', 'chocolat', '--gateway', 'server01', '--bandwidth', '204800'] result = self.invoke_with_exceptions(vlan.update, args, obj=GandiContextHelper()) self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ Can't find 'server01' in 'pouet' vlan""") self.assertEqual(result.exit_code, 0)
def test_update_gateway_multiple_ips(self): args = ['pouet', '--name', 'chocolat', '--gateway', 'server02'] result = self.invoke_with_exceptions(vlan.update, args, obj=GandiContextHelper()) self.assertEqual( re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ This vm has two ips in the vlan, don't know which one to choose \ (213.167.231.3, 192.168.232.252)""") self.assertEqual(result.exit_code, 0)
def test_create_default_background(self): args = ['--bg'] result = self.invoke_with_exceptions(disk.create, args, obj=GandiContextHelper()) self.assertEqual(result.output.strip(), """\ id : 200 step : WAIT""") self.assertEqual(result.exit_code, 0) params = self.api_calls['hosting.disk.create'][0][0] self.assertEqual(params['type'], 'data') self.assertEqual(params['size'], 3072) self.assertTrue(params['name'].startswith('vdi'))
def test_create_source(self): args = ['--source', 'sys_server01'] result = self.invoke_with_exceptions(disk.create, args, obj=GandiContextHelper()) self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ Creating your disk. \rProgress: [###] 100.00% 00:00:00""") self.assertEqual(result.exit_code, 0) params, disk_id = self.api_calls['hosting.disk.create_from'][0] self.assertEqual(params['type'], 'data') self.assertTrue(params['name'].startswith('vdi')) self.assertEqual(disk_id, 4969249)
def test_create_name(self): args = ['--name', '123456'] result = self.invoke_with_exceptions(paas.create, args, obj=GandiContextHelper(), input='ploki\nploki\n') self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ password: \nRepeat for confirmation: \n\ Creating your PaaS instance. \rProgress: [###] 100.00% 00:00:00 \n\ Your PaaS instance 123456 has been created.""") self.assertEqual(result.exit_code, 0)
def test_create_background_ok(self): args = ['--hostname', 'server500', '--background'] result = self.invoke_with_exceptions(vm.create, args, obj=GandiContextHelper(), input='plokiploki\nplokiploki\n') self.assertEqual(re.sub(r'\[#+\]', '[###]', result.output.strip()), """\ password: \nRepeat for confirmation: \n* root user will be created. * Configuration used: 1 cores, 256Mb memory, ip v6, image Debian 8\ , hostname: server500, datacenter: FR-SD5 * IAAS backend is now creating your VM and its associated resources in the \ background.""") self.assertEqual(result.exit_code, 0)