def test__string_representation_includes_only_system_id_and_hostname(self): node = nodes.Node({ "system_id": make_name_without_spaces("system-id"), "hostname": make_name_without_spaces("hostname"), }) self.assertThat(repr(node), Equals( "<Node hostname=%(hostname)r system_id=%(system_id)r>" % node._data))
def make_Event_dict(): return { "id": next(event_ids), "type": make_name_without_spaces("event-type"), "node": random.randint(1, 99), "hostname": make_name_without_spaces("host"), "level": random.choice(list(events.Level)), "created": datetime.utcnow().strftime("%a, %d %b. %Y %H:%M:%S"), "description": make_name_without_spaces("description"), }
def test__string_representation_includes_only_system_id_and_hostname(self): machine = machines.Machine({ "system_id": make_name_without_spaces("system-id"), "hostname": make_name_without_spaces("hostname"), }) self.assertThat( repr(machine), Equals("<Machine hostname=%(hostname)r system_id=%(system_id)r>" % machine._data))
def test__string_representation_includes_field_values(self): class Example(Object): alice = ObjectField("alice") bob = ObjectField("bob") example = Example({ "alice": make_name_without_spaces("alice"), "bob": make_name_without_spaces("bob"), }) self.assertThat(repr(example), Equals( "<Example alice=%(alice)r bob=%(bob)r>" % example._data))
def test_sign_adds_header(self): token_key = make_name_without_spaces("token-key") token_secret = make_name_without_spaces("token-secret") consumer_key = make_name_without_spaces("consumer-key") headers = {} utils.sign('http://example.com/', headers, ( consumer_key, token_key, token_secret)) self.assertIn('Authorization', headers) authorization = headers["Authorization"] self.assertIn('realm="OAuth"', authorization) self.assertIn('oauth_token="%s"' % token_key, authorization) self.assertIn('oauth_consumer_key="%s"' % consumer_key, authorization) self.assertIn('oauth_signature="%%26%s"' % token_secret, authorization)
def test__string_representation_can_be_limited_to_selected_fields(self): class Example(Object): alice = ObjectField("alice") bob = ObjectField("bob") example = Example({ "alice": make_name_without_spaces("alice"), "bob": make_name_without_spaces("bob"), }) # A string repr can be prepared using only the "alice" field. self.assertThat( example.__repr__(fields={"alice"}), Equals( "<Example alice=%(alice)r>" % example._data)) # Fields are always displayed in a stable order though. self.assertThat( example.__repr__(fields=["bob", "alice"]), Equals( "<Example alice=%(alice)r bob=%(bob)r>" % example._data))
def test_OAuthSigner_sign_request_adds_header(self): token_key = make_name_without_spaces("token-key") token_secret = make_name_without_spaces("token-secret") consumer_key = make_name_without_spaces("consumer-key") consumer_secret = make_name_without_spaces("consumer-secret") realm = make_name_without_spaces("realm") headers = {} auth = utils.OAuthSigner( token_key=token_key, token_secret=token_secret, consumer_key=consumer_key, consumer_secret=consumer_secret, realm=realm) auth.sign_request('http://example.com/', "GET", None, headers) self.assertIn('Authorization', headers) authorization = headers["Authorization"] self.assertIn('realm="%s"' % realm, authorization) self.assertIn('oauth_token="%s"' % token_key, authorization) self.assertIn('oauth_consumer_key="%s"' % consumer_key, authorization) self.assertIn('oauth_signature="%s%%26%s"' % ( consumer_secret, token_secret), authorization)
def test__fields(self): v_major = randrange(2, 99) v_point1 = randrange(0, 99) v_point2 = randrange(0, 99) v_tuple = v_major, v_point1, v_point2 v_string = "%d.%d.%d" % v_tuple v_sub = make_name_without_spaces("subv") v_caps = [ make_name_without_spaces("cap") for _ in range(randrange(10)) ] vsn = version.Version({ "version": v_string, "subversion": v_sub, "capabilities": v_caps, }) self.assertThat(vsn.version, Equals(v_string)) self.assertThat(vsn.version_info, Equals(v_tuple)) self.assertThat(vsn.subversion, Equals(v_sub)) self.assertThat(vsn.capabilities, Equals(frozenset(v_caps)))
def test__query_arguments_are_assembled_and_passed_to_bones_handler(self): obj = bind(events.Events) arguments = { "hostnames": ( make_name_without_spaces("hostname"), make_name_without_spaces("hostname"), ), "domains": ( make_name_without_spaces("domain"), make_name_without_spaces("domain"), ), "zones": ( make_name_without_spaces("zone"), make_name_without_spaces("zone"), ), "macs": ( make_mac_address(), make_mac_address(), ), "system_ids": ( make_name_without_spaces("system-id"), make_name_without_spaces("system-id"), ), "agent_name": make_name_without_spaces("agent"), "level": random.choice(list(events.Level)), "limit": random.randrange(1, 1000), } obj.query(**arguments) expected = { "hostname": list(arguments["hostnames"]), "domain": list(arguments["domains"]), "zone": list(arguments["zones"]), "mac_address": list(arguments["macs"]), "id": list(arguments["system_ids"]), "agent_name": [arguments["agent_name"]], "level": [arguments["level"].name], "limit": [str(arguments["limit"])], } obj._handler.query.assert_called_once_with(**expected)
def test__init_sets__data(self): data = {"alice": make_name_without_spaces("alice")} self.assertThat(Object(data)._data, Equals(data))
def make_profile(): return Profile(name=make_name_without_spaces("name"), url="http://example.com:5240/", credentials=make_credentials(), description={"resources": []}, something=make_name_without_spaces("something"))
def test__membership_can_be_tested(self): item1 = make_name_without_spaces("item") item2 = make_name_without_spaces("item") objectset = ObjectSet([item1]) self.assertThat(objectset, Contains(item1)) self.assertThat(objectset, Not(Contains(item2)))
def test__init_sets__items_from_iterable(self): items = [{"alice": make_name_without_spaces("alice")}] self.assertThat(ObjectSet(iter(items))._items, Equals(items))
def test__iteration_yield_items(self): items = [make_name_without_spaces(str(index)) for index in range(5)] objectset = ObjectSet(items) self.assertThat(list(objectset), Equals(items))
def test__reversed_yields_items_in_reverse(self): items = [make_name_without_spaces(str(index)) for index in range(5)] objectset = ObjectSet(items) self.assertThat(list(reversed(objectset)), Equals(items[::-1]))
def test__can_be_indexed(self): items = [make_name_without_spaces(str(index)) for index in range(5)] objectset = ObjectSet(items) for index, item in enumerate(items): self.assertThat(objectset[index], Equals(item))
def test__can_be_sliced(self): items = [make_name_without_spaces(str(index)) for index in range(5)] objectset1 = ObjectSet(items) objectset2 = objectset1[1:3] self.assertThat(objectset2, IsInstance(ObjectSet)) self.assertThat(list(objectset2), Equals(items[1:3]))
def test__profile_is_given_default_name_based_on_URL(self): domain = make_name_without_spaces("domain") profile = login.login("http://%s/MAAS/" % domain) self.assertThat(profile.name, Equals(domain))
def make_profile(): return Profile( name=make_name_without_spaces("name"), url="http://example.com:5240/", credentials=make_credentials(), description={"resources": []}, something=make_name_without_spaces("something"))