# Path: performanceplatform/collector/crontab.py
# def remove_existing_crontab_for_app(crontab, unique_id):
#     new_crontab = []
#     should_append = True
#     for line in crontab:
#         if line == crontab_begin_comment(unique_id):
#             should_append = False
#         if should_append:
#             new_crontab.append(line)
#         if line == crontab_end_comment(unique_id):
#             should_append = True
#     return new_crontab
# 
# Path: performanceplatform/collector/crontab.py
# NUMBER_OF_HOSTS = 2
# class ParseError(StandardError):
# def crontab_begin_comment(unique_id):
# def crontab_end_comment(unique_id):
# def remove_existing_crontab_for_app(crontab, unique_id):
# def parse_job_line(line):
# def skip_job(counter):
# def generate_crontab(current_crontab, path_to_jobs, path_to_app, unique_id):


import subprocess
import sys
from nose.tools import *
from hamcrest import *
from performanceplatform.collector.crontab import remove_existing_crontab_for_app
from tests.performanceplatform.collector.tools import temp_file
from performanceplatform.collector import crontab
from mock import patch

class TestRemoveExistingCrontab(object):

    def setUp(self):
        self.patcher = patch('socket.gethostname')
        self.mock_gethostname = self.patcher.start()
        self.mock_gethostname.return_value = 'nonnumerichostname'

    def test_jobs_are_not_removed_for_other_apps(self):
        crontab = [
            '# Begin performanceplatform.collector jobs for my-app',
            'foobar',
            '# End performanceplatform.collector jobs for my-app',
            '# Begin performanceplatform.collector jobs for other-app',
            'barfoo',
            '# End performanceplatform.collector jobs for other-app',
        ]
        new_crontab = remove_existing_crontab_for_app(crontab, 'my-app')
        assert_that(new_crontab,
                    has_item(contains_string('barfoo')))
        assert_that(new_crontab,
                    is_not(has_item(contains_string('foobar'))))

    def test_crontab_end_before_begin(self):
        crontab = [
            '# End performanceplatform.collector jobs for my-app',
            'foobar',
            '# Begin performanceplatform.collector jobs for my-app',
            'other'
        ]

# Path: performanceplatform/collector/crontab.py
# def remove_existing_crontab_for_app(crontab, unique_id):
#     new_crontab = []
#     should_append = True
#     for line in crontab:
#         if line == crontab_begin_comment(unique_id):
#             should_append = False
#         if should_append:
#             new_crontab.append(line)
#         if line == crontab_end_comment(unique_id):
#             should_append = True
#     return new_crontab
# 
# Path: performanceplatform/collector/crontab.py
# NUMBER_OF_HOSTS = 2
# class ParseError(StandardError):
# def crontab_begin_comment(unique_id):
# def crontab_end_comment(unique_id):
# def remove_existing_crontab_for_app(crontab, unique_id):
# def parse_job_line(line):
# def skip_job(counter):
# def generate_crontab(current_crontab, path_to_jobs, path_to_app, unique_id):


# Path: tests/performanceplatform/collector/test_crontab.py
import subprocess
import sys
from nose.tools import *
from hamcrest import *
from performanceplatform.collector.crontab import remove_existing_crontab_for_app
from tests.performanceplatform.collector.tools import temp_file
from performanceplatform.collector import crontab
from mock import patch


class TestRemoveExistingCrontab(object):

    def setUp(self):
        self.patcher = patch('socket.gethostname')
        self.mock_gethostname = self.patcher.start()
        self.mock_gethostname.return_value = 'nonnumerichostname'

    def test_jobs_are_not_removed_for_other_apps(self):
        crontab = [
            '# Begin performanceplatform.collector jobs for my-app',
            'foobar',
            '# End performanceplatform.collector jobs for my-app',
            '# Begin performanceplatform.collector jobs for other-app',
            'barfoo',
            '# End performanceplatform.collector jobs for other-app',
        ]
        new_crontab = remove_existing_crontab_for_app(crontab, 'my-app')
        assert_that(new_crontab,
                    has_item(contains_string('barfoo')))
        assert_that(new_crontab,
                    is_not(has_item(contains_string('foobar'))))

    def test_crontab_end_before_begin(self):
        crontab = [
            '# End performanceplatform.collector jobs for my-app',
            'foobar',
            '# Begin performanceplatform.collector jobs for my-app',
            'other'
        ]

        new_crontab = remove_existing_crontab_for_app(crontab, 'my-app')