Skip to content

Commit

Permalink
Artifacts cleanups + tests cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
mlkm committed Nov 22, 2017
1 parent 2973126 commit caa8c1b
Show file tree
Hide file tree
Showing 75 changed files with 1,863 additions and 639 deletions.
32 changes: 14 additions & 18 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
"""A module that configures the behaviour of pytest runner."""
import sys

import pytest

from grr.lib import flags
from grr.test_lib import testing_startup

SKIP_BENCHMARK = pytest.mark.skip(
reason="benchmark tests are executed only with --benchmark flag")

test_args = None


Expand Down Expand Up @@ -42,31 +47,22 @@ def pytest_runtest_setup(item):
last_module = item.module


# FIXME(hanuszczak): This is a direct port of the way the current `run_tests.py`
# script works. `pytest` has a concept of markers which is a more idiomatic way
# of handlings this. The test suite should be ported to use markers and this
# custom labelling mechanism should be scrapped.
def pytest_addoption(parser):
"""A pytest hook that is called during the argument parser initialization."""
parser.addoption(
"-L",
"--label",
type=str,
dest="labels",
default=[],
action="append",
help="run tests only with specified labels")
"-B",
"--benchmark",
dest="benchmark",
default=False,
action="store_true",
help="run tests marked as benchmarks")


def pytest_collection_modifyitems(session, config, items):
"""A pytest hook that is called when the test item collection is done."""
del session # Unused.
filtered_items = []

allowed_labels = set(config.getoption("labels") or ["small"])
benchmark = config.getoption("benchmark")
for item in items:
item_labels = set(getattr(item.cls, "labels", ["small"]))
if allowed_labels.intersection(item_labels):
filtered_items.append(item)

items[:] = filtered_items
if not benchmark and item.get_marker("benchmark"):
item.add_marker(SKIP_BENCHMARK)
2 changes: 1 addition & 1 deletion grr/artifacts/flow_templates/linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: LinuxUserProfiles
doc: |
Linux user profile information.
* recent logins in wtmp;
* recent logins in wtmp and utmp;
* user metadata in the output of getpwnam.
sources:
- type: GRR_CLIENT_ACTION
Expand Down
55 changes: 17 additions & 38 deletions grr/client/client_actions/action_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env python
# -*- mode: python; encoding: utf-8 -*-
"""Test client actions."""

import __builtin__
import collections
import glob
import os
import platform
import posix
import stat

Expand All @@ -14,6 +13,7 @@
from grr.client import actions
from grr.client import client_utils
from grr.client.client_actions import standard
from grr.client.client_actions.linux import linux

from grr.lib import flags
from grr.lib import utils
Expand All @@ -23,15 +23,6 @@
from grr.test_lib import client_test_lib
from grr.test_lib import test_lib

if platform.system() == "Linux":
# pylint: disable=g-import-not-at-top
# Trying to import this module on non-Linux platforms won't work.
from grr.client.client_actions.linux import linux
# pylint: enable=g-import-not-at-top


# pylint: mode=test


class ProgressAction(actions.ActionPlugin):
"""A mock action which just calls Progress."""
Expand Down Expand Up @@ -111,37 +102,21 @@ def testIteratedListDirectory(self):

def testEnumerateUsersLinux(self):
"""Enumerate users from the wtmp file."""
# Linux only
if platform.system() != "Linux":
return

path = os.path.join(self.base_path, "VFSFixture/var/log/wtmp")
old_open = __builtin__.open
old_listdir = os.listdir

# Mock the open call
def MockedOpen(requested_path, mode="rb"):
# Any calls to open the wtmp get the mocked out version.
if "wtmp" in requested_path:
self.assertEqual(requested_path, "/var/log/wtmp")
return old_open(path)

# Everything else has to be opened normally.
return old_open(requested_path, mode)

__builtin__.open = MockedOpen
os.listdir = lambda x: ["wtmp"]
try:
try:
fixture_path = os.path.join(self.base_path, "VFSFixture",
requested_path.lstrip("/"))
return __builtin__.open.old_target(fixture_path, mode)
except IOError:
return __builtin__.open.old_target(requested_path, mode)

with utils.MultiStubber((__builtin__, "open", MockedOpen),
(glob, "glob", lambda x: ["/var/log/wtmp"])):
results = self.RunAction(linux.EnumerateUsers)
finally:
# Restore the original methods.
__builtin__.open = old_open
os.listdir = old_listdir

found = 0
for result in results:
# This appears in ut_type RUN_LVL, not ut_type USER_PROCESS.
self.assertNotEqual("runlevel", result.username)
if result.username == "user1":
found += 1
self.assertEqual(result.last_logon, 1296552099 * 1000000)
Expand All @@ -151,6 +126,10 @@ def MockedOpen(requested_path, mode="rb"):
elif result.username == "user3":
found += 1
self.assertEqual(result.last_logon, 1296569997 * 1000000)
elif result.username == "utuser":
self.assertEqual(result.last_logon, 1510318881 * 1000000)
else:
self.fail("Unexpected user found: %s" % result.username)

self.assertEqual(found, 3)

Expand Down Expand Up @@ -247,8 +226,8 @@ def MockIsMount(path):
"""Only return True for the root path."""
return path == "/"

with utils.MultiStubber((os, "statvfs", MockStatFS), (os.path, "ismount",
MockIsMount)):
with utils.MultiStubber((os, "statvfs", MockStatFS),
(os.path, "ismount", MockIsMount)):

# This test assumes "/" is the mount point for /usr/bin
results = self.RunAction(
Expand Down
Loading

0 comments on commit caa8c1b

Please sign in to comment.