pytest-testinfra icon indicating copy to clipboard operation
pytest-testinfra copied to clipboard

Timezone test fixture?

Open codylane opened this issue 7 years ago • 0 comments

I'm wondering if anyone would benefit from from having a new test module for testing timezones for the systems under test?

Scenario

I built some new CentOS 7 machines and needed to set their timezones with ansible. This works fine, but I want to make sure those timezones stay in sync with my code so I wrote a fixture like this and placed it in conftest.py

@pytest.fixture
def TIMEZONE(host):
    def on_call(expected):
        date_cmd = host.run('date +%Z')
        assert date_cmd.rc == 0
        assert date_cmd.stdout.strip() == expected.strip()
    return on_call

Then I have a test that looks like this

def test_timezone_is_UTC(host, TIMEZONE):
    system_info = host.system_info
    distro = system_info.distribution.lower()

    if distro == 'centos':
        if system_info.release == '6':
            TIMEZONE('UTC2')
            return
        if system_info.release == '7':
            TIMEZONE('UTC')
            return

    raise NotImplementedError('No timezone tests for {host}:{release}'.format(host=distro, release=system_info.release))

I don't like to write tests like this because it adds complexity and breaks the simple test rule, but I do think that logic may be better inside a timezone module inside testinfra? I'd be happy to code it up and submit a pull request if anyone would agree it would help? Please let me know what you guys/gals think?

codylane avatar Aug 28 '18 21:08 codylane