def _get_http_defaults(default_address=None, timeout=None, retrieve_resources=None, concurrent_pool_size=4): """ :rtype: lxml.etree.Element """ cfg = etree.Element("ConfigTestElement", guiclass="HttpDefaultsGui", testclass="ConfigTestElement", testname="Defaults") if retrieve_resources: cfg.append(JMX._bool_prop("HTTPSampler.image_parser", True)) cfg.append(JMX._bool_prop("HTTPSampler.concurrentDwn", True)) if concurrent_pool_size: cfg.append(JMX._string_prop("HTTPSampler.concurrentPool", concurrent_pool_size)) params = etree.Element("elementProp", name="HTTPsampler.Arguments", elementType="Arguments", guiclass="HTTPArgumentsPanel", testclass="Arguments", testname="user_defined") cfg.append(params) if default_address: parsed_url = parse.urlsplit(default_address) if parsed_url.scheme: cfg.append(JMX._string_prop("HTTPSampler.protocol", parsed_url.scheme)) if parsed_url.hostname: cfg.append(JMX._string_prop("HTTPSampler.domain", parsed_url.hostname)) if parsed_url.port: cfg.append(JMX._string_prop("HTTPSampler.port", parsed_url.port)) if timeout: cfg.append(JMX._string_prop("HTTPSampler.connect_timeout", timeout)) cfg.append(JMX._string_prop("HTTPSampler.response_timeout", timeout)) return cfg
def test_improved_request_building(self): obj = PBenchExecutor() obj.engine = EngineEmul() obj.settings = BetterDict() obj.engine.config = BetterDict() obj.engine.config.merge( yaml.load( open(__dir__() + "/../yaml/phantom_improved_request.yml").read())) obj.execution = obj.engine.config['execution'][0] obj.settings.merge({ "path": os.path.join(os.path.dirname(__file__), '..', "phantom.sh"), }) obj.prepare() with open(obj.pbench.schedule_file) as fds: config = fds.readlines() get_requests = [ req_str.split(" ")[1] for req_str in config if req_str.startswith("GET") ] self.assertEqual(len(get_requests), 2) for get_req in get_requests: self.assertEqual( dict(parse.parse_qsl(parse.urlsplit(get_req).query)), { "get_param1": "value1", "get_param2": "value2" })
def test_improved_request_building(self): config = RESOURCES_DIR + "yaml/phantom_improved_request.yml" self.configure(yaml.load(open(config).read())) self.obj.prepare() with open(self.obj.generator.schedule_file) as fds: config = fds.readlines() get_requests = [req_str.split(" ")[1] for req_str in config if req_str.startswith("GET")] self.assertEqual(len(get_requests), 2) for get_req in get_requests: self.assertEqual(dict(parse.parse_qsl(parse.urlsplit(get_req).query)), {"get_param1": "value1", "get_param2": "value2"})
def _set_up_proxy(self): proxy_settings = self.config.get("settings").get("proxy") if proxy_settings and proxy_settings.get("address"): proxy_url = parse.urlsplit(proxy_settings.get("address")) self.log.debug("Using proxy settings: %s", proxy_url) username = proxy_settings.get("username") pwd = proxy_settings.get("password") if username and pwd: proxy_uri = "%s://%s:%s@%s" % (proxy_url.scheme, username, pwd, proxy_url.netloc) else: proxy_uri = "%s://%s" % (proxy_url.scheme, proxy_url.netloc) proxy_handler = ProxyHandler({"https": proxy_uri, "http": proxy_uri}) opener = build_opener(proxy_handler) install_opener(opener)
def _get_http_defaults(default_address=None, timeout=None, retrieve_resources=None, concurrent_pool_size=4, content_encoding=None, resources_regex=None): """ :rtype: lxml.etree.Element """ cfg = etree.Element("ConfigTestElement", guiclass="HttpDefaultsGui", testclass="ConfigTestElement", testname="Defaults") if retrieve_resources: cfg.append(JMX._bool_prop("HTTPSampler.image_parser", True)) cfg.append(JMX._bool_prop("HTTPSampler.concurrentDwn", True)) if concurrent_pool_size: cfg.append(JMX._string_prop("HTTPSampler.concurrentPool", concurrent_pool_size)) params = etree.Element("elementProp", name="HTTPsampler.Arguments", elementType="Arguments", guiclass="HTTPArgumentsPanel", testclass="Arguments", testname="user_defined") cfg.append(params) if default_address: parsed_url = parse.urlsplit(default_address) if parsed_url.scheme: cfg.append(JMX._string_prop("HTTPSampler.protocol", parsed_url.scheme)) if parsed_url.netloc: netloc = parsed_url.netloc if ':' in netloc: index = netloc.rfind(':') cfg.append(JMX._string_prop("HTTPSampler.port", netloc[index + 1:])) netloc = netloc[:index] cfg.append(JMX._string_prop("HTTPSampler.domain", netloc)) if timeout: cfg.append(JMX._string_prop("HTTPSampler.connect_timeout", timeout)) cfg.append(JMX._string_prop("HTTPSampler.response_timeout", timeout)) if content_encoding: cfg.append(JMX._string_prop("HTTPSampler.contentEncoding", content_encoding)) if resources_regex: cfg.append(JMX._string_prop("HTTPSampler.embedded_url_re", resources_regex)) return cfg
def add_proxy_settings(self, proxy_settings): if proxy_settings and proxy_settings.get("address"): self.proxy_settings = proxy_settings proxy_addr = proxy_settings.get("address") self.log.info("Using proxy %r", proxy_addr) proxy_url = parse.urlsplit(proxy_addr) self.log.debug("Using proxy settings: %s", proxy_url) username = proxy_settings.get("username") pwd = proxy_settings.get("password") scheme = proxy_url.scheme if proxy_url.scheme else 'http' if username and pwd: proxy_uri = "%s://%s:%s@%s" % (scheme, username, pwd, proxy_url.netloc) else: proxy_uri = "%s://%s" % (scheme, proxy_url.netloc) self.session.proxies = {"https": proxy_uri, "http": proxy_uri} self.session.verify = proxy_settings.get('ssl-cert', True) self.session.cert = proxy_settings.get('ssl-client-cert', None)
def prepare(self): """ Read options for uploading, check that they're sane """ super(BlazeMeterUploader, self).prepare() self.client.address = self.settings.get("address", self.client.address) self.client.data_address = self.settings.get("data-address", self.client.data_address) self.client.timeout = dehumanize_time(self.settings.get("timeout", self.client.timeout)) self.send_interval = dehumanize_time(self.settings.get("send-interval", self.send_interval)) self.browser_open = self.settings.get("browser-open", self.browser_open) token = self.settings.get("token", "") proxy_settings = self.engine.config.get("settings").get("proxy") if proxy_settings: if proxy_settings.get("address"): proxy_url = parse.urlsplit(proxy_settings.get("address")) self.log.debug("Using proxy settings: %s", proxy_url) username = proxy_settings.get("username") pwd = proxy_settings.get("password") if username and pwd: proxy_uri = "%s://%s:%s@%s" % (proxy_url.scheme, username, pwd, proxy_url.netloc) else: proxy_uri = "%s://%s" % (proxy_url.scheme, proxy_url.netloc) proxy_handler = ProxyHandler({"https": proxy_uri, "http": proxy_uri}) opener = build_opener(proxy_handler) install_opener(opener) if not token: self.log.warning("No BlazeMeter API key provided, will upload anonymously") self.client.token = token self.client.active_session_id = self.parameters.get("session-id", None) self.client.test_id = self.parameters.get("test-id", None) self.client.user_id = self.parameters.get("user-id", None) self.client.data_signature = self.parameters.get("signature", None) if not self.client.test_id: test_name = self.parameters.get("test", "Taurus Test") # TODO: provide a way to put datetime into test name try: self.client.ping() # to check connectivity and auth if token: self.test_id = self.client.test_by_name(test_name, {"type": "external"}) except HTTPError: self.log.error("Cannot reach online results storage, maybe the address/token is wrong") raise
def test_improved_request_building(self): obj = PBenchExecutor() obj.engine = EngineEmul() obj.settings = BetterDict() obj.engine.config = BetterDict() obj.engine.config.merge(yaml.load(open(__dir__() + "/../yaml/phantom_improved_request.yml").read())) obj.execution = obj.engine.config['execution'][0] obj.settings.merge({ "path": os.path.join(os.path.dirname(__file__), '..', "phantom.sh"), }) obj.prepare() with open(obj.pbench.schedule_file) as fds: config = fds.readlines() get_requests = [req_str.split(" ")[1] for req_str in config if req_str.startswith("GET")] self.assertEqual(len(get_requests), 2) for get_req in get_requests: self.assertEqual(dict(parse.parse_qsl(parse.urlsplit(get_req).query)), {"get_param1": "value1", "get_param2": "value2"})
def get_proxy_jvm_args(self): if not self.proxy_settings: return '' if not self.proxy_settings.get("address"): return '' props = OrderedDict() proxy_url = parse.urlsplit(self.proxy_settings.get("address")) username = self.proxy_settings.get("username") pwd = self.proxy_settings.get("password") for protocol in ["http", "https"]: props[protocol + '.proxyHost'] = proxy_url.hostname props[protocol + '.proxyPort'] = proxy_url.port or 80 if username and pwd: props[protocol + '.proxyUser'] = username props[protocol + '.proxyPass'] = pwd jvm_args = " ".join(("-D%s=%s" % (key, value)) for key, value in iteritems(props)) return jvm_args