From 2df29b7cf15379b504ec9892bf117c038a7a0405 Mon Sep 17 00:00:00 2001 From: apostasie Date: Wed, 26 Mar 2025 18:49:04 -0700 Subject: [PATCH] Adding tigron tests Signed-off-by: apostasie --- mod/tigron/require/requirement_test.go | 126 +++++++++++++++++++++++++ mod/tigron/test/config_test.go | 54 +++++++++++ mod/tigron/test/data_test.go | 81 ++++++++++++++++ mod/tigron/test/interfaces.go | 3 +- 4 files changed, 262 insertions(+), 2 deletions(-) create mode 100644 mod/tigron/require/requirement_test.go create mode 100644 mod/tigron/test/config_test.go create mode 100644 mod/tigron/test/data_test.go diff --git a/mod/tigron/require/requirement_test.go b/mod/tigron/require/requirement_test.go new file mode 100644 index 00000000000..aa4e0fec0f2 --- /dev/null +++ b/mod/tigron/require/requirement_test.go @@ -0,0 +1,126 @@ +/* + Copyright The containerd Authors. + + 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 require_test + +import ( + "runtime" + "testing" + + "github.com/containerd/nerdctl/mod/tigron/internal/assertive" + "github.com/containerd/nerdctl/mod/tigron/require" +) + +const ( + darwin = "darwin" + windows = "windows" + linux = "linux" +) + +func TestRequire(t *testing.T) { + t.Parallel() + + var pass bool + + switch runtime.GOOS { + case "windows": + pass, _ = require.Windows.Check(nil, nil) + case "linux": + pass, _ = require.Linux.Check(nil, nil) + case "darwin": + pass, _ = require.Darwin.Check(nil, nil) + default: + pass, _ = require.OS(runtime.GOOS).Check(nil, nil) + } + + assertive.IsEqual(t, pass, true) + + switch runtime.GOARCH { + case "amd64": + pass, _ = require.Amd64.Check(nil, nil) + case "arm64": + pass, _ = require.Arm64.Check(nil, nil) + default: + pass, _ = require.Arch(runtime.GOARCH).Check(nil, nil) + } + + assertive.IsEqual(t, pass, true) +} + +func TestNot(t *testing.T) { + t.Parallel() + + var pass bool + + switch runtime.GOOS { + case windows: + pass, _ = require.Not(require.Linux).Check(nil, nil) + case linux: + pass, _ = require.Not(require.Windows).Check(nil, nil) + case darwin: + pass, _ = require.Not(require.Windows).Check(nil, nil) + default: + pass, _ = require.Not(require.Linux).Check(nil, nil) + } + + assertive.IsEqual(t, pass, true) +} + +func TestAllSuccess(t *testing.T) { + t.Parallel() + + var pass bool + + switch runtime.GOOS { + case windows: + pass, _ = require.All(require.Not(require.Linux), require.Not(require.Darwin)). + Check(nil, nil) + case linux: + pass, _ = require.All(require.Not(require.Windows), require.Not(require.Darwin)). + Check(nil, nil) + case darwin: + pass, _ = require.All(require.Not(require.Windows), require.Not(require.Linux)). + Check(nil, nil) + default: + pass, _ = require.All(require.Not(require.Windows), require.Not(require.Linux), + require.Not(require.Darwin)).Check(nil, nil) + } + + assertive.IsEqual(t, pass, true) +} + +func TestAllOneFail(t *testing.T) { + t.Parallel() + + var pass bool + + switch runtime.GOOS { + case "windows": + pass, _ = require.All(require.Not(require.Linux), require.Not(require.Darwin)). + Check(nil, nil) + case "linux": + pass, _ = require.All(require.Not(require.Windows), require.Not(require.Darwin)). + Check(nil, nil) + case "darwin": + pass, _ = require.All(require.Not(require.Windows), require.Not(require.Linux)). + Check(nil, nil) + default: + pass, _ = require.All(require.Not(require.OS(runtime.GOOS)), require.Not(require.Linux), + require.Not(require.Darwin)).Check(nil, nil) + } + + assertive.IsEqual(t, pass, true) +} diff --git a/mod/tigron/test/config_test.go b/mod/tigron/test/config_test.go new file mode 100644 index 00000000000..6112aee0d09 --- /dev/null +++ b/mod/tigron/test/config_test.go @@ -0,0 +1,54 @@ +/* + Copyright The containerd Authors. + + 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. +*/ + +//nolint:testpackage +package test + +import ( + "testing" + + "github.com/containerd/nerdctl/mod/tigron/internal/assertive" +) + +func TestConfig(t *testing.T) { + t.Parallel() + + // Create + cfg := WithConfig("test", "something") + + assertive.IsEqual(t, string(cfg.Read("test")), "something") + + // Write + cfg.Write("test-write", "else") + + // Overwrite + cfg.Write("test", "one") + + assertive.IsEqual(t, string(cfg.Read("test")), "one") + assertive.IsEqual(t, string(cfg.Read("test-write")), "else") + + assertive.IsEqual(t, string(cfg.Read("doesnotexist")), "") + + // Test adoption + cfg2 := WithConfig("test", "two") + cfg2.Write("adopt", "two") + + //nolint:forcetypeassert + cfg.(*config).adopt(cfg2) + + assertive.IsEqual(t, string(cfg.Read("test")), "one") + assertive.IsEqual(t, string(cfg.Read("adopt")), "two") +} diff --git a/mod/tigron/test/data_test.go b/mod/tigron/test/data_test.go new file mode 100644 index 00000000000..3c9ea730206 --- /dev/null +++ b/mod/tigron/test/data_test.go @@ -0,0 +1,81 @@ +/* + Copyright The containerd Authors. + + 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. +*/ + +//nolint:testpackage +package test + +import ( + "testing" + + "github.com/containerd/nerdctl/mod/tigron/internal/assertive" +) + +func TestDataBasic(t *testing.T) { + t.Parallel() + + dataObj := WithData("test", "create") + + assertive.IsEqual(t, dataObj.Get("test"), "create") + assertive.IsEqual(t, dataObj.Get("doesnotexist"), "") + + dataObj.Set("test", "set") + assertive.IsEqual(t, dataObj.Get("test"), "set") +} + +func TestDataTempDir(t *testing.T) { + t.Parallel() + + dataObj := configureData(t, nil, nil) + + one := dataObj.TempDir() + two := dataObj.TempDir() + + assertive.IsEqual(t, one, two) + assertive.IsNotEqual(t, one, "") +} + +func TestDataIdentifier(t *testing.T) { + t.Parallel() + + dataObj := configureData(t, nil, nil) + + one := dataObj.Identifier() + two := dataObj.Identifier() + + assertive.IsEqual(t, one, two) + assertive.StringHasPrefix(t, one, "testdataidentifier") + + three := dataObj.Identifier("Some Add ∞ Funky∞Prefix") + assertive.StringHasPrefix(t, three, "testdataidentifier-some-add-funky-prefix") +} + +func TestDataIdentifierThatIsReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLong( + t *testing.T, +) { + t.Parallel() + + dataObj := configureData(t, nil, nil) + + one := dataObj.Identifier() + two := dataObj.Identifier() + + assertive.IsEqual(t, one, two) + assertive.StringHasPrefix(t, one, "testdataidentifier") + assertive.IsEqual(t, len(one), identifierMaxLength) + + three := dataObj.Identifier("Add something") + assertive.IsNotEqual(t, three, one) +} diff --git a/mod/tigron/test/interfaces.go b/mod/tigron/test/interfaces.go index 14838039bf3..ef7533e85a0 100644 --- a/mod/tigron/test/interfaces.go +++ b/mod/tigron/test/interfaces.go @@ -26,8 +26,7 @@ import ( // Data is meant to hold information about a test: // - first, any random key value data that the test implementer wants to carry / modify - this is // test data - second, some commonly useful immutable test properties (a way to generate unique -// identifiers for that test, -// temporary directory, etc.) +// identifiers for that test, temporary directory, etc.) // Note that Data is inherited, from parent test to subtest (except for Identifier and TempDir of // course). type Data interface {