From ede5e9dc8845e79f983f8fb2a40c9838b6ab47ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20P=C3=A9rez-Aradros=20Herce?= Date: Tue, 28 Aug 2018 10:37:45 +0200 Subject: [PATCH] Test modules and modules.d folder contents in resulting packages (#8107) This change ensures that `modules` (Filebeat) and `modules.d` (Filebeat & Metricbeat) folders contain files in the right place. Permissions and ownership were already tested Part of #7488 (cherry picked from commit aa6c76e3ef3f0ab3879ccb1316a22dda5960567d) --- dev-tools/mage/pkg.go | 38 ++++++++++++++++++++++++++++- dev-tools/packaging/package_test.go | 38 ++++++++++++++++++++++++++++- filebeat/magefile.go | 2 +- metricbeat/magefile.go | 2 +- 4 files changed, 76 insertions(+), 4 deletions(-) diff --git a/dev-tools/mage/pkg.go b/dev-tools/mage/pkg.go index c98d3236ea3..778583ec5ef 100644 --- a/dev-tools/mage/pkg.go +++ b/dev-tools/mage/pkg.go @@ -96,9 +96,36 @@ func (b packageBuilder) Build() error { b.Spec.Name, b.Type, b.Platform.Name) } +type testPackagesParams struct { + HasModules bool + HasModulesD bool +} + +// TestPackagesOption defines a option to the TestPackages target. +type TestPackagesOption func(params *testPackagesParams) + +// WithModules enables modules folder contents testing +func WithModules() func(params *testPackagesParams) { + return func(params *testPackagesParams) { + params.HasModules = true + } +} + +// WithModulesD enables modules.d folder contents testing +func WithModulesD() func(params *testPackagesParams) { + return func(params *testPackagesParams) { + params.HasModulesD = true + } +} + // TestPackages executes the package tests on the produced binaries. These tests // inspect things like file ownership and mode. -func TestPackages() error { +func TestPackages(options ...TestPackagesOption) error { + params := testPackagesParams{} + for _, opt := range options { + opt(¶ms) + } + fmt.Println(">> Testing package contents") goTest := sh.OutCmd("go", "test") @@ -106,6 +133,15 @@ func TestPackages() error { if mg.Verbose() { args = append(args, "-v") } + + if params.HasModules { + args = append(args, "--modules") + } + + if params.HasModulesD { + args = append(args, "--modules.d") + } + args = append(args, MustExpand("{{ elastic_beats_dir }}/dev-tools/packaging/package_test.go"), "-files", diff --git a/dev-tools/packaging/package_test.go b/dev-tools/packaging/package_test.go index 1227acd6a3a..b76487d9eaa 100644 --- a/dev-tools/packaging/package_test.go +++ b/dev-tools/packaging/package_test.go @@ -26,6 +26,7 @@ import ( "bytes" "compress/gzip" "flag" + "fmt" "io" "os" "path/filepath" @@ -54,7 +55,9 @@ var ( ) var ( - files = flag.String("files", "../build/distributions/*/*", "filepath glob containing package files") + files = flag.String("files", "../build/distributions/*/*", "filepath glob containing package files") + modules = flag.Bool("modules", false, "check modules folder contents") + modulesd = flag.Bool("modules.d", false, "check modules.d folder contents") ) func TestRPM(t *testing.T) { @@ -100,6 +103,7 @@ func checkRPM(t *testing.T, file string) { checkManifestPermissions(t, p) checkManifestOwner(t, p) checkModulesPermissions(t, p) + checkModulesPresent(t, "/etc/", p) checkModulesOwner(t, p) } @@ -114,6 +118,7 @@ func checkDeb(t *testing.T, file string, buf *bytes.Buffer) { checkConfigOwner(t, p) checkManifestPermissions(t, p) checkManifestOwner(t, p) + checkModulesPresent(t, "/etc/", p) checkModulesPermissions(t, p) checkModulesOwner(t, p) } @@ -128,6 +133,7 @@ func checkTar(t *testing.T, file string) { checkConfigPermissions(t, p) checkConfigOwner(t, p) checkManifestPermissions(t, p) + checkModulesPresent(t, "", p) checkModulesPermissions(t, p) checkModulesOwner(t, p) } @@ -141,6 +147,7 @@ func checkZip(t *testing.T, file string) { checkConfigPermissions(t, p) checkManifestPermissions(t, p) + checkModulesPresent(t, "", p) checkModulesPermissions(t, p) } @@ -246,6 +253,35 @@ func checkModulesOwner(t *testing.T, p *packageFile) { }) } +// Verify that modules.d folder is present and has module files in +func checkModulesPresent(t *testing.T, prefix string, p *packageFile) { + minExpectedModules := 4 + + test := func(name string, r *regexp.Regexp) { + t.Run(fmt.Sprintf("%s %s contents", p.Name, name), func(t *testing.T) { + total := 0 + for _, entry := range p.Contents { + if strings.HasPrefix(entry.File, prefix) && r.MatchString(entry.File) { + total++ + } + } + + if total < minExpectedModules { + t.Errorf("not enough modules found under %s: actual=%d, expected>=%d", + name, total, minExpectedModules) + } + }) + } + + if *modules { + test("modules", modulesFilePattern) + } + + if *modulesd { + test("modules.d", modulesDirPattern) + } +} + // Helpers type packageFile struct { diff --git a/filebeat/magefile.go b/filebeat/magefile.go index 41cb9ce9014..f81483584b3 100644 --- a/filebeat/magefile.go +++ b/filebeat/magefile.go @@ -83,7 +83,7 @@ func Package() { // TestPackages tests the generated packages (i.e. file modes, owners, groups). func TestPackages() error { - return mage.TestPackages() + return mage.TestPackages(mage.WithModules(), mage.WithModulesD()) } // Update updates the generated files (aka make update). diff --git a/metricbeat/magefile.go b/metricbeat/magefile.go index a84242fa73f..9843e03d281 100644 --- a/metricbeat/magefile.go +++ b/metricbeat/magefile.go @@ -83,7 +83,7 @@ func Package() { // TestPackages tests the generated packages (i.e. file modes, owners, groups). func TestPackages() error { - return mage.TestPackages() + return mage.TestPackages(mage.WithModulesD()) } // Update updates the generated files (aka make update).