def test_accepts_ttl_equals_none(self): name = factory.make_name("domain") ttl = random.randint(1, 604800) authoritative = factory.pick_bool() form = DomainForm({ "name": name, "authoritative": authoritative, "ttl": ttl }) self.assertTrue(form.is_valid(), form.errors) domain = form.save() form = DomainForm(instance=domain, data={"ttl": None}) self.assertTrue(form.is_valid(), form.errors) domain = form.save() self.assertEqual(name, domain.name) self.assertEqual(authoritative, domain.authoritative) self.assertEqual(None, domain.ttl)
def test_factory_make_DHCPSnippet_sets_iprange(self): name = factory.make_name("dhcp_snippet") value = VersionedTextFile.objects.create(data=factory.make_string()) description = factory.make_string() enabled = factory.pick_bool() subnet = factory.make_ipv4_Subnet_with_IPRanges() iprange = subnet.get_dynamic_ranges().first() iprange.save() dhcp_snippet = factory.make_DHCPSnippet( name, value, description, enabled, subnet=subnet, iprange=iprange ) self.assertEqual(name, dhcp_snippet.name) self.assertEqual(value.data, dhcp_snippet.value.data) self.assertEqual(description, dhcp_snippet.description) self.assertEqual(enabled, dhcp_snippet.enabled) self.assertEqual(subnet, dhcp_snippet.subnet) self.assertEqual(iprange, dhcp_snippet.iprange)
def test_add_user_with_external_auth_not_local(self): admin = factory.make_admin() # login before external auth is enabled to avoid requiring macaroons self.client.login(user=admin) Config.objects.set_config('external_auth_url', 'http://auth.example.com') params = { 'username': factory.make_string(), 'last_name': factory.make_string(30), 'email': factory.make_email_address(), 'is_superuser': factory.pick_bool(), } password = factory.make_string() params.update(make_password_params(password)) self.client.post(reverse('accounts-add'), params) user = User.objects.get(username=params['username']) self.assertFalse(user.userprofile.is_local)
def test_add_user_POST(self): self.client.login(user=factory.make_admin()) params = { 'username': factory.make_string(), 'last_name': factory.make_string(30), 'email': factory.make_email_address(), 'is_superuser': factory.pick_bool(), } password = factory.make_string() params.update(make_password_params(password)) response = self.client.post(reverse('accounts-add'), params) self.assertEqual(http.client.FOUND, response.status_code) user = User.objects.get(username=params['username']) self.assertAttributes(user, subset_dict(params, user_attributes)) self.assertTrue(user.check_password(password)) self.assertTrue(user.userprofile.is_local)
def test_cannt_create_dhcp_snippet_with_node_and_subnet(self): node = factory.make_Node() subnet = factory.make_Subnet() name = factory.make_name("name") value = factory.make_string() description = factory.make_string() enabled = factory.pick_bool() form = DHCPSnippetForm( data={ "name": name, "value": value, "description": description, "enabled": enabled, "node": node.system_id, "subnet": subnet.id, }) self.assertFalse(form.is_valid())
def test_POST_gets_name_from_filename(self): self.become_admin() name = factory.make_name('script') title = factory.make_name('title') description = factory.make_name('description') tags = [factory.make_name('tag') for _ in range(3)] script_type = factory.pick_choice(SCRIPT_TYPE_CHOICES) hardware_type = factory.pick_choice(HARDWARE_TYPE_CHOICES) parallel = factory.pick_choice(SCRIPT_PARALLEL_CHOICES) packages = {'apt': [factory.make_name('apt_pkg')]} timeout = random.randint(0, 1000) destructive = factory.pick_bool() script_content = factory.make_script_content() comment = factory.make_name('comment') response = self.client.post( self.get_scripts_uri(), { 'title': title, 'description': description, 'tags': ','.join(tags), 'type': script_type, 'hardware_type': hardware_type, 'parallel': parallel, 'packages': json.dumps(packages), 'timeout': timeout, 'destructive': destructive, 'comment': comment, name: factory.make_file_upload(name, script_content.encode()), }) self.assertThat(response, HasStatusCode(http.client.OK)) parsed_result = json_load_bytes(response.content) script = Script.objects.get(id=int(parsed_result['id'])) self.assertEquals(name, script.name) self.assertEquals(title, script.title) self.assertEquals(description, script.description) self.assertThat(script.tags, ContainsAll(tags)) self.assertEquals(script_type, script.script_type) self.assertEquals(hardware_type, script.hardware_type) self.assertEquals(parallel, script.parallel) self.assertDictEqual(packages, script.packages) self.assertEquals(timeout, script.timeout.seconds) self.assertEquals(destructive, script.destructive) self.assertEquals(script_content, script.script.data) self.assertEquals(comment, script.script.comment)
def test__fail_validation_on_create_cleans_value(self): node = factory.make_Node() subnet = factory.make_Subnet() name = factory.make_name('name') value = factory.make_string() description = factory.make_string() enabled = factory.pick_bool() form = DHCPSnippetForm(data={ 'name': name, 'value': value, 'description': description, 'enabled': enabled, 'node': node.system_id, 'subnet': subnet.id, }) self.assertFalse(form.is_valid()) self.assertItemsEqual([], DHCPSnippet.objects.all()) self.assertItemsEqual([], VersionedTextFile.objects.all())
def test__updates_domain(self): new_name = factory.make_name("domain") old_authoritative = factory.pick_bool() domain = factory.make_Domain(authoritative=old_authoritative) new_authoritative = not old_authoritative new_ttl = random.randint(0, 10000000) form = DomainForm(instance=domain, data={ "name": new_name, "authoritative": new_authoritative, "ttl": new_ttl, }) self.assertTrue(form.is_valid(), form.errors) form.save() domain = reload_object(domain) self.assertEqual(new_name, domain.name) self.assertEqual(new_authoritative, domain.authoritative) self.assertEqual(new_ttl, domain.ttl)
def test_instantiate_extra_fields(self): memory = 1024 * random.randint(1, 256) machine = factory.make_Machine() hugepages_backed = factory.pick_bool() vm = VirtualMachine( identifier="vm1", bmc=factory.make_BMC(power_type="lxd"), project="prj1", memory=memory, machine=machine, hugepages_backed=hugepages_backed, ) vm.save() self.assertEqual(vm.unpinned_cores, 0) self.assertEqual(vm.pinned_cores, []) self.assertEqual(vm.memory, memory) self.assertEqual(vm.hugepages_backed, hugepages_backed) self.assertIs(vm.machine, machine)
def test_create_dhcp_snippet_with_iprange_requires_subnet(self): subnet = factory.make_ipv4_Subnet_with_IPRanges() iprange = subnet.get_dynamic_ranges().first() iprange.save() name = factory.make_name("name") value = factory.make_string() description = factory.make_string() enabled = factory.pick_bool() form = DHCPSnippetForm( data={ "name": name, "value": value, "dscription": description, "enabled": enabled, "iprange": iprange.id, } ) self.assertFalse(form.is_valid(), form.errors)
def test_read_results(self): results = { 'status': random.choice( ['passed', 'failed', 'degraded', 'timedout']), 'results': { factory.make_name('key'): factory.make_name('value'), factory.make_name('key'): [ factory.make_name('value'), random.uniform(1, 2), random.randint(0, 1000), factory.pick_bool(), ] } } script_result = factory.make_ScriptResult( result=yaml.safe_dump(results).encode()) self.assertDictEqual(results, script_result.read_results())
def test__accepts_mix_of_id_and_ipaddress(self): name = factory.make_name("dnsresource") domain = factory.make_Domain() ips = [factory.make_StaticIPAddress() for _ in range(6)] in_vals = [ str(ip.id) if factory.pick_bool() else str(ip.ip) for ip in ips ] form = DNSResourceForm({ "name": name, "domain": domain.id, "ip_addresses": " ".join(in_vals), }) self.assertTrue(form.is_valid(), form.errors) dnsresource = form.save() self.assertEqual(name, dnsresource.name) self.assertEqual(domain.id, dnsresource.domain.id) actual = {ip.id for ip in dnsresource.ip_addresses.all()} self.assertItemsEqual(set(ip.id for ip in ips), actual)
def test_create_as_admin(self): admin_user = factory.make_admin() handler = UserHandler(admin_user, {}, None) params = { "username": factory.make_string(), "last_name": factory.make_string(30), "email": factory.make_email_address(), "is_superuser": factory.pick_bool(), } password = factory.make_string() params.update(make_password_params(password)) handler.create(params) user = User.objects.get(username=params["username"]) self.assertAttributes(user, subset_dict(params, user_attributes)) self.assertTrue(user.check_password(password)) self.assertTrue(user.userprofile.is_local)
def test__update(self): script = factory.make_Script() name = factory.make_name('name') title = factory.make_name('title') description = factory.make_name('description') tags = [factory.make_name('tag') for _ in range(3)] script_type = factory.pick_choice(SCRIPT_TYPE_CHOICES) timeout = random.randint(0, 1000) destructive = factory.pick_bool() script_content = factory.make_string() comment = factory.make_name('comment') orig_script_content = script.script.data form = ScriptForm(data={ 'name': name, 'title': title, 'description': description, 'tags': ','.join(tags), 'type': script_type, 'timeout': str(timeout), 'destructive': destructive, 'script': script_content, 'comment': comment, }, instance=script) self.assertTrue(form.is_valid(), form.errors) script = form.save() self.assertEquals(name, script.name) self.assertEquals(title, script.title) self.assertEquals(description, script.description) if script.destructive: tags.append('destructive') self.assertEquals(tags, script.tags) self.assertEquals(script_type, script.script_type) self.assertEquals(timedelta(0, timeout), script.timeout) self.assertEquals(destructive, script.destructive) self.assertEquals(script_content, script.script.data) self.assertEquals(comment, script.script.comment) self.assertEquals(orig_script_content, script.script.previous_version.data) self.assertEquals(None, script.script.previous_version.comment) self.assertFalse(script.default)
def test_fail_validation_on_create_cleans_value(self): node = factory.make_Node() subnet = factory.make_Subnet() name = factory.make_name("name") value = factory.make_string() description = factory.make_string() enabled = factory.pick_bool() form = DHCPSnippetForm( data={ "name": name, "value": value, "description": description, "enabled": enabled, "node": node.system_id, "subnet": subnet.id, }) self.assertFalse(form.is_valid()) self.assertItemsEqual([], DHCPSnippet.objects.all()) self.assertItemsEqual([], VersionedTextFile.objects.all())
def test_settings_third_party_drivers_POST(self): self.client.login(user=factory.make_admin()) new_enable_third_party_drivers = factory.pick_bool() response = self.client.post( reverse("settings_general"), get_prefixed_form_data( prefix="third_party_drivers", data={ "enable_third_party_drivers": (new_enable_third_party_drivers) }, ), ) self.assertEqual(http.client.FOUND, response.status_code) self.assertEqual( (new_enable_third_party_drivers, ), (Config.objects.get_config("enable_third_party_drivers"), ), )
def test_create(self): self.patch(dhcpsnippet, 'validate_dhcp_config').return_value = {} self.become_admin() name = factory.make_name('name') value = factory.make_string() description = factory.make_string() enabled = factory.pick_bool() response = self.client.post( self.get_dhcp_snippets_uri(), { 'name': name, 'value': value, 'description': description, 'enabled': enabled, }) parsed_dhcp_snippet = json.loads(response.content.decode()) self.assertEquals(name, parsed_dhcp_snippet['name']) self.assertEquals(value, parsed_dhcp_snippet['value']) self.assertEquals(description, parsed_dhcp_snippet['description']) self.assertEquals(enabled, parsed_dhcp_snippet['enabled'])
def test__creates_dhcp_snippet_with_node(self): node = factory.make_Node() name = factory.make_name('name') value = factory.make_string() description = factory.make_string() enabled = factory.pick_bool() form = DHCPSnippetForm(data={ 'name': name, 'value': value, 'description': description, 'enabled': enabled, 'node': node.system_id, }) self.assertTrue(form.is_valid(), form.errors) dhcp_snippet = form.save() self.assertEqual(value, dhcp_snippet.value.data) self.assertEqual(description, dhcp_snippet.description) self.assertEqual(enabled, dhcp_snippet.enabled) self.assertEqual(node, dhcp_snippet.node)
def test_read_results(self): results = { "status": random.choice( ["passed", "failed", "degraded", "timedout", "skipped"]), "results": { factory.make_name("key"): factory.make_name("value"), factory.make_name("key"): [ factory.make_name("value"), random.uniform(1, 2), random.randint(0, 1000), factory.pick_bool(), ], }, } script_result = factory.make_ScriptResult( result=yaml.safe_dump(results).encode()) self.assertDictEqual(results, script_result.read_results())
def test_settings_third_party_drivers_POST(self): self.client_log_in(as_admin=True) new_enable_third_party_drivers = factory.pick_bool() response = self.client.post( reverse('settings'), get_prefixed_form_data( prefix='third_party_drivers', data={ 'enable_third_party_drivers': ( new_enable_third_party_drivers), })) self.assertEqual(http.client.FOUND, response.status_code) self.assertEqual( ( new_enable_third_party_drivers, ), ( Config.objects.get_config('enable_third_party_drivers'), ))
def test_PUT(self): self.become_admin() script = factory.make_Script() name = factory.make_name('script') title = factory.make_name('title') description = factory.make_name('description') tags = [factory.make_name('tag') for _ in range(3)] script_type = factory.pick_choice(SCRIPT_TYPE_CHOICES) timeout = random.randint(0, 1000) destructive = factory.pick_bool() script_content = factory.make_string() comment = factory.make_name('comment') response = self.client.put( self.get_script_uri(script), { 'name': name, 'title': title, 'description': description, 'tags': ','.join(tags), 'type': script_type, 'timeout': timeout, 'destructive': destructive, 'script': factory.make_file_upload(content=script_content.encode()), 'comment': comment, }) self.assertThat(response, HasStatusCode(http.client.OK)) script = reload_object(script) self.assertEquals(name, script.name) self.assertEquals(title, script.title) self.assertEquals(description, script.description) if script.destructive: tags.append('destructive') self.assertItemsEqual(tags, script.tags) self.assertEquals(script_type, script.script_type) self.assertEquals(timeout, script.timeout.seconds) self.assertEquals(destructive, script.destructive) self.assertEquals(script_content, script.script.data) self.assertEquals(comment, script.script.comment) self.assertIsNotNone(script.script.previous_version)
def test__calls_start_testing_with_options(self): node = factory.make_Node(status=NODE_STATUS.DEPLOYED) user = factory.make_admin() enable_ssh = factory.pick_bool() testing_scripts = [ factory.make_Script(script_type=SCRIPT_TYPE.TESTING).name for _ in range(3) ] mock_start_testing = self.patch_autospec(node, "start_testing") form = TestForm(instance=node, user=user, data={ "enable_ssh": enable_ssh, 'testing_scripts': ','.join(testing_scripts), }) self.assertTrue(form.is_valid(), form.errors) node = form.save() self.assertIsNotNone(node) self.assertThat( mock_start_testing, MockCalledOnceWith(user, enable_ssh, testing_scripts, {}))
def test_create(self): self.patch(dhcpsnippet, "validate_dhcp_config").return_value = {} self.become_admin() name = factory.make_name("name") value = factory.make_string() description = factory.make_string() enabled = factory.pick_bool() response = self.client.post( self.get_dhcp_snippets_uri(), { "name": name, "value": value, "description": description, "enabled": enabled, }, ) parsed_dhcp_snippet = json.loads(response.content.decode()) self.assertEquals(name, parsed_dhcp_snippet["name"]) self.assertEquals(value, parsed_dhcp_snippet["value"]) self.assertEquals(description, parsed_dhcp_snippet["description"]) self.assertEquals(enabled, parsed_dhcp_snippet["enabled"])
def test__loads_yaml_embedded_attributes(self): embedded_yaml = { 'name': factory.make_name('name'), 'title': factory.make_name('title'), 'description': factory.make_name('description'), 'tags': [factory.make_name('tag') for _ in range(3)], 'script_type': factory.pick_choice(SCRIPT_TYPE_CHOICES), 'hardware_type': factory.pick_choice(HARDWARE_TYPE_CHOICES), 'parallel': factory.pick_choice(SCRIPT_PARALLEL_CHOICES), 'results': ['write_speed'], 'parameters': [{ 'type': 'storage' }, { 'type': 'runtime' }], 'packages': { 'apt': [factory.make_name('package')] }, 'timeout': random.randint(0, 1000), 'destructive': factory.pick_bool(), } script_content = factory.make_script_content(embedded_yaml) form = ScriptForm(data={'script': script_content}) self.assertTrue(form.is_valid(), form.errors) script = form.save() self.assertEquals(embedded_yaml['name'], script.name) self.assertEquals(embedded_yaml['title'], script.title) self.assertEquals(embedded_yaml['description'], script.description) self.assertThat(script.tags, ContainsAll(embedded_yaml['tags'])) self.assertEquals(embedded_yaml['script_type'], script.script_type) self.assertEquals(embedded_yaml['hardware_type'], script.hardware_type) self.assertEquals(embedded_yaml['parallel'], script.parallel) self.assertItemsEqual(embedded_yaml['results'], script.results) self.assertItemsEqual(embedded_yaml['parameters'], script.parameters) self.assertDictEqual(embedded_yaml['packages'], script.packages) self.assertEquals(timedelta(0, embedded_yaml['timeout']), script.timeout) self.assertEquals(embedded_yaml['destructive'], script.destructive) self.assertFalse(script.default) self.assertEquals(script_content, script.script.data)
def test_update(self): self.become_admin() authoritative = factory.pick_bool() domain = factory.make_Domain(authoritative=authoritative) new_name = factory.make_name("domain") new_ttl = random.randint(10, 1000) new_auth = not authoritative uri = get_domain_uri(domain) response = self.client.put( uri, {"name": new_name, "authoritative": new_auth, "ttl": new_ttl} ) self.assertEqual( http.client.OK, response.status_code, response.content ) ret = json.loads(response.content.decode(settings.DEFAULT_CHARSET)) domain = reload_object(domain) self.assertEqual(new_name, ret["name"]) self.assertEqual(new_name, domain.name) self.assertEqual(new_ttl, ret["ttl"]) self.assertEqual(new_ttl, domain.ttl) self.assertEqual(new_auth, ret["authoritative"]) self.assertEqual(new_auth, domain.authoritative)
def test_settings_storage_POST(self): self.client.login(user=factory.make_admin()) new_storage_layout = factory.pick_choice(get_storage_layout_choices()) new_enable_disk_erasing_on_release = factory.pick_bool() response = self.client.post( reverse('settings_storage'), get_prefixed_form_data(prefix='storage_settings', data={ 'default_storage_layout': new_storage_layout, 'enable_disk_erasing_on_release': (new_enable_disk_erasing_on_release), })) self.assertEqual(http.client.FOUND, response.status_code) self.assertEqual(( new_storage_layout, new_enable_disk_erasing_on_release, ), ( Config.objects.get_config('default_storage_layout'), Config.objects.get_config('enable_disk_erasing_on_release'), ))
def test_POST_gets_name_from_filename(self): self.become_admin() name = factory.make_name('script') title = factory.make_name('title') description = factory.make_name('description') tags = [factory.make_name('tag') for _ in range(3)] script_type = factory.pick_choice(SCRIPT_TYPE_CHOICES) timeout = random.randint(0, 1000) destructive = factory.pick_bool() script_content = factory.make_string() comment = factory.make_name('comment') response = self.client.post( self.get_scripts_uri(), { 'title': title, 'description': description, 'tags': ','.join(tags), 'type': script_type, 'timeout': timeout, 'destructive': destructive, 'comment': comment, name: factory.make_file_upload(name, script_content.encode()), }) self.assertThat(response, HasStatusCode(http.client.OK)) parsed_result = json_load_bytes(response.content) script = Script.objects.get(id=int(parsed_result['id'])) self.assertEquals(name, script.name) self.assertEquals(title, script.title) self.assertEquals(description, script.description) if script.destructive: tags.append('destructive') self.assertItemsEqual(tags, script.tags) self.assertEquals(script_type, script.script_type) self.assertEquals(timeout, script.timeout.seconds) self.assertEquals(destructive, script.destructive) self.assertEquals(script_content, script.script.data) self.assertEquals(comment, script.script.comment)
def test_creates_dhcp_snippet_with_node(self): node = factory.make_Node() name = factory.make_name("name") value = factory.make_string() description = factory.make_string() enabled = factory.pick_bool() form = DHCPSnippetForm( data={ "name": name, "value": value, "description": description, "enabled": enabled, "node": node.system_id, }) self.assertTrue(form.is_valid(), form.errors) endpoint = factory.pick_choice(ENDPOINT_CHOICES) request = HttpRequest() request.user = factory.make_User() dhcp_snippet = form.save(endpoint, request) self.assertEqual(value, dhcp_snippet.value.data) self.assertEqual(description, dhcp_snippet.description) self.assertEqual(enabled, dhcp_snippet.enabled) self.assertEqual(node, dhcp_snippet.node)
def test__creates_dhcp_snippet_with_subnet(self): subnet = factory.make_Subnet() name = factory.make_name('name') value = factory.make_string() description = factory.make_string() enabled = factory.pick_bool() form = DHCPSnippetForm( data={ 'name': name, 'value': value, 'description': description, 'enabled': enabled, 'subnet': subnet.id, }) self.assertTrue(form.is_valid(), form.errors) endpoint = factory.pick_choice(ENDPOINT_CHOICES) request = HttpRequest() request.user = factory.make_User() dhcp_snippet = form.save(endpoint, request) self.assertEqual(name, dhcp_snippet.name) self.assertEqual(value, dhcp_snippet.value.data) self.assertEqual(description, dhcp_snippet.description) self.assertEqual(enabled, dhcp_snippet.enabled) self.assertEqual(subnet, dhcp_snippet.subnet)
def test__create_with_defined_values(self): name = factory.make_name("name") title = factory.make_name("title") description = factory.make_name("description") tags = [factory.make_name("tag") for _ in range(3)] script_type = factory.pick_choice(SCRIPT_TYPE_CHOICES) hardware_type = factory.pick_choice(HARDWARE_TYPE_CHOICES) parallel = factory.pick_choice(SCRIPT_PARALLEL_CHOICES) packages = {"apt": [factory.make_name("package")]} timeout = random.randint(0, 1000) destructive = factory.pick_bool() script_content = factory.make_script_content() comment = factory.make_name("comment") may_reboot = factory.pick_bool() if script_type == SCRIPT_TYPE.COMMISSIONING: for_hardware = [ "modalias:%s" % factory.make_name("mod_alias"), "pci:%04X:%04x" % (random.randint(0, 9999), random.randint(0, 9999)), "usb:%04x:%04X" % (random.randint(0, 9999), random.randint(0, 9999)), "system_vendor:%s" % factory.make_name("system_name"), "system_product:%s" % factory.make_name("system_product"), "system_version:%s" % factory.make_name("system_version"), "mainboard_vendor:%s" % factory.make_name("mobo_vendor"), "mainboard_product:%s" % factory.make_name("mobo_product"), ] recommission = factory.pick_bool() else: for_hardware = [] recommission = False form = ScriptForm( data={ "name": name, "title": title, "description": description, "tags": ",".join(tags), "type": script_type, "hardware_type": hardware_type, "parallel": parallel, "packages": json.dumps(packages), "timeout": str(timeout), "destructive": destructive, "script": script_content, "comment": comment, "may_reboot": may_reboot, "for_hardware": ",".join(for_hardware), "recommission": recommission, }) self.assertTrue(form.is_valid(), form.errors) script = form.save() self.assertEquals(name, script.name) self.assertEquals(title, script.title) self.assertEquals(description, script.description) self.assertThat(script.tags, ContainsAll(tags)) self.assertEquals(script_type, script.script_type) self.assertEquals(hardware_type, script.hardware_type) self.assertEquals(parallel, script.parallel) self.assertDictEqual(packages, script.packages) self.assertDictEqual({}, script.results) self.assertDictEqual({}, script.parameters) self.assertDictEqual(packages, script.packages) self.assertEquals(timedelta(0, timeout), script.timeout) self.assertEquals(destructive, script.destructive) self.assertEquals(script_content, script.script.data) self.assertEquals(comment, script.script.comment) self.assertEquals(may_reboot, script.may_reboot) self.assertItemsEqual(for_hardware, script.for_hardware) self.assertEquals(recommission, script.recommission) self.assertFalse(script.default)