Skip to content

Commit

Permalink
Merge pull request #1664 from djdongjin/fix-test-binary
Browse files Browse the repository at this point in the history
Add RequireExecutable to testutil
  • Loading branch information
AkihiroSuda committed Dec 17, 2022
2 parents 9d3ecc0 + 162602b commit e83edd8
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 27 deletions.
5 changes: 1 addition & 4 deletions cmd/nerdctl/compose_run_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"fmt"
"io"
"os/exec"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -412,9 +411,7 @@ services:
}

func TestComposePushAndPullWithCosignVerify(t *testing.T) {
if _, err := exec.LookPath("cosign"); err != nil {
t.Skip()
}
testutil.RequireExecutable(t, "cosign")
testutil.DockerIncompatible(t)
testutil.RequiresBuild(t)
base := testutil.NewBase(t)
Expand Down
7 changes: 2 additions & 5 deletions cmd/nerdctl/image_convert_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
"fmt"
"os/exec"
"runtime"
"testing"

Expand All @@ -32,7 +31,7 @@ func TestImageConvertNydus(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("no windows support yet")
}

testutil.RequireExecutable(t, "nydus-image")
testutil.DockerIncompatible(t)
base := testutil.NewBase(t)
convertedImage := testutil.Identifier(t) + ":nydus"
Expand All @@ -50,9 +49,7 @@ func TestImageConvertNydus(t *testing.T) {
}

// skip if nydusify is not installed
if _, err := exec.LookPath("nydusify"); err != nil {
t.Skip("Nydusify is not installed")
}
testutil.RequireExecutable(t, "nydusify")

// setup local docker registry
registryPort := 15000
Expand Down
4 changes: 1 addition & 3 deletions cmd/nerdctl/image_encrypt_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ type jweKeyPair struct {
}

func newJWEKeyPair(t testing.TB) *jweKeyPair {
if _, err := exec.LookPath("openssl"); err != nil {
t.Skip(err)
}
testutil.RequireExecutable(t, "openssl")
td, err := os.MkdirTemp(t.TempDir(), "jwe-key-pair")
assert.NilError(t, err)
prv := filepath.Join(td, "mykey.pem")
Expand Down
6 changes: 1 addition & 5 deletions cmd/nerdctl/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
"fmt"
"os/exec"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -104,10 +103,7 @@ func TestLogsWithInheritedFlags(t *testing.T) {

func TestLogsOfJournaldDriver(t *testing.T) {
t.Parallel()
_, err := exec.LookPath("journalctl")
if err != nil {
t.Skipf("`journalctl` executable is required for this test: %s", err)
}
testutil.RequireExecutable(t, "journalctl")
base := testutil.NewBase(t)
containerName := testutil.Identifier(t)

Expand Down
8 changes: 2 additions & 6 deletions cmd/nerdctl/pull_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ func newCosignKeyPair(t testing.TB, path string) *cosignKeyPair {
}

func TestImageVerifyWithCosign(t *testing.T) {
if _, err := exec.LookPath("cosign"); err != nil {
t.Skip()
}
testutil.RequireExecutable(t, "cosign")
testutil.DockerIncompatible(t)
testutil.RequiresBuild(t)
t.Setenv("COSIGN_PASSWORD", "1")
Expand Down Expand Up @@ -114,9 +112,7 @@ CMD ["echo", "nerdctl-build-test-string"]
}

func TestImageVerifyWithCosignShouldFailWhenKeyIsNotCorrect(t *testing.T) {
if _, err := exec.LookPath("cosign"); err != nil {
t.Skip()
}
testutil.RequireExecutable(t, "cosign")
testutil.DockerIncompatible(t)
testutil.RequiresBuild(t)
t.Setenv("COSIGN_PASSWORD", "1")
Expand Down
5 changes: 1 addition & 4 deletions cmd/nerdctl/run_verify_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"fmt"
"os"
"os/exec"
"testing"

"github.com/containerd/nerdctl/pkg/testutil"
Expand All @@ -28,9 +27,7 @@ import (
)

func TestRunVerifyCosign(t *testing.T) {
if _, err := exec.LookPath("cosign"); err != nil {
t.Skip()
}
testutil.RequireExecutable(t, "cosign")
testutil.DockerIncompatible(t)
testutil.RequiresBuild(t)
t.Setenv("COSIGN_PASSWORD", "1")
Expand Down
7 changes: 7 additions & 0 deletions pkg/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,13 @@ func RequireSystemService(t testing.TB, sv string) {
}
}

// RequireExecutable skips tests when executable `name` is not present in PATH.
func RequireExecutable(t testing.TB, name string) {
if _, err := exec.LookPath(name); err != nil {
t.Skipf("required executable doesn't exist in PATH: %s", name)
}
}

const Namespace = "nerdctl-test"

func NewBaseWithNamespace(t *testing.T, ns string) *Base {
Expand Down

0 comments on commit e83edd8

Please sign in to comment.