misc: Make build on macOS work

This commit is contained in:
Johan Bloemberg
2019-02-06 21:38:33 +01:00
parent 0a052494c4
commit 749038c76d
8 changed files with 99 additions and 55 deletions

View File

@@ -0,0 +1,76 @@
// Mgmt
// Copyright (C) 2013-2018+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// +build !root !darwin
package resources
import (
"io/ioutil"
"os"
"testing"
fstab "github.com/deniswernert/go-fstab"
)
func TestMountExists(t *testing.T) {
const procMock1 = `/tmp/mount0 /mnt/proctest ext4 rw,seclabel,relatime,data=ordered 0 0` + "\n"
var mountExistsTests = []struct {
procMock []byte
in *fstab.Mount
out bool
}{
{
[]byte(procMock1),
&fstab.Mount{
Spec: "/tmp/mount0",
File: "/mnt/proctest",
VfsType: "ext4",
MntOps: map[string]string{"defaults": ""},
Freq: 1,
PassNo: 1,
},
true,
},
}
file, err := ioutil.TempFile("", "proc")
if err != nil {
t.Errorf("error creating temp file: %v", err)
return
}
defer os.Remove(file.Name())
for _, test := range mountExistsTests {
if err := ioutil.WriteFile(file.Name(), test.procMock, 0664); err != nil {
t.Errorf("error writing proc file: %s: %v", file.Name(), err)
return
}
if err := ioutil.WriteFile(test.in.Spec, []byte{}, 0664); err != nil {
t.Errorf("error writing fstab file: %s: %v", file.Name(), err)
return
}
result, err := mountExists(file.Name(), test.in)
if err != nil {
t.Errorf("error checking if fstab entry %s exists: %v", test.in.String(), err)
return
}
if result != test.out {
t.Errorf("mountExistsTests test wanted: %t, got: %t", test.out, result)
}
}
}

View File

@@ -29,8 +29,6 @@ import (
const fstabMock1 = `UUID=ef5726f2-615c-4350-b0ab-f106e5fc90ad / ext4 defaults 1 1` + "\n" const fstabMock1 = `UUID=ef5726f2-615c-4350-b0ab-f106e5fc90ad / ext4 defaults 1 1` + "\n"
const procMock1 = `/tmp/mount0 /mnt/proctest ext4 rw,seclabel,relatime,data=ordered 0 0` + "\n"
var fstabWriteTests = []struct { var fstabWriteTests = []struct {
in fstab.Mounts in fstab.Mounts
}{ }{
@@ -295,49 +293,3 @@ func TestMountCompare(t *testing.T) {
} }
} }
} }
var mountExistsTests = []struct {
procMock []byte
in *fstab.Mount
out bool
}{
{
[]byte(procMock1),
&fstab.Mount{
Spec: "/tmp/mount0",
File: "/mnt/proctest",
VfsType: "ext4",
MntOps: map[string]string{"defaults": ""},
Freq: 1,
PassNo: 1,
},
true,
},
}
func TestMountExists(t *testing.T) {
file, err := ioutil.TempFile("", "proc")
if err != nil {
t.Errorf("error creating temp file: %v", err)
return
}
defer os.Remove(file.Name())
for _, test := range mountExistsTests {
if err := ioutil.WriteFile(file.Name(), test.procMock, 0664); err != nil {
t.Errorf("error writing proc file: %s: %v", file.Name(), err)
return
}
if err := ioutil.WriteFile(test.in.Spec, []byte{}, 0664); err != nil {
t.Errorf("error writing fstab file: %s: %v", file.Name(), err)
return
}
result, err := mountExists(file.Name(), test.in)
if err != nil {
t.Errorf("error checking if fstab entry %s exists: %v", test.in.String(), err)
return
}
if result != test.out {
t.Errorf("mountExistsTests test wanted: %t, got: %t", test.out, result)
}
}
}

View File

@@ -15,6 +15,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// +build !darwin
package resources package resources
import ( import (

View File

@@ -15,6 +15,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// +build !darwin
package coresys package coresys
import ( import (

View File

@@ -15,6 +15,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// +build !darwin
package coresys package coresys
import ( import (

View File

@@ -9,20 +9,26 @@ cd "${ROOT}"
# if we want to run this test as root, use build tag -root to ask each test... # if we want to run this test as root, use build tag -root to ask each test...
XSUDO='' XSUDO=''
XTAGS='' XTAGS=()
if [[ "$@" = *"--root"* ]]; then if [[ "$@" = *"--root"* ]]; then
if ! timeout 1s sudo -A true; then if ! timeout 1s sudo -A true; then
echo "sudo disabled: can't run as root" echo "sudo disabled: can't run as root"
exit 1 exit 1
fi fi
XSUDO='sudo -E' XSUDO='sudo -E'
XTAGS='-tags root' XTAGS+=('root')
fi
# As per https://github.com/travis-ci/docs-travis-ci-com/blob/master/user/docker.md
# Docker is not supported on Travis macOS test instances.
if [[ "$TRAVIS_OS_NAME" == "osx" ]];then
XTAGS+=('nodocker')
fi fi
failures='' failures=''
function run-test() function run-test()
{ {
$XSUDO $@ || failures=$( [ -n "$failures" ] && echo "$failures\\n$@" || echo "$@" ) $XSUDO $@ -tags="${XTAGS[*]}" || failures=$( [ -n "$failures" ] && echo "$failures\\n$@" || echo "$@" )
} }
# NOTE: you can run `go test` with the -tags flag to skip certain tests, eg: # NOTE: you can run `go test` with the -tags flag to skip certain tests, eg:
@@ -31,17 +37,17 @@ base=$(go list .)
if [[ "$@" = *"--integration"* ]]; then if [[ "$@" = *"--integration"* ]]; then
if [[ "$@" = *"--race"* ]]; then if [[ "$@" = *"--race"* ]]; then
# adding -count=1 replaces the GOCACHE=off fix that was removed # adding -count=1 replaces the GOCACHE=off fix that was removed
run-test go test -count=1 -race "${base}/integration" -v ${XTAGS} run-test go test -count=1 -race "${base}/integration" -v
else else
run-test go test -count=1 "${base}/integration" -v ${XTAGS} run-test go test -count=1 "${base}/integration" -v
fi fi
else else
for pkg in `go list -e ./... | grep -v "^${base}/vendor/" | grep -v "^${base}/examples/" | grep -v "^${base}/test/" | grep -v "^${base}/old" | grep -v "^${base}/old/" | grep -v "^${base}/tmp" | grep -v "^${base}/tmp/" | grep -v "^${base}/integration"`; do for pkg in `go list -e ./... | grep -v "^${base}/vendor/" | grep -v "^${base}/examples/" | grep -v "^${base}/test/" | grep -v "^${base}/old" | grep -v "^${base}/old/" | grep -v "^${base}/tmp" | grep -v "^${base}/tmp/" | grep -v "^${base}/integration"`; do
echo -e "\ttesting: $pkg" echo -e "\ttesting: $pkg"
if [[ "$@" = *"--race"* ]]; then if [[ "$@" = *"--race"* ]]; then
run-test go test -count=1 -race "$pkg" ${XTAGS} run-test go test -count=1 -race "$pkg"
else else
run-test go test -count=1 "$pkg" ${XTAGS} run-test go test -count=1 "$pkg"
fi fi
done done
fi fi

View File

@@ -15,6 +15,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// +build !darwin
// Package socketset is in API for creating a select style netlink socket to // Package socketset is in API for creating a select style netlink socket to
// receive events from the kernel. // receive events from the kernel.
package socketset package socketset

View File

@@ -15,6 +15,8 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// +build !darwin
package socketset package socketset
import ( import (