def test_issue_11935_with_id(self): set_crontab('# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '0 2 * * * find /var/www -type f ' '-mtime -7 -print0 | xargs -0 ' 'clamscan -i --no-summary 2>/dev/null') cmd = ('find /var/www -type f -mtime -7 -print0 ' '| xargs -0 clamscan -i --no-summary 2>/dev/null') self.assertEqual( cron._check_cron('root', cmd, hour='2', minute='0', identifier=1), 'present') ret = cron.present(cmd, 'root', minute='0', hour='2', identifier='1') self.assertEqual(ret['changes'], {}) self.assertEqual( ret['comment'], 'Cron find /var/www -type f -mtime -7 -print0 ' '| xargs -0 clamscan -i --no-summary 2>/dev/null already present') self.assertEqual( cron._check_cron('root', cmd, hour='3', minute='0', identifier='1'), 'update') ret = cron.present(cmd, 'root', minute='0', hour='3', identifier='1') self.assertEqual( ret['changes'], { 'root': 'find /var/www -type f -mtime -7 -print0 | ' 'xargs -0 clamscan -i --no-summary 2>/dev/null' }) self.assertEqual( ret['comment'], 'Cron find /var/www -type f -mtime -7 -print0 ' '| xargs -0 clamscan -i --no-summary 2>/dev/null updated') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '0 3 * * * find /var/www -type f -mtime -7 -print0 |' ' xargs -0 clamscan -i --no-summary 2>/dev/null')
def test_issue_11935(self): set_crontab( '# Lines below here are managed by Salt, do not edit\n' '0 2 * * * find /var/www -type f ' '-mtime -7 -print0 | xargs -0 ' 'clamscan -i --no-summary 2>/dev/null' ) cmd = ( 'find /var/www -type f -mtime -7 -print0 ' '| xargs -0 clamscan -i --no-summary 2>/dev/null' ) self.assertEqual(cron._check_cron('root', cmd, hour='2', minute='0'), 'present') ret = cron.present(cmd, 'root', minute='0', hour='2') self.assertEqual(ret['changes'], {}) self.assertEqual( ret['comment'], 'Cron find /var/www -type f -mtime -7 -print0 ' '| xargs -0 clamscan -i --no-summary 2>/dev/null already present') self.assertEqual(cron._check_cron('root', cmd, hour='3', minute='0'), 'update') ret = cron.present(cmd, 'root', minute='0', hour='3') self.assertEqual(ret['changes'], {'root': 'find /var/www -type f -mtime -7 -print0 | ' 'xargs -0 clamscan -i --no-summary 2>/dev/null'}) self.assertEqual( ret['comment'], 'Cron find /var/www -type f -mtime -7 -print0 ' '| xargs -0 clamscan -i --no-summary 2>/dev/null updated') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '0 3 * * * find /var/www -type f -mtime -7 -print0 |' ' xargs -0 clamscan -i --no-summary 2>/dev/null')
def test_present_special(self): cron.present(name="foo", special="@hourly", identifier="1", user="******") self.assertMultiLineEqual( self.get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:1\n" "@hourly foo", )
def test_present_unspecial_after_special(self): """cron.present should remove an special entry with the same identifier""" cron.present(name="foo", special="@hourly", identifier="1", user="******") cron.present(name="foo", hour="1", identifier="1", user="******") self.assertMultiLineEqual( self.get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:1\n" "* 1 * * * foo", )
def test_existing_unmanaged_jobs_are_made_managed(self): set_crontab('# Lines below here are managed by Salt, do not edit\n' '0 2 * * * foo') ret = cron._check_cron('root', 'foo', hour='2', minute='0') self.assertEqual(ret, 'present') ret = cron.present('foo', 'root', minute='0', hour='2') self.assertEqual(ret['changes'], {'root': 'foo'}) self.assertEqual(ret['comment'], 'Cron foo updated') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:foo\n' '0 2 * * * foo') ret = cron.present('foo', 'root', minute='0', hour='2') self.assertEqual(ret['changes'], {}) self.assertEqual(ret['comment'], 'Cron foo already present')
def test_existing_unmanaged_jobs_are_made_managed(self): set_crontab("# Lines below here are managed by Salt, do not edit\n" "0 2 * * * foo") ret = cron._check_cron("root", "foo", hour="2", minute="0") self.assertEqual(ret, "present") ret = cron.present("foo", "root", minute="0", hour="2") self.assertEqual(ret["changes"], {"root": "foo"}) self.assertEqual(ret["comment"], "Cron foo updated") self.assertEqual( get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:foo\n" "0 2 * * * foo", ) ret = cron.present("foo", "root", minute="0", hour="2") self.assertEqual(ret["changes"], {}) self.assertEqual(ret["comment"], "Cron foo already present")
def test_existing_unmanaged_jobs_are_made_managed(self): set_crontab( '# Lines below here are managed by Salt, do not edit\n' '0 2 * * * foo' ) ret = cron._check_cron('root', 'foo', hour='2', minute='0') self.assertEqual(ret, 'present') ret = cron.present('foo', 'root', minute='0', hour='2') self.assertEqual(ret['changes'], {'root': 'foo'}) self.assertEqual(ret['comment'], 'Cron foo updated') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:foo\n' '0 2 * * * foo') ret = cron.present('foo', 'root', minute='0', hour='2') self.assertEqual(ret['changes'], {}) self.assertEqual(ret['comment'], 'Cron foo already present')
def test_present(self): cron.present( name='foo', hour='1', identifier='1', user='******') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 1 * * * foo') cron.present( name='foo', hour='2', identifier='1', user='******') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo') cron.present( name='foo', hour='2', identifier='2', user='******') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# SALT_CRON_IDENTIFIER:2\n' '* 2 * * * foo') set_crontab( '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# SALT_CRON_IDENTIFIER:2\n' '* 2 * * * foo\n' '* 2 * * * foo\n' ) cron.present( name='foo', hour='2', user='******') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# SALT_CRON_IDENTIFIER:2\n' '* 2 * * * foo\n' '# SALT_CRON_IDENTIFIER:NO ID SET\n' '* 2 * * * foo')
def test_present(self): cron.present( name='foo', hour='1', identifier='1', user='******') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 1 * * * foo') cron.present( name='foo', hour='2', identifier='1', user='******') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo') cron.present( name='foo', hour='2', identifier='2', user='******') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# SALT_CRON_IDENTIFIER:2\n' '* 2 * * * foo') set_crontab( '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# SALT_CRON_IDENTIFIER:2\n' '* 2 * * * foo\n' '* 2 * * * foo\n' ) cron.present( name='foo', hour='2', user='******', identifier=None) self.assertEqual( get_crontab(), ('# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# SALT_CRON_IDENTIFIER:2\n' '* 2 * * * foo\n' '* 2 * * * foo'))
def test_multiline_comments_are_updated(self): set_crontab( '# Lines below here are managed by Salt, do not edit\n' '# First crontab - single line comment SALT_CRON_IDENTIFIER:1\n' '* 1 * * * foo' ) cron.present( name='foo', hour='1', comment='First crontab\nfirst multi-line comment\n', identifier='1', user='******') cron.present( name='foo', hour='1', comment='First crontab\nsecond multi-line comment\n', identifier='1', user='******') cron.present( name='foo', hour='1', comment='Second crontab\nmulti-line comment\n', identifier='2', user='******') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# First crontab\n' '# second multi-line comment SALT_CRON_IDENTIFIER:1\n' '* 1 * * * foo\n' '# Second crontab\n' '# multi-line comment SALT_CRON_IDENTIFIER:2\n' '* 1 * * * foo')
def test_aissue_1072(self): set_crontab('# Lines below here are managed by Salt, do not edit\n' '# I have a multi-line comment SALT_CRON_IDENTIFIER:1\n' '* 1 * * * foo') cron.present( name='foo', hour='1', comment='1I have a multi-line comment\n2about my script here.\n', identifier='1', user='******') cron.present( name='foo', hour='1', comment='3I have a multi-line comment\n3about my script here.\n', user='******') cron.present( name='foo', hour='1', comment='I have a multi-line comment\nabout my script here.\n', identifier='2', user='******') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# 2about my script here. SALT_CRON_IDENTIFIER:1\n' '* 1 * * * foo\n' '# I have a multi-line comment\n' '# about my script here. SALT_CRON_IDENTIFIER:2\n' '* 1 * * * foo')
def test_aissue_1072(self): set_crontab( '# Lines below here are managed by Salt, do not edit\n' '# I have a multi-line comment SALT_CRON_IDENTIFIER:1\n' '* 1 * * * foo' ) cron.present( name='foo', hour='1', comment='1I have a multi-line comment\n2about my script here.\n', identifier='1', user='******') cron.present( name='foo', hour='1', comment='3I have a multi-line comment\n3about my script here.\n', user='******') cron.present( name='foo', hour='1', comment='I have a multi-line comment\nabout my script here.\n', identifier='2', user='******') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# 2about my script here. SALT_CRON_IDENTIFIER:1\n' '* 1 * * * foo\n' '# I have a multi-line comment\n' '# about my script here. SALT_CRON_IDENTIFIER:2\n' '* 1 * * * foo')
def test_existing_noid_jobs_are_updated_with_identifier(self): set_crontab('# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:NO ID SET\n' '1 * * * * foo') ret = cron._check_cron('root', 'foo', minute=1) self.assertEqual(ret, 'present') ret = cron.present('foo', 'root', minute=1) self.assertEqual(ret['changes'], {'root': 'foo'}) self.assertEqual(ret['comment'], 'Cron foo updated') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:foo\n' '1 * * * * foo')
def test_existing_noid_jobs_are_updated_with_identifier(self): set_crontab("# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:NO ID SET\n" "1 * * * * foo") ret = cron._check_cron("root", "foo", minute=1) self.assertEqual(ret, "present") ret = cron.present("foo", "root", minute=1) self.assertEqual(ret["changes"], {"root": "foo"}) self.assertEqual(ret["comment"], "Cron foo updated") self.assertEqual( get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:foo\n" "1 * * * * foo", )
def test_existing_noid_jobs_are_updated_with_identifier(self): set_crontab( '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:NO ID SET\n' '1 * * * * foo' ) ret = cron._check_cron('root', 'foo', minute=1) self.assertEqual(ret, 'present') ret = cron.present('foo', 'root', minute=1) self.assertEqual(ret['changes'], {'root': 'foo'}) self.assertEqual(ret['comment'], 'Cron foo updated') self.assertEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:foo\n' '1 * * * * foo')
def test_multiline_comments_are_updated(self): self.set_crontab( "# Lines below here are managed by Salt, do not edit\n" "# First crontab - single line comment SALT_CRON_IDENTIFIER:1\n" "* 1 * * * foo" ) cron.present( name="foo", hour="1", comment="First crontab\nfirst multi-line comment\n", identifier="1", user="******", ) cron.present( name="foo", hour="1", comment="First crontab\nsecond multi-line comment\n", identifier="1", user="******", ) cron.present( name="foo", hour="1", comment="Second crontab\nmulti-line comment\n", identifier="2", user="******", ) self.assertEqual( self.get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# First crontab\n" "# second multi-line comment\n" "# SALT_CRON_IDENTIFIER:1\n" "* 1 * * * foo\n" "# Second crontab\n" "# multi-line comment\n" "# SALT_CRON_IDENTIFIER:2\n" "* 1 * * * foo", )
def test_present(self): cron.present( name='foo', hour='1', identifier='1', user='******') self.assertMultiLineEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 1 * * * foo') cron.present( name='foo', hour='2', identifier='1', user='******') self.assertMultiLineEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo') cron.present( name='cmd1', minute='0', comment='Commented cron job', commented=True, identifier='commented_1', user='******') self.assertMultiLineEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# Commented cron job SALT_CRON_IDENTIFIER:commented_1\n' '#DISABLED#0 * * * * cmd1') cron.present( name='foo', hour='2', identifier='2', user='******') self.assertMultiLineEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# Commented cron job SALT_CRON_IDENTIFIER:commented_1\n' '#DISABLED#0 * * * * cmd1\n' '# SALT_CRON_IDENTIFIER:2\n' '* 2 * * * foo') cron.present( name='cmd2', commented=True, identifier='commented_2', user='******') self.assertMultiLineEqual( get_crontab(), '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# Commented cron job SALT_CRON_IDENTIFIER:commented_1\n' '#DISABLED#0 * * * * cmd1\n' '# SALT_CRON_IDENTIFIER:2\n' '* 2 * * * foo\n' '# SALT_CRON_IDENTIFIER:commented_2\n' '#DISABLED#* * * * * cmd2') set_crontab( '# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# SALT_CRON_IDENTIFIER:2\n' '* 2 * * * foo\n' '* 2 * * * foo\n' ) cron.present( name='foo', hour='2', user='******', identifier=None) self.assertEqual( get_crontab(), ('# Lines below here are managed by Salt, do not edit\n' '# SALT_CRON_IDENTIFIER:1\n' '* 2 * * * foo\n' '# SALT_CRON_IDENTIFIER:2\n' '* 2 * * * foo\n' '* 2 * * * foo'))
def test_present(self): cron.present(name="foo", hour="1", identifier="1", user="******") self.assertMultiLineEqual( self.get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:1\n" "* 1 * * * foo", ) cron.present(name="foo", hour="2", identifier="1", user="******") self.assertMultiLineEqual( self.get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:1\n" "* 2 * * * foo", ) cron.present( name="cmd1", minute="0", comment="Commented cron job", commented=True, identifier="commented_1", user="******", ) self.assertMultiLineEqual( self.get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:1\n" "* 2 * * * foo\n" "# Commented cron job SALT_CRON_IDENTIFIER:commented_1\n" "#DISABLED#0 * * * * cmd1", ) cron.present(name="foo", hour="2", identifier="2", user="******") self.assertMultiLineEqual( self.get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:1\n" "* 2 * * * foo\n" "# Commented cron job SALT_CRON_IDENTIFIER:commented_1\n" "#DISABLED#0 * * * * cmd1\n" "# SALT_CRON_IDENTIFIER:2\n" "* 2 * * * foo", ) cron.present(name="cmd2", commented=True, identifier="commented_2", user="******") self.assertMultiLineEqual( self.get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:1\n" "* 2 * * * foo\n" "# Commented cron job SALT_CRON_IDENTIFIER:commented_1\n" "#DISABLED#0 * * * * cmd1\n" "# SALT_CRON_IDENTIFIER:2\n" "* 2 * * * foo\n" "# SALT_CRON_IDENTIFIER:commented_2\n" "#DISABLED#* * * * * cmd2", ) self.set_crontab( "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:1\n" "* 2 * * * foo\n" "# SALT_CRON_IDENTIFIER:2\n" "* 2 * * * foo\n" "* 2 * * * foo\n" ) cron.present(name="foo", hour="2", user="******", identifier=None) self.assertEqual( self.get_crontab(), "# Lines below here are managed by Salt, do not edit\n" "# SALT_CRON_IDENTIFIER:1\n" "* 2 * * * foo\n" "# SALT_CRON_IDENTIFIER:2\n" "* 2 * * * foo\n" "* 2 * * * foo", )