Skip to content

Commit 87ce0cb

Browse files
author
Luca Bruno
authored
Merge pull request #278 from lucab/ups/travis-golang
travis: split test compilation and run phases
2 parents 39ca1b0 + 223ed74 commit 87ce0cb

File tree

10 files changed

+232
-10
lines changed

10 files changed

+232
-10
lines changed

.travis.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
1-
language: shell # We do everything inside Docker and don't want travis fiddling with steps or environment variables
2-
1+
dist: trusty
32
sudo: required
4-
53
services:
64
- docker
75

6+
language: go
7+
go:
8+
- "1.5.x"
9+
- "1.10.x"
10+
go_import_path: github.com/coreos/go-systemd
11+
812
env:
913
global:
14+
- IMPORTPATH=github.com/coreos/go-systemd
1015
- GOPATH=/opt
16+
- GO15VENDOREXPERIMENT=1
17+
- DEP_BINDIR=/tmp
1118
- BUILD_DIR=/opt/src/github.com/coreos/go-systemd
1219
matrix:
20+
- DOCKER_BASE=ubuntu:16.04
1321
- DOCKER_BASE=ubuntu:18.04
1422
- DOCKER_BASE=debian:stretch
1523

1624
before_install:
25+
- sudo apt-get -qq update
26+
- sudo apt-get install -y libsystemd-journal-dev libsystemd-daemon-dev
27+
- curl https://raw.githubusercontent.com/golang/dep/master/install.sh | INSTALL_DIRECTORY=${DEP_BINDIR} sh
28+
- ${DEP_BINDIR}/dep ensure -v
1729
- docker pull ${DOCKER_BASE}
18-
- docker run --privileged -e GOPATH=${GOPATH} --cidfile=/tmp/cidfile ${DOCKER_BASE} /bin/bash -c "apt-get update && apt-get install -y build-essential git golang dbus libsystemd-dev libpam-systemd systemd-container && go get github.com/coreos/pkg/dlopen && go get github.com/godbus/dbus"
30+
- docker run --privileged -e GOPATH=${GOPATH} --cidfile=/tmp/cidfile ${DOCKER_BASE} /bin/bash -c "apt-get update && apt-get install -y sudo build-essential git golang dbus libsystemd-dev libpam-systemd systemd-container"
1931
- docker commit `cat /tmp/cidfile` go-systemd/container-tests
2032
- rm -f /tmp/cidfile
2133

2234
install:
2335
- docker run -d --cidfile=/tmp/cidfile --privileged -e GOPATH=${GOPATH} -v ${PWD}:${BUILD_DIR} go-systemd/container-tests /bin/systemd --system
2436

2537
script:
26-
- docker exec `cat /tmp/cidfile` /bin/bash -c "cd ${BUILD_DIR} && ./test"
38+
- ./scripts/travis/pr-test.sh go_fmt
39+
- ./scripts/travis/pr-test.sh build_source
40+
- ./scripts/travis/pr-test.sh build_tests
41+
- docker exec `cat /tmp/cidfile` /bin/bash -c "cd ${BUILD_DIR} && ./scripts/travis/pr-test.sh run_tests"
42+
- ./scripts/travis/pr-test.sh go_vet
43+
- ./scripts/travis/pr-test.sh license_check
2744

2845
after_script:
2946
- docker kill `cat /tmp/cidfile`

Gopkg.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[constraint]]
2+
name = "github.com/coreos/pkg"
3+
version = "4.0.0"
4+
5+
[[constraint]]
6+
name = "github.com/godbus/dbus"
7+
version = "4.1.0"
8+
9+
[prune]
10+
go-tests = true
11+
unused-packages = true

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
[![Build Status](https://travis-ci.org/coreos/go-systemd.png?branch=master)](https://travis-ci.org/coreos/go-systemd)
44
[![godoc](https://godoc.org/github.com/coreos/go-systemd?status.svg)](http://godoc.org/github.com/coreos/go-systemd)
5+
![minimum golang 1.5](https://img.shields.io/badge/golang-1.5%2B-orange.svg)
6+
57

68
Go bindings to systemd. The project has several packages:
79

activation/common_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2018 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package activation
16+
17+
import (
18+
"fmt"
19+
"os"
20+
)
21+
22+
// exampleCmd returns the command line for the specified example binary.
23+
func exampleCmd(binaryName string) (string, []string) {
24+
sourcePath := fmt.Sprintf("../examples/activation/%s.go", binaryName)
25+
sourceCmdLine := []string{"go", "run", sourcePath}
26+
binaryPath := fmt.Sprintf("../test_bins/%s.example", binaryName)
27+
if _, err := os.Stat(binaryPath); err != nil && os.IsNotExist(err) {
28+
return sourceCmdLine[0], sourceCmdLine
29+
}
30+
return binaryPath, []string{binaryPath}
31+
}

activation/files_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ func correctStringWritten(t *testing.T, r *os.File, expected string) bool {
3838
// TestActivation forks out a copy of activation.go example and reads back two
3939
// strings from the pipes that are passed in.
4040
func TestActivation(t *testing.T) {
41-
cmd := exec.Command("go", "run", "../examples/activation/activation.go")
41+
arg0, cmdline := exampleCmd("activation")
42+
cmd := exec.Command(arg0, cmdline...)
4243

4344
r1, w1, _ := os.Pipe()
4445
r2, w2, _ := os.Pipe()
@@ -60,7 +61,8 @@ func TestActivation(t *testing.T) {
6061
}
6162

6263
func TestActivationNoFix(t *testing.T) {
63-
cmd := exec.Command("go", "run", "../examples/activation/activation.go")
64+
arg0, cmdline := exampleCmd("activation")
65+
cmd := exec.Command(arg0, cmdline...)
6466
cmd.Env = os.Environ()
6567
cmd.Env = append(cmd.Env, "LISTEN_FDS=2")
6668

@@ -71,7 +73,8 @@ func TestActivationNoFix(t *testing.T) {
7173
}
7274

7375
func TestActivationNoFiles(t *testing.T) {
74-
cmd := exec.Command("go", "run", "../examples/activation/activation.go")
76+
arg0, cmdline := exampleCmd("activation")
77+
cmd := exec.Command(arg0, cmdline...)
7578
cmd.Env = os.Environ()
7679
cmd.Env = append(cmd.Env, "LISTEN_FDS=0", "FIX_LISTEN_PID=1")
7780

activation/listeners_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ func correctStringWrittenNet(t *testing.T, r net.Conn, expected string) bool {
3838
// TestActivation forks out a copy of activation.go example and reads back two
3939
// strings from the pipes that are passed in.
4040
func TestListeners(t *testing.T) {
41-
cmd := exec.Command("go", "run", "../examples/activation/listen.go")
41+
arg0, cmdline := exampleCmd("listen")
42+
cmd := exec.Command(arg0, cmdline...)
4243

4344
l1, err := net.Listen("tcp", ":9999")
4445
if err != nil {

activation/packetconns_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import (
2424
// TestActivation forks out a copy of activation.go example and reads back two
2525
// strings from the pipes that are passed in.
2626
func TestPacketConns(t *testing.T) {
27-
cmd := exec.Command("go", "run", "../examples/activation/udpconn.go")
27+
arg0, cmdline := exampleCmd("udpconn")
28+
cmd := exec.Command(arg0, cmdline...)
2829

2930
u1, err := net.ListenUDP("udp", &net.UDPAddr{Port: 9999})
3031
if err != nil {

journal/journal_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2018 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package journal
16+
17+
import (
18+
"testing"
19+
)
20+
21+
func TestValidaVarName(t *testing.T) {
22+
testCases := []struct {
23+
testcase string
24+
valid bool
25+
}{
26+
{
27+
"TEST",
28+
true,
29+
},
30+
{
31+
"test",
32+
false,
33+
},
34+
}
35+
36+
for _, tt := range testCases {
37+
valid := validVarName(tt.testcase)
38+
if valid != tt.valid {
39+
t.Fatalf("expected %t, got %t", tt.valid, valid)
40+
}
41+
}
42+
}

scripts/travis/pr-test.sh

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
set -o pipefail
4+
5+
PROJ="go-systemd"
6+
ORG_PATH="github.com/coreos"
7+
REPO_PATH="${ORG_PATH}/${PROJ}"
8+
9+
PACKAGES="activation daemon dbus journal login1 machine1 sdjournal unit util"
10+
EXAMPLES="activation listen udpconn"
11+
12+
function build_source {
13+
go build ./...
14+
}
15+
16+
function build_tests {
17+
rm -rf ./test_bins ; mkdir -p ./test_bins
18+
for pkg in ${PACKAGES}; do
19+
echo " - ${pkg}"
20+
go test -c -o ./test_bins/${pkg}.test ./${pkg}
21+
done
22+
for ex in ${EXAMPLES}; do
23+
echo " - examples/${ex}"
24+
go build -o ./test_bins/${ex}.example ./examples/activation/${ex}.go
25+
done
26+
}
27+
28+
function run_tests {
29+
pushd test_bins
30+
sudo -v
31+
for pkg in ${PACKAGES}; do
32+
echo " - ${pkg}"
33+
sudo -E ./${pkg}.test -test.v
34+
done
35+
popd
36+
rm -rf ./test_bins
37+
}
38+
39+
function go_fmt {
40+
for pkg in ${PACKAGES}; do
41+
echo " - ${pkg}"
42+
fmtRes=$(gofmt -l "./${pkg}")
43+
if [ -n "${fmtRes}" ]; then
44+
echo -e "gofmt checking failed:\n${fmtRes}"
45+
exit 255
46+
fi
47+
done
48+
}
49+
50+
function go_vet {
51+
for pkg in ${PACKAGES}; do
52+
echo " - ${pkg}"
53+
vetRes=$(go vet "./${pkg}")
54+
if [ -n "${vetRes}" ]; then
55+
echo -e "govet checking failed:\n${vetRes}"
56+
exit 254
57+
fi
58+
done
59+
}
60+
61+
function license_check {
62+
licRes=$(for file in $(find . -type f -iname '*.go' ! -path './vendor/*'); do
63+
head -n3 "${file}" | grep -Eq "(Copyright|generated|GENERATED)" || echo -e " ${file}"
64+
done;)
65+
if [ -n "${licRes}" ]; then
66+
echo -e "license header checking failed:\n${licRes}"
67+
exit 253
68+
fi
69+
}
70+
71+
export GO15VENDOREXPERIMENT=1
72+
73+
subcommand="$1"
74+
case "$subcommand" in
75+
"build_source" )
76+
echo "Building source..."
77+
build_source
78+
;;
79+
80+
"build_tests" )
81+
echo "Building tests..."
82+
build_tests
83+
;;
84+
85+
"run_tests" )
86+
echo "Running tests..."
87+
run_tests
88+
;;
89+
90+
"go_fmt" )
91+
echo "Checking gofmt..."
92+
go_fmt
93+
;;
94+
95+
"go_vet" )
96+
echo "Checking govet..."
97+
go_vet
98+
;;
99+
100+
"license_check" )
101+
echo "Checking licenses..."
102+
license_check
103+
;;
104+
105+
* )
106+
echo "Error: unrecognized subcommand."
107+
exit 1
108+
;;
109+
esac

util/util_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ func TestRunningFromSystemService(t *testing.T) {
4040
func TestCurrentUnitName(t *testing.T) {
4141
testIsRunningSystemd(t)
4242

43+
fromService, err := RunningFromSystemService()
44+
if err != nil || !fromService {
45+
t.Skip("Not running from a systemd service")
46+
}
47+
4348
s, err := CurrentUnitName()
4449
if err != nil {
4550
t.Error(err.Error())

0 commit comments

Comments
 (0)