def test_win_local_nupkg(request): assert has_choco(), "choco not installed" # Get nupkg path from command line flag to pytest nupkg_path = request.config.getoption("--test-nupkg-path") if not nupkg_path: raise ValueError( "You must specify the --test-nupkg-path flag to run choco tests") nupkg_path = os.path.abspath(nupkg_path) assert os.path.isfile(nupkg_path), f"{nupkg_path} not found!" nupkg_dir = os.path.dirname(nupkg_path) uninstall_win_agent() with ensure_fake_backend() as backend: try: params = f"/access_token:MYTOKEN /ingest_url:{backend.ingest_url} /api_url:{backend.api_url}" run_win_command( f"choco install signalfx-agent -y -s {nupkg_dir} --params=\"'{params}'\"" ) assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "host-metadata")), "Datapoints didn't come through" finally: print("\nDatapoints received:") for dp in backend.datapoints: print_dp_or_event(dp) print("\nEvents received:") for event in backend.events: print_dp_or_event(event) print(f"\nDimensions set: {backend.dims}") run_win_command("choco uninstall -y signalfx-agent") uninstall_win_agent()
def run_win_puppet_agent(backend, monitors, agent_version, stage, config_path=WIN_CONFIG_PATH, install_dir=WIN_INSTALL_DIR): with open(WIN_HIERA_DEST_PATH, "w+", encoding="utf-8") as fd: hiera_yaml = get_hiera(WIN_HIERA_SRC_PATH, backend, monitors) print(hiera_yaml) fd.write(hiera_yaml) with tempfile.TemporaryDirectory() as tmpdir: manifest_path = os.path.join(tmpdir, "agent.pp") config = get_config(agent_version, stage, config_path=config_path, install_dir=install_dir) print(config) with open(manifest_path, "w+", encoding="utf-8") as fd: fd.write(config) cmd = f"puppet apply {manifest_path}" run_win_command(cmd, returncodes=[0, 2]) installed_version = get_win_agent_version( os.path.join(install_dir, "SignalFxAgent", "bin", "signalfx-agent.exe")) assert installed_version == agent_version, "installed agent version is '%s', expected '%s'" % ( installed_version, agent_version, )
def run_win_puppet_agent(backend, monitors, agent_version, stage): with tempfile.TemporaryDirectory() as tmpdir: manifest_path = os.path.join(tmpdir, "agent.pp") config = get_config(backend, monitors, agent_version, stage) print(config) with open(manifest_path, "w+") as fd: fd.write(config) cmd = f"puppet apply {manifest_path}" run_win_command(cmd, returncodes=[0, 2]) installed_version = get_win_agent_version() assert installed_version == agent_version, "installed agent version is '%s', expected '%s'" % ( installed_version, agent_version, )
def run_win_puppet_setup(puppet_version): assert has_choco(), "choco not installed!" uninstall_win_agent() if puppet_version == "latest": run_win_command(f"choco upgrade -y -f puppet-agent") else: run_win_command( f"choco upgrade -y -f puppet-agent --version {puppet_version}") if WIN_PUPPET_BIN_DIR not in os.environ.get("PATH"): os.environ["PATH"] = WIN_PUPPET_BIN_DIR + ";" + os.environ.get("PATH") if os.path.isdir(WIN_PUPPET_MODULE_DEST_DIR): shutil.rmtree(WIN_PUPPET_MODULE_DEST_DIR) shutil.copytree(WIN_PUPPET_MODULE_SRC_DIR, WIN_PUPPET_MODULE_DEST_DIR) run_win_command("puppet module install puppet-archive") run_win_command("puppet module install puppetlabs-powershell") run_win_command("puppet module install puppetlabs-registry")
def run_win_chef_setup(chef_version): assert has_choco(), "choco not installed!" uninstall_win_agent() if run_win_command("chef-client --version", []).returncode == 0: run_win_command("choco uninstall -y -f chef-client") if chef_version == "latest": run_win_command(f"choco upgrade -y -f chef-client") else: run_win_command( f"choco upgrade -y -f chef-client --version {chef_version}") if WIN_CHEF_BIN_DIR not in os.environ.get("PATH"): os.environ["PATH"] = WIN_CHEF_BIN_DIR + ";" + os.environ.get("PATH") os.makedirs(WIN_CHEF_COOKBOOKS_DIR, exist_ok=True) if os.path.isdir(WIN_AGENT_COOKBOOK_DEST_DIR): shutil.rmtree(WIN_AGENT_COOKBOOK_DEST_DIR) shutil.copytree(WIN_AGENT_COOKBOOK_SRC_DIR, WIN_AGENT_COOKBOOK_DEST_DIR) if not os.path.isdir(WINDOWS_COOKBOOK_DIR): run_win_command( f'powershell -command "curl -outfile windows.tar.gz {WINDOWS_COOKBOOK_URL}"', cwd=WIN_CHEF_COOKBOOKS_DIR) run_win_command('powershell -command "tar -zxvf windows.tar.gz"', cwd=WIN_CHEF_COOKBOOKS_DIR)
def get_win_chef_version(): output = run_win_command("chef-client --version").stdout.decode("utf-8") match = re.search(r"(\d+\.\d+\.\d+)", output) assert match and match.group( 1).strip(), "failed to get version from output:\n%s" % output return match.group(1).strip()
def run_win_installer(backend, args=""): installer_cmd = f'"{WIN_INSTALLER_PATH}" -ingest_url {backend.ingest_url} -api_url {backend.api_url} {args}'.strip( ) run_win_command(f"powershell {installer_cmd}", cwd="c:\\") return get_win_agent_version()
def test_puppet_on_windows(puppet_version): run_win_puppet_setup(puppet_version) with ensure_fake_backend() as backend: try: monitors = [{"type": "host-metadata"}] run_win_puppet_agent(backend, monitors, INITIAL_VERSION, STAGE) assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "host-metadata")), "Datapoints didn't come through" if UPGRADE_VERSION: # upgrade agent run_win_puppet_agent(backend, monitors, UPGRADE_VERSION, STAGE) backend.reset_datapoints() assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "host-metadata")), "Datapoints didn't come through" # downgrade agent run_win_puppet_agent(backend, monitors, INITIAL_VERSION, STAGE) backend.reset_datapoints() assert wait_for( p(has_datapoint_with_dim, backend, "plugin", "host-metadata")), "Datapoints didn't come through" # change agent config monitors = [{"type": "internal-metrics"}] run_win_puppet_agent(backend, monitors, INITIAL_VERSION, STAGE) backend.reset_datapoints() assert wait_for( p(has_datapoint_with_metric_name, backend, "sfxagent.datapoints_sent") ), "Didn't get internal metric datapoints" # re-apply without any changes run_win_puppet_agent(backend, monitors, INITIAL_VERSION, STAGE) backend.reset_datapoints() assert wait_for( p(has_datapoint_with_metric_name, backend, "sfxagent.datapoints_sent") ), "Didn't get internal metric datapoints" # change agent config path if os.path.isfile(WIN_CONFIG_PATH): os.remove(WIN_CONFIG_PATH) with tempfile.TemporaryDirectory() as tmpdir: new_config_path = os.path.join(tmpdir, "agent.yaml") run_win_puppet_agent(backend, monitors, INITIAL_VERSION, STAGE, config_path=new_config_path) backend.reset_datapoints() assert wait_for( p(has_datapoint_with_metric_name, backend, "sfxagent.datapoints_sent") ), "Didn't get internal metric datapoints" # change agent installation path with tempfile.TemporaryDirectory() as new_install_dir: new_install_dir = os.path.realpath(new_install_dir) try: run_win_puppet_agent(backend, monitors, INITIAL_VERSION, STAGE, install_dir=new_install_dir) backend.reset_datapoints() assert wait_for( p(has_datapoint_with_metric_name, backend, "sfxagent.datapoints_sent") ), "Didn't get internal metric datapoints" finally: agent_path = os.path.join(new_install_dir, "SignalFxAgent", "bin", "signalfx-agent.exe") if os.path.isfile(agent_path): run_win_command([ "powershell.exe", "-command", "Stop-Service -Name signalfx-agent" ]) run_win_command([agent_path, "-service", "uninstall"]) finally: print("\nDatapoints received:") for dp in backend.datapoints: print_dp_or_event(dp) print("\nEvents received:") for event in backend.events: print_dp_or_event(event) print(f"\nDimensions set: {backend.dims}")