# Path: hoplite/builtin_plugins/download_folder_from_ftp_job.py
# def run(config, status):
#     """
#     Job to recursively download a directory from an FTP server
#     This will overwrite any files that are in the dest_root directory
#     """
#     ftp_addr = config.get(KEYS.SERVER_ADDRESS, "localhost")
#     ftp_port = config.get(KEYS.SERVER_PORT, 21)
#     ftp_root = config.get(KEYS.FTP_ROOT, "/")
#     dest_root = config.get(KEYS.DEST_ROOT, "C:\\temp\\")
#     user = config.get(KEYS.USERNAME, "")
#     password = config.get(KEYS.PASSWORD, "")
# 
#     try:
#         ftp_session = FTP()
#         ftp_session.connect(ftp_addr, ftp_port)
#         ftp_session.login(user, password)
#     except socket.gaierror, e:
#         status.update({"error": str(e)})
#         logger.error(e)
#         return
#     logger.debug("connected {0}")
#     download_files(ftp_session, ftp_root, dest_root, status)
#     ftp_session.close()
# 
# Path: hoplite/builtin_plugins/constants.py
# class DownloadFolderFromFtpJobConstants(object):
#     DEST_ROOT = "dest_root"
#     FTP_ROOT = "ftp_root"
#     SERVER_ADDRESS = "server_address"
#     SERVER_PORT = "server_port"
#     USERNAME = "username"
#     PASSWORD = "password"
# 
# Path: hoplite/client/status_updater.py
# class MockStatusUpdater(object):
#     """
#     For use while developing a job
#     """
#     def __init__(self, addr="0.0.0.0:5000", uuid="{0}", api_key=""):
#         self.addr = addr
#         self._uuid = uuid
#         self._daemon_addr = 'http://{0}'.format(self.addr)
#         self._api_key = api_key
#         self.status = {}
# 
#     def update(self, status):
#         """
#         Prints the status to stdout for debugging and sets the
#         member variable status for use in testing status updates
# 
#         :param status: status dictionary
#         """
#         self.status = status
#         print(self.status)


import unittest2
import os
import socket
import tempfile
import threading
import shutil
import platform
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from hoplite.builtin_plugins.download_folder_from_ftp_job import run
from hoplite.builtin_plugins.constants import DownloadFolderFromFtpJobConstants as FtpKeys
from hoplite.client.status_updater import MockStatusUpdater
            self.assertEqual(status.status, {"error": "[Errno 11004] getaddrinfo failed"})
        elif opsys == 'linux':
            self.assertEqual(status.status, {'error': '[Errno -2] Name or service not known'})
        elif opsys == 'darwin':
            self.assertEqual(status.status, {'error': '[Errno 8] nodename nor servname provided, or not known'})

    def test_file_already_exists_on_local_overwrites(self):
        # stat()[-2] is the time of mose recent content modification
        self._run_job(self.config)
        first_mod_date = os.stat(os.path.join(self.temp_dir, "test_file_1.txt"))[-2]
        self._run_job(self.config)
        second_mod_date = os.stat(os.path.join(self.temp_dir, "test_file_1.txt"))[-2]
        self.assertGreaterEqual(second_mod_date, first_mod_date)

    def _validate_directory(self, directory):
        file_1_path = os.path.join(directory, "test_file_1.txt")
        file_2_path = os.path.join(directory, "test_directory_1", "test_file_2.txt")

        with open(file_1_path) as f:
            expected = "Hello world!"
            actual = f.read()
            self.assertEqual(expected, actual)

        with open(file_2_path) as f:
            expected = "Hello moto!"
            actual = f.read()
            self.assertEqual(expected, actual)

    def _run_job(self, config):
        status = MockStatusUpdater()

# Path: hoplite/builtin_plugins/download_folder_from_ftp_job.py
# def run(config, status):
#     """
#     Job to recursively download a directory from an FTP server
#     This will overwrite any files that are in the dest_root directory
#     """
#     ftp_addr = config.get(KEYS.SERVER_ADDRESS, "localhost")
#     ftp_port = config.get(KEYS.SERVER_PORT, 21)
#     ftp_root = config.get(KEYS.FTP_ROOT, "/")
#     dest_root = config.get(KEYS.DEST_ROOT, "C:\\temp\\")
#     user = config.get(KEYS.USERNAME, "")
#     password = config.get(KEYS.PASSWORD, "")
# 
#     try:
#         ftp_session = FTP()
#         ftp_session.connect(ftp_addr, ftp_port)
#         ftp_session.login(user, password)
#     except socket.gaierror, e:
#         status.update({"error": str(e)})
#         logger.error(e)
#         return
#     logger.debug("connected {0}")
#     download_files(ftp_session, ftp_root, dest_root, status)
#     ftp_session.close()
# 
# Path: hoplite/builtin_plugins/constants.py
# class DownloadFolderFromFtpJobConstants(object):
#     DEST_ROOT = "dest_root"
#     FTP_ROOT = "ftp_root"
#     SERVER_ADDRESS = "server_address"
#     SERVER_PORT = "server_port"
#     USERNAME = "username"
#     PASSWORD = "password"
# 
# Path: hoplite/client/status_updater.py
# class MockStatusUpdater(object):
#     """
#     For use while developing a job
#     """
#     def __init__(self, addr="0.0.0.0:5000", uuid="{0}", api_key=""):
#         self.addr = addr
#         self._uuid = uuid
#         self._daemon_addr = 'http://{0}'.format(self.addr)
#         self._api_key = api_key
#         self.status = {}
# 
#     def update(self, status):
#         """
#         Prints the status to stdout for debugging and sets the
#         member variable status for use in testing status updates
# 
#         :param status: status dictionary
#         """
#         self.status = status
#         print(self.status)


# Path: tests/builtin_plugins/test_download_folder_from_ftp_job.py
import unittest2
import os
import socket
import tempfile
import threading
import shutil
import platform
from pyftpdlib.authorizers import DummyAuthorizer
from pyftpdlib.handlers import FTPHandler
from pyftpdlib.servers import FTPServer
from hoplite.builtin_plugins.download_folder_from_ftp_job import run
from hoplite.builtin_plugins.constants import DownloadFolderFromFtpJobConstants as FtpKeys
from hoplite.client.status_updater import MockStatusUpdater

            self.assertEqual(status.status, {"error": "[Errno 11004] getaddrinfo failed"})
        elif opsys == 'linux':
            self.assertEqual(status.status, {'error': '[Errno -2] Name or service not known'})
        elif opsys == 'darwin':
            self.assertEqual(status.status, {'error': '[Errno 8] nodename nor servname provided, or not known'})

    def test_file_already_exists_on_local_overwrites(self):
        # stat()[-2] is the time of mose recent content modification
        self._run_job(self.config)
        first_mod_date = os.stat(os.path.join(self.temp_dir, "test_file_1.txt"))[-2]
        self._run_job(self.config)
        second_mod_date = os.stat(os.path.join(self.temp_dir, "test_file_1.txt"))[-2]
        self.assertGreaterEqual(second_mod_date, first_mod_date)

    def _validate_directory(self, directory):
        file_1_path = os.path.join(directory, "test_file_1.txt")
        file_2_path = os.path.join(directory, "test_directory_1", "test_file_2.txt")

        with open(file_1_path) as f:
            expected = "Hello world!"
            actual = f.read()
            self.assertEqual(expected, actual)

        with open(file_2_path) as f:
            expected = "Hello moto!"
            actual = f.read()
            self.assertEqual(expected, actual)

    def _run_job(self, config):
        status = MockStatusUpdater()

        run(config, status)