From 4ce214b288ba7b4dd4255f1b8aa035ac4f079adf Mon Sep 17 00:00:00 2001 From: Khai Do Date: Wed, 29 Oct 2014 14:57:21 -0700 Subject: [PATCH] add test for globbed parameters feature add a test to verify commmand processing of the globbed parameters feature to delete jobs. Change-Id: Ib13a2c644388ac65cb1af5728019912d6452d852 --- tests/cmd/fixtures/cmd-002.yaml | 23 +++++++++++++++++++++++ tests/cmd/test_cmd.py | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/cmd/fixtures/cmd-002.yaml diff --git a/tests/cmd/fixtures/cmd-002.yaml b/tests/cmd/fixtures/cmd-002.yaml new file mode 100644 index 000000000..f844cc763 --- /dev/null +++ b/tests/cmd/fixtures/cmd-002.yaml @@ -0,0 +1,23 @@ +- job-template: + name: 'bar001' + description: 'My first job' + +- job-template: + name: 'bar002' + description: 'My second job' + +- job-template: + name: 'baz001' + description: 'My third job' + +- job-template: + name: 'bam001' + description: 'My fourth job' + +- project: + name: my-projects + jobs: + - 'bar001' + - 'bar002' + - 'baz001' + - 'bam001' diff --git a/tests/cmd/test_cmd.py b/tests/cmd/test_cmd.py index 61bdc6cb0..eb487700a 100644 --- a/tests/cmd/test_cmd.py +++ b/tests/cmd/test_cmd.py @@ -11,12 +11,21 @@ from jenkins_jobs import cmd # attempting to create the cache directory multiple times as the tests # are run in parallel. Stub out the CacheStorage to ensure that each # test can safely create the cache directory without risk of interference. -@mock.patch('jenkins_jobs.builder.CacheStorage', mock.MagicMock) class CmdTests(testtools.TestCase): fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') parser = cmd.create_parser() + def setUp(self): + super(CmdTests, self).setUp() + self.cache_patch = mock.patch('jenkins_jobs.builder.CacheStorage', + autospec=True) + self.cache_patch.start() + + def tearDown(self): + self.cache_patch.stop() + super(CmdTests, self).tearDown() + def test_with_empty_args(self): """ User passes no args, should fail with SystemExit @@ -230,3 +239,25 @@ class CmdTests(testtools.TestCase): config = configparser.ConfigParser() config.readfp(StringIO(cmd.DEFAULT_CONF)) cmd.execute(args, config) # passes if executed without error + + @mock.patch('jenkins_jobs.builder.Jenkins.delete_job') + def test_delete_using_glob_params(self, delete_job_mock): + """ + Test handling the deletion of multiple Jenkins jobs using the glob + parameters feature. + """ + + args = self.parser.parse_args(['delete', + '--path', + os.path.join(self.fixtures_path, + 'cmd-002.yaml'), + '*bar*']) + config = configparser.ConfigParser() + config.readfp(StringIO(cmd.DEFAULT_CONF)) + cmd.execute(args, config) + calls = [mock.call('bar001'), mock.call('bar002')] + delete_job_mock.assert_has_calls(calls, any_order=True) + self.assertEquals(delete_job_mock.call_count, len(calls), + "Jenkins.delete_job() was called '%s' times when " + "expected '%s'" % (delete_job_mock.call_count, + len(calls)))