From 451e64cec0f5d23298609372372052fd25cbc3d4 Mon Sep 17 00:00:00 2001 From: Yaron de Leeuw Date: Thu, 12 Sep 2019 12:25:33 -0400 Subject: [PATCH 1/2] Move bazel tests to a separate dir, update README with instructions. It was impossible to use our existing BUILD and WORKSPACE as examples, since they where intertwined with the actual build. Now with the separate exmple_bazel directory, this works. --- .gitignore | 1 + .travis.yml | 7 +- BUILD.bazel | 66 ---------------- README.md | 78 ++++++++++++++++++- WORKSPACE | 61 +-------------- deps.bzl | 28 +++++++ example_bazel/BUILD.bazel | 70 +++++++++++++++++ example_bazel/WORKSPACE | 69 ++++++++++++++++ {testdata => example_bazel}/content1.txt | 0 .../docker_tests.sh | 0 {testdata => example_bazel}/golden_V.txt | 0 {testdata => example_bazel}/golden_ls.txt | 0 .../golden_preinst.txt | 0 13 files changed, 251 insertions(+), 129 deletions(-) create mode 100644 deps.bzl create mode 100644 example_bazel/BUILD.bazel create mode 100644 example_bazel/WORKSPACE rename {testdata => example_bazel}/content1.txt (100%) rename docker_tests.sh => example_bazel/docker_tests.sh (100%) rename {testdata => example_bazel}/golden_V.txt (100%) rename {testdata => example_bazel}/golden_ls.txt (100%) rename {testdata => example_bazel}/golden_preinst.txt (100%) diff --git a/.gitignore b/.gitignore index e72ee71..35b994a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /bazel-* +/example_bazel/bazel-* diff --git a/.travis.yml b/.travis.yml index d6442fa..90677a0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,5 +21,8 @@ jobs: - curl -L -o "bazel-installer" https://github.com/bazelbuild/bazel/releases/download/0.29.0/bazel-0.29.0-installer-linux-x86_64.sh - bash bazel-installer --user script: - - ~/bin/bazel build --curses=no //... - - ~/bin/bazel test --test_output=all --curses=no //... + - ~/bin/bazel build --curses=no //:all + - ~/bin/bazel test --test_output=all --curses=no //:all + - cd example_bazel + - ~/bin/bazel build --curses=no //:all + - ~/bin/bazel test --test_output=all --curses=no //:all diff --git a/BUILD.bazel b/BUILD.bazel index 32edb95..7e93852 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -2,11 +2,8 @@ # For running basic build/run/test you can also use the regular go tools, # this is currently added to assist in external testing. -load("@io_bazel_rules_docker//container:container.bzl", "container_image") load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") -load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") load("@bazel_gazelle//:def.bzl", "gazelle") -load("//:def.bzl", "pkg_tar2rpm") # gazelle:prefix github.com/google/rpmpack gazelle(name = "gazelle") @@ -39,66 +36,3 @@ go_test( embed = [":go_default_library"], deps = ["@com_github_google_go_cmp//cmp:go_default_library"], ) - -pkg_tar( - name = "rpmtest-tar", - srcs = ["//:testdata/content1.txt"], - mode = "0644", - ownername = "root.root", - package_dir = "var/lib/rpmpack", -) - -pkg_tar2rpm( - name = "rpmtest", - data = ":rpmtest-tar", - pkg_name = "rpmtest", - release = "3.4", - version = "1.2", - prein = "echo \"This is preinst\" > /tmp/preinst.txt", -) - -container_image( - name = "centos_with_rpm", - testonly = True, - base = "@centos//image", - directory = "/root/", - files = [":rpmtest.rpm"], - legacy_run_behavior = False, -) - -container_image( - name = "centos_V", - testonly = True, - base = ":centos_with_rpm", - cmd = "echo ===marker=== && rpm -i /root/rpmtest.rpm && rpm -Vv rpmtest", - legacy_run_behavior = False, -) - -container_image( - name = "centos_ls", - testonly = True, - base = ":centos_with_rpm", - cmd = "echo ===marker=== && rpm -i /root/rpmtest.rpm && ls -l /var/lib/rpmpack", - legacy_run_behavior = False, -) - -container_image( - name = "centos_preinst", - testonly = True, - base = ":centos_with_rpm", - cmd = "echo ===marker=== && rpm -i /root/rpmtest.rpm && cat /tmp/preinst.txt", - legacy_run_behavior = False, -) - -sh_test( - name = "docker_tests", - srcs = ["docker_tests.sh"], - data = [ - ":centos_V", - ":centos_ls", - ":centos_preinst", - ":testdata/golden_V.txt", - ":testdata/golden_ls.txt", - ":testdata/golden_preinst.txt", - ], -) diff --git a/README.md b/README.md index 365a07c..1d36cf7 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ $ go get -u github.com/google/rpmpack/... This will make the `tar2rpm` tool available in `${GOPATH}/bin`, which by default means `~/go/bin`. -## Usage +## Usage of the binary (tar2rpm) `tar2rpm` takes a `tar` file (from `stdin` or a specified filename), and outputs an `rpm`. @@ -35,6 +35,80 @@ Options: the package version ``` +## Usage of the library (rpmpack) + +API documentation for `rpmpack` can be found in [![GoDoc](https://godoc.org/github.com/google/rpmpack?status.svg)](https://godoc.org/github.com/google/rpmpack). + +```go +import "github.com/google/rpmpack" +... +r, err := rpmpack.NewRPM(rpmpack.RPMMetadata{Name: "example", Version: "3"}) +if err != nil { + ... +} +r.AddFile(rpmpack.RPMFile{ + Name: "/usr/local/hello", + Body: []byte("content of the file"), +}) +if err := r.Write(w); err != nil { + ... +} +``` + +## Usage in the bazel build system (pkg_tar2rpm) + +There is a working example inside [example_bazel](example_bazel/) + +In `WORKSPACE` +``` +load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") +git_repository( + name = "rpmpack", + remote = "https://github.com/google/rpmpack.git", + branch = "master", +) + +# The following will load the requirements to build rpmpack +http_archive( + name = "io_bazel_rules_go", + sha256 = "ae8c36ff6e565f674c7a3692d6a9ea1096e4c1ade497272c2108a810fb39acd2", + urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.19.4/rules_go-0.19.4.tar.gz"], + ) +http_archive( + name = "bazel_gazelle", + sha256 = "7fc87f4170011201b1690326e8c16c5d802836e3a0d617d8f75c3af2b23180c4", + urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/0.18.2/bazel-gazelle-0.18.2.tar.gz"], + ) + +load("@com_github_google_rpmpack//:deps.bzl", "rpmpack_dependencies") + +rpmpack_dependencies() +``` + +In `BUILD` or `BUILD.bazel` +``` +load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") +load("@com_github_google_rpmpack//:def.bzl", "pkg_tar2rpm") + +pkg_tar( + name = "rpmtest-tar", + srcs = [":content1.txt"], + mode = "0644", + ownername = "root.root", + package_dir = "var/lib/rpmpack", +) + +pkg_tar2rpm( + name = "rpmtest", + data = ":rpmtest-tar", + pkg_name = "rpmtest", + release = "3.4", + version = "1.2", + prein = "echo \"This is preinst\" > /tmp/preinst.txt", +) +``` + + ## Features - You put files into the rpm, so that rpm/yum will install them on a host. @@ -52,7 +126,7 @@ Options: - May easily wreak havoc on rpm based systems. It is surprisingly easy to cause rpm to segfault on corrupt rpm files. - Many features are missing. - - All of the artifactes are stored in memory, sometimes more than once. + - All of the artifacts are stored in memory, sometimes more than once. - Less backwards compatible than `rpmbuild`. ## Philosophy diff --git a/WORKSPACE b/WORKSPACE index 5227aee..49e89ab 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -14,63 +14,6 @@ http_archive( urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/0.18.2/bazel-gazelle-0.18.2.tar.gz"], ) -http_archive( - name = "io_bazel_rules_docker", - sha256 = "e513c0ac6534810eb7a14bf025a0f159726753f97f74ab7863c650d26e01d677", - strip_prefix = "rules_docker-0.9.0", - urls = ["https://github.com/bazelbuild/rules_docker/archive/v0.9.0.tar.gz"], -) - -load( - "@io_bazel_rules_docker//repositories:repositories.bzl", - container_repositories = "repositories", -) - -container_repositories() - -load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains") - -go_rules_dependencies() - -go_register_toolchains() - -load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository") - -gazelle_dependencies() - -load( - "@io_bazel_rules_docker//repositories:deps.bzl", - container_deps = "deps", -) - -container_deps() - -load( - "@io_bazel_rules_docker//container:container.bzl", - "container_pull", -) - -go_repository( - name = "com_github_pkg_errors", - importpath = "github.com/pkg/errors", - tag = "v0.8.1", -) +load("//:deps.bzl", "rpmpack_dependencies") -go_repository( - name = "com_github_google_go_cmp", - importpath = "github.com/google/go-cmp", - tag = "v0.2.0", -) - -go_repository( - name = "com_github_cavaliercoder_go_cpio", - commit = "925f9528c45e", - importpath = "github.com/cavaliercoder/go-cpio", -) - -container_pull( - name = "centos", - digest = "sha256:365fc7f33107869dfcf2b3ba220ce0aa42e16d3f8e8b3c21d72af1ee622f0cf0", - registry = "index.docker.io", - repository = "library/centos", -) +rpmpack_dependencies() diff --git a/deps.bzl b/deps.bzl new file mode 100644 index 0000000..3587222 --- /dev/null +++ b/deps.bzl @@ -0,0 +1,28 @@ +load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains") +load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository") + + +def rpmpack_dependencies(): + go_rules_dependencies() + go_register_toolchains() + gazelle_dependencies() + + + go_repository( + name = "com_github_pkg_errors", + importpath = "github.com/pkg/errors", + tag = "v0.8.1", + ) + + go_repository( + name = "com_github_google_go_cmp", + importpath = "github.com/google/go-cmp", + tag = "v0.2.0", + ) + + go_repository( + name = "com_github_cavaliercoder_go_cpio", + commit = "925f9528c45e", + importpath = "github.com/cavaliercoder/go-cpio", + ) + diff --git a/example_bazel/BUILD.bazel b/example_bazel/BUILD.bazel new file mode 100644 index 0000000..3ec2019 --- /dev/null +++ b/example_bazel/BUILD.bazel @@ -0,0 +1,70 @@ +# A build file for rpmpack. +# For running basic build/run/test you can also use the regular go tools, +# this is currently added to assist in external testing. + +load("@io_bazel_rules_docker//container:container.bzl", "container_image") +load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar") +load("@com_github_google_rpmpack//:def.bzl", "pkg_tar2rpm") + +pkg_tar( + name = "rpmtest-tar", + srcs = [":content1.txt"], + mode = "0644", + ownername = "root.root", + package_dir = "var/lib/rpmpack", +) + +pkg_tar2rpm( + name = "rpmtest", + data = ":rpmtest-tar", + pkg_name = "rpmtest", + prein = "echo \"This is preinst\" > /tmp/preinst.txt", + release = "3.4", + version = "1.2", +) + +container_image( + name = "centos_with_rpm", + testonly = True, + base = "@centos//image", + directory = "/root/", + files = [":rpmtest.rpm"], + legacy_run_behavior = False, +) + +container_image( + name = "centos_V", + testonly = True, + base = ":centos_with_rpm", + cmd = "echo ===marker=== && rpm -i /root/rpmtest.rpm && rpm -Vv rpmtest", + legacy_run_behavior = False, +) + +container_image( + name = "centos_ls", + testonly = True, + base = ":centos_with_rpm", + cmd = "echo ===marker=== && rpm -i /root/rpmtest.rpm && ls -l /var/lib/rpmpack", + legacy_run_behavior = False, +) + +container_image( + name = "centos_preinst", + testonly = True, + base = ":centos_with_rpm", + cmd = "echo ===marker=== && rpm -i /root/rpmtest.rpm && cat /tmp/preinst.txt", + legacy_run_behavior = False, +) + +sh_test( + name = "docker_tests", + srcs = ["docker_tests.sh"], + data = [ + ":centos_V", + ":centos_ls", + ":centos_preinst", + ":golden_V.txt", + ":golden_ls.txt", + ":golden_preinst.txt", + ], +) diff --git a/example_bazel/WORKSPACE b/example_bazel/WORKSPACE new file mode 100644 index 0000000..ae96e37 --- /dev/null +++ b/example_bazel/WORKSPACE @@ -0,0 +1,69 @@ +workspace(name = "com_github_google_rpmpack_example_bazel") + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +# NOTE: In order to keep this in sync with the parent, we use local_repository +# in our tests. You'll want to use git_repository +# +#load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") +#git_repository( +# name = "rpmpack", +# remote = "https://github.com/google/rpmpack.git", +# branch = "master", +#) +local_repository( + name = "com_github_google_rpmpack", + path = "../", +) + +# The following will load the requirements to build rpmpack +http_archive( + name = "io_bazel_rules_go", + sha256 = "ae8c36ff6e565f674c7a3692d6a9ea1096e4c1ade497272c2108a810fb39acd2", + urls = ["https://github.com/bazelbuild/rules_go/releases/download/0.19.4/rules_go-0.19.4.tar.gz"], +) + +http_archive( + name = "bazel_gazelle", + sha256 = "7fc87f4170011201b1690326e8c16c5d802836e3a0d617d8f75c3af2b23180c4", + urls = ["https://github.com/bazelbuild/bazel-gazelle/releases/download/0.18.2/bazel-gazelle-0.18.2.tar.gz"], +) + +load("@com_github_google_rpmpack//:deps.bzl", "rpmpack_dependencies") + +rpmpack_dependencies() + +# From here on, this is a testsuite using docker containers to test rpmpack's results +# on an actual centos environment. This is not required for most users of rpmpack. +http_archive( + name = "io_bazel_rules_docker", + sha256 = "e513c0ac6534810eb7a14bf025a0f159726753f97f74ab7863c650d26e01d677", + strip_prefix = "rules_docker-0.9.0", + urls = ["https://github.com/bazelbuild/rules_docker/archive/v0.9.0.tar.gz"], +) + +load( + "@io_bazel_rules_docker//repositories:repositories.bzl", + container_repositories = "repositories", +) + +container_repositories() + +load( + "@io_bazel_rules_docker//repositories:deps.bzl", + container_deps = "deps", +) + +container_deps() + +load( + "@io_bazel_rules_docker//container:container.bzl", + "container_pull", +) + +container_pull( + name = "centos", + digest = "sha256:365fc7f33107869dfcf2b3ba220ce0aa42e16d3f8e8b3c21d72af1ee622f0cf0", + registry = "index.docker.io", + repository = "library/centos", +) diff --git a/testdata/content1.txt b/example_bazel/content1.txt similarity index 100% rename from testdata/content1.txt rename to example_bazel/content1.txt diff --git a/docker_tests.sh b/example_bazel/docker_tests.sh similarity index 100% rename from docker_tests.sh rename to example_bazel/docker_tests.sh diff --git a/testdata/golden_V.txt b/example_bazel/golden_V.txt similarity index 100% rename from testdata/golden_V.txt rename to example_bazel/golden_V.txt diff --git a/testdata/golden_ls.txt b/example_bazel/golden_ls.txt similarity index 100% rename from testdata/golden_ls.txt rename to example_bazel/golden_ls.txt diff --git a/testdata/golden_preinst.txt b/example_bazel/golden_preinst.txt similarity index 100% rename from testdata/golden_preinst.txt rename to example_bazel/golden_preinst.txt From 5ef114d5779ef0f1d0459450f0bcbecfc4680b2d Mon Sep 17 00:00:00 2001 From: Yaron de Leeuw Date: Thu, 19 Sep 2019 15:33:44 -0400 Subject: [PATCH 2/2] Update docker_tests.sh to the new testdata path --- example_bazel/docker_tests.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/example_bazel/docker_tests.sh b/example_bazel/docker_tests.sh index afe79bd..8917925 100755 --- a/example_bazel/docker_tests.sh +++ b/example_bazel/docker_tests.sh @@ -33,11 +33,11 @@ diff_test () { return 0 } -diff_test centos_V testdata/golden_V.txt +diff_test centos_V golden_V.txt V_result="$?" -diff_test centos_ls testdata/golden_ls.txt +diff_test centos_ls golden_ls.txt ls_result="$?" -diff_test centos_preinst testdata/golden_preinst.txt +diff_test centos_preinst golden_preinst.txt preinst_result="$?" if [[ "$V_result" -ne 0 || "$ls_result" -ne 0 || "$preinst_result" -ne 0 ]]; then