示例#1
0
文件: gitfs.py 项目: bsemar/IntCont
 def prep_server(cls):
     '''
     Set up all the webserver paths. Designed to be run once in a
     setUpClass function.
     '''
     cls.root_dir = tempfile.mkdtemp(dir=TMP)
     cls.config_dir = os.path.join(cls.root_dir, 'config')
     cls.nginx_conf = os.path.join(cls.config_dir, 'nginx.conf')
     cls.uwsgi_conf = os.path.join(cls.config_dir, 'uwsgi.yml')
     cls.git_dir = os.path.join(cls.root_dir, 'git')
     cls.repo_dir = os.path.join(cls.git_dir, 'repos')
     cls.venv_dir = os.path.join(cls.root_dir, 'venv')
     cls.uwsgi_bin = os.path.join(cls.venv_dir, 'bin', 'uwsgi')
     cls.nginx_port = cls.uwsgi_port = get_unused_localhost_port()
     while cls.uwsgi_port == cls.nginx_port:
         # Ensure we don't hit a corner case in which two sucessive calls to
         # get_unused_localhost_port() return identical port numbers.
         cls.uwsgi_port = get_unused_localhost_port()
     cls.url = 'http://127.0.0.1:{port}/repo.git'.format(port=cls.nginx_port)
     cls.ext_opts = {'url': cls.url}
     # Add auth params if present (if so this will trigger the spawned
     # server to turn on HTTP basic auth).
     for credential_param in ('user', 'password'):
         if hasattr(cls, credential_param):
             cls.ext_opts[credential_param] = getattr(cls, credential_param)
示例#2
0
 def setUp(self):
     self.listen_on = "127.0.0.1"
     self.port = get_unused_localhost_port()
     self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     self.sock.bind((self.listen_on, self.port))
     self.sock.listen(1)
示例#3
0
 def setUp(self):
     self.test_server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
     port = get_unused_localhost_port()
     self.test_server.bind(("127.0.0.1", port))
     self.test_server.settimeout(2)
     self.logger = logging.getLogger("test_logstash_logger")
     self.logger.setLevel(logging.DEBUG)
     self.logger.addHandler(DatagramLogstashHandler("127.0.0.1", port))
示例#4
0
    def setUp(self):
        self.context = zmq.Context()
        port = get_unused_localhost_port()

        self.zmq_server = self.context.socket(zmq.SUB)
        self.zmq_server.setsockopt(zmq.SUBSCRIBE, b"")
        self.zmq_server.bind("tcp://127.0.0.1:{}".format(port))

        self.logger = logging.getLogger("test_logstash_logger")
        self.logger.setLevel(logging.DEBUG)
        self.logger.addHandler(ZMQLogstashHander("tcp://127.0.0.1:{}".format(port)))
示例#5
0
 def setUpClass(cls):
     ret_port = get_unused_localhost_port()
     publish_port = get_unused_localhost_port()
     tcp_master_pub_port = get_unused_localhost_port()
     tcp_master_pull_port = get_unused_localhost_port()
     tcp_master_publish_pull = get_unused_localhost_port()
     tcp_master_workers = get_unused_localhost_port()
     cls.master_config = cls.get_temp_config(
         'master',
         **{'transport': 'zeromq',
            'auto_accept': True,
            'ret_port': ret_port,
            'publish_port': publish_port,
            'tcp_master_pub_port': tcp_master_pub_port,
            'tcp_master_pull_port': tcp_master_pull_port,
            'tcp_master_publish_pull': tcp_master_publish_pull,
            'tcp_master_workers': tcp_master_workers,
            'sign_pub_messages': False,
         }
     )
     salt.master.SMaster.secrets['aes'] = {
         'secret': multiprocessing.Array(
             ctypes.c_char,
             six.b(salt.crypt.Crypticle.generate_key_string()),
         ),
     }
     cls.minion_config = cls.get_temp_config(
         'minion',
         **{'transport': 'zeromq',
            'master_ip': '127.0.0.1',
            'master_port': ret_port,
            'auth_timeout': 5,
            'auth_tries': 1,
            'master_uri': 'tcp://127.0.0.1:{0}'.format(ret_port)}
     )
示例#6
0
 def setUpClass(cls):
     ret_port = get_unused_localhost_port()
     publish_port = get_unused_localhost_port()
     tcp_master_pub_port = get_unused_localhost_port()
     tcp_master_pull_port = get_unused_localhost_port()
     tcp_master_publish_pull = get_unused_localhost_port()
     tcp_master_workers = get_unused_localhost_port()
     cls.master_config = cls.get_temp_config(
         "master", **{
             "transport": "zeromq",
             "auto_accept": True,
             "ret_port": ret_port,
             "publish_port": publish_port,
             "tcp_master_pub_port": tcp_master_pub_port,
             "tcp_master_pull_port": tcp_master_pull_port,
             "tcp_master_publish_pull": tcp_master_publish_pull,
             "tcp_master_workers": tcp_master_workers,
             "sign_pub_messages": False,
         })
     salt.master.SMaster.secrets["aes"] = {
         "secret":
         multiprocessing.Array(
             ctypes.c_char,
             six.b(salt.crypt.Crypticle.generate_key_string()),
         ),
     }
     cls.minion_config = cls.get_temp_config(
         "minion", **{
             "transport": "zeromq",
             "master_ip": "127.0.0.1",
             "master_port": ret_port,
             "auth_timeout": 5,
             "auth_tries": 1,
             "master_uri": "tcp://127.0.0.1:{0}".format(ret_port),
         })
示例#7
0
 def setUpClass(cls):
     overrides = {
         'publish_port': get_unused_localhost_port(),
         'ret_port': get_unused_localhost_port(),
         'tcp_master_pub_port': get_unused_localhost_port(),
         'tcp_master_pull_port': get_unused_localhost_port(),
         'tcp_master_publish_pull': get_unused_localhost_port(),
         'tcp_master_workers': get_unused_localhost_port(),
         'runtests_conn_check_port': get_unused_localhost_port(),
         'runtests_log_port': get_unused_localhost_port()
     }
     overrides['pytest_engine_port'] = overrides['runtests_conn_check_port']
     temp_config = AdaptedConfigurationTestCaseMixin.get_temp_config('master', **overrides)
     cls.root_dir = temp_config['root_dir']
     cls.config_dir = os.path.dirname(temp_config['conf_file'])
示例#8
0
文件: gitfs.py 项目: bsemar/IntCont
 def prep_server(cls):
     cls.sshd_config_dir = tempfile.mkdtemp(dir=TMP)
     cls.sshd_config = os.path.join(cls.sshd_config_dir, 'sshd_config')
     cls.sshd_port = get_unused_localhost_port()
     cls.url = 'ssh://{username}@127.0.0.1:{port}/~/repo.git'.format(
         username=cls.username,
         port=cls.sshd_port)
     home = '/root/.ssh'
     cls.ext_opts = {
         'url': cls.url,
         'privkey_nopass': os.path.join(home, cls.id_rsa_nopass),
         'pubkey_nopass': os.path.join(home, cls.id_rsa_nopass + '.pub'),
         'privkey_withpass': os.path.join(home, cls.id_rsa_withpass),
         'pubkey_withpass': os.path.join(home, cls.id_rsa_withpass + '.pub'),
         'passphrase': cls.passphrase}
示例#9
0
    def setUpClass(cls):
        ret_port = get_unused_localhost_port()
        publish_port = get_unused_localhost_port()
        tcp_master_pub_port = get_unused_localhost_port()
        tcp_master_pull_port = get_unused_localhost_port()
        tcp_master_publish_pull = get_unused_localhost_port()
        tcp_master_workers = get_unused_localhost_port()
        cls.master_config = cls.get_temp_config(
            "master",
            **{
                "transport": "zeromq",
                "auto_accept": True,
                "ret_port": ret_port,
                "publish_port": publish_port,
                "tcp_master_pub_port": tcp_master_pub_port,
                "tcp_master_pull_port": tcp_master_pull_port,
                "tcp_master_publish_pull": tcp_master_publish_pull,
                "tcp_master_workers": tcp_master_workers,
            }
        )

        cls.minion_config = salt.config.minion_config(os.path.join(RUNTIME_VARS.TMP_CONF_DIR, 'minion'))
        cls.minion_config = cls.get_temp_config(
            "minion",
            **{
                "transport": "zeromq",
                "master_ip": "127.0.0.1",
                "master_port": ret_port,
                "master_uri": "tcp://127.0.0.1:{0}".format(ret_port),
            }
        )

        cls.process_manager = salt.utils.process.ProcessManager(
            name="ReqServer_ProcessManager"
        )

        cls.server_channel = salt.transport.server.PubServerChannel.factory(
            cls.master_config
        )
        cls.server_channel.pre_fork(cls.process_manager)

        # we also require req server for auth
        cls.req_server_channel = salt.transport.server.ReqServerChannel.factory(
            cls.master_config
        )
        cls.req_server_channel.pre_fork(cls.process_manager)

        cls._server_io_loop = salt.ext.tornado.ioloop.IOLoop()
        cls.evt = threading.Event()
        cls.req_server_channel.post_fork(
            cls._handle_payload, io_loop=cls._server_io_loop
        )
        cls.server_thread = threading.Thread(
            target=run_loop_in_thread, args=(cls._server_io_loop, cls.evt)
        )
        cls.server_thread.start()
示例#10
0
 def setUpClass(cls):
     overrides = {
         "publish_port": get_unused_localhost_port(),
         "ret_port": get_unused_localhost_port(),
         "tcp_master_pub_port": get_unused_localhost_port(),
         "tcp_master_pull_port": get_unused_localhost_port(),
         "tcp_master_publish_pull": get_unused_localhost_port(),
         "tcp_master_workers": get_unused_localhost_port(),
         "runtests_conn_check_port": get_unused_localhost_port(),
         "runtests_log_port": get_unused_localhost_port(),
     }
     overrides["pytest_engine_port"] = overrides["runtests_conn_check_port"]
     temp_config = AdaptedConfigurationTestCaseMixin.get_temp_config(
         "master", **overrides)
     cls.root_dir = temp_config["root_dir"]
     cls.config_dir = os.path.dirname(temp_config["conf_file"])
     if temp_config["transport"] == "tcp":
         pytest.skip("Test only applicable to the ZMQ transport")
示例#11
0
 def setUpClass(cls):
     '''
     start tornado app on thread
     and wait till its running
     '''
     cls.server_port = get_unused_localhost_port()
     cls.server_thread = threading.Thread(target=cls.webserver)
     cls.server_thread.daemon = True
     cls.server_thread.start()
     cls.archive_tar_source = 'http://localhost:{0}/custom.tar.gz'.format(cls.server_port)
     # check if tornado app is up
     port_closed = True
     while port_closed:
         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         result = sock.connect_ex(('127.0.0.1', cls.server_port))
         if result == 0:
             port_closed = False
示例#12
0
    def setUpClass(cls):
        ret_port = get_unused_localhost_port()
        publish_port = get_unused_localhost_port()
        tcp_master_pub_port = get_unused_localhost_port()
        tcp_master_pull_port = get_unused_localhost_port()
        tcp_master_publish_pull = get_unused_localhost_port()
        tcp_master_workers = get_unused_localhost_port()
        cls.master_config = cls.get_temp_config(
            'master', **{
                'transport': 'tcp',
                'auto_accept': True,
                'ret_port': ret_port,
                'publish_port': publish_port,
                'tcp_master_pub_port': tcp_master_pub_port,
                'tcp_master_pull_port': tcp_master_pull_port,
                'tcp_master_publish_pull': tcp_master_publish_pull,
                'tcp_master_workers': tcp_master_workers
            })

        cls.minion_config = cls.get_temp_config(
            'minion', **{
                'transport': 'tcp',
                'master_ip': '127.0.0.1',
                'auth_timeout': 1,
                'master_port': ret_port,
                'master_uri': 'tcp://127.0.0.1:{0}'.format(ret_port)
            })

        cls.process_manager = salt.utils.process.ProcessManager(
            name='ReqServer_ProcessManager')

        cls.server_channel = salt.transport.server.PubServerChannel.factory(
            cls.master_config)
        cls.server_channel.pre_fork(cls.process_manager)

        # we also require req server for auth
        cls.req_server_channel = salt.transport.server.ReqServerChannel.factory(
            cls.master_config)
        cls.req_server_channel.pre_fork(cls.process_manager)

        cls._server_io_loop = tornado.ioloop.IOLoop()
        cls.req_server_channel.post_fork(cls._handle_payload,
                                         io_loop=cls._server_io_loop)

        def run_loop_in_thread(loop):
            loop.make_current()
            loop.start()

        cls.server_thread = threading.Thread(target=run_loop_in_thread,
                                             args=(cls._server_io_loop, ))
        cls.server_thread.daemon = True
        cls.server_thread.start()
示例#13
0
    def setUpClass(cls):
        if not hasattr(cls, "_handle_payload"):
            return
        ret_port = get_unused_localhost_port()
        publish_port = get_unused_localhost_port()
        tcp_master_pub_port = get_unused_localhost_port()
        tcp_master_pull_port = get_unused_localhost_port()
        tcp_master_publish_pull = get_unused_localhost_port()
        tcp_master_workers = get_unused_localhost_port()
        cls.master_config = cls.get_temp_config(
            "master",
            **{
                "transport": "zeromq",
                "auto_accept": True,
                "ret_port": ret_port,
                "publish_port": publish_port,
                "tcp_master_pub_port": tcp_master_pub_port,
                "tcp_master_pull_port": tcp_master_pull_port,
                "tcp_master_publish_pull": tcp_master_publish_pull,
                "tcp_master_workers": tcp_master_workers,
            }
        )

        cls.minion_config = cls.get_temp_config(
            "minion",
            **{
                "transport": "zeromq",
                "master_ip": "127.0.0.1",
                "master_port": ret_port,
                "auth_timeout": 5,
                "auth_tries": 1,
                "master_uri": "tcp://127.0.0.1:{0}".format(ret_port),
            }
        )

        cls.process_manager = salt.utils.process.ProcessManager(
            name="ReqServer_ProcessManager"
        )

        cls.server_channel = salt.transport.server.ReqServerChannel.factory(
            cls.master_config
        )
        cls.server_channel.pre_fork(cls.process_manager)

        cls.io_loop = salt.ext.tornado.ioloop.IOLoop()
        cls.evt = threading.Event()
        cls.server_channel.post_fork(cls._handle_payload, io_loop=cls.io_loop)
        cls.server_thread = threading.Thread(
            target=run_loop_in_thread, args=(cls.io_loop, cls.evt)
        )
        cls.server_thread.start()
示例#14
0
    def setUpClass(cls):
        ret_port = get_unused_localhost_port()
        publish_port = get_unused_localhost_port()
        tcp_master_pub_port = get_unused_localhost_port()
        tcp_master_pull_port = get_unused_localhost_port()
        tcp_master_publish_pull = get_unused_localhost_port()
        tcp_master_workers = get_unused_localhost_port()
        cls.master_config = cls.get_temp_config(
            'master', **{
                'transport': 'zeromq',
                'auto_accept': True,
                'ret_port': ret_port,
                'publish_port': publish_port,
                'tcp_master_pub_port': tcp_master_pub_port,
                'tcp_master_pull_port': tcp_master_pull_port,
                'tcp_master_publish_pull': tcp_master_publish_pull,
                'tcp_master_workers': tcp_master_workers
            })

        cls.minion_config = salt.config.minion_config(
            os.path.join(TMP_CONF_DIR, 'minion'))
        cls.minion_config = cls.get_temp_config(
            'minion', **{
                'transport': 'zeromq',
                'master_ip': '127.0.0.1',
                'master_port': ret_port,
                'master_uri': 'tcp://127.0.0.1:{0}'.format(ret_port)
            })

        cls.process_manager = salt.utils.process.ProcessManager(
            name='ReqServer_ProcessManager')

        cls.server_channel = salt.transport.server.PubServerChannel.factory(
            cls.master_config)
        cls.server_channel.pre_fork(cls.process_manager)

        # we also require req server for auth
        cls.req_server_channel = salt.transport.server.ReqServerChannel.factory(
            cls.master_config)
        cls.req_server_channel.pre_fork(cls.process_manager)

        cls._server_io_loop = zmq.eventloop.ioloop.ZMQIOLoop()
        cls.evt = threading.Event()
        cls.req_server_channel.post_fork(cls._handle_payload,
                                         io_loop=cls._server_io_loop)
        cls.server_thread = threading.Thread(target=run_loop_in_thread,
                                             args=(cls._server_io_loop,
                                                   cls.evt))
        cls.server_thread.start()
示例#15
0
    def setUpClass(cls):
        if not hasattr(cls, '_handle_payload'):
            return
        ret_port = get_unused_localhost_port()
        publish_port = get_unused_localhost_port()
        tcp_master_pub_port = get_unused_localhost_port()
        tcp_master_pull_port = get_unused_localhost_port()
        tcp_master_publish_pull = get_unused_localhost_port()
        tcp_master_workers = get_unused_localhost_port()
        cls.master_config = cls.get_temp_config(
            'master', **{
                'transport': 'tcp',
                'auto_accept': True,
                'ret_port': ret_port,
                'publish_port': publish_port,
                'tcp_master_pub_port': tcp_master_pub_port,
                'tcp_master_pull_port': tcp_master_pull_port,
                'tcp_master_publish_pull': tcp_master_publish_pull,
                'tcp_master_workers': tcp_master_workers
            })

        cls.minion_config = cls.get_temp_config(
            'minion', **{
                'transport': 'tcp',
                'master_ip': '127.0.0.1',
                'master_port': ret_port,
                'master_uri': 'tcp://127.0.0.1:{0}'.format(ret_port)
            })

        cls.process_manager = salt.utils.process.ProcessManager(
            name='ReqServer_ProcessManager')

        cls.server_channel = salt.transport.server.ReqServerChannel.factory(
            cls.master_config)
        cls.server_channel.pre_fork(cls.process_manager)
        cls.io_loop = tornado.ioloop.IOLoop()
        cls.stop = threading.Event()
        cls.server_channel.post_fork(cls._handle_payload, io_loop=cls.io_loop)
        cls.server_thread = threading.Thread(
            target=run_loop_in_thread,
            args=(
                cls.io_loop,
                cls.stop,
            ),
        )
        cls.server_thread.start()
示例#16
0
    def setUpClass(cls):
        if not hasattr(cls, '_handle_payload'):
            return
        ret_port = get_unused_localhost_port()
        publish_port = get_unused_localhost_port()
        tcp_master_pub_port = get_unused_localhost_port()
        tcp_master_pull_port = get_unused_localhost_port()
        tcp_master_publish_pull = get_unused_localhost_port()
        tcp_master_workers = get_unused_localhost_port()
        cls.master_config = cls.get_temp_config(
            'master', **{
                'transport': 'zeromq',
                'auto_accept': True,
                'ret_port': ret_port,
                'publish_port': publish_port,
                'tcp_master_pub_port': tcp_master_pub_port,
                'tcp_master_pull_port': tcp_master_pull_port,
                'tcp_master_publish_pull': tcp_master_publish_pull,
                'tcp_master_workers': tcp_master_workers
            })

        cls.minion_config = cls.get_temp_config(
            'minion', **{
                'transport': 'zeromq',
                'master_ip': '127.0.0.1',
                'master_port': ret_port,
                'auth_timeout': 5,
                'auth_tries': 1,
                'master_uri': 'tcp://127.0.0.1:{0}'.format(ret_port)
            })

        cls.process_manager = salt.utils.process.ProcessManager(
            name='ReqServer_ProcessManager')

        cls.server_channel = salt.transport.server.ReqServerChannel.factory(
            cls.master_config)
        cls.server_channel.pre_fork(cls.process_manager)

        cls.io_loop = zmq.eventloop.ioloop.ZMQIOLoop()
        cls.io_loop.make_current()
        cls.server_channel.post_fork(cls._handle_payload, io_loop=cls.io_loop)

        cls.server_thread = threading.Thread(target=cls.io_loop.start)
        cls.server_thread.daemon = True
        cls.server_thread.start()
示例#17
0
文件: gitfs.py 项目: fake-name/salt
    def setUpClass(cls):  # pylint: disable=arguments-differ
        super(SSHDMixin, cls).setUpClass()
        try:
            log.info("%s: prep_server()", cls.__name__)
            cls.sshd_bin = salt.utils.path.which("sshd")
            cls.sshd_config_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
            cls.sshd_config = os.path.join(cls.sshd_config_dir, "sshd_config")
            cls.sshd_port = get_unused_localhost_port()
            cls.url = "ssh://{username}@127.0.0.1:{port}/~/repo.git".format(
                username=cls.username, port=cls.sshd_port
            )
            cls.url_extra_repo = "ssh://{username}@127.0.0.1:{port}/~/extra_repo.git".format(
                username=cls.username, port=cls.sshd_port
            )
            home = "/root/.ssh"
            cls.ext_opts = {
                "url": cls.url,
                "url_extra_repo": cls.url_extra_repo,
                "privkey_nopass": os.path.join(home, cls.id_rsa_nopass),
                "pubkey_nopass": os.path.join(home, cls.id_rsa_nopass + ".pub"),
                "privkey_withpass": os.path.join(home, cls.id_rsa_withpass),
                "pubkey_withpass": os.path.join(home, cls.id_rsa_withpass + ".pub"),
                "passphrase": cls.passphrase,
            }

            if cls.prep_states_ran is False:
                ret = cls.cls_run_function(
                    "state.apply",
                    mods="git_pillar.ssh",
                    pillar={
                        "git_pillar": {
                            "git_ssh": cls.git_ssh,
                            "id_rsa_nopass": cls.id_rsa_nopass,
                            "id_rsa_withpass": cls.id_rsa_withpass,
                            "sshd_bin": cls.sshd_bin,
                            "sshd_port": cls.sshd_port,
                            "sshd_config_dir": cls.sshd_config_dir,
                            "master_user": cls.user,
                            "user": cls.username,
                        }
                    },
                )
                assert next(six.itervalues(ret))["result"] is True
                cls.prep_states_ran = True
                log.info("%s: States applied", cls.__name__)
            if cls.sshd_proc is not None:
                if not psutil.pid_exists(cls.sshd_proc.pid):
                    log.info(
                        "%s: sshd started but appears to be dead now. Will try to restart it.",
                        cls.__name__,
                    )
                    cls.sshd_proc = None
            if cls.sshd_proc is None:
                cls.sshd_proc = start_daemon(
                    cls.sshd_bin, cls.sshd_config_dir, cls.sshd_port, SshdDaemon
                )
                log.info("%s: sshd started", cls.__name__)
        except AssertionError:
            cls.tearDownClass()
            six.reraise(*sys.exc_info())

        if cls.known_hosts_setup is False:
            known_hosts_ret = cls.cls_run_function(
                "ssh.set_known_host",
                user=cls.user,
                hostname="127.0.0.1",
                port=cls.sshd_port,
                enc="ssh-rsa",
                fingerprint="fd:6f:7f:5d:06:6b:f2:06:0d:26:93:9e:5a:b5:19:46",
                hash_known_hosts=False,
                fingerprint_hash_type="md5",
            )
            if "error" in known_hosts_ret:
                cls.tearDownClass()
                raise AssertionError(
                    "Failed to add key to {0} user's known_hosts "
                    "file: {1}".format(
                        cls.master_opts["user"], known_hosts_ret["error"]
                    )
                )
            cls.known_hosts_setup = True
示例#18
0
    def test_cache_remote_file(self):
        '''
        cp.cache_file
        '''
        nginx_port = get_unused_localhost_port()
        url_prefix = 'http://localhost:{0}/'.format(nginx_port)
        temp_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
        self.addCleanup(shutil.rmtree, temp_dir, ignore_errors=True)
        nginx_root_dir = os.path.join(temp_dir, 'root')
        nginx_conf_dir = os.path.join(temp_dir, 'conf')
        nginx_conf = os.path.join(nginx_conf_dir, 'nginx.conf')
        nginx_pidfile = os.path.join(nginx_conf_dir, 'nginx.pid')
        file_contents = 'Hello world!'

        for dirname in (nginx_root_dir, nginx_conf_dir):
            os.makedirs(dirname)

        # Write the temp file
        with salt.utils.files.fopen(os.path.join(nginx_root_dir, 'actual_file'), 'w') as fp_:
            fp_.write(salt.utils.stringutils.to_str(file_contents))

        # Write the nginx config
        with salt.utils.files.fopen(nginx_conf, 'w') as fp_:
            fp_.write(textwrap.dedent(salt.utils.stringutils.to_str(
                '''\
                user root;
                worker_processes 1;
                error_log {nginx_conf_dir}/server_error.log;
                pid {nginx_pidfile};

                events {{
                    worker_connections 1024;
                }}

                http {{
                    include       /etc/nginx/mime.types;
                    default_type  application/octet-stream;

                    access_log {nginx_conf_dir}/access.log;
                    error_log {nginx_conf_dir}/error.log;

                    server {{
                        listen {nginx_port} default_server;
                        server_name cachefile.local;
                        root {nginx_root_dir};

                        location ~ ^/301$ {{
                            return 301 /actual_file;
                        }}

                        location ~ ^/302$ {{
                            return 302 /actual_file;
                        }}
                    }}
                }}'''.format(**locals())
            )))

        self.run_function(
            'cmd.run',
            [['nginx', '-c', nginx_conf]],
            python_shell=False
        )
        with salt.utils.files.fopen(nginx_pidfile) as fp_:
            nginx_pid = int(fp_.read().strip())
            nginx_proc = psutil.Process(pid=nginx_pid)
            self.addCleanup(nginx_proc.send_signal, signal.SIGQUIT)

        for code in ('', '301', '302'):
            url = url_prefix + (code or 'actual_file')
            log.debug('attempting to cache %s', url)
            ret = self.run_function('cp.cache_file', [url])
            self.assertTrue(ret)
            with salt.utils.files.fopen(ret) as fp_:
                cached_contents = salt.utils.stringutils.to_unicode(fp_.read())
                self.assertEqual(cached_contents, file_contents)
示例#19
0
    def setUpClass(cls):  # pylint: disable=arguments-differ
        super(SSHDMixin, cls).setUpClass()
        try:
            log.info('%s: prep_server()', cls.__name__)
            cls.sshd_bin = salt.utils.path.which('sshd')
            cls.sshd_config_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
            cls.sshd_config = os.path.join(cls.sshd_config_dir, 'sshd_config')
            cls.sshd_port = get_unused_localhost_port()
            cls.url = 'ssh://{username}@127.0.0.1:{port}/~/repo.git'.format(
                username=cls.username, port=cls.sshd_port)
            cls.url_extra_repo = 'ssh://{username}@127.0.0.1:{port}/~/extra_repo.git'.format(
                username=cls.username, port=cls.sshd_port)
            home = '/root/.ssh'
            cls.ext_opts = {
                'url': cls.url,
                'url_extra_repo': cls.url_extra_repo,
                'privkey_nopass': os.path.join(home, cls.id_rsa_nopass),
                'pubkey_nopass': os.path.join(home,
                                              cls.id_rsa_nopass + '.pub'),
                'privkey_withpass': os.path.join(home, cls.id_rsa_withpass),
                'pubkey_withpass': os.path.join(home,
                                                cls.id_rsa_withpass + '.pub'),
                'passphrase': cls.passphrase
            }

            if cls.prep_states_ran is False:
                ret = cls.cls_run_function('state.apply',
                                           mods='git_pillar.ssh',
                                           pillar={
                                               'git_pillar': {
                                                   'git_ssh': cls.git_ssh,
                                                   'id_rsa_nopass':
                                                   cls.id_rsa_nopass,
                                                   'id_rsa_withpass':
                                                   cls.id_rsa_withpass,
                                                   'sshd_bin': cls.sshd_bin,
                                                   'sshd_port': cls.sshd_port,
                                                   'sshd_config_dir':
                                                   cls.sshd_config_dir,
                                                   'master_user': cls.user,
                                                   'user': cls.username
                                               }
                                           })
                assert next(six.itervalues(ret))['result'] is True
                cls.prep_states_ran = True
                log.info('%s: States applied', cls.__name__)
            if cls.sshd_proc is not None:
                if not psutil.pid_exists(cls.sshd_proc.pid):
                    log.info(
                        '%s: sshd started but appears to be dead now. Will try to restart it.',
                        cls.__name__)
                    cls.sshd_proc = None
            if cls.sshd_proc is None:
                cls.sshd_proc = start_daemon(cls.sshd_bin, cls.sshd_config_dir,
                                             cls.sshd_port, SshdDaemon)
                log.info('%s: sshd started', cls.__name__)
        except AssertionError:
            cls.tearDownClass()
            six.reraise(*sys.exc_info())

        if cls.known_hosts_setup is False:
            known_hosts_ret = cls.cls_run_function(
                'ssh.set_known_host',
                user=cls.user,
                hostname='127.0.0.1',
                port=cls.sshd_port,
                enc='ssh-rsa',
                fingerprint='fd:6f:7f:5d:06:6b:f2:06:0d:26:93:9e:5a:b5:19:46',
                hash_known_hosts=False,
                fingerprint_hash_type='md5',
            )
            if 'error' in known_hosts_ret:
                cls.tearDownClass()
                raise AssertionError(
                    'Failed to add key to {0} user\'s known_hosts '
                    'file: {1}'.format(cls.master_opts['user'],
                                       known_hosts_ret['error']))
            cls.known_hosts_setup = True
示例#20
0
    def setUpClass(cls):  # pylint: disable=arguments-differ
        '''
        Set up all the webserver paths. Designed to be run once in a
        setUpClass function.
        '''
        super(WebserverMixin, cls).setUpClass()
        cls.root_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
        cls.config_dir = os.path.join(cls.root_dir, 'config')
        cls.nginx_conf = os.path.join(cls.config_dir, 'nginx.conf')
        cls.uwsgi_conf = os.path.join(cls.config_dir, 'uwsgi.yml')
        cls.git_dir = os.path.join(cls.root_dir, 'git')
        cls.repo_dir = os.path.join(cls.git_dir, 'repos')
        cls.venv_dir = os.path.join(cls.root_dir, 'venv')
        cls.uwsgi_bin = os.path.join(cls.venv_dir, 'bin', 'uwsgi')
        cls.nginx_port = cls.uwsgi_port = get_unused_localhost_port()
        while cls.uwsgi_port == cls.nginx_port:
            # Ensure we don't hit a corner case in which two sucessive calls to
            # get_unused_localhost_port() return identical port numbers.
            cls.uwsgi_port = get_unused_localhost_port()
        cls.url = 'http://127.0.0.1:{port}/repo.git'.format(
            port=cls.nginx_port)
        cls.url_extra_repo = 'http://127.0.0.1:{port}/extra_repo.git'.format(
            port=cls.nginx_port)
        cls.ext_opts = {'url': cls.url, 'url_extra_repo': cls.url_extra_repo}
        # Add auth params if present (if so this will trigger the spawned
        # server to turn on HTTP basic auth).
        for credential_param in ('user', 'password'):
            if hasattr(cls, credential_param):
                cls.ext_opts[credential_param] = getattr(cls, credential_param)
        auth_enabled = hasattr(cls, 'username') and hasattr(cls, 'password')
        pillar = {
            'git_pillar': {
                'config_dir': cls.config_dir,
                'git_dir': cls.git_dir,
                'venv_dir': cls.venv_dir,
                'root_dir': cls.root_dir,
                'nginx_port': cls.nginx_port,
                'uwsgi_port': cls.uwsgi_port,
                'auth_enabled': auth_enabled
            }
        }

        # Different libexec dir for git backend on Debian-based systems
        git_core = '/usr/libexec/git-core'
        if not os.path.exists(git_core):
            git_core = '/usr/lib/git-core'

        if not os.path.exists(git_core):
            cls.tearDownClass()
            raise AssertionError(
                '{} not found. Either git is not installed, or the test '
                'class needs to be updated.'.format(git_core))

        pillar['git_pillar']['git-http-backend'] = os.path.join(
            git_core, 'git-http-backend')
        try:
            if cls.prep_states_ran is False:
                ret = cls.cls_run_function('state.apply',
                                           mods='git_pillar.http',
                                           pillar=pillar)
                assert next(six.itervalues(ret))['result'] is True
                cls.prep_states_ran = True
                log.info('%s: States applied', cls.__name__)
            if cls.uwsgi_proc is not None:
                if not psutil.pid_exists(cls.uwsgi_proc.pid):
                    log.warning(
                        '%s: uWsgi started but appears to be dead now. Will try to restart it.',
                        cls.__name__)
                    cls.uwsgi_proc = None
            if cls.uwsgi_proc is None:
                cls.uwsgi_proc = start_daemon(cls.uwsgi_bin, cls.config_dir,
                                              cls.uwsgi_port, UwsgiDaemon)
                log.info('%s: %s started', cls.__name__, cls.uwsgi_bin)
            if cls.nginx_proc is not None:
                if not psutil.pid_exists(cls.nginx_proc.pid):
                    log.warning(
                        '%s: nginx started but appears to be dead now. Will try to restart it.',
                        cls.__name__)
                    cls.nginx_proc = None
            if cls.nginx_proc is None:
                cls.nginx_proc = start_daemon('nginx', cls.config_dir,
                                              cls.nginx_port, NginxDaemon)
                log.info('%s: nginx started', cls.__name__)
        except AssertionError:
            cls.tearDownClass()
            six.reraise(*sys.exc_info())
示例#21
0
    def setUpClass(cls):  # pylint: disable=arguments-differ
        """
        Set up all the webserver paths. Designed to be run once in a
        setUpClass function.
        """
        super(WebserverMixin, cls).setUpClass()
        cls.root_dir = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP)
        cls.config_dir = os.path.join(cls.root_dir, "config")
        cls.nginx_conf = os.path.join(cls.config_dir, "nginx.conf")
        cls.uwsgi_conf = os.path.join(cls.config_dir, "uwsgi.yml")
        cls.git_dir = os.path.join(cls.root_dir, "git")
        cls.repo_dir = os.path.join(cls.git_dir, "repos")
        cls.venv_dir = os.path.join(cls.root_dir, "venv")
        cls.uwsgi_bin = os.path.join(cls.venv_dir, "bin", "uwsgi")
        cls.nginx_port = cls.uwsgi_port = get_unused_localhost_port()
        while cls.uwsgi_port == cls.nginx_port:
            # Ensure we don't hit a corner case in which two sucessive calls to
            # get_unused_localhost_port() return identical port numbers.
            cls.uwsgi_port = get_unused_localhost_port()
        cls.url = "http://127.0.0.1:{port}/repo.git".format(
            port=cls.nginx_port)
        cls.url_extra_repo = "http://127.0.0.1:{port}/extra_repo.git".format(
            port=cls.nginx_port)
        cls.ext_opts = {"url": cls.url, "url_extra_repo": cls.url_extra_repo}
        # Add auth params if present (if so this will trigger the spawned
        # server to turn on HTTP basic auth).
        for credential_param in ("user", "password"):
            if hasattr(cls, credential_param):
                cls.ext_opts[credential_param] = getattr(cls, credential_param)
        auth_enabled = hasattr(cls, "username") and hasattr(cls, "password")
        pillar = {
            "git_pillar": {
                "config_dir": cls.config_dir,
                "git_dir": cls.git_dir,
                "venv_dir": cls.venv_dir,
                "root_dir": cls.root_dir,
                "nginx_port": cls.nginx_port,
                "uwsgi_port": cls.uwsgi_port,
                "auth_enabled": auth_enabled,
            }
        }

        # Different libexec dir for git backend on Debian-based systems
        git_core = "/usr/libexec/git-core"
        if not os.path.exists(git_core):
            git_core = "/usr/lib/git-core"

        if not os.path.exists(git_core):
            cls.tearDownClass()
            raise AssertionError(
                "{} not found. Either git is not installed, or the test "
                "class needs to be updated.".format(git_core))

        pillar["git_pillar"]["git-http-backend"] = os.path.join(
            git_core, "git-http-backend")
        try:
            if cls.prep_states_ran is False:
                ret = cls.cls_run_function("state.apply",
                                           mods="git_pillar.http",
                                           pillar=pillar)
                assert next(six.itervalues(ret))["result"] is True
                cls.prep_states_ran = True
                log.info("%s: States applied", cls.__name__)
            if cls.uwsgi_proc is not None:
                if not psutil.pid_exists(cls.uwsgi_proc.pid):
                    log.warning(
                        "%s: uWsgi started but appears to be dead now. Will try to restart it.",
                        cls.__name__,
                    )
                    cls.uwsgi_proc = None
            if cls.uwsgi_proc is None:
                cls.uwsgi_proc = start_daemon(cls.uwsgi_bin, cls.config_dir,
                                              cls.uwsgi_port, UwsgiDaemon)
                log.info("%s: %s started", cls.__name__, cls.uwsgi_bin)
            if cls.nginx_proc is not None:
                if not psutil.pid_exists(cls.nginx_proc.pid):
                    log.warning(
                        "%s: nginx started but appears to be dead now. Will try to restart it.",
                        cls.__name__,
                    )
                    cls.nginx_proc = None
            if cls.nginx_proc is None:
                cls.nginx_proc = start_daemon("nginx", cls.config_dir,
                                              cls.nginx_port, NginxDaemon)
                log.info("%s: nginx started", cls.__name__)
        except AssertionError:
            cls.tearDownClass()
            six.reraise(*sys.exc_info())