# Path: kingpin/zk_update_monitor/zk_util.py
# def get_md5_digest(zk_data):
# def get_md5_hash_sum(zk_data):
# def split_problematic_endpoints_line(line):
# def construct_s3_path(s3_key, timestamp):
# def _kazoo_client(zk_hosts):
# def _zk_path_exists(zk_hosts, path):
# def _kill(message):
# def parse_zk_hosts_file(zk_hosts_file_path):
# 
# Path: kingpin/zk_update_monitor/zk_update_monitor.py
# def transform_command_with_value(command, value, notification_timestamp):
#     python_download_script = 'zk_download_data.py'
# 
#     if len(value) > _LONG_VALUE_THRESHOLD:
#         # If the value is too long (serverset is too large), OSError may be thrown.
#         # Instead of passing it in command line, write to a temp file and
#         # let zk_download_data read from it.
#         value = value.replace("\n", "").replace("\r", "")
#         md5digest = zk_util.get_md5_digest(value)
#         tmp_filename = 'zk_update_largefile_' + md5digest + '_' + str(notification_timestamp)
#         tmp_dir = tempfile.gettempprefix()
#         tmp_filepath = os.path.join('/', tmp_dir, tmp_filename)
# 
#         log.info("This is a long value, write it to temp file %s", tmp_filepath)
#         try:
#             with open(tmp_filepath, 'w') as f:
#                 f.write(value + '\n' + md5digest)
#         except Exception as e:
#             log.exception(
#                 "%s: Failed to generate temp file %s for storing large size values"
#                 % (e.message, tmp_filepath))
#             return (None, None)
#         finally:
#             f.close()
#         transformed_command = command.replace(
#             python_download_script, "%s -l %s" % (
#                 python_download_script, tmp_filepath))
#         return transformed_command, tmp_filepath
#     else:
#         transformed_command = command.replace(
#             python_download_script, "%s -v '%s'" % (
#                 python_download_script, value))
#         return transformed_command, None


from unittest import TestCase
from kingpin.zk_update_monitor import zk_util
from kingpin.zk_update_monitor.zk_update_monitor import transform_command_with_value
import os
    import simplejson as json
    import json
            (transformed_command, tmp_filepath) = transform_command_with_value(
                command, large_serverset_value, notification_timestamp)

            santinized_value = large_serverset_value.strip('\n').strip('\r')

            expected_tmp_filepath = '/tmp/zk_update_largefile_' + zk_util.get_md5_digest(
                santinized_value) + '_' + str(notification_timestamp)
            expected_transformed_command = '/usr/local/bin/zk_download_data.py -l ' \
                                           + expected_tmp_filepath + ' -f /var/serverset/discovery.stingray_dsl_mapper.prod -p /discovery/stingray_dsl_mapper/prod -m serverset'
            self.assertEqual(expected_transformed_command, transformed_command)
            self.assertEqual(expected_tmp_filepath, tmp_filepath)

            # Validate the file content
            tmpfile = open(expected_tmp_filepath)
            tmpfile_value = tmpfile.readline().rstrip('\n')
            tmpfile_md5 = tmpfile.readline()

            expected_tmpfile_value = santinized_value
            expected_tmpfile_md5 = zk_util.get_md5_digest(expected_tmpfile_value)

            self.assertEqual(expected_tmpfile_value, tmpfile_value)
            self.assertEqual(expected_tmpfile_md5, tmpfile_md5)
        finally:
            if os.path.isfile(tmp_filepath):
                os.remove(tmp_filepath)

    def test_transform_command_for_small_serverset(self):
        command = '/usr/local/bin/zk_download_data.py -f /var/serverset/discovery.stingray_dsl_mapper.prod -p /discovery/stingray_dsl_mapper/prod -m serverset'
        notification_timestamp = 1426859717.707331
        small_serverset_value = SMALL_SERVERSET_EXAMPLE

# Path: kingpin/zk_update_monitor/zk_util.py
# def get_md5_digest(zk_data):
# def get_md5_hash_sum(zk_data):
# def split_problematic_endpoints_line(line):
# def construct_s3_path(s3_key, timestamp):
# def _kazoo_client(zk_hosts):
# def _zk_path_exists(zk_hosts, path):
# def _kill(message):
# def parse_zk_hosts_file(zk_hosts_file_path):
# 
# Path: kingpin/zk_update_monitor/zk_update_monitor.py
# def transform_command_with_value(command, value, notification_timestamp):
#     python_download_script = 'zk_download_data.py'
# 
#     if len(value) > _LONG_VALUE_THRESHOLD:
#         # If the value is too long (serverset is too large), OSError may be thrown.
#         # Instead of passing it in command line, write to a temp file and
#         # let zk_download_data read from it.
#         value = value.replace("\n", "").replace("\r", "")
#         md5digest = zk_util.get_md5_digest(value)
#         tmp_filename = 'zk_update_largefile_' + md5digest + '_' + str(notification_timestamp)
#         tmp_dir = tempfile.gettempprefix()
#         tmp_filepath = os.path.join('/', tmp_dir, tmp_filename)
# 
#         log.info("This is a long value, write it to temp file %s", tmp_filepath)
#         try:
#             with open(tmp_filepath, 'w') as f:
#                 f.write(value + '\n' + md5digest)
#         except Exception as e:
#             log.exception(
#                 "%s: Failed to generate temp file %s for storing large size values"
#                 % (e.message, tmp_filepath))
#             return (None, None)
#         finally:
#             f.close()
#         transformed_command = command.replace(
#             python_download_script, "%s -l %s" % (
#                 python_download_script, tmp_filepath))
#         return transformed_command, tmp_filepath
#     else:
#         transformed_command = command.replace(
#             python_download_script, "%s -v '%s'" % (
#                 python_download_script, value))
#         return transformed_command, None


# Path: kingpin/tests/zk_update_monitor/test_zk_update_monitor.py
from unittest import TestCase
from kingpin.zk_update_monitor import zk_util
from kingpin.zk_update_monitor.zk_update_monitor import transform_command_with_value
import os
    import simplejson as json
    import json

            (transformed_command, tmp_filepath) = transform_command_with_value(
                command, large_serverset_value, notification_timestamp)

            santinized_value = large_serverset_value.strip('\n').strip('\r')

            expected_tmp_filepath = '/tmp/zk_update_largefile_' + zk_util.get_md5_digest(
                santinized_value) + '_' + str(notification_timestamp)
            expected_transformed_command = '/usr/local/bin/zk_download_data.py -l ' \
                                           + expected_tmp_filepath + ' -f /var/serverset/discovery.stingray_dsl_mapper.prod -p /discovery/stingray_dsl_mapper/prod -m serverset'
            self.assertEqual(expected_transformed_command, transformed_command)
            self.assertEqual(expected_tmp_filepath, tmp_filepath)

            # Validate the file content
            tmpfile = open(expected_tmp_filepath)
            tmpfile_value = tmpfile.readline().rstrip('\n')
            tmpfile_md5 = tmpfile.readline()

            expected_tmpfile_value = santinized_value
            expected_tmpfile_md5 = zk_util.get_md5_digest(expected_tmpfile_value)

            self.assertEqual(expected_tmpfile_value, tmpfile_value)
            self.assertEqual(expected_tmpfile_md5, tmpfile_md5)
        finally:
            if os.path.isfile(tmp_filepath):
                os.remove(tmp_filepath)

    def test_transform_command_for_small_serverset(self):
        command = '/usr/local/bin/zk_download_data.py -f /var/serverset/discovery.stingray_dsl_mapper.prod -p /discovery/stingray_dsl_mapper/prod -m serverset'
        notification_timestamp = 1426859717.707331
        small_serverset_value = SMALL_SERVERSET_EXAMPLE

        (transformed_command, tmp_filepath) = transform_command_with_value(