Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions tests/integration_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# This file is part of cloud-init. See LICENSE file for license information.
import datetime
import logging
import os
import pytest
import sys
from tarfile import TarFile
from contextlib import contextmanager
from pathlib import Path

from tests.integration_tests import integration_settings
from tests.integration_tests.clouds import (
Expand All @@ -14,6 +17,7 @@
LxdContainerCloud,
LxdVmCloud,
)
from tests.integration_tests.instances import IntegrationInstance


log = logging.getLogger('integration_testing')
Expand All @@ -29,6 +33,8 @@
'lxd_vm': LxdVmCloud,
}

session_start_time = datetime.datetime.now().strftime('%y%m%d%H%M%S')


def pytest_runtest_setup(item):
"""Skip tests on unsupported clouds.
Expand Down Expand Up @@ -114,6 +120,43 @@ def setup_image(session_cloud):
log.info('Done with environment setup')


def _collect_logs(instance: IntegrationInstance, node_id: str,
test_failed: bool):
"""Collect logs from remote instance.

Args:
instance: The current IntegrationInstance to collect logs from
node_id: The pytest representation of this test, E.g.:
tests/integration_tests/test_example.py::TestExample.test_example
test_failed: If test failed or not
"""
if any([
integration_settings.COLLECT_LOGS == 'NEVER',
integration_settings.COLLECT_LOGS == 'ON_ERROR' and not test_failed
]):
return
instance.execute(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Future me wants a docstring describing params, specifically I'd like an example of what node_id value is generally expected to be so I don't have to look it up from the PR subtext.

'cloud-init collect-logs -u -t /var/tmp/cloud-init.tar.gz')
node_id_path = Path(
node_id
.replace('.py', '') # Having a directory with '.py' would be weird
.replace('::', os.path.sep) # Turn classes/tests into paths
.replace('[', '-') # For parametrized names
.replace(']', '') # For parameterized names
)
log_dir = Path(
integration_settings.LOCAL_LOG_PATH
) / session_start_time / node_id_path
if not log_dir.exists():
log_dir.mkdir(parents=True)
tarball_path = log_dir / 'cloud-init.tar.gz'
instance.pull_file('/var/tmp/cloud-init.tar.gz', tarball_path)

tarball = TarFile.open(str(tarball_path))
tarball.extractall(path=str(log_dir))
tarball_path.unlink()


@contextmanager
def _client(request, fixture_utils, session_cloud):
"""Fixture implementation for the client fixtures.
Expand All @@ -132,7 +175,10 @@ def _client(request, fixture_utils, session_cloud):
with session_cloud.launch(
user_data=user_data, launch_kwargs=launch_kwargs
) as instance:
previous_failures = request.session.testsfailed
yield instance
test_failed = request.session.testsfailed - previous_failures > 0
_collect_logs(instance, request.node.nodeid, test_failed)


@pytest.yield_fixture
Expand Down
8 changes: 4 additions & 4 deletions tests/integration_tests/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ def execute(self, command, *, use_sudo=None) -> Result:
def pull_file(self, remote_path, local_path):
# First copy to a temporary directory because of permissions issues
tmp_path = _get_tmp_path()
self.instance.execute('cp {} {}'.format(remote_path, tmp_path))
self.instance.pull_file(tmp_path, local_path)
self.instance.execute('cp {} {}'.format(str(remote_path), tmp_path))
self.instance.pull_file(tmp_path, str(local_path))

def push_file(self, local_path, remote_path):
# First push to a temporary directory because of permissions issues
tmp_path = _get_tmp_path()
self.instance.push_file(local_path, tmp_path)
self.execute('mv {} {}'.format(tmp_path, remote_path))
self.instance.push_file(str(local_path), tmp_path)
self.execute('mv {} {}'.format(tmp_path, str(remote_path)))

def read_from_file(self, remote_path) -> str:
result = self.execute('cat {}'.format(remote_path))
Expand Down
10 changes: 10 additions & 0 deletions tests/integration_tests/integration_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
# A path to a valid package to be uploaded and installed
CLOUD_INIT_SOURCE = 'NONE'

# Before an instance is torn down, we run `cloud-init collect-logs`
# and transfer them locally. These settings specify when to collect these
# logs and where to put them on the local filesystem
# One of:
# 'ALWAYS'
# 'ON_ERROR'
# 'NEVER'
COLLECT_LOGS = 'ON_ERROR'
LOCAL_LOG_PATH = '/tmp/cloud_init_test_logs'

##################################################################
# GCE SPECIFIC SETTINGS
##################################################################
Expand Down