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
126 changes: 126 additions & 0 deletions mod/tigron/require/requirement_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
54 changes: 54 additions & 0 deletions mod/tigron/test/config_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
81 changes: 81 additions & 0 deletions mod/tigron/test/data_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 1 addition & 2 deletions mod/tigron/test/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down