Example #1
0
def run_ansible(cont,
                init_system,
                backend,
                monitors,
                agent_version,
                stage,
                user="******"):
    with tempfile.NamedTemporaryFile(mode="w+") as fd:
        config_yaml = get_config(backend, monitors, agent_version, stage, user)
        print(config_yaml)
        fd.write(config_yaml)
        fd.flush()
        copy_file_into_container(fd.name, cont, CONFIG_DEST_PATH)
    code, output = cont.exec_run(ANSIBLE_CMD)
    assert code == 0, output.decode("utf-8")
    print_lines(output)
    verify_override_files(cont, init_system, user)
    installed_version = get_agent_version(cont).replace("~", "-")
    agent_version = re.sub(r"-\d+$", "", agent_version).replace("~", "-")
    assert installed_version == agent_version, "installed agent version is '%s', expected '%s'" % (
        installed_version,
        agent_version,
    )
    assert is_agent_running_as_non_root(
        cont, user=user), f"Agent is not running as {user} user"
Example #2
0
    def run_as_subproc(self):
        self.write_config()

        with run_subprocess([AGENT_BIN, "-config", self.config_path] +
                            (["-debug"] if self.debug else []),
                            env=self.env,
                            close_fds=False) as [get_output, pid]:
            self.pid = pid
            self.get_output = get_output
            try:
                yield
            finally:
                print("\nAgent output:")
                print_lines(self.get_output())
                if self.debug:
                    print("\nDatapoints received:")
                    for dp in self.fake_services.datapoints:
                        print_dp_or_event(dp)
                    print("\nEvents received:")
                    for event in self.fake_services.events:
                        print_dp_or_event(event)
                    print("\nTrace spans received:")
                    for span in self.fake_services.spans:
                        print(span)
                    print(f"\nDimensions set: {self.fake_services.dims}")

                    print("\nSplunk log entries received:")
                    for entry in self.fake_services.splunk_entries:
                        print(entry)
Example #3
0
def _run_tests(base_image,
               init_system,
               installer_args,
               user=None,
               **extra_run_kwargs):
    if user:
        installer_args = f"--service-user {user} --service-group {user} {installer_args}"
    else:
        user = "******"
    with run_init_system_image(base_image,
                               **extra_run_kwargs) as [cont, backend]:
        copy_file_into_container(INSTALLER_PATH, cont, "/opt/install.sh")

        # Unfortunately, wget and curl both don't like self-signed certs, even
        # if they are in the system bundle, so we need to use the --insecure
        # flag.
        code, output = cont.exec_run(
            f"sh /opt/install.sh --insecure {installer_args}")
        print("Output of install script:")
        print_lines(output)
        assert code == 0, "Agent could not be installed!"

        try:
            verify_override_files(cont, init_system, user)
            assert is_agent_running_as_non_root(
                cont, user), f"Agent is not running as {user} user"
            yield backend, cont
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #4
0
def _test_service_list(container, init_system, service_name="signalfx-agent"):
    code, output = container.exec_run(INIT_LIST_COMMAND[init_system])
    print("%s list command output:" % init_system)
    print_lines(output)
    assert code == 0, "Failed to get service list"
    assert service_name in output.decode(
        "utf-8"), "Agent service not registered"
Example #5
0
def test_chef(base_image, init_system, chef_version):
    if (base_image, init_system) in DEB_DISTROS:
        distro_type = "deb"
    else:
        distro_type = "rpm"
    if base_image == "centos8" and chef_version != "latest" and int(
            chef_version.split(".")[0]) < 15:
        pytest.skip(f"chef {chef_version} not supported on centos 8")
    buildargs = {"CHEF_INSTALLER_ARGS": ""}
    if chef_version != "latest":
        buildargs["CHEF_INSTALLER_ARGS"] = f"-v {chef_version}"
    opts = {
        "path": REPO_ROOT_DIR,
        "dockerfile": DOCKERFILES_DIR / f"Dockerfile.{base_image}",
        "buildargs": buildargs
    }
    with run_init_system_image(base_image, **opts) as [cont, backend]:
        import_old_key(cont, distro_type)
        try:
            agent_version = INITIAL_VERSION
            monitors = [{"type": "host-metadata"}]
            run_chef_client(cont, chef_version, agent_version, STAGE, monitors)
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "host-metadata")), "Datapoints didn't come through"

            assert_old_key_removed(cont, distro_type)

            if UPGRADE_VERSION:
                # upgrade agent
                agent_version = UPGRADE_VERSION
                run_chef_client(cont, chef_version, agent_version, STAGE,
                                monitors)
                backend.reset_datapoints()
                assert wait_for(
                    p(has_datapoint_with_dim, backend, "plugin",
                      "host-metadata")), "Datapoints didn't come through"

                # downgrade agent for distros that support package downgrades
                if base_image not in ("debian-7-wheezy", "debian-8-jessie",
                                      "ubuntu1404"):
                    agent_version = INITIAL_VERSION
                    run_chef_client(cont, chef_version, agent_version, STAGE,
                                    monitors)
                    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_chef_client(cont, chef_version, agent_version, STAGE, monitors)
            backend.reset_datapoints()
            assert wait_for(
                p(has_datapoint_with_metric_name, backend,
                  "sfxagent.datapoints_sent")
            ), "Didn't get internal metric datapoints"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #6
0
def test_chef(base_image, init_system, chef_version):
    buildargs = {"CHEF_INSTALLER_ARGS": ""}
    if chef_version != "latest":
        buildargs["CHEF_INSTALLER_ARGS"] = f"-v {chef_version}"
    opts = {
        "path": REPO_ROOT_DIR,
        "dockerfile": DOCKERFILES_DIR / f"Dockerfile.{base_image}",
        "buildargs": buildargs
    }
    with run_init_system_image(base_image, **opts) as [cont, backend]:
        try:
            run_chef_client(cont, chef_version, INITIAL_VERSION, STAGE)
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "host-metadata")), "Datapoints didn't come through"

            # upgrade agent
            run_chef_client(cont, chef_version, 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 for distros that support package downgrades
            if base_image not in ("debian-7-wheezy", "debian-8-jessie",
                                  "ubuntu1404"):
                run_chef_client(cont, chef_version, INITIAL_VERSION, STAGE)
                backend.reset_datapoints()
                assert wait_for(
                    p(has_datapoint_with_dim, backend, "plugin",
                      "host-metadata")), "Datapoints didn't come through"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #7
0
def test_chef_upgrade_downgrade(base_image, init_system):
    if base_image == "debian-7-wheezy":
        pytest.skip(
            "Bug in chef-client, fails with 'Malformed version number string' when parsing the apt version. \
            Latest chef-client no longer supports debian 7."
        )

    dockerfile = os.path.join(DOCKERFILES_DIR, "Dockerfile.%s" % base_image)
    with run_init_system_image(base_image, path=PROJECT_DIR, dockerfile=dockerfile) as [cont, backend]:
        try:
            agent_version = run_chef_client(cont, "4.1.1-1")
            assert agent_version == "4.1.1", "agent version is not 4.1.1: %s" % agent_version
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin", "signalfx-metadata")
            ), "Datapoints didn't come through"

            # upgrade agent
            agent_version = run_chef_client(cont, "4.2.0-1")
            assert agent_version == "4.2.0", "agent version is not 4.2.0: %s" % agent_version
            backend.reset_datapoints()
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin", "signalfx-metadata")
            ), "Datapoints didn't come through"

            # downgrade agent for distros that support package downgrades
            if base_image not in ("debian-7-wheezy", "debian-8-jessie", "ubuntu1404"):
                agent_version = run_chef_client(cont, "4.1.0-1")
                assert agent_version == "4.1.0", "agent version is not 4.1.0: %s" % agent_version
                backend.reset_datapoints()
                assert wait_for(
                    p(has_datapoint_with_dim, backend, "plugin", "signalfx-metadata")
                ), "Datapoints didn't come through"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
def test_intaller(base_image, init_system):
    with run_init_system_image(base_image) as [cont, backend]:
        copy_file_into_container(INSTALLER_PATH, cont, "/opt/install.sh")

        # Unfortunately, wget and curl both don't like self-signed certs, even
        # if they are in the system bundle, so we need to use the --insecure
        # flag.
        code, output = cont.exec_run(
            "sh /opt/install.sh MYTOKEN --beta --insecure")
        print("Output of install script:")
        print_lines(output)
        assert code == 0, "Agent could not be installed!"

        try:
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "signalfx-metadata")), "Datapoints didn't come through"
            assert is_agent_running_as_non_root(
                cont), "Agent is not running as non-root user"

            cont.stop(timeout=3)
            cont.start()

            backend.datapoints.clear()

            assert wait_for(p(is_agent_running_as_non_root,
                              cont)), "Agent is not running as non-root user"
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "signalfx-metadata")
            ), "Datapoints didn't come through after restart"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #9
0
def test_chef_upgrade_downgrade(base_image, init_system):
    dockerfile = DOCKERFILES_DIR / f"Dockerfile.{base_image}"
    with run_init_system_image(base_image, path=REPO_ROOT_DIR, dockerfile=dockerfile) as [cont, backend]:
        try:
            agent_version = run_chef_client(cont, "4.1.1")
            assert agent_version == "4.1.1", "agent version is not 4.1.1: %s" % agent_version
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin", "host-metadata")
            ), "Datapoints didn't come through"

            # upgrade agent
            agent_version = run_chef_client(cont, "4.2.0")
            assert agent_version == "4.2.0", "agent version is not 4.2.0: %s" % agent_version
            backend.reset_datapoints()
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin", "host-metadata")
            ), "Datapoints didn't come through"

            # downgrade agent for distros that support package downgrades
            if base_image not in ("debian-7-wheezy", "debian-8-jessie", "ubuntu1404"):
                agent_version = run_chef_client(cont, "4.1.0")
                assert agent_version == "4.1.0", "agent version is not 4.1.0: %s" % agent_version
                backend.reset_datapoints()
                assert wait_for(
                    p(has_datapoint_with_dim, backend, "plugin", "host-metadata")
                ), "Datapoints didn't come through"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #10
0
def _test_service_status(container, init_system, expected_status):
    _, output = container.exec_run(INIT_STATUS_COMMAND[init_system])
    print("%s status command output:" % init_system)
    print_lines(output)
    assert INIT_STATUS_OUTPUT[init_system][expected_status] in output.decode("utf-8"), (
        "'%s' expected in status output" % INIT_STATUS_OUTPUT[init_system][expected_status]
    )
def _test_package_install(base_image, package_path, init_system):
    with run_init_system_image(base_image, with_socat=False) as [cont, backend]:
        _, package_ext = os.path.splitext(package_path)
        copy_file_into_container(package_path, cont, "/opt/signalfx-agent%s" % package_ext)

        code, output = cont.exec_run(INSTALL_COMMAND[package_ext])
        print("Output of package install:")
        print_lines(output)
        assert code == 0, "Package could not be installed!"

        if init_system == INIT_SYSTEMD:
            assert not path_exists_in_container(cont, "/etc/init.d/signalfx-agent")
        else:
            assert path_exists_in_container(cont, "/etc/init.d/signalfx-agent")

        cont.exec_run("bash -ec 'echo -n testing123 > /etc/signalfx/token'")
        update_agent_yaml(cont, backend, hostname="test-" + base_image)

        try:
            _test_service_list(cont, init_system)
            _test_service_restart(cont, init_system, backend)
            _test_service_status(cont, init_system, "active")
            _test_service_stop(cont, init_system, backend)
            _test_service_status(cont, init_system, "inactive")
            _test_service_start(cont, init_system, backend)
            _test_service_status(cont, init_system, "active")
            _test_service_stop(cont, init_system, backend)
            _test_system_restart(cont, init_system, backend)
            if init_system == INIT_SYSTEMD:
                _test_service_status_redirect(cont)
        finally:
            cont.reload()
            if cont.status.lower() == "running":
                print("Agent log:")
                print_lines(get_agent_logs(cont, init_system))
Example #12
0
def test_ansible(base_image, init_system, ansible_version):
    if (base_image, init_system) in DEB_DISTROS:
        distro_type = "deb"
    else:
        distro_type = "rpm"
    buildargs = {"ANSIBLE_VERSION": ""}
    if ansible_version != "latest":
        buildargs = {"ANSIBLE_VERSION": f"=={ansible_version}"}
    opts = {
        "path": REPO_ROOT_DIR,
        "dockerfile": DOCKERFILES_DIR / f"Dockerfile.{base_image}",
        "buildargs": buildargs,
        "with_socat": False,
    }
    with run_init_system_image(base_image, **opts) as [cont, backend]:
        import_old_key(cont, distro_type)
        try:
            monitors = [{"type": "host-metadata"}]
            run_ansible(cont, init_system, backend, monitors, INITIAL_VERSION,
                        STAGE)
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "host-metadata")), "Datapoints didn't come through"

            assert_old_key_removed(cont, distro_type)

            if UPGRADE_VERSION:
                # upgrade agent
                run_ansible(cont,
                            init_system,
                            backend,
                            monitors,
                            UPGRADE_VERSION,
                            STAGE,
                            user="******")
                backend.reset_datapoints()
                assert wait_for(
                    p(has_datapoint_with_dim, backend, "plugin",
                      "host-metadata")), "Datapoints didn't come through"

                # downgrade agent
                run_ansible(cont, init_system, 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_ansible(cont, init_system, 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"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
def run_puppet_agent(cont,
                     init_system,
                     backend,
                     monitors,
                     agent_version,
                     stage,
                     user="******"):
    with tempfile.NamedTemporaryFile(mode="w+") as fd:
        hiera_yaml = get_hiera(HIERA_SRC_PATH, backend, monitors)
        print(hiera_yaml)
        fd.write(hiera_yaml)
        fd.flush()
        copy_file_into_container(fd.name, cont, HIERA_DEST_PATH)
    with tempfile.NamedTemporaryFile(mode="w+") as fd:
        config = get_config(agent_version, stage, user=user)
        print(config)
        fd.write(config)
        fd.flush()
        copy_file_into_container(fd.name, cont, "/root/agent.pp")
    code, output = cont.exec_run("puppet apply /root/agent.pp")
    assert code in (0, 2), output.decode("utf-8")
    print_lines(output)
    verify_override_files(cont, init_system, user)
    installed_version = get_agent_version(cont)
    assert installed_version == agent_version, "installed agent version is '%s', expected '%s'" % (
        installed_version,
        agent_version,
    )
    assert is_agent_running_as_non_root(
        cont, user=user), f"Agent is not running as {user} user"
Example #14
0
def test_puppet(base_image, init_system, puppet_release):
    if (base_image, init_system) in DEB_DISTROS:
        distro_type = "deb"
    else:
        distro_type = "rpm"
    buildargs = {"PUPPET_RELEASE": ""}
    if puppet_release != "latest":
        buildargs = {"PUPPET_RELEASE": puppet_release}
    opts = {
        "path": REPO_ROOT_DIR,
        "dockerfile": DOCKERFILES_DIR / f"Dockerfile.{base_image}",
        "buildargs": buildargs,
        "with_socat": False,
    }
    with run_init_system_image(base_image, **opts) as [cont, backend]:
        import_old_key(cont, distro_type)
        if (base_image, init_system) in DEB_DISTROS:
            code, output = cont.exec_run(
                f"puppet module install puppetlabs-apt --version {APT_MODULE_VERSION}"
            )
            assert code == 0, output.decode("utf-8")
            print_lines(output)
        try:
            monitors = [{"type": "host-metadata"}]
            run_puppet_agent(cont, backend, monitors, INITIAL_VERSION, STAGE)
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "host-metadata")), "Datapoints didn't come through"

            assert_old_key_removed(cont, distro_type)

            if UPGRADE_VERSION:
                # upgrade agent
                run_puppet_agent(cont, 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_puppet_agent(cont, 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_puppet_agent(cont, 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"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #15
0
def _test_service_start(container, init_system, backend, user="******"):
    code, output = container.exec_run(INIT_START_COMMAND[init_system])
    print("%s start command output:" % init_system)
    print_lines(output)
    backend.reset_datapoints()
    assert code == 0, "Agent could not be started"
    assert wait_for(p(is_agent_running_as_non_root, container, user=user), timeout_seconds=INIT_START_TIMEOUT)
    assert wait_for(p(has_datapoint, backend, metric_name="disk.utilization")), "Datapoints didn't come through"
def _test_service_start(container, init_system, backend):
    code, output = container.exec_run(INIT_START_COMMAND[init_system])
    print("%s start command output:" % init_system)
    print_lines(output)
    backend.reset_datapoints()
    assert code == 0, "Agent could not be started"
    assert wait_for(p(is_agent_running_as_non_root, container), timeout_seconds=INIT_START_TIMEOUT)
    assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "signalfx-metadata")), "Datapoints didn't come through"
Example #17
0
def _test_service_restart(container, init_system, backend):
    old_pid = get_agent_pid(container)
    code, output = container.exec_run(INIT_RESTART_COMMAND[init_system])
    print("Init restart command output:")
    print_lines(output)
    backend.datapoints.clear()
    assert code == 0, "Agent could not be restarted"
    assert wait_for(p(is_agent_running_as_non_root, container), timeout_seconds=INIT_RESTART_TIMEOUT)
    assert agent_has_new_pid(container, old_pid), "Agent pid the same after service restart"
    assert wait_for(p(has_datapoint_with_dim, backend, "plugin", "signalfx-metadata")), "Datapoints didn't come through"
Example #18
0
def _test_service_restart(container, init_system, backend):
    old_pid = get_agent_pid(container)
    code, output = container.exec_run(INIT_RESTART_COMMAND[init_system])
    print("%s restart command output:" % init_system)
    print_lines(output)
    backend.reset_datapoints()
    assert code == 0, "Agent could not be restarted"
    assert wait_for(p(is_agent_running_as_non_root, container), timeout_seconds=INIT_RESTART_TIMEOUT)
    assert agent_has_new_pid(container, old_pid), "Agent pid the same after service restart"
    assert wait_for(p(has_datapoint, backend, metric_name="disk.utilization")), "Datapoints didn't come through"
Example #19
0
def _test_service_stop(container, init_system, backend):
    code, output = container.exec_run(INIT_STOP_COMMAND[init_system])
    print("%s stop command output:" % init_system)
    print_lines(output)
    assert code == 0, "Agent could not be stop"
    assert wait_for(
        lambda: not get_agent_pid(container), timeout_seconds=INIT_STOP_TIMEOUT
    ), "Timed out waiting for agent process to stop"
    if init_system in [INIT_SYSV, INIT_UPSTART]:
        assert not path_exists_in_container(container, PIDFILE_PATH), "%s exists when agent is stopped" % PIDFILE_PATH
    backend.reset_datapoints()
Example #20
0
def test_chef(base_image, init_system, chef_version):
    dockerfile = DOCKERFILES_DIR / f"Dockerfile.{base_image}"
    buildargs = {"CHEF_INSTALLER_ARGS": ""}
    if chef_version != "latest":
        buildargs["CHEF_INSTALLER_ARGS"] = f"-v {chef_version}"
    with run_init_system_image(base_image,
                               path=REPO_ROOT_DIR,
                               dockerfile=dockerfile,
                               buildargs=buildargs) as [
                                   cont,
                                   backend,
                               ]:
        try:
            installed_version = run_chef_client(cont, chef_version,
                                                INITIAL_VERSION,
                                                STAGE).replace("-", "~")
            assert installed_version == INITIAL_VERSION, "installed agent version is '%s', expected '%s'" % (
                installed_version,
                INITIAL_VERSION,
            )
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "host-metadata")), "Datapoints didn't come through"

            # upgrade agent
            installed_version = run_chef_client(cont, chef_version,
                                                UPGRADE_VERSION,
                                                STAGE).replace("-", "~")
            assert installed_version == UPGRADE_VERSION, "installed agent version is '%s', expected '%s'" % (
                installed_version,
                UPGRADE_VERSION,
            )
            backend.reset_datapoints()
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "host-metadata")), "Datapoints didn't come through"

            # downgrade agent for distros that support package downgrades
            if base_image not in ("debian-7-wheezy", "debian-8-jessie",
                                  "ubuntu1404"):
                installed_version = run_chef_client(cont, chef_version,
                                                    INITIAL_VERSION,
                                                    STAGE).replace("-", "~")
                assert installed_version == INITIAL_VERSION, "installed agent version is '%s', expected '%s'" % (
                    installed_version,
                    INITIAL_VERSION,
                )
                backend.reset_datapoints()
                assert wait_for(
                    p(has_datapoint_with_dim, backend, "plugin",
                      "host-metadata")), "Datapoints didn't come through"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #21
0
def test_chef(base_image, init_system):
    dockerfile = DOCKERFILES_DIR / f"Dockerfile.{base_image}"
    with run_init_system_image(base_image, path=REPO_ROOT_DIR, dockerfile=dockerfile) as [cont, backend]:
        try:
            # install latest agent
            run_chef_client(cont)
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin", "host-metadata")
            ), "Datapoints didn't come through"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #22
0
def test_chef(base_image, init_system):
    dockerfile = os.path.join(DOCKERFILES_DIR, "Dockerfile.%s" % base_image)
    with run_init_system_image(base_image,
                               path=PROJECT_DIR,
                               dockerfile=dockerfile) as [cont, backend]:
        try:
            # install latest agent
            run_chef_client(cont)
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "signalfx-metadata")), "Datapoints didn't come through"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
def test_bundle(request, base_image):
    # Get bundle path from command line flag to pytest
    bundle_path = request.config.getoption("--test-bundle-path")
    if not bundle_path:
        raise ValueError(
            "You must specify the --test-bundle-path flag to run bundle tests")

    with run_service("activemq") as activemq_container:
        with run_init_system_image(base_image,
                                   command="/usr/bin/tail -f /dev/null") as [
                                       cont, backend
                                   ]:
            copy_file_into_container(bundle_path, cont, "/opt/bundle.tar.gz")

            code, output = cont.exec_run(f"tar -xf /opt/bundle.tar.gz -C /opt")
            assert code == 0, f"Could not untar bundle: {output}"

            code, output = cont.exec_run(
                f"/opt/signalfx-agent/bin/patch-interpreter /opt/signalfx-agent"
            )
            assert code == 0, f"Could not patch interpreter: {output}"

            write_agent_config(cont, activemq_container)

            _, output = cont.exec_run(
                [
                    "/bin/sh", "-c",
                    "exec /opt/signalfx-agent/bin/signalfx-agent > /var/log/signalfx-agent.log"
                ],
                detach=True,
                stream=True,
            )

            try:
                assert wait_for(
                    p(has_datapoint, backend, metric_name="cpu.utilization"),
                    timeout_seconds=10
                ), "Python metadata datapoint didn't come through"
                assert wait_for(
                    p(has_datapoint,
                      backend,
                      metric_name="gauge.amq.queue.QueueSize")
                ), "Didn't get activemq queue size datapoint"
                code, output = cont.exec_run(
                    "/opt/signalfx-agent/bin/agent-status")
                assert code == 0, f"failed to execute agent-status:\n{output.decode('utf-8')}"
            finally:
                print("Agent log:")
                _, output = cont.exec_run("cat /var/log/signalfx-agent.log")
                print_lines(output)
Example #24
0
def run_puppet_agent(cont, backend, monitors, agent_version, stage):
    with tempfile.NamedTemporaryFile(mode="w+") as fd:
        fd.write(get_config(backend, monitors, agent_version, stage))
        fd.flush()
        copy_file_into_container(fd.name, cont, "/root/agent.pp")
    code, output = cont.exec_run("puppet apply /root/agent.pp")
    assert code in (0, 2), output.decode("utf-8")
    print_lines(output)
    installed_version = get_agent_version(cont)
    assert installed_version == agent_version, "installed agent version is '%s', expected '%s'" % (
        installed_version,
        agent_version,
    )
    assert is_agent_running_as_non_root(cont), "Agent is not running as non-root user"
Example #25
0
def _test_package_install(base_image, package_path, init_system):
    with run_init_system_image(base_image,
                               with_socat=False) as [cont, backend]:
        _, package_ext = os.path.splitext(package_path)
        copy_file_into_container(package_path, cont,
                                 "/opt/signalfx-agent%s" % package_ext)

        install_deps(cont, base_image)

        if "opensuse" in base_image:
            install_cmd = "rpm -ivh --nodeps /opt/signalfx-agent.rpm"
        else:
            install_cmd = INSTALL_COMMAND[package_ext]

        _test_install_package(cont, install_cmd)

        _test_package_verify(cont, package_ext, base_image)

        if init_system == INIT_SYSTEMD:
            assert not path_exists_in_container(cont,
                                                "/etc/init.d/signalfx-agent")
        else:
            assert path_exists_in_container(cont, "/etc/init.d/signalfx-agent")

        cont.exec_run("bash -ec 'echo -n testing123 > /etc/signalfx/token'")
        update_agent_yaml(cont, backend, hostname="test-" + base_image)

        try:
            _test_service_list(cont, init_system)
            _test_service_restart(cont, init_system, backend)
            _test_service_status(cont, init_system, "active")
            _test_service_stop(cont, init_system, backend)
            _test_service_status(cont, init_system, "inactive")
            _test_service_start(cont, init_system, backend)
            _test_service_status(cont, init_system, "active")
            _test_service_stop(cont, init_system, backend)
            _test_system_restart(cont, init_system, backend)
            if init_system == INIT_SYSTEMD:
                _test_service_status_redirect(cont)
            _test_agent_status(cont)
            _test_service_override(cont, init_system, backend)
        finally:
            cont.reload()
            if cont.status.lower() == "running":
                print("Agent service status:")
                print_lines(
                    cont.exec_run(INIT_STATUS_COMMAND[init_system]).output)
                print("Agent log:")
                print_lines(get_agent_logs(cont, init_system))
Example #26
0
def test_puppet(base_image, init_system):
    dockerfile = os.path.join(DOCKERFILES_DIR, "Dockerfile.%s" % base_image)
    with run_init_system_image(base_image,
                               path=REPO_ROOT_DIR,
                               dockerfile=dockerfile,
                               with_socat=False) as [
                                   cont,
                                   backend,
                               ]:
        if (base_image, init_system) in DEB_DISTROS:
            code, output = cont.exec_run(
                f"puppet module install puppetlabs-apt --version {APT_MODULE_VERSION}"
            )
            assert code == 0, output.decode("utf-8")
            print_lines(output)
        try:
            monitors = '{ type => "host-metadata" },'
            run_puppet_agent(cont, backend, monitors, INITIAL_VERSION, STAGE)
            assert is_agent_running_as_non_root(
                cont), "Agent is not running as non-root user"
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "host-metadata")), "Datapoints didn't come through"
            run_puppet_agent(cont, backend, monitors, UPGRADE_VERSION, STAGE)
            backend.reset_datapoints()
            assert is_agent_running_as_non_root(
                cont), "Agent is not running as non-root user"
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "host-metadata")), "Datapoints didn't come through"
            run_puppet_agent(cont, backend, monitors, INITIAL_VERSION, STAGE)
            backend.reset_datapoints()
            assert is_agent_running_as_non_root(
                cont), "Agent is not running as non-root user"
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin",
                  "host-metadata")), "Datapoints didn't come through"
            monitors = '{ type => "internal-metrics" },'
            run_puppet_agent(cont, backend, monitors, INITIAL_VERSION, STAGE)
            backend.reset_datapoints()
            assert is_agent_running_as_non_root(
                cont), "Agent is not running as non-root user"
            assert wait_for(
                p(has_datapoint_with_metric_name, backend,
                  "sfxagent.datapoints_sent")
            ), "Didn't get internal metric datapoints"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #27
0
def _test_package_install(base_image, package_path, init_system):
    with run_init_system_image(base_image,
                               with_socat=False) as [cont, backend]:
        _, package_ext = os.path.splitext(package_path)
        copy_file_into_container(package_path, cont,
                                 "/opt/signalfx-agent%s" % package_ext)

        if "opensuse" in base_image:
            # suse-specific installation (this will usually be handled by the installer script)
            code, output = cont.exec_run(
                "zypper install -y -l libcap2 libcap-progs libpcap1 shadow")
            assert code == 0, "Failed to install dependencies:\n%s" % output.decode(
                "utf-8")
            install_cmd = "rpm -ivh --nodeps /opt/signalfx-agent.rpm"
        else:
            install_cmd = INSTALL_COMMAND[package_ext]

        code, output = cont.exec_run(install_cmd)
        print("Output of package install:")
        print_lines(output)
        assert code == 0, "Package could not be installed!"

        if init_system == INIT_SYSTEMD:
            assert not path_exists_in_container(cont,
                                                "/etc/init.d/signalfx-agent")
        else:
            assert path_exists_in_container(cont, "/etc/init.d/signalfx-agent")

        cont.exec_run("bash -ec 'echo -n testing123 > /etc/signalfx/token'")
        update_agent_yaml(cont, backend, hostname="test-" + base_image)

        try:
            _test_service_list(cont, init_system)
            _test_service_restart(cont, init_system, backend)
            _test_service_status(cont, init_system, "active")
            _test_service_stop(cont, init_system, backend)
            _test_service_status(cont, init_system, "inactive")
            _test_service_start(cont, init_system, backend)
            _test_service_status(cont, init_system, "active")
            _test_service_stop(cont, init_system, backend)
            _test_system_restart(cont, init_system, backend)
            if init_system == INIT_SYSTEMD:
                _test_service_status_redirect(cont)
            _test_agent_status(cont)
        finally:
            cont.reload()
            if cont.status.lower() == "running":
                print("Agent log:")
                print_lines(get_agent_logs(cont, init_system))
Example #28
0
def test_salt(base_image, init_system):
    if (base_image, init_system) in DEB_DISTROS:
        distro_type = "deb"
    else:
        distro_type = "rpm"

    opts = {"path": REPO_ROOT_DIR, "dockerfile": DOCKERFILES_DIR / f"Dockerfile.{base_image}", "with_socat": False}
    with run_init_system_image(base_image, **opts) as [cont, backend]:
        try:
            import_old_key(cont, distro_type)

            monitors = [{"type": "host-metadata"}]
            run_salt(cont, init_system, backend, INITIAL_VERSION, monitors, STAGE)
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin", "host-metadata")
            ), "Datapoints didn't come through"

            assert_old_key_removed(cont, distro_type)

            if UPGRADE_VERSION:
                # upgrade agent
                user = "******"
                if tuple(UPGRADE_VERSION.split(".")) >= ("5", "1", "0"):
                    user = "******"
                run_salt(cont, init_system, backend, UPGRADE_VERSION, monitors, STAGE, user)
                backend.reset_datapoints()
                assert wait_for(
                    p(has_datapoint_with_dim, backend, "plugin", "host-metadata")
                ), "Datapoints didn't come through"

                # downgrade agent
                run_salt(cont, init_system, backend, INITIAL_VERSION, monitors, 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_salt(cont, init_system, backend, INITIAL_VERSION, monitors, STAGE)
            backend.reset_datapoints()
            assert wait_for(
                p(has_datapoint_with_metric_name, backend, "sfxagent.datapoints_sent")
            ), "Didn't get internal metric datapoints"

        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #29
0
def test_ansible(base_image, init_system, ansible_version):
    if base_image == "centos8" and ansible_version != "latest" and tuple(
            ansible_version.split(".")) < ("2", "8", "1"):
        pytest.skip(f"ansible {ansible_version} not supported on {base_image}")
    buildargs = {"ANSIBLE_VERSION": ""}
    if ansible_version != "latest":
        buildargs = {"ANSIBLE_VERSION": f"=={ansible_version}"}
    opts = {
        "path": REPO_ROOT_DIR,
        "dockerfile": DOCKERFILES_DIR / f"Dockerfile.{base_image}",
        "buildargs": buildargs,
        "with_socat": False,
    }
    with run_init_system_image(base_image, **opts) as [cont, backend]:
        try:
            monitors = [{"type": "host-metadata"}]
            run_ansible(cont, 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_ansible(cont, 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_ansible(cont, 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_ansible(cont, 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"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))
Example #30
0
def test_chef(base_image, init_system):
    if base_image == "debian-7-wheezy":
        pytest.skip(
            "Bug in chef-client, fails with 'Malformed version number string' when parsing the apt version. \
            Latest chef-client no longer supports debian 7."
        )

    dockerfile = os.path.join(DOCKERFILES_DIR, "Dockerfile.%s" % base_image)
    with run_init_system_image(base_image, path=PROJECT_DIR, dockerfile=dockerfile) as [cont, backend]:
        try:
            # install latest agent
            run_chef_client(cont)
            assert wait_for(
                p(has_datapoint_with_dim, backend, "plugin", "signalfx-metadata")
            ), "Datapoints didn't come through"
        finally:
            print("Agent log:")
            print_lines(get_agent_logs(cont, init_system))