Merge lp:~davidpbritton/tarmac/artifacts-copy into lp:~davidpbritton/tarmac/backout-409

Proposed by David Britton on 2015-09-29
Status: Merged
Merged at revision: 440
Proposed branch: lp:~davidpbritton/tarmac/artifacts-copy
Merge into: lp:~davidpbritton/tarmac/backout-409
Diff against target: 179 lines (+129/-6)
3 files modified
docs/introduction.txt (+22/-0)
tarmac/plugins/artifact_copy.py (+8/-6)
tarmac/plugins/tests/test_artifact_copy.py (+99/-0)
To merge this branch: bzr merge lp:~davidpbritton/tarmac/artifacts-copy
Reviewer Review Type Date Requested Status
Free Ekanayaka (community) 2015-09-29 Approve on 2015-09-29
Adam Collard (community) 2015-09-29 Approve on 2015-09-29
Review via email: mp+272685@code.launchpad.net

Description of the change

Tests and docs for new 'artifact_copy' plugin that was committed as a trivial to get more data on tarmac failures earlier. Please review that file and the tests.

It should be runnable by following the 'HACKING' doc, and then running './run-tests'

to narrow down on a smaller subset:

nosetests -v tarmac/plugins/tests

To post a comment you must log in.
441. By David Britton on 2015-09-29

add section in docs explaining artifacts_copy.

Adam Collard (adam-collard) wrote :

+1, looks good

review: Approve
Free Ekanayaka (free.ekanayaka) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'docs/introduction.txt'
2--- docs/introduction.txt 2014-04-10 20:33:15 +0000
3+++ docs/introduction.txt 2015-09-29 04:15:19 +0000
4@@ -284,6 +284,28 @@
5 default_milestone = 12.10
6
7
8+Artifact Copy
9+=============
10+The artifact copy plugin will copy back artifacts from the tree where testing
11+was performed into the current working directory in a subdirectory called
12+`tarmac-artifacts`. If no artifacts are specified, nothing is copied back.
13+If artifacts are missing, a warning noting as much will be logged. If a
14+directory is specified, the whole directory contents will be copied back.
15+
16+Paths should be relative to the tree under test.
17+
18+This plugin supports options:
19+
20+ [lp:target_branch]
21+ # Space seaprated files or directories, relative to the tree that is
22+ # under test.
23+ artifacts = "build_directory single_file.log logs/component/interesting.log"
24+
25+ # You may optionally specify the artifacts_dest, if unspecified, it will
26+ # default to `artifacts-dest`
27+ artifacts_dest = "artifacts-directory-name"
28+
29+
30 ========================
31 Installing other plugins
32 ========================
33
34=== modified file 'tarmac/plugins/artifact_copy.py'
35--- tarmac/plugins/artifact_copy.py 2015-09-28 17:25:36 +0000
36+++ tarmac/plugins/artifact_copy.py 2015-09-29 04:15:19 +0000
37@@ -9,23 +9,25 @@
38 """Copy artifacts for archiving purposes."""
39
40 def run(self, command, target, *args, **kwargs):
41- """Check the allowed contributors list."""
42+ """Copy artifacts for archiving purposes."""
43
44 artifacts = self.get_config(
45- "artifacts", "", target, command).split()
46+ "artifacts", "", target, command).strip().split()
47 if not artifacts:
48 self.logger.debug("No artifacts listed to copy")
49 return
50
51- dest = self.get_config(
52- "artifacts_dest", "tarmac-artifacts", target, command)
53+ dest = os.path.join(os.getcwd(), self.get_config(
54+ "artifacts_dest", "tarmac-artifacts", target, command))
55
56 for artifact in artifacts:
57 artifact_src = os.path.join(target.tree.basedir, artifact)
58- artifact_dst = os.path.join(os.getcwd(), dest, artifact)
59+ artifact_dst = os.path.join(dest, artifact)
60 self.logger.debug("Copy from %s to %s" % (
61 artifact_src, artifact_dst))
62- if os.path.isdir(artifact_src):
63+ if not os.path.exists(artifact_src):
64+ self.logger.warning("Missing artifact: %s" % artifact_src)
65+ elif os.path.isdir(artifact_src):
66 shutil.copytree(artifact_src, artifact_dst)
67 else:
68 if not os.path.exists(os.path.dirname(artifact_dst)):
69
70=== added directory 'tarmac/plugins/tests/artifact_sample_dir'
71=== added directory 'tarmac/plugins/tests/artifact_sample_dir/dir1'
72=== added file 'tarmac/plugins/tests/artifact_sample_dir/dir1/file1'
73=== added file 'tarmac/plugins/tests/artifact_sample_dir/dir1/file2'
74=== added directory 'tarmac/plugins/tests/artifact_sample_dir/dir2'
75=== added file 'tarmac/plugins/tests/artifact_sample_dir/file1'
76=== added file 'tarmac/plugins/tests/artifact_sample_dir/file2'
77=== added file 'tarmac/plugins/tests/test_artifact_copy.py'
78--- tarmac/plugins/tests/test_artifact_copy.py 1970-01-01 00:00:00 +0000
79+++ tarmac/plugins/tests/test_artifact_copy.py 2015-09-29 04:15:19 +0000
80@@ -0,0 +1,99 @@
81+"""Tests for the artifact_copy plugin."""
82+import filecmp
83+import os
84+from mock import MagicMock
85+import tempfile
86+
87+from tarmac.plugins.artifact_copy import (
88+ ArtifactCopy)
89+from tarmac.tests import TarmacTestCase
90+from tarmac.tests import Thing
91+from tarmac.config import TarmacConfig
92+
93+
94+class TestArtifactCopy(TarmacTestCase):
95+
96+ def _create_sample_file(self, dir):
97+ """Create a sample file in a directory, rooted under dir.
98+
99+ Return tuple of file and dirname/filename string."""
100+ sample_dir = tempfile.mkdtemp(dir=dir)
101+ with open(os.path.join(sample_dir, "sample-file"), 'w') as f:
102+ f.write("contents")
103+ return (f, os.path.join(
104+ os.path.basename(sample_dir), os.path.basename(f)))
105+
106+ def setUp(self):
107+ super(TestArtifactCopy, self).setUp()
108+ self.artifact_copy = ArtifactCopy()
109+ self.config = TarmacConfig()
110+
111+ # Change my CWD to a tempdir
112+ self.tempdir = tempfile.mkdtemp()
113+ self.oldcwd = os.getcwd()
114+ os.chdir(self.tempdir)
115+
116+ # Create a "target" branch, which holds my tree dir,
117+ # where artifacts are
118+ self.artifact_sample_dir = os.path.join(
119+ os.path.dirname(__file__), "artifact_sample_dir")
120+ self.target = Thing()
121+ self.target.tree = Thing()
122+ self.target.tree.basedir = self.artifact_sample_dir
123+ self.target.config = TarmacConfig()
124+
125+ def tearDown(self):
126+ super(TestArtifactCopy, self).tearDown()
127+ os.chdir(self.oldcwd)
128+
129+ def test_run_no_config(self):
130+ """No configuration, should not copy files."""
131+ self.artifact_copy.run(Thing(), self.target)
132+ self.assertEqual(os.listdir(os.getcwd()), [])
133+
134+ def test_run_empty_config(self):
135+ """Empty configuration, should not copy files."""
136+ self.target.config.artifacts = ""
137+ self.artifact_copy.run(Thing(), self.target)
138+ self.assertEqual(os.listdir(os.getcwd()), [])
139+
140+ def test_run_copy_file_change_dest(self):
141+ """Copy a single sample file, change the dest directory name"""
142+ self.target.config.artifacts = "file2"
143+ self.target.config.artifacts_dest = "foo-my-dest"
144+ self.artifact_copy.run(Thing(), self.target)
145+ self.assertEqual(os.listdir(os.getcwd()), ["foo-my-dest"])
146+ self.assertEqual(
147+ os.listdir(os.path.join(os.getcwd(), "foo-my-dest")),
148+ ["file2"])
149+
150+ def test_run_copy_all(self):
151+ """Copy all components to check files, directories, multiples.
152+
153+ Also use the default dest directory name."""
154+ self.target.config.artifacts = "file1 file2 dir1 dir2"
155+ self.artifact_copy.run(Thing(), self.target)
156+ self.assertEqual(os.listdir(os.getcwd()), ["tarmac-artifacts"])
157+ comparison = filecmp.dircmp(
158+ self.artifact_sample_dir,
159+ os.path.join(os.getcwd(), "tarmac-artifacts"))
160+ self.assertListEqual(
161+ sorted(comparison.common_files), ['file1', 'file2'])
162+ self.assertListEqual(
163+ sorted(comparison.common_dirs), ['dir1', 'dir2'])
164+ self.assertListEqual(comparison.diff_files, [])
165+ self.assertListEqual(comparison.left_only, [])
166+ self.assertListEqual(comparison.right_only, [])
167+
168+ def test_run_copy_missing_artifact(self):
169+ """Copy a single sample file, that is not there.
170+
171+ No artifacts should be copied, warning logged."""
172+ log_mock = MagicMock()
173+ self.artifact_copy.logger = log_mock
174+ self.target.config.artifacts = "foo-file-not-found"
175+ src_path = os.path.join(self.target.tree.basedir, "foo-file-not-found")
176+ self.artifact_copy.run(Thing(), self.target)
177+ self.assertEqual(os.listdir(os.getcwd()), [])
178+ log_mock.warning.assert_called_once_with(
179+ "Missing artifact: %s" % src_path)

Subscribers

People subscribed via source and target branches

to all changes: