Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom create_tag method to ec2 resource #160

Merged
merged 3 commits into from
Jul 14, 2015

Conversation

kyleknap
Copy link
Contributor

The create tag method cannot be expressed via the resource model because you need to map a set of tags to multiple resources possibly and return a list of each tag for each resource. In order to preserve backward compatibility a new custom method needed to be added to the service resource.

Fixes: #137

cc @jamesls @mtdowling

creating multiple tag resources based on the fact you can apply a set
of tags to multiple ec2 resources.
"""
if event_name == 'creating-resource-class.ec2':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this if statement necessary since this function must always be plugged into the event system using a specific event name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this isn't needed is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it is needed. The creation of the service resource emits 'creating-resource-class.ec2' while other resources like Instance emits 'creating-resource-class.ec2.Instance'. If I did not have the check, my custom create_tags() method would be added to every resource due to how the event system works.

The only other option would be to add a 'creating-resource-class.ec2.ServiceResource' event when creating the service resource. Thoughts?

@mtdowling
Copy link
Contributor

LGTM

@jamesls
Copy link
Member

jamesls commented Jul 6, 2015

Will the doc system just automatically work with this customization because it's we're replacing an existing method on a resource?

Assuming the docs are good, and the unneeded if check for the event is removed, :shipit:

@kyleknap
Copy link
Contributor Author

kyleknap commented Jul 6, 2015

The docs are fine. The way that the doc system works is that it checks if a method name matches an action name in the resource model. If it does match, it will autogenerate docs for that method and in this case, the custom create_tags method matches what is in the resource model so its docs get autogenerated.

The create tag method cannot be expressed via the resource model because
you need to map a set of tag to multiple resources possibly and return
a list of each tag for each resource. In order to preserve backward
combatability a new custom method needed to be added to the service
resource.
Updated create_tags method as well to use the new event name. This also
updated the service resource class name to what it is documented as.
@kyleknap
Copy link
Contributor Author

@jamesls @mtdowling

I updated the event name for the service resource to make it more usable. Also the class name for the service resource was updated along with the create_tags method. Let me know what you think.

class TestCreateTagsInjection(unittest.TestCase):
def test_create_tags_injected_to_resource(self):
session = boto3.session.Session(region_name='us-west-2')
with mock.patch('boto3.ec2.createtags.create_tags') as mock_method:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels too much like testing implementation details for a functional test. If we move this method or rename it, this test will fail.

If we want something higher level than a unit test, I'd prefer an integration test.

@jamesls
Copy link
Member

jamesls commented Jul 10, 2015

The event rename looks good. I would prefer we don't have the functional test or switch it to an integration test. Otherwise looks good.

@kyleknap
Copy link
Contributor Author

@jamesls
I guess that I can switch the test out of that category. I want to keep that test though because I want to ensure that the custom method gets applied to the service resource. There are no tests for that.

I am leaning toward against adding an integration test for this because in order to reproduce the bug I need to spin up more than one ec2 resources, apply tags, check the tags are there, and then clean them up. It seems that this would be testing the service more than the actual functionality of the customization which is already covered by the unit test. In the end, we just need to make sure the customization overrides the default method.

This also raises the question what is the criteria for what goes into the functional test suit? The functional test that was added for all intents and purposes was checking that the resource had the appropriate method, which is what a bunch of the tests in the functional test suite do. The only caveat was that patching was needed to determine if the resource had the appropriate method.

Is the criteria that to be in the functional test suite that no mock's or patches are used (unless you are mocking out the make_request call) and is testing the end to end functionality of the feature?

Thoughts?

@kyleknap
Copy link
Contributor Author

In the meantime, I made the change to switch the functional test to a unit test.

@jamesls
Copy link
Member

jamesls commented Jul 14, 2015

I think the changes look good for now. We can look at improvements in the future.

As for functional tests, I think of them as tests that operate at the API used by the customer (and on a technical level don't call out to AWS). The whole idea is to exercise end to end functionality and catch problems such as events not hooked up properly, wrong assumptions about file system access, customizations we make that don't actually work with real world JSON models we use, etc. The more that gets mocked/patched out, the less useful functional tests become for me.

You mentioned that are similar functional tests, but I think the differences between them are what make the difference for me. For example, other functional tests only care that upload_file was added to a client. They don't which upload_file method or how it was added, just that by the time you get a client, upload_file exists.
This gives us flexibility to alter the implementation as long as we make sure upload_file gets added. I get that it's not as simple in this test because we're replacing, not adding a method, so you have to check that it was replaced with a specific method via assertIs. That's why I feel here that an integ test would have been better.

FWIW, you could just create a resource that's cheap to create, like CreateVPC.

But either way, I think it's something we can look at later if this becomes an issue.

@kyleknap
Copy link
Contributor Author

Sounds good. Thanks for the clarification. Merging.

kyleknap added a commit that referenced this pull request Jul 14, 2015
Add custom create_tag method to ec2 resource
@kyleknap kyleknap merged commit 7740461 into boto:develop Jul 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants