Skip to content

Commit

Permalink
[wip] travis: run unit tests on MacOS
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksa Sarai <asarai@suse.de>
  • Loading branch information
cyphar committed Jun 20, 2020
1 parent fc7a6dc commit 21a812d
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 41 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ matrix:
# Clear DOCKER_IMAGE since we don't use it.
- DOCKER_IMAGE=""
script:
# TODO: Run the integration and unit tests, so we don't need to
# special-case MacOS here.
# TODO: Run the integration tests and rest of the CI, so we don't need
# to special-case MacOS here.
- make local-validate-build
- make local-test-unit
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ umociimage:
docker build -t $(UMOCI_IMAGE) --build-arg DOCKER_IMAGE=$(DOCKER_IMAGE) .

ifndef COVERAGE
COVERAGE := $(basename $(shell mktemp -u umoci.cov.XXXXXX))
COVERAGE := $(notdir $(shell mktemp -u umoci.cov.XXXXXX))
export COVERAGE
endif

.PHONY: test-unit
Expand Down
3 changes: 2 additions & 1 deletion hack/publish-site.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
# limitations under the License.

set -Eeuo pipefail
source "$(dirname "$BASH_SOURCE")/readlinkf.sh"

# Change to site root.
site_root="$(readlink -f "$(dirname "${BASH_SOURCE}")/../.site")"
site_root="$(readlinkf_posix "$(dirname "${BASH_SOURCE}")/../.site")"
cd "$site_root"

# Make sure that we've checked out submodules.
Expand Down
43 changes: 43 additions & 0 deletions hack/readlinkf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash
# readlinkf: POSIX-compliant implementation of readlink -f.
# Author: Koichi Nakashima <koichi@nksm.name>
# Licensed under the Creative Commons Zero v1.0 Universal license.
# <https://creativecommons.org/publicdomain/zero/1.0/>

# Copied verbatim from v1.1.0 of <https://github.com/ko1nksm/readlinkf>.

# POSIX compliant version
readlinkf_posix() {
[ "${1:-}" ] || return 1
max_symlinks=40
CDPATH='' # to avoid changing to an unexpected directory

target=$1
[ -e "${target%/}" ] || target=${1%"${1##*[!/]}"} # trim trailing slashes
[ -d "${target:-/}" ] && target="$target/"

cd -P . 2>/dev/null || return 1
while [ "$max_symlinks" -ge 0 ] && max_symlinks=$((max_symlinks - 1)); do
if [ ! "$target" = "${target%/*}" ]; then
case $target in
/*) cd -P "${target%/*}/" 2>/dev/null || break ;;
*) cd -P "./${target%/*}" 2>/dev/null || break ;;
esac
target=${target##*/}
fi

if [ ! -L "$target" ]; then
target="${PWD%/}${target:+/}${target}"
printf '%s\n' "${target:-/}"
return 0
fi

# `ls -dl` format: "%s %u %s %s %u %s %s -> %s\n",
# <file mode>, <number of links>, <owner name>, <group name>,
# <size>, <date and time>, <pathname of link>, <contents of link>
# https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html
link=$(ls -dl -- "$target" 2>/dev/null) || break
target=${link#*" $target -> "}
done
return 1
}
3 changes: 2 additions & 1 deletion hack/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

set -Eeuo pipefail
source "$(dirname "$BASH_SOURCE")/readlinkf.sh"

## --->
# Project-specific options and functions. In *theory* you shouldn't need to
# touch anything else in this script in order to use this elsewhere.
project="umoci"
root="$(readlink -f "$(dirname "${BASH_SOURCE}")/..")"
root="$(readlinkf_posix "$(dirname "${BASH_SOURCE}")/..")"

# These functions allow you to configure how the defaults are computed.
function get_arch() { go env GOARCH || uname -m; }
Expand Down
8 changes: 5 additions & 3 deletions hack/test-integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
# limitations under the License.

set -Eeuxo pipefail
source "$(dirname "$BASH_SOURCE")/readlinkf.sh"

export COVER="${COVER:-0}"
export ROOT="$(readlinkf_posix "$(dirname "$BASH_SOURCE")/..")"

# Set up the root and coverage directories.
export ROOT="$(readlink -f "$(dirname "$(readlink -f "$BASH_SOURCE")")/..")"
# Set up the coverage directory.
COVERAGE="${COVERAGE:-}"
export COVER="${COVER:-0}"
if [ "$COVER" -eq 1 ]; then
export COVERAGE_DIR=$(mktemp -dt umoci-coverage.XXXXXX)
fi
Expand Down
7 changes: 4 additions & 3 deletions hack/test-unit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
# limitations under the License.

set -Eeuxo pipefail
source "$(dirname "$BASH_SOURCE")/readlinkf.sh"

export ROOT="$(readlinkf_posix "$(dirname "$BASH_SOURCE")/..")"

GO="${GO:-go}"
COVERAGE="${COVERAGE:-}"
PROJECT="${PROJECT:-github.com/opencontainers/umoci}"

# Set up the root and coverage directories.
export ROOT="$(readlink -f "$(dirname "$(readlink -f "$BASH_SOURCE")")/..")"

# Run the tests.
extra_args=()
if [ -n "$COVERAGE" ]
Expand Down
3 changes: 2 additions & 1 deletion hack/test-vendor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# limitations under the License.

set -Eeuxo pipefail
source "$(dirname "$BASH_SOURCE")/readlinkf.sh"

# Generate a hash-of-hashes for the entire vendor/ tree.
function gethash() {
Expand All @@ -26,7 +27,7 @@ function gethash() {
}

# Figure out root directory.
ROOT="$(readlink -f "$(dirname "$(readlink -f "$BASH_SOURCE")")/..")"
ROOT="$(readlinkf_posix "$(dirname "$BASH_SOURCE")/..")"
STASHED_ROOT="$(mktemp -dt umoci-vendor.XXXXXX)"

# Stash away old vendor tree, and restore it on-exit.
Expand Down
11 changes: 6 additions & 5 deletions oci/layer/tar_extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"time"

rspec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/umoci/pkg/testutils"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -226,8 +227,8 @@ func TestUnpackEntryWhiteout(t *testing.T) {
{"HiddenDirInSubdir", "another/path/.hiddendir", true},
} {
t.Logf("running Test%s", test.name)
testMtime := time.Unix(123, 456)
testAtime := time.Unix(789, 111)
testMtime := testutils.Unix(123, 456)
testAtime := testutils.Unix(789, 111)

dir, err := ioutil.TempDir("", "umoci-TestUnpackEntryWhiteout")
if err != nil {
Expand Down Expand Up @@ -344,9 +345,9 @@ func TestUnpackOpaqueWhiteout(t *testing.T) {
Typeflag: ph.typeflag,
Mode: int64(mode),
Size: size,
ModTime: time.Unix(1210393, 4528036),
AccessTime: time.Unix(7892829, 2341211),
ChangeTime: time.Unix(8731293, 8218947),
ModTime: testutils.Unix(1210393, 4528036),
AccessTime: testutils.Unix(7892829, 2341211),
ChangeTime: testutils.Unix(8731293, 8218947),
}, r
}

Expand Down
17 changes: 9 additions & 8 deletions pkg/system/utime_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"
"time"

"github.com/opencontainers/umoci/pkg/testutils"
"golang.org/x/sys/unix"
)

Expand All @@ -42,8 +43,8 @@ func TestLutimesFile(t *testing.T) {
t.Fatal(err)
}

atime := time.Unix(125812851, 128518257)
mtime := time.Unix(257172893, 995216512)
atime := testutils.Unix(125812851, 128518257)
mtime := testutils.Unix(257172893, 995216512)

if err := unix.Lstat(path, &fiOld); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -91,8 +92,8 @@ func TestLutimesDirectory(t *testing.T) {
t.Fatal(err)
}

atime := time.Unix(128551231, 273285257)
mtime := time.Unix(185726393, 752135712)
atime := testutils.Unix(128551231, 273285257)
mtime := testutils.Unix(185726393, 752135712)

if err := unix.Lstat(path, &fiOld); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -140,8 +141,8 @@ func TestLutimesSymlink(t *testing.T) {
t.Fatal(err)
}

atime := time.Unix(128551231, 273285257)
mtime := time.Unix(185726393, 752135712)
atime := testutils.Unix(128551231, 273285257)
mtime := testutils.Unix(185726393, 752135712)

if err := unix.Lstat(path, &fiOld); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -218,8 +219,8 @@ func TestLutimesRelative(t *testing.T) {
t.Fatal(err)
}

atime := time.Unix(134858232, 258921237)
mtime := time.Unix(171257291, 425815288)
atime := testutils.Unix(134858232, 258921237)
mtime := testutils.Unix(171257291, 425815288)

if err := unix.Lstat(path, &fiOld); err != nil {
t.Fatal(err)
Expand Down
28 changes: 28 additions & 0 deletions pkg/testutils/ftimes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* umoci: Umoci Modifies Open Containers' Images
* Copyright (C) 2016-2020 SUSE LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package testutils

import (
"time"
)

// Unix returns the local Time corresponding to the given Unix time, rounded to
// the timestamp granularity for this system.
func Unix(sec, nsec int64) time.Time {
return time.Unix(sec, nsec).Round(FtimeGranularity())
}
29 changes: 29 additions & 0 deletions pkg/testutils/ftimes_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* umoci: Umoci Modifies Open Containers' Images
* Copyright (C) 2016-2020 SUSE LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package testutils

import (
"time"
)

// FtimeGranularity is the filesystem timestamp granularity for this system.
func FtimeGranularity() time.Duration {
// On OS X, filesystem timestamps have second-level granularity.
// Nanosecond values are silently truncated if you try to set them.
return time.Second
}
32 changes: 32 additions & 0 deletions pkg/testutils/ftimes_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// +build !darwin

/*
* umoci: Umoci Modifies Open Containers' Images
* Copyright (C) 2016-2020 SUSE LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package testutils

import (
"time"
)

// FtimeGranularity is the filesystem timestamp granularity for this system.
// TODO: We should probably make this dependent on the path since some
// filesystems can have different granularities...
func FtimeGranularity() time.Duration {
// On Linux, most filesystems have nano-second-level granularity.
return time.Nanosecond
}
14 changes: 7 additions & 7 deletions pkg/unpriv/unpriv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"path/filepath"
"reflect"
"testing"
"time"

"github.com/opencontainers/umoci/pkg/testutils"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -891,8 +891,8 @@ func TestChtimes(t *testing.T) {
hdrOld, _ := tar.FileInfoHeader(fi, "")

// Modify the times.
atime := time.Unix(12345678, 12421512)
mtime := time.Unix(11245631, 13373321)
atime := testutils.Unix(12345678, 12421512)
mtime := testutils.Unix(11245631, 13373321)
if err := Chtimes(filepath.Join(dir, "some", "parent", "directories"), atime, mtime); err != nil {
t.Errorf("unexpected error from unpriv.chtimes: %s", err)
}
Expand Down Expand Up @@ -1008,8 +1008,8 @@ func TestLutimes(t *testing.T) {
hdrDirOld, _ := tar.FileInfoHeader(fi, "")

// Modify the times.
atime := time.Unix(12345678, 12421512)
mtime := time.Unix(11245631, 13373321)
atime := testutils.Unix(12345678, 12421512)
mtime := testutils.Unix(11245631, 13373321)
if err := Lutimes(filepath.Join(dir, "some", "parent", "directories"), atime, mtime); err != nil {
t.Errorf("unexpected error from unpriv.lutimes: %s", err)
}
Expand All @@ -1035,8 +1035,8 @@ func TestLutimes(t *testing.T) {
}

// Do the same for a symlink.
atime = time.Unix(18127518, 12421122)
mtime = time.Unix(15245123, 19912991)
atime = testutils.Unix(18127518, 12421122)
mtime = testutils.Unix(15245123, 19912991)

fi, err = Lstat(filepath.Join(dir, "some", "parent", "directories", "link2"))
if err != nil {
Expand Down
Loading

0 comments on commit 21a812d

Please sign in to comment.