From 442d409f0098e53cf1310f3df6c117f4d19c4502 Mon Sep 17 00:00:00 2001 From: siddharthist Date: Sun, 27 Mar 2016 16:59:13 -0700 Subject: [PATCH] Support apk packages in addition to pacman, deb, rpm Progress on #113 --- checks/packages.go | 74 ++++++++++++++++----------------- checks/users-and-groups_test.go | 26 ------------ glide.lock | 23 +++++++++- 3 files changed, 57 insertions(+), 66 deletions(-) diff --git a/checks/packages.go b/checks/packages.go index da8848c..ee194bb 100644 --- a/checks/packages.go +++ b/checks/packages.go @@ -1,13 +1,17 @@ package checks import ( + "os/exec" + "regexp" + "strings" + "github.com/CiscoCloud/distributive/chkutil" "github.com/CiscoCloud/distributive/errutil" "github.com/CiscoCloud/distributive/tabular" log "github.com/Sirupsen/logrus" - "os/exec" - "regexp" - "strings" + gossresource "github.com/aelsabbahy/goss/resource" + gosssystem "github.com/aelsabbahy/goss/system" + gossutil "github.com/aelsabbahy/goss/util" ) // TODO implement package managers as interfaces @@ -230,10 +234,10 @@ func existsRepoWithProperty(prop string, val *regexp.Regexp, manager string) (in #### RepoExists Description: Is this repo present? Parameters: - - Pakage manager: rpm | dpkg | pacman - - Regexp (regexp): Regexp to match the name of the repo +- Pakage manager: rpm | dpkg | pacman +- Regexp (regexp): Regexp to match the name of the repo Example parameters: - - "base", "firefox-[nN]ightly" +- "base", "firefox-[nN]ightly" */ type RepoExists struct { @@ -267,12 +271,12 @@ func (chk RepoExists) Status() (int, string, error) { #### RepoExistsURI Description: Is a repo with this URI present? Parameters: - - Pakage manager: rpm | dpkg | pacman - - Regexp (regexp): Regexp to match the URI of the repo +- Pakage manager: rpm | dpkg | pacman +- Regexp (regexp): Regexp to match the URI of the repo Example parameters: - - "http://my-repo.example.com", "/path/to/repo" +- "http://my-repo.example.com", "/path/to/repo" Depedencies: - - dpkg | pacman +- dpkg | pacman */ type RepoExistsURI struct { @@ -306,11 +310,11 @@ func (chk RepoExistsURI) Status() (int, string, error) { #### PacmanIgnore Description: Are upgrades to this package ignored by pacman? Parameters: - - Package (string): Name of the package +- Package (string): Name of the package Example parameters: - - node, python, etcd +- node, python, etcd Depedencies: - - pacman, specifically /etc/pacman.conf +- pacman, specifically /etc/pacman.conf */ type PacmanIgnore struct{ pkg string } @@ -347,11 +351,11 @@ func (chk PacmanIgnore) Status() (int, string, error) { #### Installed Description: Is this package Installed? Parameters: - - Package (string): Name of the package +- Package (string): Name of the package Example parameters: - - node, python, etcd +- node, python, etcd Depedencies: - - pacman | dpkg | rpm +- pacman | dpkg | rpm | apk */ type Installed struct{ pkg string } @@ -367,29 +371,23 @@ func (chk Installed) New(params []string) (chkutil.Check, error) { } func (chk Installed) Status() (int, string, error) { - name := getManager() - options := managers[name] - cmd := exec.Command(name, options, chk.pkg) - out, err := cmd.CombinedOutput() - outstr := string(out) - var msg string - switch { - case err == nil && (name == "rpm" || name == "pacman"): - return errutil.Success() - // failures due to mising package - case name == "dpkg" && strings.Contains(outstr, "not installed"): - case name == "pacman" && strings.Contains(outstr, "not found"): - case name == "rpm" && strings.Contains(outstr, "not installed"): - msg := "Package was not found:" - msg += "\n\tPackage name: " + chk.pkg - msg += "\n\tPackage manager: " + name - msg += "\n\tCommand output: " + outstr - return 1, msg, nil - // failures that were not due to packages not being installed - case err != nil: - errutil.ExecError(cmd, outstr, err) + var pkg gosssystem.Package + switch gosssystem.DetectPackageManager() { + case "deb": + pkg = gosssystem.NewDebPackage(chk.pkg, nil, gossutil.Config{}) + case "apk": + pkg = gosssystem.NewAlpinePackage(chk.pkg, nil, gossutil.Config{}) + case "pacman": + pkg = gosssystem.NewPacmanPackage(chk.pkg, nil, gossutil.Config{}) default: + pkg = gosssystem.NewRpmPackage(chk.pkg, nil, gossutil.Config{}) + } + // initialize the package + pkg2, err := gossresource.NewPackage(pkg, gossutil.Config{}) + if err != nil { + return 1, "", err + } else if pkg2.Installed { return errutil.Success() } - return 1, msg, nil + return 1, "Package not found", nil } diff --git a/checks/users-and-groups_test.go b/checks/users-and-groups_test.go index bcde8a6..e1f16e4 100644 --- a/checks/users-and-groups_test.go +++ b/checks/users-and-groups_test.go @@ -31,22 +31,6 @@ func TestGroupID(t *testing.T) { testCheck(goodEggs, badEggs, GroupID{}, t) } -func TestLookupUser(t *testing.T) { - t.Parallel() - user, err := lookupUser("root") - user2, err2 := lookupUser("0") - msg := "Couldn't successfully lookup root user" - if user == nil || err != nil { - msg += "\n\tUsername: root" - msg += "\n\tError: " + err.Error() - t.Error(msg) - } else if user2 == nil || err2 != nil { - msg += "\n\tUID: 0" - msg += "\n\tError: " + err2.Error() - t.Error(msg) - } -} - func TestUserExists(t *testing.T) { t.Parallel() testParameters(names, notLengthOne, UserExists{}, t) @@ -73,16 +57,6 @@ func TestUserHasGID(t *testing.T) { testCheck(goodEggs, badEggs, UserHasGID{}, t) } -func TestUserHasUsername(t *testing.T) { - t.Parallel() - validInputs := reverseAppendParameter(names, "0") - invalidInputs := notLengthTwo - goodEggs := [][]string{[]string{"0", "root"}} // not always true - badEggs := appendParameter(names, "nonsense") - testParameters(validInputs, invalidInputs, UserHasUsername{}, t) - testCheck(goodEggs, badEggs, UserHasUsername{}, t) -} - func TestUserHasHomeDir(t *testing.T) { t.Parallel() validInputs := appendParameter(names, "/home/") diff --git a/glide.lock b/glide.lock index 74600a9..089365d 100644 --- a/glide.lock +++ b/glide.lock @@ -1,13 +1,18 @@ hash: 96c6cc063222cd885c21f622b7830fc4bee8fb3aae2998c8f0768dcf064703a4 -updated: 2016-03-30T11:10:31.005910067-07:00 +updated: 2016-04-11T14:42:58.639851742-07:00 imports: - name: github.com/aelsabbahy/GOnetstat version: 2907f74398ebea717cab8187513bee184b1fdd26 - name: github.com/aelsabbahy/goss - version: d28f3cc6d708fb012ea614acf712eb56712a7de3 + version: 105a300d709ac0a9618b5251c3421df88beb6266 subpackages: - system - util + - resource +- name: github.com/cheekybits/genny + version: 670ed678f7799e3ead194eaf9a0848920a494889 + subpackages: + - generic - name: github.com/codegangsta/cli version: 565493f259bf868adb54d45d5f4c68d405117adf - name: github.com/coreos/go-systemd @@ -41,6 +46,20 @@ imports: version: 0dd3f918b21bec95ace9dc86c7e70266cfc5c702 - name: github.com/mitchellh/panicwrap version: 1655d88c8ff7495ae9d2c19fd8f445f4657e22b0 +- name: github.com/onsi/gomega + version: c72df929b80ef4930aaa75d5e486887ff2f3e06a + subpackages: + - types + - internal/assertion + - internal/asyncassertion + - internal/testingtsupport + - matchers + - internal/oraclematcher + - format + - matchers/support/goraph/bipartitegraph + - matchers/support/goraph/edge + - matchers/support/goraph/node + - matchers/support/goraph/util - name: github.com/opencontainers/runc version: 519529febe2e0acb14e39cf54112c292ccb2eabe subpackages: