def test_json_product_list(self): """ When listing product endpoints, the resulting JSON should contain a service object, and a workspace object, and within that, an array of product descriptors. """ cf = cloudfeeds.CloudFeeds(tenant_id='1234', clock=None) cf.register_product(title="The hoohaw product", href="hoohaw") cf.register_product(title="The goober product", href="goober") listing = MatchesDict({ "service": MatchesDict({ "workspace": MatchesSetwise( MatchesDict({ "collection": MatchesDict({ "href": Equals("hoohaw"), "title": Equals("The hoohaw product"), }), "title": Equals("The hoohaw product"), }), MatchesDict({ "collection": MatchesDict({ "href": Equals("goober"), "title": Equals("The goober product"), }), "title": Equals("The goober product"), }), ), }) }) self.assertEquals( listing.match(cloudfeeds.render_product_endpoints_dict( cf.get_product_endpoints() )), None )
def test_json_description(self): """ When listing product endpoints, we expect our JSON to look a certain way. The easiest way to do that is to acquire the corresponding dict, then pass it through json.dump with your preferred formatting settings. """ cfp = cloudfeeds.CloudFeedsProduct(title='title', href='href') d = cloudfeeds.render_product_dict(cfp) productDescription = MatchesDict({ "title": Equals("title"), "collection": MatchesDict({ "href": Equals("href"), "title": Equals("title"), }), }) self.assertEquals(productDescription.match(d), None)
def test_oauth_headers(self): now = time.time() is_about_now = MatchesAll( GreaterThanOrEqual(int(now)), LessThanOrEqual(int(now) + 3), ) url = factory.make_name("url") consumer_key = factory.make_name("consumer_key") token_key = factory.make_name("token_key") token_secret = factory.make_name("token_secret") consumer_secret = factory.make_name("consumer_secret") headers = maas_api_helper.oauth_headers(url, consumer_key, token_key, token_secret, consumer_secret) authorization = headers['Authorization'] self.assertRegex(authorization, '^OAuth .*') authorization = authorization.replace('OAuth ', '') oauth_arguments = {} for argument in authorization.split(', '): key, value = argument.split('=') oauth_arguments[key] = value.replace('"', '') self.assertIn('oauth_nonce', oauth_arguments) oauth_arguments.pop('oauth_nonce', None) self.assertThat( oauth_arguments, MatchesDict({ 'oauth_timestamp': AfterPreprocessing(int, is_about_now), 'oauth_version': Equals('1.0'), 'oauth_signature_method': Equals('PLAINTEXT'), 'oauth_consumer_key': Equals(consumer_key), 'oauth_token': Equals(token_key), 'oauth_signature': Equals("%s%%26%s" % (consumer_secret, token_secret)), }))
def test_rpc_info_when_rpc_advertise_running(self): region = factory.make_RegionController() self.useFixture(MAASIDFixture(region.system_id)) region.owner = factory.make_admin() region.save() self.useFixture(RegionEventLoopFixture("rpc", "rpc-advertise")) eventloop.start().wait(5) self.addCleanup(lambda: eventloop.reset().wait(5)) getServiceNamed = eventloop.services.getServiceNamed @wait_for(5) @inlineCallbacks def wait_for_startup(): # Wait for the rpc and the rpc-advertise services to start. yield getServiceNamed("rpc").starting yield getServiceNamed("rpc-advertise").starting # Force an update, because it's very hard to track when the # first iteration of the rpc-advertise service has completed. yield getServiceNamed("rpc-advertise")._tryUpdate() wait_for_startup() response = self.client.get(reverse('rpc-info')) self.assertEqual("application/json", response["Content-Type"]) info = json.loads(response.content.decode("unicode_escape")) self.assertThat(info, KeysEqual("eventloops")) self.assertThat( info["eventloops"], MatchesDict({ # Each entry in the endpoints dict is a mapping from an # event loop to a list of (host, port) tuples. Each tuple is # a potential endpoint for connecting into that event loop. eventloop.loop.name: MatchesSetwise(*(MatchesListwise((Equals(addr), is_valid_port)) for addr in get_all_interface_addresses() if not IPAddress(addr).is_link_local() and not IPAddress(addr).is_loopback())), }))
def test_yields_internal_forward_zones(self): default_domain = Domain.objects.get_default_domain() subnet = factory.make_Subnet(cidr=str(IPNetwork("10/29").cidr)) domains = [] for _ in range(3): record = InternalDomainResourseRecord( rrtype="A", rrdata=factory.pick_ip_in_Subnet(subnet)) resource = InternalDomainResourse( name=factory.make_name("resource"), records=[record]) domain = InternalDomain( name=factory.make_name("domain"), ttl=random.randint(15, 300), resources=[resource], ) domains.append(domain) zones = ZoneGenerator( [], [subnet], serial=random.randint(0, 65535), internal_domains=domains, ).as_list() self.assertThat( zones, MatchesSetwise(*[ MatchesAll( forward_zone(domain.name), MatchesStructure(_other_mapping=MatchesDict({ domain.resources[0].name: MatchesStructure(rrset=MatchesSetwise( Equals(( domain.ttl, domain.resources[0].records[0].rrtype, domain.resources[0].records[0].rrdata, )))) })), ) for domain in domains ] + [ reverse_zone(default_domain.name, "10/29"), reverse_zone(default_domain.name, "10/24"), ]), )
def test_activity_rule_removed(self): owner = self.factory.makeTeam() member = self.factory.makePerson(member_of=[owner]) repository = self.factory.makeGitRepository(owner=owner) rule = self.factory.makeGitRule(repository=repository, ref_pattern="refs/heads/*") with person_logged_in(member): rule.destroySelf(member) self.assertThat( repository.getActivity().first(), MatchesStructure(repository=Equals(repository), changer=Equals(member), changee=Is(None), what_changed=Equals(GitActivityType.RULE_REMOVED), old_value=MatchesDict({ "ref_pattern": Equals("refs/heads/*"), "position": Equals(0), }), new_value=Is(None)))
def test_activity_rule_added(self): owner = self.factory.makeTeam() member = self.factory.makePerson(member_of=[owner]) repository = self.factory.makeGitRepository(owner=owner) self.factory.makeGitRule(repository=repository, creator=member) self.factory.makeGitRule(repository=repository, ref_pattern="refs/heads/stable/*", creator=member) self.assertThat( repository.getActivity().first(), MatchesStructure(repository=Equals(repository), changer=Equals(member), changee=Is(None), what_changed=Equals(GitActivityType.RULE_ADDED), old_value=Is(None), new_value=MatchesDict({ "ref_pattern": Equals("refs/heads/stable/*"), "position": Equals(1), })))
def test_oauth_headers(self): now = time.time() is_about_now = MatchesAll(GreaterThanOrEqual(int(now)), LessThanOrEqual(int(now) + 3)) url = factory.make_name("url") consumer_key = factory.make_name("consumer_key") token_key = factory.make_name("token_key") token_secret = factory.make_name("token_secret") consumer_secret = factory.make_name("consumer_secret") headers = maas_api_helper.oauth_headers(url, consumer_key, token_key, token_secret, consumer_secret) authorization = headers["Authorization"] self.assertRegex(authorization, "^OAuth .*") authorization = authorization.replace("OAuth ", "") oauth_arguments = {} for argument in authorization.split(", "): key, value = argument.split("=") oauth_arguments[key] = value.replace('"', "") self.assertIn("oauth_nonce", oauth_arguments) oauth_arguments.pop("oauth_nonce", None) self.assertThat( oauth_arguments, MatchesDict({ "oauth_timestamp": AfterPreprocessing(int, is_about_now), "oauth_version": Equals("1.0"), "oauth_signature_method": Equals("PLAINTEXT"), "oauth_consumer_key": Equals(consumer_key), "oauth_token": Equals(token_key), "oauth_signature": Equals("%s%%26%s" % (consumer_secret, token_secret)), }), )
def test__node_not_None(self): event = factory.make_Event() self.assertThat( event_to_dict(event), MatchesDict({ "username": Equals(event.owner), "node": Equals(event.node.system_id), "hostname": Equals(event.hostname), "id": Equals(event.id), "level": Equals(event.type.level_str), "created": Equals(event.created.strftime('%a, %d %b. %Y %H:%M:%S')), "type": Equals(event.type.description), "description": Equals(event.description), }))
def MatchesNotification(notification): """Match the expected JSON rendering of `notification`.""" return MatchesDict( { "id": Equals(notification.id), "ident": Equals(notification.ident), "user": ( Is(None) if notification.user_id is None else ContainsDict( {"username": Equals(notification.user.username)} ) ), "users": Is(notification.users), "admins": Is(notification.admins), "message": Equals(notification.message), "context": Equals(notification.context), "category": Equals(notification.category), "dismissable": Equals(notification.dismissable), "resource_uri": Equals(get_notification_uri(notification)), } )
def test_checksumming_tree_signed_with_external_run_parts(self): # Checksum files can be signed using an external run-parts helper. # We disable subprocess.call because there's just too much going on, # so we can't test this completely, but we can at least test that # run_parts is called. self.enableRunParts(distribution_name=self.distro.name) run_parts_fixture = self.useFixture( MonkeyPatch("lp.archivepublisher.archivesigningkey.run_parts", FakeMethod())) self.setUpUefiKeys() self.setUpKmodKeys() self.setUpOpalKeys() self.openArchive("test", "1.0", "amd64") self.tarfile.add_file("1.0/empty.efi", "") self.tarfile.add_file("1.0/empty.ko", "") self.tarfile.add_file("1.0/empty.opal", "") self.process_emulate() sha256file = os.path.join(self.getSignedPath("test", "amd64"), "1.0", "SHA256SUMS") self.assertTrue(os.path.exists(sha256file)) self.assertEqual(1, run_parts_fixture.new_value.call_count) args, kwargs = run_parts_fixture.new_value.calls[-1] self.assertEqual((self.distro.name, "sign.d"), args) self.assertThat( kwargs["env"], MatchesDict({ "ARCHIVEROOT": Equals(os.path.join(self.temp_dir, self.distro.name)), "INPUT_PATH": Equals(sha256file), "OUTPUT_PATH": Equals("%s.gpg" % sha256file), "MODE": Equals("detached"), "DISTRIBUTION": Equals(self.distro.name), "SUITE": Equals(self.suite), }))
def MatchesRenderedNotification(ntfn): """Return a matcher for a rendered notification.""" return MatchesDict({ "id": Equals(ntfn.id), "ident": Is(None) if ntfn.ident is None else Equals(ntfn.ident), "user": Is(None) if ntfn.user_id is None else Equals(ntfn.user_id), "users": Is(ntfn.users), "admins": Is(ntfn.admins), "updated": Equals(dehydrate_datetime(ntfn.updated)), "created": Equals(dehydrate_datetime(ntfn.created)), "message": Equals(ntfn.render()), "category": Equals(ntfn.category), })
def assertInvalidConfigurationSetting(self, name, response): self.addDetail( "Response for op={get,set}_config&name=%s" % name, text_content( response.serialize().decode(settings.DEFAULT_CHARSET) ), ) self.expectThat( response, MatchesAll( # An HTTP 400 response, MatchesStructure(status_code=Equals(http.client.BAD_REQUEST)), # with a JSON body, AfterPreprocessing( itemgetter("Content-Type"), Equals("application/json") ), # containing a serialised ValidationError. AfterPreprocessing( lambda response: json.loads( response.content.decode(settings.DEFAULT_CHARSET) ), MatchesDict( { name: MatchesListwise( [ DocTestMatches( name + " is not a valid config setting " "(valid settings are: ...)." ) ] ) } ), ), first_only=True, ), )
def test_sign_with_external_run_parts(self): self.enableRunParts(distribution_name=self.distro.name) archiveroot = getPubConfig(self.archive).archiveroot filename = os.path.join(archiveroot, "file") write_file(filename, "contents") self.assertIsNone(self.archive.signing_key) run_parts_fixture = self.useFixture( MonkeyPatch("lp.archivepublisher.archivesigningkey.run_parts", FakeMethod())) custom_processor = CustomUpload() custom_processor.sign(self.archive, "suite", filename) args, kwargs = run_parts_fixture.new_value.calls[0] self.assertEqual((self.distro.name, "sign.d"), args) self.assertThat( kwargs["env"], MatchesDict({ "ARCHIVEROOT": Equals(archiveroot), "INPUT_PATH": Equals(filename), "OUTPUT_PATH": Equals("%s.gpg" % filename), "MODE": Equals("detached"), "DISTRIBUTION": Equals(self.distro.name), "SUITE": Equals("suite"), }))
def test_type_level_AUDIT(self): event = factory.make_Event() self.assertThat( event_to_dict(event), MatchesDict({ "username": Equals(event.owner), "node": Equals(event.node.system_id), "hostname": Equals(event.hostname), "id": Equals(event.id), "level": Equals(event.type.level_str), "created": Equals(event.created.strftime("%a, %d %b. %Y %H:%M:%S")), "type": Equals(event.type.description), "description": Equals(event.render_audit_description), }), )
def test_separate_eliot_log(self, base_test_case): """ Flocker base test cases attach the eliot log as a detail separate from the Twisted log. """ # XXX: If debugging is enabled (either by setting this to True or by # removing this line and running --debug-stacktraces, then the log # fixtures in this test are empty. However, if we run a failing test # manually, the logs appear in the details. Not sure what's going on, # so disabling debugging for now. self.useFixture(DebugTwisted(False)) message_type = MessageType(u'foo', fields(name=str), u'test message') class SomeTest(base_test_case): def test_something(self): from twisted.python import log log.msg('foo') message_type(name='qux').write() test = SomeTest('test_something') result = TestResult() test.run(result) self.expectThat(result, has_results(tests_run=Equals(1))) self.assertThat( test.getDetails(), MatchesDict({ 'twisted-log': match_text_content( MatchesRegex( r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}[+-]\d{4} \[-\] foo' )), _SplitEliotLogs._ELIOT_LOG_DETAIL_NAME: match_text_content( Contains(" message_type: 'foo'\n" " name: 'qux'\n")), }))
async def test__obtains_credentials_from_server(self): builder = ApplicationBuilder(self.description) @builder.handle("anon:Version.read") async def version(request): return {"capabilities": ["authenticate-api"]} credentials = make_Credentials() parameters = None @builder.route("POST", "/accounts/authenticate/") async def deploy(request): nonlocal parameters parameters = await request.post() return { "consumer_key": credentials.consumer_key, "token_key": credentials.token_key, "token_secret": credentials.token_secret, } username = make_name_without_spaces("username") password = make_name_without_spaces("password") async with builder.serve() as baseurl: credentials_observed = await helpers.authenticate( baseurl, username, password) self.assertThat(credentials_observed, Equals(credentials)) self.assertThat( parameters, MatchesDict({ "username": Equals(username), "password": Equals(password), "consumer": IsInstance(str), }), )
def test__node_and_user_is_None(self): event = factory.make_Event() event.node = None event.user = None self.assertThat( event_to_dict(event), MatchesDict({ "username": Equals(event.username), "node": Equals(None), "hostname": Equals(event.node_hostname), "id": Equals(event.id), "level": Equals(event.type.level_str), "created": Equals(event.created.strftime('%a, %d %b. %Y %H:%M:%S')), "type": Equals(event.type.description), "description": Equals(event.description), }))
def test_extraBuildArgs_git_private(self): # extraBuildArgs returns appropriate arguments if asked to build a # job for a private Git branch. self.useFixture(FeatureFixture({SNAP_PRIVATE_FEATURE_FLAG: "on"})) self.useFixture(InProcessAuthServerFixture()) self.pushConfig("launchpad", internal_macaroon_secret_key="some-secret") [ ref ] = self.factory.makeGitRefs(information_type=InformationType.USERDATA) job = self.makeJob(git_ref=ref, private=True) expected_archives, expected_trusted_keys = ( yield get_sources_list_for_building(job.build, job.build.distro_arch_series, None)) args = yield job.extraBuildArgs() split_browse_root = urlsplit(config.codehosting.git_browse_root) self.assertThat( args, MatchesDict({ "archive_private": Is(False), "archives": Equals(expected_archives), "arch_tag": Equals("i386"), "build_source_tarball": Is(False), "build_url": Equals(canonical_url(job.build)), "fast_cleanup": Is(True), "git_repository": AfterPreprocessing( urlsplit, MatchesStructure( scheme=Equals(split_browse_root.scheme), username=Equals(""), password=AfterPreprocessing( Macaroon.deserialize, MatchesStructure( location=Equals( config.vhost.mainsite.hostname), identifier=Equals("snap-build"), caveats=MatchesListwise([ MatchesStructure.byEquality( caveat_id="lp.snap-build %s" % job.build.id), ]))), hostname=Equals(split_browse_root.hostname), port=Equals(split_browse_root.port))), "git_path": Equals(ref.name), "name": Equals("test-snap"), "private": Is(True), "proxy_url": self.getProxyURLMatcher(job), "revocation_endpoint": self.getRevocationEndpointMatcher(job), "series": Equals("unstable"), "trusted_keys": Equals(expected_trusted_keys), }))
def test_create_snapshot(self, author, folder_name, path_in_folder, some_content): """ A **POST** to **/v1/snapshot/:folder-name** with a **path** query argument creates a new local snapshot for the file at the given path in the named folder. """ local_path = FilePath(self.mktemp()) local_path.makedirs() some_file = local_path.preauthChild(path_in_folder).asBytesMode( "utf-8") some_file.parent().makedirs(ignoreExistingDirectory=True) some_file.setContent(some_content) treq = treq_for_folders( Clock(), FilePath(self.mktemp()), AUTH_TOKEN, { folder_name: magic_folder_config(author, FilePath(self.mktemp()), local_path) }, # Unlike test_wait_for_completion above we start the folder # services. This will allow the local snapshot to be created and # our request to receive a response. start_folder_services=True, ) self.assertThat( authorized_request( treq, AUTH_TOKEN, b"POST", self.url.child(folder_name).set(u"path", path_in_folder), ), succeeded(matches_response(code_matcher=Equals(CREATED), ), ), ) self.assertThat( authorized_request( treq, AUTH_TOKEN, b"GET", self.url, ), succeeded( matches_response( code_matcher=Equals(OK), headers_matcher=header_contains({ u"Content-Type": Equals([u"application/json"]), }), body_matcher=AfterPreprocessing( loads, MatchesDict({ folder_name: MatchesDict({ path_in_folder: MatchesListwise([ MatchesDict({ u"type": Equals(u"local"), u"identifier": is_hex_uuid(), # XXX It would be nice to see some # parents if there are any. u"parents": Equals([]), u"content-path": AfterPreprocessing( lambda path: FilePath(path). getContent(), Equals(some_content), ), u"author": Equals(author.to_remote_author(). to_json()), }), ]), }), }), ), ), ), )
def _test_error_traceback(case, traceback_matcher): """Match result log of single test that errored out. ``traceback_matcher`` is applied to the text of the traceback. """ return MatchesListwise([ Equals(('startTest', case)), MatchesListwise([ Equals('addError'), Equals(case), MatchesDict({ 'traceback': AfterPreprocessing( lambda x: x.as_text(), traceback_matcher, ) }) ]), Equals(('stopTest', case)), ]) """ A list that can be used with testscenarios to test every deterministic sample case that we have. """ deterministic_sample_cases_scenarios = multiply_scenarios( _make_behavior_scenarios('set_up'), _make_behavior_scenarios('body'),
def test_rpc_info_empty(self): response = self.client.get(reverse("rpc-info")) self.assertEqual("application/json", response["Content-Type"]) info = json.loads(response.content.decode("unicode_escape")) self.assertThat(info, KeysEqual("eventloops")) self.assertThat(info["eventloops"], MatchesDict({}))
def test_compose_enlistment_preseed(self): rack_controller = factory.make_RackController() url = factory.make_simple_http_url() request = make_HttpRequest() apt_proxy = get_apt_proxy(request, rack_controller) preseed = yaml.safe_load( compose_enlistment_preseed( request, rack_controller, { "metadata_enlist_url": url, "syslog_host_port": url }, )) self.assertDictEqual({"MAAS": { "metadata_url": url }}, preseed["datasource"]) self.assertTrue(preseed["manage_etc_hosts"]) self.assertDictEqual({"remotes": {"maas": url}}, preseed["rsyslog"]) self.assertDictEqual( { "delay": "now", "mode": "poweroff", "timeout": 1800, "condition": "test ! -e /tmp/block-poweroff", }, preseed["power_state"], ) self.assertItemsEqual( [ "python3-yaml", "python3-oauthlib", "freeipmi-tools", "ipmitool", "sshpass", "archdetect-deb", "jq", ], preseed["packages"], ) default = PackageRepository.get_main_archive().url ports = PackageRepository.get_ports_archive().url self.assertThat( preseed, ContainsDict({ "apt": ContainsDict({ "preserve_sources_list": Equals(False), "primary": MatchesListwise([ MatchesDict({ "arches": Equals(["amd64", "i386"]), "uri": Equals(default), }), MatchesDict({ "arches": Equals(["default"]), "uri": Equals(ports), }), ]), "proxy": Equals(apt_proxy), "security": MatchesListwise([ MatchesDict({ "arches": Equals(["amd64", "i386"]), "uri": Equals(default), }), MatchesDict({ "arches": Equals(["default"]), "uri": Equals(ports), }), ]), }) }), )
def test_setGrants_add(self): owner = self.factory.makeTeam() member = self.factory.makePerson(member_of=[owner]) rule = self.factory.makeGitRule(owner=owner) grantee = self.factory.makePerson() removeSecurityProxy(rule.repository.getActivity()).remove() with person_logged_in(member): rule.setGrants([ IGitNascentRuleGrant({ "grantee_type": GitGranteeType.REPOSITORY_OWNER, "can_create": True, "can_force_push": True, }), IGitNascentRuleGrant({ "grantee_type": GitGranteeType.PERSON, "grantee": grantee, "can_push": True, }), ], member) self.assertThat( rule.grants, MatchesSetwise( MatchesStructure(rule=Equals(rule), grantor=Equals(member), grantee_type=Equals( GitGranteeType.REPOSITORY_OWNER), grantee=Is(None), can_create=Is(True), can_push=Is(False), can_force_push=Is(True)), MatchesStructure(rule=Equals(rule), grantor=Equals(member), grantee_type=Equals(GitGranteeType.PERSON), grantee=Equals(grantee), can_create=Is(False), can_push=Is(True), can_force_push=Is(False)))) self.assertThat( list(rule.repository.getActivity()), MatchesListwise([ MatchesStructure(repository=Equals(rule.repository), changer=Equals(member), changee=Equals(grantee), what_changed=Equals( GitActivityType.GRANT_ADDED), old_value=Is(None), new_value=MatchesDict({ "changee_type": Equals("Person"), "ref_pattern": Equals(rule.ref_pattern), "can_create": Is(False), "can_push": Is(True), "can_force_push": Is(False), })), MatchesStructure(repository=Equals(rule.repository), changer=Equals(member), changee=Is(None), what_changed=Equals( GitActivityType.GRANT_ADDED), old_value=Is(None), new_value=MatchesDict({ "changee_type": Equals("Repository owner"), "ref_pattern": Equals(rule.ref_pattern), "can_create": Is(True), "can_push": Is(False), "can_force_push": Is(True), })), ]))
def assertSystemInfo(self, config): self.assertThat( config, ContainsDict( { "system_info": MatchesDict( { "package_mirrors": MatchesListwise( [ MatchesDict( { "arches": Equals( ["i386", "amd64"] ), "search": MatchesDict( { "primary": Equals( [ PackageRepository.get_main_archive().url ] ), "security": Equals( [ PackageRepository.get_main_archive().url ] ), } ), "failsafe": MatchesDict( { "primary": Equals( "http://archive.ubuntu.com/ubuntu" ), "security": Equals( "http://security.ubuntu.com/ubuntu" ), } ), } ), MatchesDict( { "arches": Equals(["default"]), "search": MatchesDict( { "primary": Equals( [ PackageRepository.get_ports_archive().url ] ), "security": Equals( [ PackageRepository.get_ports_archive().url ] ), } ), "failsafe": MatchesDict( { "primary": Equals( "http://ports.ubuntu.com/ubuntu-ports" ), "security": Equals( "http://ports.ubuntu.com/ubuntu-ports" ), } ), } ), ] ) } ) } ), )
def test_setGrants_remove(self): owner = self.factory.makeTeam() members = [ self.factory.makePerson(member_of=[owner]) for _ in range(2) ] rule = self.factory.makeGitRule(owner=owner) grantees = [self.factory.makePerson() for _ in range(2)] self.factory.makeGitRuleGrant(rule=rule, grantee=GitGranteeType.REPOSITORY_OWNER, grantor=members[0], can_create=True) self.factory.makeGitRuleGrant(rule=rule, grantee=grantees[0], grantor=members[0], can_push=True) self.factory.makeGitRuleGrant(rule=rule, grantee=grantees[1], grantor=members[0], can_force_push=True) date_created = get_transaction_timestamp(Store.of(rule)) transaction.commit() removeSecurityProxy(rule.repository.getActivity()).remove() with person_logged_in(members[1]): rule.setGrants([ IGitNascentRuleGrant({ "grantee_type": GitGranteeType.PERSON, "grantee": grantees[0], "can_push": True, }), ], members[1]) self.assertThat( rule.grants, MatchesSetwise( MatchesStructure(rule=Equals(rule), grantor=Equals(members[0]), grantee_type=Equals(GitGranteeType.PERSON), grantee=Equals(grantees[0]), can_create=Is(False), can_push=Is(True), can_force_push=Is(False), date_created=Equals(date_created), date_last_modified=Equals(date_created)))) self.assertThat( list(rule.repository.getActivity()), MatchesSetwise( MatchesStructure(repository=Equals(rule.repository), changer=Equals(members[1]), changee=Is(None), what_changed=Equals( GitActivityType.GRANT_REMOVED), old_value=MatchesDict({ "changee_type": Equals("Repository owner"), "ref_pattern": Equals(rule.ref_pattern), "can_create": Is(True), "can_push": Is(False), "can_force_push": Is(False), }), new_value=Is(None)), MatchesStructure(repository=Equals(rule.repository), changer=Equals(members[1]), changee=Equals(grantees[1]), what_changed=Equals( GitActivityType.GRANT_REMOVED), old_value=MatchesDict({ "changee_type": Equals("Person"), "ref_pattern": Equals(rule.ref_pattern), "can_create": Is(False), "can_push": Is(False), "can_force_push": Is(True), }), new_value=Is(None)), ))
def test_pod_asdict(self): hostname = factory.make_name("hostname") cores = random.randint(1, 8) cpu_speed = random.randint(1000, 2000) memory = random.randint(4096, 8192) local_storage = random.randint(4096, 8192) local_disks = random.randint(1, 8) iscsi_storage = random.randint(4096, 8192) hints = DiscoveredPodHints( cores=random.randint(1, 8), cpu_speed=random.randint(1000, 2000), memory=random.randint(4096, 8192), local_storage=random.randint(4096, 8192), local_disks=random.randint(1, 8), iscsi_storage=random.randint(4096, 8192), ) machines = [] tags = [factory.make_name("tag") for _ in range(3)] storage_pools = [ DiscoveredPodStoragePool( id=factory.make_name("id"), name=factory.make_name("name"), storage=random.randint(4096, 8192), type="dir", path="/var/%s" % factory.make_name("dir"), ) for _ in range(3) ] for _ in range(3): cores = random.randint(1, 8) cpu_speed = random.randint(1000, 2000) memory = random.randint(4096, 8192) power_state = factory.make_name("unknown") power_parameters = {"power_id": factory.make_name("power_id")} interfaces = [ DiscoveredMachineInterface( mac_address=factory.make_mac_address()) for _ in range(3) ] block_devices = [ DiscoveredMachineBlockDevice( model=factory.make_name("model"), serial=factory.make_name("serial"), size=random.randint(512, 1024), id_path=factory.make_name("/dev/vda"), ) for _ in range(3) ] for _ in range(3): block_devices.append( DiscoveredMachineBlockDevice( model=None, serial=None, size=random.randint(512, 1024), type=BlockDeviceType.ISCSI, iscsi_target=self.make_iscsi_target(), storage_pool=factory.make_name("pool"), )) tags = [factory.make_name("tag") for _ in range(3)] machines.append( DiscoveredMachine( hostname=hostname, architecture="amd64/generic", cores=cores, cpu_speed=cpu_speed, memory=memory, power_state=power_state, power_parameters=power_parameters, interfaces=interfaces, block_devices=block_devices, tags=tags, )) pod = DiscoveredPod( architectures=["amd64/generic"], cores=cores, cpu_speed=cpu_speed, memory=memory, local_storage=local_storage, local_disks=local_disks, iscsi_storage=iscsi_storage, hints=hints, machines=machines, tags=tags, storage_pools=storage_pools, ) self.assertThat( pod.asdict(), MatchesDict({ "architectures": Equals(["amd64/generic"]), "name": Equals(None), "cores": Equals(cores), "cpu_speed": Equals(cpu_speed), "memory": Equals(memory), "local_storage": Equals(local_storage), "local_disks": Equals(local_disks), "iscsi_storage": Equals(iscsi_storage), "mac_addresses": Equals([]), "capabilities": Equals(pod.capabilities), "hints": MatchesDict({ "cores": Equals(hints.cores), "cpu_speed": Equals(hints.cpu_speed), "memory": Equals(hints.memory), "local_storage": Equals(hints.local_storage), "local_disks": Equals(hints.local_disks), "iscsi_storage": Equals(hints.iscsi_storage), }), "machines": MatchesListwise([ MatchesDict({ "hostname": Equals(machine.hostname), "architecture": Equals("amd64/generic"), "cores": Equals(machine.cores), "cpu_speed": Equals(machine.cpu_speed), "memory": Equals(machine.memory), "power_state": Equals(machine.power_state), "power_parameters": Equals(machine.power_parameters), "interfaces": MatchesListwise([ MatchesDict({ "mac_address": Equals(interface.mac_address), "vid": Equals(interface.vid), "tags": Equals(interface.tags), "boot": Equals(False), "attach_type": Is(None), "attach_name": Is(None), }) for interface in machine.interfaces ]), "block_devices": MatchesListwise([ MatchesDict({ "model": Equals(block_device.model), "serial": Equals(block_device.serial), "size": Equals(block_device.size), "block_size": Equals(block_device.block_size), "tags": Equals(block_device.tags), "id_path": Equals(block_device.id_path), "type": Equals(block_device.type), "iscsi_target": Equals(block_device.iscsi_target), "storage_pool": Equals(block_device.storage_pool), }) for block_device in machine.block_devices ]), "tags": Equals(machine.tags), }) for machine in machines ]), "tags": Equals(tags), "storage_pools": MatchesListwise([ MatchesDict({ "id": Equals(pool.id), "name": Equals(pool.name), "storage": Equals(pool.storage), "type": Equals(pool.type), "path": Equals(pool.path), }) for pool in storage_pools ]), }), )
# ordinarily get squashed. return Failure( amp.UnknownRemoteError( "%s: %s" % (reflect.qual(error.type), reflect.safe_str(error.value)))) d.addErrback(eb_massage_error) return d are_valid_tls_parameters = MatchesDict({ "tls_localCertificate": IsInstance(ssl.PrivateCertificate), "tls_verifyAuthorities": MatchesAll( IsInstance(collections.Sequence), AllMatch(IsInstance(ssl.Certificate)), ), }) class MockClusterToRegionRPCFixtureBase(fixtures.Fixture, metaclass=ABCMeta): """Patch in a stub region RPC implementation to enable end-to-end testing. This is an abstract base class. Derive concrete fixtures from this by implementing the `connect` method. """ starting = None stopping = None
def test_compose_enlistment_preseed(self): rack_controller = factory.make_RackController() url = factory.make_simple_http_url() request = make_HttpRequest() apt_proxy = get_apt_proxy(request, rack_controller) preseed = yaml.safe_load( compose_enlistment_preseed(request, rack_controller, { 'metadata_enlist_url': url, 'syslog_host_port': url, })) self.assertDictEqual({'MAAS': { 'metadata_url': url }}, preseed['datasource']) self.assertTrue(preseed['manage_etc_hosts']) self.assertDictEqual({'remotes': {'maas': url}}, preseed['rsyslog']) self.assertDictEqual( { 'delay': 'now', 'mode': 'poweroff', 'timeout': 1800, 'condition': 'test ! -e /tmp/block-poweroff', }, preseed['power_state']) self.assertItemsEqual([ 'python3-yaml', 'python3-oauthlib', 'freeipmi-tools', 'ipmitool', 'sshpass', 'archdetect-deb', 'jq' ], preseed['packages']) default = PackageRepository.get_main_archive().url ports = PackageRepository.get_ports_archive().url self.assertThat( preseed, ContainsDict({ 'apt': ContainsDict({ 'preserve_sources_list': Equals(False), 'primary': MatchesListwise([ MatchesDict({ "arches": Equals(["amd64", "i386"]), "uri": Equals(default), }), MatchesDict({ "arches": Equals(["default"]), "uri": Equals(ports), }), ]), 'proxy': Equals(apt_proxy), 'security': MatchesListwise([ MatchesDict({ "arches": Equals(["amd64", "i386"]), "uri": Equals(default), }), MatchesDict({ "arches": Equals(["default"]), "uri": Equals(ports), }), ]), }) }))
def test_delegates_to_agent_for_location(self, agent): """ When a request is made using the agent, the added agents are delegated to based on the URI location/authority. """ agent.add_agent(b'foo:8080', DummyAgent()) agent.add_agent(b'bar:8080', FailingAgent(RuntimeError('bar'))) agent.add_agent(b'foo:9090', FailingAgent(RuntimeError('9090'))) d = agent.request(b'GET', b'http://foo:8080') assert_that( d, succeeded( MatchesListwise([ MatchesListwise( [Equals(b'GET'), Equals(b'http://foo:8080')]), MatchesDict({ 'headers': Is(None), 'bodyProducer': Is(None) }) ]))) # Scheme doesn't matter d = agent.request(b'GET', b'https://foo:8080') assert_that( d, succeeded( MatchesListwise([ MatchesListwise( [Equals(b'GET'), Equals(b'https://foo:8080')]), MatchesDict({ 'headers': Is(None), 'bodyProducer': Is(None) }) ]))) # Path doesn't matter d = agent.request(b'GET', b'http://foo:8080/bar/baz') assert_that( d, succeeded( MatchesListwise([ MatchesListwise( [Equals(b'GET'), Equals(b'http://foo:8080/bar/baz')]), MatchesDict({ 'headers': Is(None), 'bodyProducer': Is(None) }) ]))) # Hostname *does* matter d = agent.request(b'GET', b'http://bar:8080') assert_that( d, failed( MatchesStructure( value=MatchesAll(IsInstance(RuntimeError), MatchesPredicate(str, Equals('bar')))))) # Port *does* matter d = agent.request(b'GET', b'http://foo:9090') assert_that( d, failed( MatchesStructure( value=MatchesAll(IsInstance(RuntimeError), MatchesPredicate(str, Equals('9090')))))) # Other args passed through d = agent.request(b'GET', b'http://foo:8080', 'bar', 'baz') assert_that( d, succeeded( MatchesListwise([ MatchesListwise( [Equals(b'GET'), Equals(b'http://foo:8080')]), MatchesDict({ 'headers': Equals('bar'), 'bodyProducer': Equals('baz') }) ])))
def test_machine_asdict(self): hostname = factory.make_name("hostname") cores = random.randint(1, 8) cpu_speed = random.randint(1000, 2000) memory = random.randint(4096, 8192) interfaces = [RequestedMachineInterface() for _ in range(3)] known_host_interfaces = [KnownHostInterface() for _ in range(3)] block_devices = [ RequestedMachineBlockDevice( size=random.randint(512, 1024), tags=[factory.make_name("tag") for _ in range(3)], ) for _ in range(3) ] machine = RequestedMachine( hostname=hostname, architecture="amd64/generic", cores=cores, cpu_speed=cpu_speed, memory=memory, interfaces=interfaces, known_host_interfaces=known_host_interfaces, block_devices=block_devices, ) self.assertThat( machine.asdict(), MatchesDict({ "hostname": Equals(hostname), "architecture": Equals("amd64/generic"), "cores": Equals(cores), "cpu_speed": Equals(cpu_speed), "memory": Equals(memory), "interfaces": MatchesListwise([ MatchesDict({ "ifname": Is(None), "requested_ips": Equals([]), "ip_mode": Is(None), "attach_name": Equals(interface.attach_name), "attach_type": Equals(interface.attach_type), "attach_options": Equals(interface.attach_options), }) for interface in interfaces ]), "known_host_interfaces": MatchesListwise([ MatchesDict({ "ifname": Equals(known_host_interface.ifname), "attach_type": Equals(known_host_interface.attach_type), "dhcp_enabled": Equals(False), }) for known_host_interface in known_host_interfaces ]), "block_devices": MatchesListwise([ MatchesDict({ "size": Equals(block_device.size), "tags": Equals(block_device.tags), }) for block_device in block_devices ]), }), )
def test__read(self): resource_id = random.randint(0, 100) rtype = random.choice(["Synced", "Uploaded", "Generated"]) name = "%s/%s" % (make_name_without_spaces("os"), make_name_without_spaces("release")) architecture = "%s/%s" % (make_name_without_spaces("arch"), make_name_without_spaces("subarch")) subarches = ",".join( make_name_without_spaces("subarch") for _ in range(3)) sets = {} for _ in range(3): version = make_name_without_spaces("version") files = {} for _ in range(3): filename = make_name_without_spaces("filename") files[filename] = { "filename": filename, "filetype": make_name_without_spaces("filetype"), "size": random.randint(1000, 10000), "sha256": make_name_without_spaces("sha256"), "complete": pick_bool(), } sets[version] = { "version": version, "size": random.randint(1000, 10000), "label": make_name_without_spaces("label"), "complete": pick_bool(), "files": files, } BootResource = make_origin().BootResource BootResource._handler.read.return_value = { "id": resource_id, "type": rtype, "name": name, "architecture": architecture, "subarches": subarches, "sets": sets } resource = BootResource.read(resource_id) BootResource._handler.read.assert_called_once_with(id=resource_id) self.assertThat( resource, MatchesStructure( id=Equals(resource_id), type=Equals(rtype), name=Equals(name), architecture=Equals(architecture), subarches=Equals(subarches), sets=MatchesDict({ version: MatchesStructure( version=Equals(version), size=Equals(rset["size"]), label=Equals(rset["label"]), complete=Equals(rset["complete"]), files=MatchesDict({ filename: MatchesStructure( filename=Equals(filename), filetype=Equals(rfile["filetype"]), size=Equals(rfile["size"]), sha256=Equals(rfile["sha256"]), complete=Equals(rfile["complete"]), ) for filename, rfile in rset["files"].items() }), ) for version, rset in sets.items() })))