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

Skip tests affected by bug 695 #49

Merged
merged 2 commits into from
Dec 5, 2015
Merged

Skip tests affected by bug 695 #49

merged 2 commits into from
Dec 5, 2015

Conversation

Ichimonji10
Copy link
Contributor

Add a decorator for skipping tests, pulp_smash.utils.require. Use it to skip a test that is known to fail under Pulp 2.6 and older. See individual commit messages for more details.

@Ichimonji10
Copy link
Contributor Author

At this point, you have to manually configure Pulp Smash and tell it which version of Pulp you're targeting, and there's also no integration with the bug tracker at pulp.plan.io. But it's a start, and it demonstrates how to use the new "version" attribute on the pulp_smash.config.ServerConfig object.

@bmbouter
Copy link
Member

bmbouter commented Dec 4, 2015

This looks good. I do have one suggestion. Can a decorator be introduced? It would be like this:

     @requires('2.7')
     # 'https://pulp.plan.io/issues/695'
     def test_location_header(self):

Or maybe this:

     @requires('2.7', 'https://pulp.plan.io/issues/695')
     def test_location_header(self):

@Ichimonji10
Copy link
Contributor Author

Yes, we can do that. The problem with using decorators here is that they are evaluated when defined, but we don't want to be instantiating ServerConfig objects and inspecting their "version" attribute or possibly calling @skip until the tests are executed. As a consequence, the decorators are going to be complicated and bug-prone.

It's definitely doable. It's done in Robottelo, and @elyezer in particular has expertise with writing them. But I'm nervous about adopting something like that right away. This approach is not as concise, but it serves the lowest common denominator. My spidey senses tell me to do other things (like integrate with pulp.plan.io and expand the test suite) first.

@Ichimonji10
Copy link
Contributor Author

By the way, I do like the two-argument form you've given. I like the idea of requiring that users provide a bug URL in order to skip a test. It might also allow for some nice analytics down the line when we get good TCMS integration in place.

@bmbouter
Copy link
Member

bmbouter commented Dec 4, 2015

The complexity of introducing a decorator versus integrating with a web server are significantly different. Regarding making a decorator I could write one up and post it here if that is helpful.

I really think this is an idiom we want to include from the start. I'm not concerned if ServerConfigs get instantiated a whole lot. We could easily add a caching there so it's only read from disk the first time.

What do you think?

Also if we want to be sure it's a bug (which could be good to require) we could use it like:

@requires('2.7', 695)

@Ichimonji10
Copy link
Contributor Author

We could easily add a caching there so it's only read from disk the first time.

pulp_smash.config.get_config() already has caching, so instantiating lots of objects isn't an issue.

@Ichimonji10
Copy link
Contributor Author

What I'm much more worried about is that ServerConfig objects might be instantiated when each decorator is evaluated, and that they would persist until when each test is run. I'd strongly prefer if ServerConfig objects were instantiated as lazily as possible. There's no problem with the agressive instantiation approach at this time, but it invites unexpected behaviour should we ever need to do something wild like clear or edit the pulp_smash.config._CONFIG cache at run-time.

@Ichimonji10
Copy link
Contributor Author

But hey, if we can get a well-engineered decorator now - that'd be great. Please do put something together if you have a few minutes. 😄

@Ichimonji10
Copy link
Contributor Author

@bmbouter Actually, is there even any need to state the version at which a bug is fixed? We could just do something like this:

@skip_if_open(695)
def test_foo(self):
    pass

And at run-time, we can grab a ServerConfig object and fetch bug info from the server, and compare the version on the former with the "target release" attribute from the latter.

@dkliban
Copy link
Member

dkliban commented Dec 4, 2015

This would require integration with Redmine at this time. It would be advantageous to hold off on doing that integration till later.

@Ichimonji10
Copy link
Contributor Author

Why so? Integration is extremely easy.

@dkliban
Copy link
Member

dkliban commented Dec 4, 2015

Even though it is easy, it is still another step that we have to take before we can start skipping tests.

@Ichimonji10
Copy link
Contributor Author

We can skip tests right now!

@Ichimonji10
Copy link
Contributor Author

Basically, if we're going to go above and beyond serving the lowest common denominator, let's make sure what we come up with is orthogonal to what we already have.

@bmbouter
Copy link
Member

bmbouter commented Dec 4, 2015

Here is a simple decorator for it. Personally I think introducing the decorator is good to do now. More than redmine integration, I think all the core tools bugs need to be done. I like the integration, but I don't think it's blocking test writing. I'm still blocked on #31

def require(func, version_string):
    def new_func(self, *args, **kwargs):
        if self.cfg.version < Version(version_string):
            self.skipTest('Test requires version %s' % version_string)
        else:
            return func(self, *args, **kwargs)
    return new_func

@Ichimonji10
Copy link
Contributor Author

@bmbouter Concise. I like it.

@Ichimonji10
Copy link
Contributor Author

I'll add that as a second commit in this PR and ask for a re-review then.

This may come in handy later:

#!/usr/bin/env python
"""Fetch the target release of a bug.

This can fail in at least two obvious cases:

* There is an error while communicating with the server. (e.g. cannot be
  reached)
* There is no "target release" set on the bug.

In either case, an appropriately cautious response is to assume that the bug is
applicable and open.

"""
from __future__ import print_function

import requests


response = requests.get('https://pulp.plan.io/issues/695.json')
response.raise_for_status()
target_releases = [
    custom_field for custom_field in response.json()['issue']['custom_fields']
    if custom_field['id'] == 4  # 'name': 'Target Release'
]
assert len(target_releases) == 1
print(target_releases[0]['value'])

Fix #29, "Add mechanism to select tests based on Pulp version":

> Some tests are only applicable to a particular Python version. Let's add some
> mechanism that allows users to run only tests which apply to a particular Pulp
> version or a range of Pulp versions.

A new "version" attribute was recently added to
`pulp_smash.config.ServerConfig`. Build on top of that work by adding a new
decorator, `pulp_smash.utils.requires`. As its docstring states:

    This decorator concisely encapsulates a common pattern for skipping tests.
    It can be used like so:

    >>> from pulp_smash.config import get_config
    >>> from pulp_smash.utils import require
    >>> from unittest import TestCase
    >>> class MyTestCase(TestCase):
    ...
    ...     @classmethod
    ...     def setUpClass(cls):
    ...         cls.cfg = get_config()
    ...
    ...     @require('2.7')  # References `self.cfg`
    ...     def test_foo(self):
    ...         pass  # Add a test for Pulp 2.7+ here.

    Notice that ``cls.cfg`` is assigned to. This is a **requirement**.

Of special note is that the decorator will throw an exception if an invalid
version is passed. For example, this will throw an exception:

    @require('2.7.foobar')
    def test_foo(self):
        pass

Many thanks to @bmbouter for help with this change.
Skip test `CreateSuccessTestCase.test_location_header` in module
`pulp_smash.tests.platform.api_v2.test_repository` if Pulp 2.6 or older is
targeted.

Test suite results do not change when run against Pulp 2.7 (and the Pulp Smash
settings file is configured appropriately). This fixes two failing tests when
run against Pulp 2.6.

Test results before this change:

    $ PULP_SMASH_CONFIG_FILE=pulp-2.6.json python -m unittest2 \
    >   pulp_smash.tests.platform.api_v2.test_repository
    …
    Ran 11 tests in 1.947s

    FAILED (failures=8)

Test results with this change applied:

    $ PULP_SMASH_CONFIG_FILE=pulp-2.6.json python -m unittest2 \
    >   pulp_smash.tests.platform.api_v2.test_repository
    …
    Ran 11 tests in 1.961s

    FAILED (failures=6, skipped=1)
@Ichimonji10
Copy link
Contributor Author

Decorator usage boils down to this:

@require('2.7')  # https://pulp.plan.io/issues/695
def test_foo(self):
    pass

I implemented the decorator so as to fail if an invalid version string is passed. This will throw an exception:

@require('2.7.foobar')  # https://pulp.plan.io/issues/695
def test_foo(self):
    pass

@bmbouter
Copy link
Member

bmbouter commented Dec 5, 2015

@Ichimonji10 nice! I really like it.

@Ichimonji10
Copy link
Contributor Author

I've manually verified this change one more time. When the test suite is run
against Pulp 2.6, we've correctly removed two failures and added one skipped
test.

branch         results
-------------  ---------------------------------------
master         FAILED (failures=9, errors=1, skipped=5)
skip-failures  FAILED (failures=7, errors=1, skipped=6)

When the test suite is run against Pulp 2.7, nothing changes:

branch         results
-------------  ---------------------------------------
master         FAILED (failures=8, errors=1, skipped=5)
skip-failures  FAILED (failures=8, errors=1, skipped=5)

Merging changes now.

@Ichimonji10 Ichimonji10 merged commit c0b568a into pulp:master Dec 5, 2015
@Ichimonji10 Ichimonji10 deleted the skip-failures branch December 5, 2015 22:38
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.

None yet

3 participants