def test_builder(): dr = Docker(registry, namespace, user, password) directory = mktree(BUILDER_SOURCE_TREE, START_TIME=str(START_TIME)) name = "buildertest_%s" % START_TIME version = "t%s" % START_TIME builder = dr.builder(directory, os.path.join(directory, "Dockerfile"), name, version, {}) try: # create a builder container based on the Dockerfile result = builder.run("cat", "timestamp.txt") assert result.output == str(START_TIME) # create an image from the builder with no incremental mods builder.commit(name, version) # check the image has the correct timestamp and result = dr.run(name, version, "cat", "timestamp.txt") assert result.output == str(START_TIME) # check that the original CMD and ENTRYPOINT are preserved result = sh("docker", "run", "--rm", "-it", dr.image(name, version)) assert result.output.strip() == "original_content" # update the timestamp in the builder image builder.run("/bin/sh", "-c", "echo updated > timestamp.txt") result = builder.run("cat", "timestamp.txt") assert result.output.strip() == "updated" # create a new image from the updated builder builder.commit(name, version + "_updated") result = dr.run(name, version + "_updated", "cat", "timestamp.txt") assert result.output.strip() == "updated" # check that the original CMD and ENTRYPOINT are preserved in # the image created from the updated container result = sh("docker", "run", "--rm", "-it", dr.image(name, version + "_updated")) assert result.output.strip() == "original_content" # now let's update the Dockerfile and make sure we launch a new builder with open(os.path.join(directory, "Dockerfile"), "write") as fd: fd.write("""FROM alpine:3.5 COPY timestamp.txt . RUN echo updated_content > content.txt ENTRYPOINT ["cat"] CMD ["content.txt"] """) builder = dr.builder(directory, os.path.join(directory, "Dockerfile"), name, version, {}) builder.commit(name, version) # check that the updated CMD and ENTRYPOINT are present result = sh("docker", "run", "--rm", "-it", dr.image(name, version)) assert result.output.strip() == "updated_content" finally: builder.kill()
def decrypt(secret_file_dir, secret_file_name): key_check() secret_file_path = os.path.join(secret_file_dir, secret_file_name) temp_secret_file_path = os.path.join(secret_file_dir, "tmp-" + secret_file_name) os.rename(secret_file_path, temp_secret_file_path) decrypted_content = sh("sops", "--output-type", "binary", "-d", temp_secret_file_path).output with open(secret_file_path, "w") as decrypted_file: decrypted_file.write(decrypted_content)
def do_ENV(self, arg): parts = arg.split(None, 1) if len(parts) > 1: key, value = parts value = sh("sh", "-c", "echo -n " + value, env=self.environ).output else: key = parts[0] value = "" self.environ[key] = value
def edit_secret(secret_file_path, create): if not os.path.exists(secret_file_path): if not create: raise TaskError("no such file: %s" % secret_file_path) key_check() content = sh("sops", "--input-type", "binary", "-e", "/dev/null").output try: with open(secret_file_path, "w") as fd: fd.write(content) except IOError, e: raise TaskError(e)
def decrypt(filename): return sh("sops", "--output-type", "binary", "-d", filename).output
def mkgittree(treespec, **substitutions): directory = mktree(treespec, **substitutions) sh("git", "init", ".", cwd=directory) sh("git", "add", ".", cwd=directory) sh("git", "commit", "-m", "initial commit", cwd=directory) return directory
def test_sh_cwd_env(): result = sh("echo", "hello", cwd="/tmp", env={"FOO": "bar"}) assert result.command.startswith("[/tmp] ") assert result.command[7:].startswith("FOO=bar ")
def test_sh_env(): result = sh("echo", "hello", env={"FOO": "bar"}) assert result.command.startswith("FOO=bar ")
def test_sh_cwd(): result = sh("echo", "hello", cwd="/tmp") assert result.command.startswith("[/tmp] ")
def test_sh_error(): try: sh("ls", "nonexistentfile") except TaskError, e: assert "command 'ls nonexistentfile' failed" in str(e)
def test_sh_nonexist(): try: sh("nonexistent-command") except TaskError, e: assert 'error executing command' in str(e)
def test_sh(): assert "hello" == sh("echo", "-n", "hello").output
def test_apply_namespace(): sh("kubectl", "create", "namespace", mangle("dev-MANGLE")) test_apply(namespace=mangle("dev-MANGLE"))
def kget(namespace, type, name): cmd = "kubectl", "get", "-o", "name", type, name if namespace: cmd += "--namespace", namespace return sh(*cmd)
def test_sh_error(): try: sh("ls", "nonexistentfile") except TaskError, e: assert 'command failed' in str(e)
def load_metadata(directory): result = sh("forge", "build", "metadata", cwd=directory) return yaml.load(result.output)
def run_image(directory): md = load_metadata(directory) image = md["build"]["images"]["Dockerfile"] result = sh("docker", "run", "--rm", "-it", image) return result.output
def test_sh_expected_error(): sh("ls", "nonexistentfile", expected=(2, ))