Merge pull request #4891 from Lussebullen/test-coverage

Add tests for increased coverage.
This commit is contained in:
Alano Terblanche 2024-03-21 16:05:27 +01:00 committed by GitHub
commit 4468148f37
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 301 additions and 0 deletions

View File

@ -16,6 +16,16 @@ func TestNamespaceScope(t *testing.T) {
assert.Check(t, is.Equal("foo_bar", scoped)) assert.Check(t, is.Equal("foo_bar", scoped))
} }
func TestNamespaceDescope(t *testing.T) {
descoped := Namespace{name: "foo"}.Descope("foo_bar")
assert.Check(t, is.Equal("bar", descoped))
}
func TestNamespaceName(t *testing.T) {
namespaceName := Namespace{name: "foo"}.Name()
assert.Check(t, is.Equal("foo", namespaceName))
}
func TestAddStackLabel(t *testing.T) { func TestAddStackLabel(t *testing.T) {
labels := map[string]string{ labels := map[string]string{
"something": "labeled", "something": "labeled",

View File

@ -61,6 +61,15 @@ func TestConvertEnvironment(t *testing.T) {
assert.Check(t, is.DeepEqual([]string{"foo=bar", "key=value"}, env)) assert.Check(t, is.DeepEqual([]string{"foo=bar", "key=value"}, env))
} }
func TestConvertEnvironmentWhenNilValueExists(t *testing.T) {
source := map[string]*string{
"key": strPtr("value"),
"keyWithNoValue": nil,
}
env := convertEnvironment(source)
assert.Check(t, is.DeepEqual([]string{"key=value", "keyWithNoValue"}, env))
}
func TestConvertExtraHosts(t *testing.T) { func TestConvertExtraHosts(t *testing.T) {
source := composetypes.HostsList{ source := composetypes.HostsList{
"zulu:127.0.0.2", "zulu:127.0.0.2",

View File

@ -9,6 +9,55 @@ import (
is "gotest.tools/v3/assert/cmp" is "gotest.tools/v3/assert/cmp"
) )
func TestVolumesWithMultipleServiceVolumeConfigs(t *testing.T) {
namespace := NewNamespace("foo")
serviceVolumes := []composetypes.ServiceVolumeConfig{
{
Type: "volume",
Target: "/foo",
},
{
Type: "volume",
Target: "/foo/bar",
},
}
expected := []mount.Mount{
{
Type: "volume",
Target: "/foo",
},
{
Type: "volume",
Target: "/foo/bar",
},
}
mnt, err := Volumes(serviceVolumes, volumes{}, namespace)
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(expected, mnt))
}
func TestVolumesWithMultipleServiceVolumeConfigsWithUndefinedVolumeConfig(t *testing.T) {
namespace := NewNamespace("foo")
serviceVolumes := []composetypes.ServiceVolumeConfig{
{
Type: "volume",
Source: "foo",
Target: "/foo",
},
{
Type: "volume",
Target: "/foo/bar",
},
}
_, err := Volumes(serviceVolumes, volumes{}, namespace)
assert.Error(t, err, "undefined volume \"foo\"")
}
func TestConvertVolumeToMountAnonymousVolume(t *testing.T) { func TestConvertVolumeToMountAnonymousVolume(t *testing.T) {
config := composetypes.ServiceVolumeConfig{ config := composetypes.ServiceVolumeConfig{
Type: "volume", Type: "volume",
@ -104,6 +153,49 @@ func TestConvertVolumeToMountConflictingOptionsTmpfsInBind(t *testing.T) {
assert.Error(t, err, "tmpfs options are incompatible with type bind") assert.Error(t, err, "tmpfs options are incompatible with type bind")
} }
func TestConvertVolumeToMountConflictingOptionsClusterInVolume(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "volume",
Target: "/target",
Cluster: &composetypes.ServiceVolumeCluster{},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "cluster options are incompatible with type volume")
}
func TestConvertVolumeToMountConflictingOptionsClusterInBind(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "bind",
Source: "/foo",
Target: "/target",
Bind: &composetypes.ServiceVolumeBind{
Propagation: "slave",
},
Cluster: &composetypes.ServiceVolumeCluster{},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "cluster options are incompatible with type bind")
}
func TestConvertVolumeToMountConflictingOptionsClusterInTmpfs(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "tmpfs",
Target: "/target",
Tmpfs: &composetypes.ServiceVolumeTmpfs{
Size: 1000,
},
Cluster: &composetypes.ServiceVolumeCluster{},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "cluster options are incompatible with type tmpfs")
}
func TestConvertVolumeToMountConflictingOptionsBindInTmpfs(t *testing.T) { func TestConvertVolumeToMountConflictingOptionsBindInTmpfs(t *testing.T) {
namespace := NewNamespace("foo") namespace := NewNamespace("foo")
@ -132,6 +224,50 @@ func TestConvertVolumeToMountConflictingOptionsVolumeInTmpfs(t *testing.T) {
assert.Error(t, err, "volume options are incompatible with type tmpfs") assert.Error(t, err, "volume options are incompatible with type tmpfs")
} }
func TestHandleNpipeToMountAnonymousNpipe(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "npipe",
Target: "/target",
Volume: &composetypes.ServiceVolumeVolume{
NoCopy: true,
},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "invalid npipe source, source cannot be empty")
}
func TestHandleNpipeToMountConflictingOptionsTmpfsInNpipe(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "npipe",
Source: "/foo",
Target: "/target",
Tmpfs: &composetypes.ServiceVolumeTmpfs{
Size: 1000,
},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "tmpfs options are incompatible with type npipe")
}
func TestHandleNpipeToMountConflictingOptionsVolumeInNpipe(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "npipe",
Source: "/foo",
Target: "/target",
Volume: &composetypes.ServiceVolumeVolume{
NoCopy: true,
},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "volume options are incompatible with type npipe")
}
func TestConvertVolumeToMountNamedVolume(t *testing.T) { func TestConvertVolumeToMountNamedVolume(t *testing.T) {
stackVolumes := volumes{ stackVolumes := volumes{
"normal": composetypes.VolumeConfig{ "normal": composetypes.VolumeConfig{
@ -344,6 +480,27 @@ func TestConvertTmpfsToMountVolumeWithSource(t *testing.T) {
assert.Error(t, err, "invalid tmpfs source, source must be empty") assert.Error(t, err, "invalid tmpfs source, source must be empty")
} }
func TestHandleNpipeToMountBind(t *testing.T) {
namespace := NewNamespace("foo")
expected := mount.Mount{
Type: mount.TypeNamedPipe,
Source: "/bar",
Target: "/foo",
ReadOnly: true,
BindOptions: &mount.BindOptions{Propagation: mount.PropagationShared},
}
config := composetypes.ServiceVolumeConfig{
Type: "npipe",
Source: "/bar",
Target: "/foo",
ReadOnly: true,
Bind: &composetypes.ServiceVolumeBind{Propagation: "shared"},
}
mnt, err := convertVolumeToMount(config, volumes{}, namespace)
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(expected, mnt))
}
func TestConvertVolumeToMountAnonymousNpipe(t *testing.T) { func TestConvertVolumeToMountAnonymousNpipe(t *testing.T) {
config := composetypes.ServiceVolumeConfig{ config := composetypes.ServiceVolumeConfig{
Type: "npipe", Type: "npipe",
@ -427,3 +584,98 @@ func TestConvertVolumeMountClusterGroup(t *testing.T) {
assert.NilError(t, err) assert.NilError(t, err)
assert.Check(t, is.DeepEqual(expected, mnt)) assert.Check(t, is.DeepEqual(expected, mnt))
} }
func TestHandleClusterToMountAnonymousCluster(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "cluster",
Target: "/target",
Volume: &composetypes.ServiceVolumeVolume{
NoCopy: true,
},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "invalid cluster source, source cannot be empty")
}
func TestHandleClusterToMountConflictingOptionsTmpfsInCluster(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "cluster",
Source: "/foo",
Target: "/target",
Tmpfs: &composetypes.ServiceVolumeTmpfs{
Size: 1000,
},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "tmpfs options are incompatible with type cluster")
}
func TestHandleClusterToMountConflictingOptionsVolumeInCluster(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "cluster",
Source: "/foo",
Target: "/target",
Volume: &composetypes.ServiceVolumeVolume{
NoCopy: true,
},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "volume options are incompatible with type cluster")
}
func TestHandleClusterToMountWithUndefinedVolumeConfig(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "cluster",
Source: "foo",
Target: "/srv",
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "undefined volume \"foo\"")
}
func TestHandleClusterToMountWithVolumeConfigName(t *testing.T) {
stackVolumes := volumes{
"foo": composetypes.VolumeConfig{
Name: "bar",
},
}
config := composetypes.ServiceVolumeConfig{
Type: "cluster",
Source: "foo",
Target: "/srv",
}
expected := mount.Mount{
Type: mount.TypeCluster,
Source: "bar",
Target: "/srv",
ClusterOptions: &mount.ClusterOptions{},
}
mnt, err := convertVolumeToMount(config, stackVolumes, NewNamespace("foo"))
assert.NilError(t, err)
assert.Check(t, is.DeepEqual(expected, mnt))
}
func TestHandleClusterToMountBind(t *testing.T) {
namespace := NewNamespace("foo")
config := composetypes.ServiceVolumeConfig{
Type: "cluster",
Source: "/bar",
Target: "/foo",
ReadOnly: true,
Bind: &composetypes.ServiceVolumeBind{Propagation: "shared"},
}
_, err := convertVolumeToMount(config, volumes{}, namespace)
assert.Error(t, err, "bind options are incompatible with type cluster")
}

View File

@ -135,3 +135,18 @@ func TestNetworkOptAdvancedSyntaxInvalid(t *testing.T) {
}) })
} }
} }
func TestNetworkOptStringNetOptString(t *testing.T) {
networkOpt := &NetworkOpt{}
result := networkOpt.String()
assert.Check(t, is.Equal("", result))
if result != "" {
t.Errorf("Expected an empty string, got %s", result)
}
}
func TestNetworkOptTypeNetOptType(t *testing.T) {
networkOpt := &NetworkOpt{}
result := networkOpt.Type()
assert.Check(t, is.Equal("network", result))
}

View File

@ -465,3 +465,18 @@ func TestParseLink(t *testing.T) {
t.Fatalf("Expected error 'bad format for links: link:alias:wrong' but got: %v", err) t.Fatalf("Expected error 'bad format for links: link:alias:wrong' but got: %v", err)
} }
} }
func TestGetAllOrEmptyReturnsNilOrValue(t *testing.T) {
opts := NewListOpts(nil)
assert.Check(t, is.DeepEqual(opts.GetAllOrEmpty(), []string{}))
opts.Set("foo")
assert.Check(t, is.DeepEqual(opts.GetAllOrEmpty(), []string{"foo"}))
}
func TestParseCPUsReturnZeroOnInvalidValues(t *testing.T) {
resValue, _ := ParseCPUs("foo")
var z1 int64 = 0
assert.Equal(t, z1, resValue)
resValue, _ = ParseCPUs("1e-32")
assert.Equal(t, z1, resValue)
}