Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup testinfra for provisioner #215

Merged
merged 14 commits into from
Apr 28, 2023
Merged
39 changes: 0 additions & 39 deletions .plzconfig

This file was deleted.

5 changes: 0 additions & 5 deletions BUILD.plz

This file was deleted.

2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bazel_dep(name = "platforms", version = "0.0.6")
bazel_dep(name = "bazel_skylib", version = "1.4.1")

# ------------------------------------ rules_pkg ------------------------------------ #
bazel_dep(name = "rules_pkg", version = "0.9.0")
bazel_dep(name = "rules_pkg", version = "0.7.0")

# ------------------------------------ rules_task ------------------------------------ #
bazel_dep(name = "rules_task", version = "0.1.0")
Expand Down
52 changes: 42 additions & 10 deletions WORKSPACE.bzlmod
Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")

# ------------------------------------ rules_go ------------------------------------ #
http_archive(
name = "io_bazel_rules_go",
sha256 = "f2dcd210c7095febe54b804bb1cd3a58fe8435a909db2ec04e31542631cf715c",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.31.0/rules_go-v0.31.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.31.0/rules_go-v0.31.0.zip",
],
)

load(
"@io_bazel_rules_go//go:deps.bzl",
"go_download_sdk",
"go_rules_dependencies",
)

go_rules_dependencies()

# From https://github.com/buildbuddy-io/buildbuddy/blob/73ec4544d3bf813141314970275d4c2eaa5091a8/WORKSPACE#L57-L83
go_download_sdk(
name = "go_sdk_linux",
goarch = "amd64",
goos = "linux",
version = "1.18", # Keep in sync with .github/workflows/checkstyle.yaml
)

go_download_sdk(
name = "go_sdk_linux_arm64",
goarch = "arm64",
goos = "linux",
version = "1.18",
)

# go_register_toolchains(version="1.18")

# ------------------------------------ rules_docker ------------------------------------ #
# https://github.com/bazelbuild/rules_docker/pull/2201
http_archive(
name = "io_bazel_rules_docker",
sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf",
urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"],
urls = [
"https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz",
],
)

# load("@io_bazel_rules_docker//toolchains/docker:toolchain.bzl",
# docker_toolchain_configure="toolchain_configure"
# )
# docker_toolchain_configure(
# name = "docker_config",
# docker_path="<enter absolute path to the docker binary (in the remote exec env) here>",
# )

load(
"@io_bazel_rules_docker//repositories:repositories.bzl",
container_repositories = "repositories",
Expand Down Expand Up @@ -129,7 +159,9 @@ http_archive(
name = "io_buildbuddy_buildbuddy_toolchain",
sha256 = "e899f235b36cb901b678bd6f55c1229df23fcbc7921ac7a3585d29bff2bf9cfd",
strip_prefix = "buildbuddy-toolchain-fd351ca8f152d66fc97f9d98009e0ae000854e8f",
urls = ["https://github.com/buildbuddy-io/buildbuddy-toolchain/archive/fd351ca8f152d66fc97f9d98009e0ae000854e8f.tar.gz"],
urls = [
"https://github.com/buildbuddy-io/buildbuddy-toolchain/archive/fd351ca8f152d66fc97f9d98009e0ae000854e8f.tar.gz",
],
)

load("@io_buildbuddy_buildbuddy_toolchain//:deps.bzl", "buildbuddy_deps")
Expand Down
42 changes: 0 additions & 42 deletions firecracker/BUILD.plz

This file was deleted.

19 changes: 0 additions & 19 deletions hypervisor/BUILD.plz

This file was deleted.

71 changes: 0 additions & 71 deletions pleasew

This file was deleted.

106 changes: 89 additions & 17 deletions provisioner/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("//tools/pyinfra:defs.bzl", "pyinfra_run")
load("@rules_task//:defs.bzl", "cmd", "task")
load("//tools/docker:docker.bzl", "docker_load_and_run")
load("@rules_task//:defs.bzl", "cmd", "task", "task_test")
load("//tools/docker:docker.bzl", "docker_load")
load("@pip-setup//:requirements.bzl", "requirement")

pyinfra_run(
name = "provision",
Expand All @@ -20,30 +21,101 @@ pyinfra_run(
inventory = "inventory.py",
)

docker_load_and_run(
name = "dev_image_run",
command = "/sbin/init",
docker_args = [
"--rm",
"--name provisioner_dev",
"--detach",
"--tmpfs /run",
"--tmpfs /run/lock",
"--tmpfs /tmp",
"--privileged",
"-v /lib/modules:/lib/modules:ro",
],
docker_load(
name = "load_dev_image",
exec_properties = {
"workload-isolation-type": "firecracker",
"init-dockerd": "true",
"recycle-runner": "true",
},
image = "//tools/ubuntu:ubuntu_snap_base_image",
)

task(
name = "run_dev_image",
cmds = [
"export CONTAINER_IMAGE=$($load_dev_image)",
cmd.shell(
"docker run",
"--rm",
"--detach",
"--tmpfs /run",
"--tmpfs /run/lock",
"--tmpfs /tmp",
"--privileged",
"-v /lib/modules:/lib/modules:ro",
"-h provisioner",
"$CLI_ARGS",
"$CONTAINER_IMAGE",
"/sbin/init",
),
],
env = {
"load_dev_image": cmd.executable(":load_dev_image"),
},
exec_properties = {
"workload-isolation-type": "firecracker",
"init-dockerd": "true",
"recycle-runner": "true",
},
)

task(
name = "dev",
cmds = [
"CONTAINER_ID=$($run_dev_image)",
"docker rm -f provisioner_dev",
"export CONTAINER_ID=$($run_dev_image)",
{"defer": "docker rm -f $CONTAINER_ID"},
"docker logs -f $CONTAINER_ID",
],
env = {
"run_dev_image": cmd.executable(":dev_image_run"),
"run_dev_image": cmd.shell(
cmd.executable(":run_dev_image"),
"--name provisioner_dev",
),
},
)

task(
name = "validate",
cmds = [
cmd.python("""
import os
setup_env = os.environ.get("SETUP_ENV", 'dev')

if setup_env == 'test':
os.environ['VALIDATE_HOST'] = os.environ['CONTAINER_ID']
else:
os.environ['VALIDATE_HOST'] = 'provisioner_dev'
"""),
cmd.python_entry_point("pytest:console_main", "-vv", "-ra", "--hosts=\"docker://root@$VALIDATE_HOST\"", "$tests"),
],
env = {
"tests": cmd.files("test_provisioner.py"),
},
deps = [
requirement("pytest-testinfra"),
requirement("pyyaml"),
],
)

task_test(
name = "test",
cmds = [
"export CONTAINER_ID=$($run_dev_image)",
{"defer": "docker rm -f $CONTAINER_ID"},
"$provision",
"$validate",
],
env = {
"run_dev_image": cmd.executable(":run_dev_image"),
"provision": cmd.executable(":provision"),
"validate": cmd.executable(":validate"),
"SETUP_ENV": "test",
},
exec_properties = {
"workload-isolation-type": "firecracker",
"init-dockerd": "true",
"recycle-runner": "true",
},
)
Loading