From 3a641b56f6082096e311cc63e9e2100886c73c11 Mon Sep 17 00:00:00 2001 From: Shengjing Zhu Date: Wed, 5 Jan 2022 22:54:23 +0800 Subject: [PATCH] katatestutils: remove distro constraints The distro constraint parses os release files, which may not contain distro version(VERSION_ID field), for example rolling release distributions like Debian testing, archlinux. These distro constraints are not used anyway, so removing them instead of fixing the complex version detection. Fixes: #1864 Signed-off-by: Shengjing Zhu --- src/runtime/pkg/katatestutils/README.md | 30 - src/runtime/pkg/katatestutils/constraints.go | 111 +--- .../pkg/katatestutils/constraints_api.go | 141 +---- .../pkg/katatestutils/constraints_test.go | 564 +----------------- 4 files changed, 24 insertions(+), 822 deletions(-) diff --git a/src/runtime/pkg/katatestutils/README.md b/src/runtime/pkg/katatestutils/README.md index 4e8221cb8a..3a84afd49a 100644 --- a/src/runtime/pkg/katatestutils/README.md +++ b/src/runtime/pkg/katatestutils/README.md @@ -102,36 +102,6 @@ func TestOnlyRunWhenNotRoot(t *testing.T) { } ``` -#### Skip tests based on distro - -Use the `NeedDistro()` constraint to skip a test unless running on a -particular Linux distribution: - -```go -func TestOnlyRunOnUbuntu(t *testing.T) { - - if tc.NotValid(ktu.NeedDistro("ubuntu")) { - t.Skip("skipping test as not running on ubuntu") - } - - // Test code to run on Ubuntu only ... -} -``` - -Use the `NeedDistroNotEquals()` constraint to skip a test unless running -on a Linux distribution other than the one specified: - -```go -func TestDontRunOnFedora(t *testing.T) { - - if tc.NotValid(ktu.NeedDistroNotEquals("fedora")) { - t.Skip("skipping test as running on fedora") - } - - // Test code to run on any distro apart from Fedora ... -} -``` - #### Skip tests based on kernel version Use the `NeedKernelVersionGE()` constraint to skip a test unless running on a diff --git a/src/runtime/pkg/katatestutils/constraints.go b/src/runtime/pkg/katatestutils/constraints.go index fd73d0781b..48b3e5d3b3 100644 --- a/src/runtime/pkg/katatestutils/constraints.go +++ b/src/runtime/pkg/katatestutils/constraints.go @@ -18,17 +18,9 @@ import ( const ( TestDisabledNeedRoot = "Test disabled as requires root user" TestDisabledNeedNonRoot = "Test disabled as requires non-root user" - - // See https://www.freedesktop.org/software/systemd/man/os-release.html - osRelease = "/etc/os-release" - osReleaseAlternative = "/usr/lib/os-release" ) -var ( - errUnknownDistroName = errors.New("unknown distro name") - errUnknownDistroVersion = errors.New("unknown distro version") - errInvalidOpForConstraint = errors.New("invalid operator for constraint type") -) +var errInvalidOpForConstraint = errors.New("invalid operator for constraint type") // String converts the operator to a human-readable value. func (o Operator) String() (s string) { @@ -87,54 +79,6 @@ func getKernelVersion() (string, error) { return fixKernelVersion(fields[2]), nil } -// getDistroDetails returns the distributions name and version string. -// If it is not possible to determine both values an error is -// returned. -func getDistroDetails() (name, version string, err error) { - files := []string{osRelease, osReleaseAlternative} - name = "" - version = "" - - for _, file := range files { - contents, err := getFileContents(file) - if err != nil { - if os.IsNotExist(err) { - continue - } - - return "", "", err - } - - lines := strings.Split(contents, "\n") - - for _, line := range lines { - if strings.HasPrefix(line, "ID=") && name == "" { - fields := strings.Split(line, "=") - name = strings.Trim(fields[1], `"`) - name = strings.ToLower(name) - } else if strings.HasPrefix(line, "VERSION_ID=") && version == "" { - fields := strings.Split(line, "=") - version = strings.Trim(fields[1], `"`) - version = strings.ToLower(version) - } - } - - if name != "" && version != "" { - return name, version, nil - } - } - - if name == "" { - return "", "", errUnknownDistroName - } - - if version == "" { - return "", "", errUnknownDistroVersion - } - - return name, version, nil -} - // fixKernelVersion replaces underscores with dashes in a version string. // This change is primarily for Fedora, RHEL and CentOS version numbers which // can contain underscores. By replacing them with dashes, a valid semantic @@ -157,42 +101,6 @@ func fixKernelVersion(version string) string { return strings.Replace(version, "+", "", -1) } -// handleDistroName checks that the current distro is compatible with -// the constraint specified by the arguments. -func (tc *TestConstraint) handleDistroName(name string, op Operator) (result Result, err error) { - if name == "" { - return Result{}, fmt.Errorf("distro name cannot be blank") - } - - name = strings.ToLower(name) - - var success bool - - switch op { - case eqOperator: - success = name == tc.DistroName - case neOperator: - success = name != tc.DistroName - default: - return Result{}, errInvalidOpForConstraint - } - - descr := fmt.Sprintf("need distro %s %q, got distro %q", op, name, tc.DistroName) - - result = Result{ - Description: descr, - Success: success, - } - - return result, nil -} - -// handleDistroVersion checks that the current distro version is compatible with -// the constraint specified by the arguments. -func (tc *TestConstraint) handleDistroVersion(version string, op Operator) (result Result, err error) { - return handleVersionType("distro", tc.DistroVersion, op, version) -} - // handleKernelVersion checks that the current kernel version is compatible with // the constraint specified by the arguments. func (tc *TestConstraint) handleKernelVersion(version string, op Operator) (result Result, err error) { @@ -459,23 +367,6 @@ func (tc *TestConstraint) constraintValid(fn Constraint) bool { } } - if c.DistroName != "" { - result, err := tc.handleDistroName(c.DistroName, c.Operator) - tc.handleResults(result, err) - if !result.Success { - return false - } - } - - if c.DistroVersion != "" { - result, err := tc.handleDistroVersion(c.DistroVersion, c.Operator) - tc.handleResults(result, err) - if !result.Success { - return false - } - - } - if c.KernelVersion != "" { result, err := tc.handleKernelVersion(c.KernelVersion, c.Operator) tc.handleResults(result, err) diff --git a/src/runtime/pkg/katatestutils/constraints_api.go b/src/runtime/pkg/katatestutils/constraints_api.go index 1bae25541b..ce27ee424a 100644 --- a/src/runtime/pkg/katatestutils/constraints_api.go +++ b/src/runtime/pkg/katatestutils/constraints_api.go @@ -9,7 +9,6 @@ package katatestutils import ( "os" - "strings" ) // Operator represents an operator to apply to a test constraint value. @@ -28,13 +27,6 @@ const ( type Constraints struct { Issue string - // DistroName is the name of a distro in all lower-case letters. - DistroName string - - // DistroVersion is the version of the particular distro in string - // format. It may contain periods and dashes. - DistroVersion string - // KernelVersion is the version of a particular kernel. KernelVersion string @@ -54,8 +46,6 @@ type Constraint func(c *Constraints) // TestConstraint records details about test constraints. type TestConstraint struct { - DistroName string - DistroVersion string KernelVersion string // Optionally used to record an issue number that relates to the @@ -76,11 +66,6 @@ type TestConstraint struct { // NewKataTest creates a new TestConstraint object and is the main interface // to the test constraints feature. func NewTestConstraint(debug bool) TestConstraint { - distroName, distroVersion, err := getDistroDetails() - if err != nil { - panic(err) - } - kernelVersion, err := getKernelVersion() if err != nil { panic(err) @@ -90,8 +75,6 @@ func NewTestConstraint(debug bool) TestConstraint { Debug: debug, ActualEUID: os.Geteuid(), - DistroName: distroName, - DistroVersion: distroVersion, KernelVersion: kernelVersion, } } @@ -102,7 +85,7 @@ func NewTestConstraint(debug bool) TestConstraint { // Notes: // // - Constraints are applied in the order specified. -// - A constraint type (user, distro) can only be specified once. +// - A constraint type (user, kernel) can only be specified once. // - If the function fails to determine whether it can check the constraints, // it will panic. Since this is facility is used for testing, this seems like // the best approach as it unburdens the caller from checking for an error @@ -146,99 +129,7 @@ func NeedNonRoot() Constraint { return NeedUID(0, neOperator) } -// NeedDistroWithOp skips the test unless the distro constraint specified by -// the arguments is true. -func NeedDistroWithOp(distro string, op Operator) Constraint { - return func(c *Constraints) { - c.DistroName = strings.ToLower(distro) - c.Operator = op - } -} - -// NeedDistroEquals will skip the test unless running on the specified distro. -func NeedDistroEquals(distro string) Constraint { - return NeedDistroWithOp(distro, eqOperator) -} - -// NeedDistroNotEquals will skip the test unless run a distro that does not -// match the specified name. -func NeedDistroNotEquals(distro string) Constraint { - return NeedDistroWithOp(distro, neOperator) -} - -// NeedDistro will skip the test unless running on the specified distro. -func NeedDistro(distro string) Constraint { - return NeedDistroEquals(distro) -} - -// NeedDistroVersionWithOp skips the test unless the distro version constraint -// specified by the arguments is true. -// -// Note: distro versions vary in format. -func NeedDistroVersionWithOp(version string, op Operator) Constraint { - return func(c *Constraints) { - c.DistroVersion = version - c.Operator = op - } -} - -// NeedDistroVersionEquals will skip the test unless the distro version is the -// same as the specified version. -// -// Note: distro versions vary in format. -func NeedDistroVersionEquals(version string) Constraint { - return NeedDistroVersionWithOp(version, eqOperator) -} - -// NeedDistroVersionNotEquals will skip the test unless the distro version is -// different to the specified version. -// -// Note: distro versions vary in format. -func NeedDistroVersionNotEquals(version string) Constraint { - return NeedDistroVersionWithOp(version, neOperator) -} - -// NeedDistroVersionLE will skip the test unless the distro version is older -// than or the same as the specified version. -// -// Note: distro versions vary in format. -func NeedDistroVersionLE(version string) Constraint { - return NeedDistroVersionWithOp(version, leOperator) -} - -// NeedDistroVersionLT will skip the test unless the distro version is older -// than the specified version. -// -// Note: distro versions vary in format. -func NeedDistroVersionLT(version string) Constraint { - return NeedDistroVersionWithOp(version, ltOperator) -} - -// NeedDistroVersionGE will skip the test unless the distro version is newer -// than or the same as the specified version. -// -// Note: distro versions vary in format. -func NeedDistroVersionGE(version string) Constraint { - return NeedDistroVersionWithOp(version, geOperator) -} - -// NeedDistroVersionGT will skip the test unless the distro version is newer -// than the specified version. -// -// Note: distro versions vary in format. -func NeedDistroVersionGT(version string) Constraint { - return NeedDistroVersionWithOp(version, gtOperator) -} - -// NeedDistroVersion will skip the test unless running on the specified -// (exact) version of some distro. -// -// Note: distro versions vary in format. -func NeedDistroVersion(version string) Constraint { - return NeedDistroVersionEquals(version) -} - -// NeedKernelVersionWithOp skips the test unless the distro version constraint +// NeedKernelVersionWithOp skips the test unless the kernel version constraint // specified by the arguments is true. func NeedKernelVersionWithOp(version string, op Operator) Constraint { return func(c *Constraints) { @@ -247,54 +138,44 @@ func NeedKernelVersionWithOp(version string, op Operator) Constraint { } } -// NeedKernelVersionLT will skip the test unless the distro version is older -// than the specified version. +// NeedKernelVersionEquals will skip the test unless the kernel version is same as +// the specified version. func NeedKernelVersionEquals(version string) Constraint { return NeedKernelVersionWithOp(version, eqOperator) } -// NeedKernelVersionNotEquals will skip the test unless the distro version is +// NeedKernelVersionNotEquals will skip the test unless the kernel version is // different to the specified version. func NeedKernelVersionNotEquals(version string) Constraint { return NeedKernelVersionWithOp(version, neOperator) } -// NeedKernelVersionLT will skip the test unless the distro version is older +// NeedKernelVersionLT will skip the test unless the kernel version is older // than the specified version. -// -// Note: distro versions vary in format. func NeedKernelVersionLT(version string) Constraint { return NeedKernelVersionWithOp(version, ltOperator) } -// NeedKernelVersionLE will skip the test unless the distro version is older +// NeedKernelVersionLE will skip the test unless the kernel version is older // than or the same as the specified version. -// -// Note: distro versions vary in format. func NeedKernelVersionLE(version string) Constraint { return NeedKernelVersionWithOp(version, leOperator) } -// NeedKernelVersionGT will skip the test unless the distro version is newer +// NeedKernelVersionGT will skip the test unless the kernel version is newer // than the specified version. -// -// Note: distro versions vary in format. func NeedKernelVersionGT(version string) Constraint { return NeedKernelVersionWithOp(version, gtOperator) } -// NeedKernelVersionGE will skip the test unless the distro version is newer +// NeedKernelVersionGE will skip the test unless the kernel version is newer // than or the same as the specified version. -// -// Note: distro versions vary in format. func NeedKernelVersionGE(version string) Constraint { return NeedKernelVersionWithOp(version, geOperator) } -// NeedKernelVersion will skip the test unless running on the specified -// (exact) version of some distro. -// -// Note: distro versions vary in format. +// NeedKernelVersion will skip the test unless the kernel version is same as +// the specified version. func NeedKernelVersion(version string) Constraint { return NeedKernelVersionEquals(version) } diff --git a/src/runtime/pkg/katatestutils/constraints_test.go b/src/runtime/pkg/katatestutils/constraints_test.go index 314d017c1e..ec7d43ecd1 100644 --- a/src/runtime/pkg/katatestutils/constraints_test.go +++ b/src/runtime/pkg/katatestutils/constraints_test.go @@ -9,7 +9,6 @@ import ( "errors" "fmt" "os" - "os/exec" "path/filepath" "strconv" "strings" @@ -21,8 +20,6 @@ import ( const ( invalidOperator = 1234 - - skipUnknownDistroName = "skipping test as cannot determine distro name" ) // nolint: govet @@ -32,36 +29,16 @@ type testDataUID struct { c Constraints } -// nolint: govet -type testDataDistro struct { - distro string - op Operator - c Constraints -} +var ( + thisUID = os.Getuid() + rootUID = 0 +) -var distros = []string{ - "centos", - "clear-linux-os", - "debian", - "fedora", - "opensuse", - "rhel", - "sles", - "ubuntu", -} - -var thisUID = os.Getuid() -var rootUID = 0 - -// name and version of current distro and kernel version of system tests are +// name and version of current kernel version of system tests are // running on -var distroName string -var distroVersion string var kernelVersion string -// error saved when attempting to determine distro name+version and kernel -// version. -var getDistroErr error +// error saved when attempting to determine kernel version. var getKernelErr error // true if running as root @@ -87,49 +64,8 @@ var uidNotEqualsRootData = testDataUID{ }, } -var distroEqualsCurrentData testDataDistro -var distroNotEqualsCurrentData testDataDistro - func init() { - distroName, distroVersion, getDistroErr = testGetDistro() kernelVersion, getKernelErr = testGetKernelVersion() - - distroEqualsCurrentData = testDataDistro{ - distro: distroName, - op: eqOperator, - c: Constraints{ - DistroName: distroName, - Operator: eqOperator, - }, - } - - distroNotEqualsCurrentData = testDataDistro{ - distro: distroName, - op: neOperator, - c: Constraints{ - DistroName: distroName, - Operator: neOperator, - }, - } -} - -func fileExists(path string) bool { - if _, err := os.Stat(path); os.IsNotExist(err) { - return false - } - - return true -} - -// getAnotherDistro returns a distro name not equal to the one specified. -func getAnotherDistro(distro string) string { - for _, d := range distros { - if d != distro { - return d - } - } - - panic(fmt.Sprintf("failed to find a distro different to %s", distro)) } func checkUIDConstraints(assert *assert.Assertions, a, b Constraints, desc string) { @@ -140,13 +76,6 @@ func checkUIDConstraints(assert *assert.Assertions, a, b Constraints, desc strin assert.Equal(a.UIDSet, b.UIDSet, msg) } -func checkDistroConstraints(assert *assert.Assertions, a, b Constraints, desc string) { - msg := fmt.Sprintf("%s: a: %+v, b: %+v", desc, a, b) - - assert.Equal(a.DistroName, b.DistroName, msg) - assert.Equal(a.Operator, b.Operator, msg) -} - func checkKernelConstraint(assert *assert.Assertions, f Constraint, version string, op Operator, msg string) { c := Constraints{} @@ -156,19 +85,6 @@ func checkKernelConstraint(assert *assert.Assertions, f Constraint, version stri assert.Equal(c.Operator, op, msg) } -// runCommand runs a command and returns its output -func runCommand(args ...string) ([]string, error) { - cmd := exec.Command(args[0], args[1:]...) - bytes, err := cmd.Output() - if err != nil { - return []string{}, err - } - - output := strings.Split(string(bytes), "\n") - - return output, nil -} - // semverBumpVersion takes an existing semantic version and increments one or // more parts of it, returning the new version number as a string. func semverBumpVersion(ver semver.Version, bumpMajor, bumpMinor, bumpPatch bool) (string, error) { @@ -263,56 +179,6 @@ func decrementVersion(version string) (string, error) { return changeVersion(version, true) } -// testGetDistro is an alternative implementation of getDistroDetails() used -// for testing. -func testGetDistro() (name, version string, err error) { - files := []string{"/etc/os-release", "/usr/lib/os-release"} - - for _, file := range files { - if !fileExists(file) { - continue - } - - output, err := runCommand("grep", "^ID=", file) - if err != nil { - return "", "", err - } - - line := output[0] - fields := strings.Split(line, "=") - if name == "" { - name = strings.Trim(fields[1], `"`) - name = strings.ToLower(name) - } - - output, err = runCommand("grep", "^VERSION_ID=", file) - if err != nil { - return "", "", err - } - - line = output[0] - fields = strings.Split(line, "=") - if version == "" { - version = strings.Trim(fields[1], `"`) - version = strings.ToLower(version) - } - } - - if name != "" && version != "" { - return name, version, nil - } - - if name == "" { - return "", "", errUnknownDistroName - } - - if version == "" { - return "", "", errUnknownDistroVersion - } - - return "", "", errors.New("BUG: something bad happened") -} - func testGetKernelVersion() (version string, err error) { const file = "/proc/version" @@ -360,11 +226,6 @@ func TestOperatorString(t *testing.T) { } func TestNewTestConstraint(t *testing.T) { - if getDistroErr != nil { - t.Skipf("skipping as unable to determine distro name/version: %v", - getDistroErr) - } - if getKernelErr != nil { t.Skipf("skipping as unable to determine kernel version: %v", getKernelErr) @@ -379,17 +240,11 @@ func TestNewTestConstraint(t *testing.T) { assert.Equal(debug, c.Debug, msg) - assert.Equal(distroName, c.DistroName, msg) - assert.Equal(distroVersion, c.DistroVersion, msg) assert.Equal(kernelVersion, c.KernelVersion, msg) assert.Equal(thisUID, c.ActualEUID) toCheck := []string{ - distroName, - distroVersion, kernelVersion, - c.DistroName, - c.DistroVersion, c.KernelVersion, } @@ -438,26 +293,6 @@ func TestGetFileContents(t *testing.T) { } } -func TestGetDistroDetails(t *testing.T) { - assert := assert.New(t) - - if getDistroErr == errUnknownDistroName { - t.Skip(skipUnknownDistroName) - } - - assert.NoError(getDistroErr) - assert.NotNil(distroName) - assert.NotNil(distroVersion) - - name, version, err := getDistroDetails() - assert.NoError(err) - assert.NotNil(name) - assert.NotNil(version) - - assert.Equal(name, distroName) - assert.Equal(version, distroVersion) -} - func TestGetKernelVersion(t *testing.T) { assert := assert.New(t) @@ -471,158 +306,6 @@ func TestGetKernelVersion(t *testing.T) { assert.Equal(version, kernelVersion) } -func TestConstraintHandleDistroName(t *testing.T) { - assert := assert.New(t) - - // nolint: govet - type testData struct { - distro string - op Operator - result Result - expectError bool - } - - distroName, _, err := testGetDistro() - if err != nil && err == errUnknownDistroName { - t.Skip(skipUnknownDistroName) - } - - // Look for the first distro that is not the same as the distro this - // test is currently running on. - differentDistro := getAnotherDistro(distroName) - - data := []testData{ - {"", eqOperator, Result{}, true}, - {"", neOperator, Result{}, true}, - {"", invalidOperator, Result{}, true}, - {distroName, invalidOperator, Result{}, true}, - {distroName, invalidOperator, Result{}, true}, - - { - distroName, - eqOperator, - Result{ - Description: distroName, - Success: true, - }, - false, - }, - { - distroName, - neOperator, - Result{ - Description: distroName, - Success: false, - }, - false, - }, - { - differentDistro, - eqOperator, - Result{ - Description: differentDistro, - Success: false, - }, - false, - }, - - { - differentDistro, - neOperator, - Result{ - Description: differentDistro, - Success: true, - }, - false, - }, - } - - for _, debug := range []bool{true, false} { - tc := NewTestConstraint(debug) - - for i, d := range data { - result, err := tc.handleDistroName(d.distro, d.op) - - msg := fmt.Sprintf("test[%d]: %+v, result: %+v", i, d, result) - - if d.expectError { - assert.Error(err, msg) - continue - - } - - assert.NoError(err, msg) - assert.Equal(result.Success, d.result.Success, msg) - assert.NotNil(result.Description, msg) - } - } -} - -func TestConstraintHandleDistroVersion(t *testing.T) { - assert := assert.New(t) - - assert.NotNil(distroVersion) - - // Generate a new distro version for testing purposes. Since we don't - // know the format of this particular distros versioning scheme, we - // need to calculate it. - higherVersion, err := incrementVersion(distroVersion) - assert.NoError(err) - assert.NotEqual(distroVersion, higherVersion) - - // nolint: govet - type testData struct { - version string - op Operator - result Result - expectError bool - } - - data := []testData{ - {"", eqOperator, Result{}, true}, - {"", geOperator, Result{}, true}, - {"", gtOperator, Result{}, true}, - {"", leOperator, Result{}, true}, - {"", ltOperator, Result{}, true}, - {"", neOperator, Result{}, true}, - - {distroVersion, eqOperator, Result{Success: true}, false}, - {higherVersion, eqOperator, Result{Success: false}, false}, - - {distroVersion, gtOperator, Result{Success: false}, false}, - {higherVersion, gtOperator, Result{Success: false}, false}, - - {distroVersion, geOperator, Result{Success: true}, false}, - {higherVersion, geOperator, Result{Success: false}, false}, - - {distroVersion, ltOperator, Result{Success: false}, false}, - {higherVersion, ltOperator, Result{Success: true}, false}, - - {distroVersion, leOperator, Result{Success: true}, false}, - {higherVersion, leOperator, Result{Success: true}, false}, - - {distroVersion, neOperator, Result{Success: false}, false}, - {higherVersion, neOperator, Result{Success: true}, false}, - } - - for _, debug := range []bool{true, false} { - tc := NewTestConstraint(debug) - - for i, d := range data { - result, err := tc.handleDistroVersion(d.version, d.op) - - msg := fmt.Sprintf("test[%d]: %+v, result: %+v", i, d, result) - - if d.expectError { - assert.Error(err, msg) - continue - } - - assert.Equal(d.result.Success, result.Success, msg) - } - } -} - func TestConstraintHandleVersionType(t *testing.T) { assert := assert.New(t) @@ -638,7 +321,6 @@ func TestConstraintHandleVersionType(t *testing.T) { data := []testData{ //---------- - {"", "", eqOperator, "", Result{}, true}, {"name", "foo", eqOperator, "", Result{}, true}, @@ -960,10 +642,12 @@ func TestNeedUID(t *testing.T) { data := []testDataUID{ uidEqualsRootData, uidNotEqualsRootData, - {thisUID, eqOperator, Constraints{ - Operator: eqOperator, - UID: thisUID, - UIDSet: true}, + { + thisUID, eqOperator, Constraints{ + Operator: eqOperator, + UID: thisUID, + UIDSet: true, + }, }, } @@ -1000,62 +684,6 @@ func TestNeedNonRoot(t *testing.T) { checkUIDConstraints(assert, c, uidNotEqualsRootData.c, "TestNeedNonRoot") } -func TestNeedDistroWithOp(t *testing.T) { - assert := assert.New(t) - - if getDistroErr == errUnknownDistroName { - t.Skip(skipUnknownDistroName) - } - - data := []testDataDistro{ - distroEqualsCurrentData, - distroNotEqualsCurrentData, - - // check name provided is lower-cased - { - strings.ToUpper(distroName), - eqOperator, - Constraints{ - DistroName: distroName, - Operator: eqOperator, - }, - }, - } - - for i, d := range data { - - c := Constraints{} - - f := NeedDistroWithOp(d.distro, d.op) - f(&c) - - desc := fmt.Sprintf("test[%d]: %+v, constraints: %+v", i, d, c) - checkDistroConstraints(assert, d.c, c, desc) - } -} - -func TestNeedDistroEquals(t *testing.T) { - assert := assert.New(t) - - c := Constraints{} - - f := NeedDistroEquals(distroName) - f(&c) - - checkDistroConstraints(assert, c, distroEqualsCurrentData.c, "TestNeedDistroEquals") -} - -func TestNeedDistroNotEquals(t *testing.T) { - assert := assert.New(t) - - c := Constraints{} - - f := NeedDistroNotEquals(distroName) - f(&c) - - checkDistroConstraints(assert, c, distroNotEqualsCurrentData.c, "TestNeedDistroNotEquals") -} - func TestWithIssue(t *testing.T) { assert := assert.New(t) @@ -1171,22 +799,7 @@ func TestConstraintNotValid(t *testing.T) { assert.False(result) } - // Now test specification of multiple constraints - if root { - result := tc.NotValid(NeedRoot(), NeedDistro(distroName)) - assert.False(result) - - result = tc.NotValid(NeedNonRoot(), NeedDistro(distroName)) - assert.True(result) - } else { - result := tc.NotValid(NeedRoot(), NeedDistro(distroName)) - assert.True(result) - - result = tc.NotValid(NeedNonRoot(), NeedDistro(distroName)) - assert.False(result) - } } - } func TestConstraintNotValidKernelVersion(t *testing.T) { @@ -1278,62 +891,6 @@ func TestConstraintNotValidKernelVersion(t *testing.T) { } } -func TestConstraintNotValidDistroVersion(t *testing.T) { - assert := assert.New(t) - - assert.NotNil(distroVersion) - - // Generate new distro versions for testing purposes based on the - // current kernel version. - higherVersion, err := incrementVersion(distroVersion) - assert.NoError(err) - assert.NotEqual(distroVersion, higherVersion) - - lowerVersion, err := decrementVersion(distroVersion) - assert.NoError(err) - assert.NotEqual(distroVersion, lowerVersion) - - for _, debug := range []bool{true, false} { - tc := NewTestConstraint(debug) - - result := tc.NotValid(NeedDistroVersionEquals(higherVersion)) - assert.True(result) - - result = tc.NotValid(NeedDistroVersionEquals(distroVersion)) - assert.False(result) - - result = tc.NotValid(NeedDistroVersionLE(higherVersion)) - assert.False(result) - - result = tc.NotValid(NeedDistroVersionLE(distroVersion)) - assert.False(result) - - result = tc.NotValid(NeedDistroVersionLT(higherVersion)) - assert.False(result) - - result = tc.NotValid(NeedDistroVersionLT(distroVersion)) - assert.True(result) - - result = tc.NotValid(NeedDistroVersionGE(higherVersion)) - assert.True(result) - - result = tc.NotValid(NeedDistroVersionGE(distroVersion)) - assert.False(result) - - result = tc.NotValid(NeedDistroVersionGT(higherVersion)) - assert.True(result) - - result = tc.NotValid(NeedDistroVersionGT(distroVersion)) - assert.True(result) - - result = tc.NotValid(NeedDistroVersionNotEquals(higherVersion)) - assert.False(result) - - result = tc.NotValid(NeedDistroVersionNotEquals(distroVersion)) - assert.True(result) - } -} - func TestConstraintConstraintValid(t *testing.T) { assert := assert.New(t) @@ -1352,100 +909,6 @@ func TestConstraintConstraintValid(t *testing.T) { true, TestConstraint{Issue: issue}, }, - - { - NeedDistroWithOp(distroName, eqOperator), - true, - TestConstraint{ - Passed: []Result{ - {Success: true}, - }, - }, - }, - { - NeedDistroWithOp(distroName, neOperator), - false, - TestConstraint{ - Failed: []Result{ - {Success: false}, - }, - }, - }, - { - NeedDistroWithOp(getAnotherDistro(distroName), eqOperator), - false, - TestConstraint{ - Failed: []Result{ - {Success: false}, - }, - }, - }, - { - NeedDistroWithOp(getAnotherDistro(distroName), neOperator), - true, - TestConstraint{ - Failed: []Result{ - {Success: true}, - }, - }, - }, - - { - NeedDistroEquals(distroName), - true, - TestConstraint{ - Passed: []Result{ - {Success: true}, - }, - }, - }, - { - NeedDistroEquals(getAnotherDistro(distroName)), - false, - TestConstraint{ - Failed: []Result{ - {Success: false}, - }, - }, - }, - - { - NeedDistroNotEquals(getAnotherDistro(distroName)), - true, - TestConstraint{ - Passed: []Result{ - {Success: true}, - }, - }, - }, - { - NeedDistroNotEquals(distroName), - false, - TestConstraint{ - Failed: []Result{ - {Success: false}, - }, - }, - }, - - { - NeedDistro(distroName), - true, - TestConstraint{ - Passed: []Result{ - {Success: true}, - }, - }, - }, - { - NeedDistro(getAnotherDistro(distroName)), - false, - TestConstraint{ - Failed: []Result{ - {Success: false}, - }, - }, - }, } if root { @@ -1541,7 +1004,6 @@ func TestEvalIntVersion(t *testing.T) { data := []testData{ //---------- - {"", eqOperator, "", false, true}, {"", eqOperator, "1", false, true}, {"1", eqOperator, "", false, true}, @@ -1647,7 +1109,6 @@ func TestEvalFloatVersion(t *testing.T) { data := []testData{ //---------- - {"", eqOperator, "", false, true}, {"foo", eqOperator, "", false, true}, {"", eqOperator, "foo", false, true}, @@ -1763,7 +1224,6 @@ func TestEvalSemverVersion(t *testing.T) { data := []testData{ //---------- - {"", eqOperator, "", false, true}, {"foo", eqOperator, "", false, true}, {"", eqOperator, "foo", false, true},