Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
)

require (
github.com/Masterminds/semver/v3 v3.3.1 // indirect
github.com/Masterminds/semver/v3 v3.4.0 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/google/go-cmp v0.7.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/Masterminds/semver/v3 v3.3.1 h1:QtNSWtVZ3nBfk8mAOu/B6v7FMJ+NHTIgUPi7rj+4nv4=
github.com/Masterminds/semver/v3 v3.3.1/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0=
github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
Expand Down
49 changes: 49 additions & 0 deletions option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
)

Expand All @@ -19,6 +20,11 @@ const (
nerdctlVersion feature = iota
)

var (
nerdctlVersionRegex = regexp.MustCompile(`nerdctl\s+version\s+(\S+)`)
finchNerdctlVersionRegex = regexp.MustCompile(`nerdctl:\s+Version:\s+(\S+)`)
)

// Option customizes how tests are run.
//
// If a testing function needs special customizations other than the ones specified in Option,
Expand Down Expand Up @@ -127,3 +133,46 @@ func (o *Option) isNerdctlVersion(cmp func(string) bool) bool {

return cmp(version)
}

// Subject returns the subject stored in the option.
func (o *Option) Subject() []string {
return o.subject
}

// GetNerdctlVersion gets the nerdctl version from the subject. If the subject is neither "nerdctl" nor "finch", it will return an error.
func (o *Option) GetNerdctlVersion() (string, error) {
switch o.subject[0] {
case "nerdctl":
//nolint:gosec // G204 is not an issue because subject is fully controlled by the user.
versionBytes, err := exec.Command(o.subject[0], "--version").Output()
if err != nil {
return "", fmt.Errorf("failed to run nerdctl --version: %w", err)
}
version, err := getNerdctlVersionMatch(nerdctlVersionRegex, string(versionBytes))
if err != nil {
return "", err
}
return version, nil
case "finch":
//nolint:gosec // G204 is not an issue because subject is fully controlled by the user.
versionBytes, err := exec.Command(o.subject[0], "version").Output()
if err != nil {
return "", fmt.Errorf("failed to run nerdctl --version: %w", err)
}
version, err := getNerdctlVersionMatch(finchNerdctlVersionRegex, string(versionBytes))
if err != nil {
return "", err
}
return version, nil
default:
return "", fmt.Errorf("unsupported subject %s", o.subject[0])
}
}

func getNerdctlVersionMatch(nerdctlVersionRegexp *regexp.Regexp, versionOutput string) (string, error) {
matches := nerdctlVersionRegexp.FindStringSubmatch(versionOutput)
if len(matches) < 2 {
return "", fmt.Errorf("failed to parse nerdctl version from: %s", versionOutput)
}
return matches[1], nil
}
13 changes: 13 additions & 0 deletions tests/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/runfinch/common-tests/command"
"github.com/runfinch/common-tests/ffs"
"github.com/runfinch/common-tests/option"
"github.com/runfinch/common-tests/testutil"
)

type testCase struct {
Expand Down Expand Up @@ -79,6 +80,9 @@ func HealthCheck(o *option.Option) {

func testCliHealthCheckFlags(o *option.Option) {
ginkgo.Describe("test container healthcheck flags", func() {
ginkgo.BeforeEach(func() {
testutil.RequireNerdctlVersion(o, ">= 2.2.1")
})
ginkgo.BeforeEach(func() {
command.RemoveAll(o)
})
Expand Down Expand Up @@ -162,6 +166,9 @@ func testImageHealthCheckFlags(o *option.Option) {
ginkgo.Describe("test image healthcheck flags", func() {
var buildContext string

ginkgo.BeforeEach(func() {
testutil.RequireNerdctlVersion(o, ">= 2.2.1")
})
ginkgo.BeforeEach(func() {
buildContext = ffs.CreateBuildContext(fmt.Sprintf(`FROM %s
HEALTHCHECK --interval=30s --timeout=10s CMD wget -q http://localhost:8080 || exit 1
Expand Down Expand Up @@ -229,6 +236,9 @@ func testImageHealthCheckFlags(o *option.Option) {

func testHealthCheckStatus(o *option.Option) {
ginkgo.Describe("test automated container healthcheck status", func() {
ginkgo.BeforeEach(func() {
testutil.RequireNerdctlVersion(o, ">= 2.2.1")
})
ginkgo.BeforeEach(func() {
command.RemoveAll(o)
})
Expand Down Expand Up @@ -411,6 +421,9 @@ func testHealthCheckStatus(o *option.Option) {

func testHealthCheckStatusNegativeCases(o *option.Option) {
ginkgo.Describe("test manual container healthcheck status", func() {
ginkgo.BeforeEach(func() {
testutil.RequireNerdctlVersion(o, ">= 2.2.1")
})
ginkgo.BeforeEach(func() {
command.RemoveAll(o)
})
Expand Down
36 changes: 36 additions & 0 deletions testutil/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

// Package testutil provides utility functions for the common tests.
package testutil

import (
"fmt"

"github.com/Masterminds/semver/v3"
"github.com/onsi/ginkgo/v2"

"github.com/runfinch/common-tests/option"
)

// RequireNerdctlVersion skips a test if the nerdctl version does not satisfy the constraint.
func RequireNerdctlVersion(o *option.Option, constraint string) {
c, err := semver.NewConstraint(constraint)
if err != nil {
ginkgo.Fail(fmt.Sprintf("failed to construct constraint from %s: %s", constraint, err.Error()))
return
}
nerdctlVersion, err := o.GetNerdctlVersion()
if err != nil {
ginkgo.Fail(fmt.Sprintf("failed to get nerdctl version: %s", err.Error()))
return
}
v, err := semver.NewVersion(nerdctlVersion)
if err != nil {
ginkgo.Fail(fmt.Sprintf("failed to construct semver from %s: %s", nerdctlVersion, err.Error()))
return
}
if !c.Check(v) {
ginkgo.Skip(fmt.Sprintf("nerdctl version %s does not satisfy constraint %s", v, constraint))
}
}