def test_box_name_completion(self): b1 = Box('box-1', './uname.yml', 'hostname', 'ip') b2 = Box('box-2', './uname.yml', 'hostname', 'ip') self.cli.provider.add_box(b1) self.cli.provider.add_box(b2) completions = self.cli.complete_box_names('', 'provision box-', 14, 14) self.cli.provider.remove_box(b1) self.cli.provider.remove_box(b2) self.assertEqual(set(completions), {'1', '2'})
def define_box(self): vagrant_boxes = self._action( action="box", action_args=("list",), output=False ).splitlines() if not vagrant_boxes: print ('\nThere are no available Vagrant (base) boxes.') print ('Please search for a suitable one at ' \ 'https://atlas.hashicorp.com/boxes/search.') print ('Once you\'ve chosen the <box> add it using the following cmd:' \ ' \'$ vagrant box add <box>\'.\n') else: playbook = io.input_path('playbook path') hostname = self.fetch_box_hosts(playbook) name = io.input_value('box name', self.suggest_name(hostname)) ip = io.input_value('internal IP') ext = VagrantExt() mem = io.input_value('amount of RAM in GB', 1) ext.set_mem(mem * 1024) ext.set_shares(self._input_shares()) (img, provider) = self._input_img(vagrant_boxes) ext.set_image(img) ext.set_provider(provider) return Box(name, playbook, hostname, ip, self.DEFAULT_USER, self.DEFAULT_PWD, ext)
def test_should_list_tag(self): e_box = Box('simple-box', './uname.yml', 'hostname', '0.0.0.0') self.provider.load_tags(e_box) self.assertEqual(e_box.name in self.provider.tags, True) # the tag 'echo' is not returned since Ansible introduced dynamic includes # might be fixed in some next 2.x version # self.assertEqual(self.provider.tags[e_box.name], ['one', 'echo']) self.assertEqual(self.provider.tags[e_box.name], ['one'])
def define_box(self): ip = None ext = DOExt() if io.input_yes_no('register an existing droplet'): droplet_id = io.input_value('droplet id') droplet_info = self.manager.show_droplet(droplet_id) ext.id = droplet_id name = droplet_info['name'] created = droplet_info['created_at'] print ('\nFound droplet \'{0}\' (created {1})'.format(name, created)) ip = io.xstr(droplet_info['ip_address']) print ('IP: %s' % ip) ext.image = droplet_info['image']['id'] print ('Image: %s' % ext.image) ext.size = droplet_info['size']['slug'] print ('Size: %s' % ext.size) ext.region = droplet_info['region']['slug'] print ('Region: %s\n' % ext.region) playbook = io.input_path('playbook path') hostname = self.fetch_box_hosts(playbook) user = io.input_value('remote user', self.active_user) else: playbook = io.input_path('playbook path') hostname = self.fetch_box_hosts(playbook) name = io.input_value('box name', self.suggest_name(hostname)) user = io.input_value('remote user', self.active_user) all_images = self.manager.all_images() print ('\nAvailable images: \n%s' % self._print_object_id_name(all_images)) default_image = next((img for img in all_images if img['slug'] and self.DEFAULT_IMAGE_SLUG in img['slug']), None) if default_image: image_desc = '{0} - {1} {2}'.format( default_image['id'], default_image['distribution'], default_image['name'] ) ext.image = io.input_value('image', default_image['id'], image_desc) else: ext.image = io.input_value('image') all_sizes = self.manager.sizes() sizes_slug = [o['slug'] for o in all_sizes] print ('\nAvailable sizes: \n%s' % '\n'.join(sizes_slug)) ext.size = io.input_choice('size', self.DEFAULT_SIZE_SLUG, choices=sizes_slug) all_regions = self.manager.all_regions() regions_slug = [o['slug'] for o in all_regions] print ('\nAvailable regions: \n%s' % '\n'.join(regions_slug)) ext.region = io.input_choice('region', self.DEFAULT_REGION_SLUG, choices=regions_slug) ext.keys = self._input_ssh_keys() return Box(name, playbook, hostname, ip, user, extra=ext)
def test_phoenix_tags_are_propagated(self): expected_box_name = 'do-box' expected_box = Box(expected_box_name, './uname.yml', 'localbox-host', '127.0.0.1') self.mockedProvider.get_box.return_value = expected_box self.cli.do_phoenix('%s tag1 tag2' % expected_box_name) self.mockedProvider.rebuild.assert_called_with(expected_box) self.mockedProvider.provision.assert_called_with( expected_box, ['tag1', 'tag2'])
def define_box(self): playbook = io.input_path('playbook path') hostname = self.fetch_box_hosts(playbook) name = io.input_value('box name', self.suggest_name(hostname)) ip = io.input_value('instance address or inventory') user = io.input_value('remote user', self.active_user) pwd = io.input_value( 'password for the remote user', default_description='ssh key', mandatory=False, hidden=True ) return Box(name, playbook, hostname, ip, user, pwd)
def redefine_box(self, previous_box): playbook = io.input_path('playbook path', previous_box.playbook) hostname = self.fetch_box_hosts(playbook) ip = io.input_value('internal IP', previous_box.ip) ext = VagrantExt() mem = io.input_value('amount of RAM in GB', previous_box.extra.mem / 1024) ext.set_mem(mem * 1024) ext.set_shares(self._input_shares()) ext.set_image(previous_box.extra.image) ext.set_provider(previous_box.extra.provider) return Box(previous_box.name, playbook, hostname, ip, self.DEFAULT_USER, self.DEFAULT_PWD, ext)
def test_provision_sample_task(self): r_box = Box('local-testbox', './prudentia_vars.yml', 'tasks-host', '127.0.0.1') self.provider.add_box(r_box) self.provider.provision(r_box, []) self.assertEqual(self.provider.provisioned, True) self.provider.provision(r_box, ['uname']) self.assertEqual(self.provider.provisioned, True) self.provider.provision(r_box, ['vaultid']) self.assertEqual(self.provider.provisioned, True) self.provider.remove_box(r_box)
def test_reconfigure(self): r_box = Box('reconf-testbox', './uname.yml', 'localbox-host', '127.0.0.1') self.provider.add_box(r_box) def input_side_effect(*args): input_values = {'playbook path': './prudentia_vars.yml'} return input_values[args[0]] with patch('prudentia.utils.io.input_value') as iv: iv.side_effect = input_side_effect self.provider.reconfigure(r_box) box = self.provider.get_box('reconf-testbox') self.assertNotEqual(box, None) self.assertTrue('prudentia_vars.yml' in box.playbook)
def test_create_tasks_box(self): ext = VagrantExt() ext.set_mem(1024) ext.set_shares([]) ext.set_image('img') ext.set_provider('provider') box = Box('vagrant-testbox', self.tests_path + '/../examples/boxes/tasks.yml', 'tasks-host', '10.10.0.23', VagrantProvider.DEFAULT_USER, VagrantProvider.DEFAULT_PWD, ext) # The Vagrant add_box will invoke Vagrant as well, we're only interested in the generated Vagrantfile SimpleProvider.add_box(self.provider, box) self.provider._generate_vagrant_file() self.assertTrue('vm.define' in self._read_vagrant_file()) SimpleProvider.remove_box(self.provider, box) self.provider._generate_vagrant_file() self.assertFalse('vm.define' in self._read_vagrant_file())
def redefine_box(self, previous_box): playbook = io.input_path('playbook path', previous_box.playbook) hostname = self.fetch_box_hosts(playbook) ip = io.input_value('instance address or inventory', previous_box.ip) user = io.input_value('remote user', previous_box.remote_user) if previous_box.remote_pwd: pwd = io.input_value( 'password for the remote user', default_value=previous_box.remote_pwd, default_description='*****', mandatory=False, hidden=True ) else: pwd = io.input_value( 'password for the remote user', default_description='ssh key', mandatory=False, hidden=True ) return Box(previous_box.name, playbook, hostname, ip, user, pwd)
def redefine_box(self, previous_box): playbook = io.input_path('playbook path', previous_box.playbook) hostname = self.fetch_box_hosts(playbook) ip = previous_box.ip user = io.input_value('remote user', previous_box.remote_user) if not previous_box.extra.id: ext = DOExt() ext.id = previous_box.extra.id all_images = self.manager.all_images() print ('\nAvailable images: \n%s' % self._print_object_id_name(all_images)) prev_image = next((img for img in all_images if previous_box.extra.image == img['id']), None) if prev_image: prev_image_desc = '{0} - {1} {2}'.format( prev_image['id'], prev_image['distribution'], prev_image['name'] ) ext.image = io.input_value('image', prev_image['id'], prev_image_desc) else: ext.image = io.input_value('image') all_sizes = self.manager.sizes() sizes_slug = [o['slug'] for o in all_sizes] print ('\nAvailable sizes: \n%s' % '\n'.join(sizes_slug)) ext.size = io.input_value('size', previous_box.extra.size) all_regions = self.manager.all_regions() regions_slug = [o['slug'] for o in all_regions] print ('\nAvailable regions: \n%s' % '\n'.join(regions_slug)) ext.region = io.input_value('region', previous_box.extra.region) ext.keys = self._input_ssh_keys(previous_box.extra.keys) else: ext = previous_box.extra return Box(previous_box.name, playbook, hostname, ip, user, extra=ext)
def test_redefine_box(self): expected_box = Box(name='ssh-test-box', playbook=path.join(self.tests_path, 'uname.yml'), hostname='localhost', ip='4.3.2.1', remote_user='******', remote_pwd='xxx') def input_side_effect(*args, **kwargs): input_values = { 'playbook path': 'uname.yml', 'box name': expected_box.name, 'instance address or inventory': '4.3.2.1', 'remote user': '******', 'password for the remote user': '******' } return input_values[args[0]] with patch('prudentia.utils.io.input_value') as iv: iv.side_effect = input_side_effect self.cli.do_reconfigure(expected_box.name) self.assertEqual(self.cli.provider.get_box(expected_box.name), expected_box)
def test_should_not_list_tags_if_box_not_exists(self): ne_box = Box('simple-box-2', 'xxx.yml', 'ssh-hostname', '0.0.0.0') self.provider.load_tags(ne_box) self.assertEqual(ne_box.name in self.provider.tags, False)
def test_gather_facts(self): box = Box('simple-box', './uname.yml', 'hostname', './facts_hosts') self.assertTrue(self.provider.facts(box, '*mb'))
def setUp(self): self.env = Environment(id_env='env-test') self.test_box = Box('box-name', 'uname.yml', 'box-host', '0.0.0.0') self.env.add(self.test_box)
def test_add_existing_name(self): b2 = Box('box-name', 'dev2', 'box-hostname', '1.2.3.4') self.assertRaises(ValueError, self.env.add, b2)
def define_box(self): playbook = io.input_path('playbook path') hostname = self.fetch_box_hosts(playbook) name = io.input_value('box name', self.suggest_name(hostname)) return Box(name, playbook, hostname, '127.0.0.1')
def redefine_box(self, previous_box): playbook = io.input_path('playbook path', previous_box.playbook) hostname = self.fetch_box_hosts(playbook) return Box(previous_box.name, playbook, hostname, previous_box.ip)