Exemplo n.º 1
0
    def queries(self):
        if DEV:
            cmd = ShellCommand("docker", "ps", "-qf", "name=%s" % self.path.k8s)

            if not cmd.check(f'docker check for {self.path.k8s}'):
                if not cmd.stdout.strip():
                    log_cmd = ShellCommand("docker", "logs", self.path.k8s, stderr=subprocess.STDOUT)

                    if log_cmd.check(f'docker logs for {self.path.k8s}'):
                        print(cmd.stdout)

                    pytest.exit(f'container failed to start for {self.path.k8s}')

        return ()
Exemplo n.º 2
0
    def check(self):
        cmd = ShellCommand("kubectl", "exec", self.path.k8s, "cat", self.log_path)
        if not cmd.check("check envoy access log"):
            pytest.exit("envoy access log does not exist")

        for line in cmd.stdout.splitlines():
            assert access_log_entry_regex.match(line), f"{line} does not match {access_log_entry_regex}"
Exemplo n.º 3
0
    def run_hook(self, watt_k8s: WattDict) -> Tuple[bool, bool]:
        json.dump({ 'Consul': {}, 'Kubernetes': watt_k8s },
                  open("/tmp/mockery.json", "w"), sort_keys=True, indent=4)

        cmdline = shlex.split(self.watch)

        if self.debug:
            cmdline.append("--debug")

        cmdline.append("/tmp/mockery.json")

        self.logger.info(f"Running watch hook {cmdline}")

        hook = ShellCommand(*cmdline)

        if not hook.check(" ".join(cmdline)):
            return False, False

        for line in hook.stderr.splitlines(keepends=False):
            self.logger.info(f"hook stderr: {line}")

        any_changes = False
        hook_output = hook.stdout

        if hook_output:
            new_watches = json.loads(hook_output)
            self.logger.info(f"new watches: {new_watches}")

            for w in new_watches.get("kubernetes-watches") or []:
                potential = WatchSpec(
                    logger=self.logger,
                    kind=w['kind'],
                    namespace=w.get('namespace'),
                    labels=w.get('label-selector'),
                    fields=w.get('field-selector'),
                    bootstrap=False
                )

                if self.maybe_add(potential):
                    any_changes = True

        return True, any_changes
Exemplo n.º 4
0
    def post_manifest(self):
        if not DEV:
            return

        if os.environ.get('KAT_SKIP_DOCKER'):
            return

        image = os.environ["AMBASSADOR_DOCKER_IMAGE"]
        cached_image = os.environ["BASE_PY_IMAGE"]
        ambassador_base_image = os.environ["BASE_GO_IMAGE"]

        if not AmbassadorTest.IMAGE_BUILT:
            AmbassadorTest.IMAGE_BUILT = True

            cmd = ShellCommand('docker', 'ps', '-a', '-f',
                               'label=kat-family=ambassador', '--format',
                               '{{.ID}}')

            if cmd.check('find old docker container IDs'):
                ids = cmd.stdout.split('\n')

                while ids:
                    if ids[-1]:
                        break

                    ids.pop()

                if ids:
                    print("Killing old containers...")
                    ShellCommand.run('kill old containers',
                                     'docker',
                                     'kill',
                                     *ids,
                                     verbose=True)
                    ShellCommand.run('rm old containers',
                                     'docker',
                                     'rm',
                                     *ids,
                                     verbose=True)

            context = os.path.dirname(
                os.path.dirname(os.path.dirname(__file__)))

            print("Starting docker build...", end="")
            sys.stdout.flush()

            cmd = ShellCommand(
                "docker", "build", "--build-arg",
                "BASE_PY_IMAGE={}".format(cached_image), "--build-arg",
                "BASE_GO_IMAGE={}".format(ambassador_base_image), context,
                "-t", image)

            if cmd.check("docker build Ambassador image"):
                print("done.")
            else:
                pytest.exit("container failed to build")

        fname = "/tmp/k8s-%s.yaml" % self.path.k8s
        if os.path.exists(fname):
            with open(fname) as fd:
                content = fd.read()
        else:
            nsp = getattr(self, 'namespace', None) or 'default'

            cmd = ShellCommand("kubectl", "get", "-n", nsp, "-o", "yaml",
                               "secret", self.path.k8s)

            if not cmd.check(f'fetch secret for {self.path.k8s}'):
                pytest.exit(f'could not fetch secret for {self.path.k8s}')

            content = cmd.stdout

            with open(fname, "wb") as fd:
                fd.write(content.encode('utf-8'))

        try:
            secret = yaml.load(content, Loader=yaml_loader)
        except Exception as e:
            print("could not parse YAML:\n%s" % content)
            raise e

        data = secret['data']
        # secret_dir = tempfile.mkdtemp(prefix=self.path.k8s, suffix="secret")
        secret_dir = "/tmp/%s-ambassadormixin-%s" % (self.path.k8s, 'secret')

        shutil.rmtree(secret_dir, ignore_errors=True)
        os.mkdir(secret_dir, 0o777)

        for k, v in data.items():
            with open(os.path.join(secret_dir, k), "wb") as f:
                f.write(base64.decodebytes(bytes(v, "utf8")))
        print("Launching %s container." % self.path.k8s)
        command = [
            "docker", "run", "-d", "-l", "kat-family=ambassador", "--name",
            self.path.k8s
        ]

        envs = [
            "KUBERNETES_SERVICE_HOST=kubernetes",
            "KUBERNETES_SERVICE_PORT=443", "AMBASSADOR_SNAPSHOT_COUNT=1",
            "AMBASSADOR_CONFIG_BASE_DIR=/tmp/ambassador",
            "AMBASSADOR_ID=%s" % self.ambassador_id
        ]

        if self.namespace:
            envs.append("AMBASSADOR_NAMESPACE=%s" % self.namespace)

        if self.single_namespace:
            envs.append("AMBASSADOR_SINGLE_NAMESPACE=yes")

        if self.disable_endpoints:
            envs.append("AMBASSADOR_DISABLE_ENDPOINTS=yes")

        if self.debug_diagd:
            envs.append("AMBASSADOR_DEBUG=diagd")

        envs.extend(self.env)
        [command.extend(["-e", env]) for env in envs]

        ports = [
            "%s:8877" % (8877 + self.index),
            "%s:8080" % (8080 + self.index),
            "%s:8443" % (8443 + self.index)
        ]

        if self.extra_ports:
            for port in self.extra_ports:
                ports.append(f'{port}:{port}')

        [command.extend(["-p", port]) for port in ports]

        volumes = [
            "%s:/var/run/secrets/kubernetes.io/serviceaccount" % secret_dir
        ]
        [command.extend(["-v", volume]) for volume in volumes]

        command.append(image)

        if os.environ.get('KAT_SHOW_DOCKER'):
            print(" ".join(command))

        cmd = ShellCommand(*command)

        if not cmd.check(f'start container for {self.path.k8s}'):
            pytest.exit(f'could not start container for {self.path.k8s}')
Exemplo n.º 5
0
 def check(self):
     cmd = ShellCommand('kubectl', 'exec', self.path.k8s, '--',
                        'ambassador', '--help')
     assert cmd.check("ambassador cli")