Skip to content

Commit

Permalink
Support apk packages in addition to pacman, deb, rpm
Browse files Browse the repository at this point in the history
Progress on #113
  • Loading branch information
langston-barrett committed Apr 11, 2016
1 parent 305e0ce commit 442d409
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 66 deletions.
74 changes: 36 additions & 38 deletions checks/packages.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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 }
Expand All @@ -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
}
26 changes: 0 additions & 26 deletions checks/users-and-groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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/")
Expand Down
23 changes: 21 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 442d409

Please sign in to comment.