# 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
        small_serverset_value = SMALL_SERVERSET_EXAMPLE
        (transformed_command, tmp_filepath) = transform_command_with_value(
            command, small_serverset_value, notification_timestamp)

        expected_tmp_filepath = None
        expected_transformed_command = '/usr/local/bin/zk_download_data.py -v ' \
                                       + "'" + small_serverset_value + "'" + ' -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)


class SplitProblematicEndpointsLineTestCase(TestCase):

    def assert_splitted_parts_equal(self, desired_list, actual_list):
        desired_list.sort()
        actual_list.sort()
        self.assertEqual(desired_list, actual_list)

    def test_split_problematic_endpoints_line(self):
        line = "10.99.184.69:9001"
        splitted_parts = \
            zk_util.split_problematic_endpoints_line(line)
        self.assert_splitted_parts_equal(["10.99.184.69:9001"], splitted_parts)

        line = "10.99.184.69:900010.37.170.125:9006"
        splitted_parts = zk_util.split_problematic_endpoints_line(line)
        desired_answer = ["10.99.184.69:9000", "10.37.170.125:9006"]
        self.assert_splitted_parts_equal(desired_answer, splitted_parts)

        line = "10.99.184.69:900010.37.170.125:900610.99.184.69:9008"

# 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

        small_serverset_value = SMALL_SERVERSET_EXAMPLE
        (transformed_command, tmp_filepath) = transform_command_with_value(
            command, small_serverset_value, notification_timestamp)

        expected_tmp_filepath = None
        expected_transformed_command = '/usr/local/bin/zk_download_data.py -v ' \
                                       + "'" + small_serverset_value + "'" + ' -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)


class SplitProblematicEndpointsLineTestCase(TestCase):

    def assert_splitted_parts_equal(self, desired_list, actual_list):
        desired_list.sort()
        actual_list.sort()
        self.assertEqual(desired_list, actual_list)

    def test_split_problematic_endpoints_line(self):
        line = "10.99.184.69:9001"
        splitted_parts = \
            zk_util.split_problematic_endpoints_line(line)
        self.assert_splitted_parts_equal(["10.99.184.69:9001"], splitted_parts)

        line = "10.99.184.69:900010.37.170.125:9006"
        splitted_parts = zk_util.split_problematic_endpoints_line(line)
        desired_answer = ["10.99.184.69:9000", "10.37.170.125:9006"]
        self.assert_splitted_parts_equal(desired_answer, splitted_parts)

        line = "10.99.184.69:900010.37.170.125:900610.99.184.69:9008"

        splitted_parts = zk_util.split_problematic_endpoints_line(line)