Files
nomad/command/volume_register_test.go
Seth Hoenig 4d83733909 tests: swap testify for test in more places (#20028)
* tests: swap testify for test in plugins/csi/client_test.go

* tests: swap testify for test in testutil/

* tests: swap testify for test in host_test.go

* tests: swap testify for test in plugin_test.go

* tests: swap testify for test in utils_test.go

* tests: swap testify for test in scheduler/

* tests: swap testify for test in parse_test.go

* tests: swap testify for test in attribute_test.go

* tests: swap testify for test in plugins/drivers/

* tests: swap testify for test in command/

* tests: fixup some test usages

* go: run go mod tidy

* windows: cpuset test only on linux
2024-02-29 12:11:35 -06:00

206 lines
4.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"testing"
"github.com/hashicorp/hcl"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/ci"
"github.com/shoenig/test/must"
)
func TestVolumeDispatchParse(t *testing.T) {
ci.Parallel(t)
cases := []struct {
hcl string
t string
err string
}{{
hcl: `
type = "foo"
rando = "bar"
`,
t: "foo",
err: "",
}, {
hcl: `{"id": "foo", "type": "foo", "other": "bar"}`,
t: "foo",
err: "",
}}
for _, c := range cases {
t.Run(c.hcl, func(t *testing.T) {
_, s, err := parseVolumeType(c.hcl)
must.Eq(t, c.t, s)
if c.err == "" {
must.NoError(t, err)
} else {
must.ErrorContains(t, err, c.err)
}
})
}
}
func TestCSIVolumeDecode(t *testing.T) {
ci.Parallel(t)
cases := []struct {
name string
hcl string
expected *api.CSIVolume
err string
}{{
name: "volume creation",
hcl: `
id = "testvolume"
namespace = "prod"
name = "test"
type = "csi"
plugin_id = "myplugin"
capacity_min = "10 MiB"
capacity_max = "1G"
snapshot_id = "snap-12345"
mount_options {
fs_type = "ext4"
mount_flags = ["ro"]
}
secrets {
password = "xyzzy"
}
parameters {
skuname = "Premium_LRS"
}
capability {
access_mode = "single-node-writer"
attachment_mode = "file-system"
}
capability {
access_mode = "single-node-reader-only"
attachment_mode = "block-device"
}
topology_request {
preferred {
topology { segments {rack = "R1"} }
}
required {
topology { segments {rack = "R1"} }
topology { segments {rack = "R2", zone = "us-east-1a"} }
}
}
`,
expected: &api.CSIVolume{
ID: "testvolume",
Namespace: "prod",
Name: "test",
PluginID: "myplugin",
SnapshotID: "snap-12345",
RequestedCapacityMin: 10485760,
RequestedCapacityMax: 1000000000,
RequestedCapabilities: []*api.CSIVolumeCapability{
{
AccessMode: api.CSIVolumeAccessModeSingleNodeWriter,
AttachmentMode: api.CSIVolumeAttachmentModeFilesystem,
},
{
AccessMode: api.CSIVolumeAccessModeSingleNodeReader,
AttachmentMode: api.CSIVolumeAttachmentModeBlockDevice,
},
},
MountOptions: &api.CSIMountOptions{
FSType: "ext4",
MountFlags: []string{"ro"},
},
Parameters: map[string]string{"skuname": "Premium_LRS"},
Secrets: map[string]string{"password": "xyzzy"},
RequestedTopologies: &api.CSITopologyRequest{
Required: []*api.CSITopology{
{Segments: map[string]string{"rack": "R1"}},
{Segments: map[string]string{"rack": "R2", "zone": "us-east-1a"}},
},
Preferred: []*api.CSITopology{
{Segments: map[string]string{"rack": "R1"}},
},
},
Topologies: nil, // this is left empty
},
err: "",
}, {
name: "volume registration",
hcl: `
id = "testvolume"
namespace = "prod"
external_id = "vol-12345"
name = "test"
type = "csi"
plugin_id = "myplugin"
capacity_min = "" # meaningless for registration
capability {
access_mode = "single-node-writer"
attachment_mode = "file-system"
}
topology_request {
# make sure we safely handle empty blocks even
# if they're invalid
preferred {
topology {}
topology { segments {} }
}
required {
topology { segments { rack = "R2", zone = "us-east-1a"} }
}
}
`,
expected: &api.CSIVolume{
ID: "testvolume",
Namespace: "prod",
ExternalID: "vol-12345",
Name: "test",
PluginID: "myplugin",
RequestedCapabilities: []*api.CSIVolumeCapability{
{
AccessMode: api.CSIVolumeAccessModeSingleNodeWriter,
AttachmentMode: api.CSIVolumeAttachmentModeFilesystem,
},
},
RequestedTopologies: &api.CSITopologyRequest{
Required: []*api.CSITopology{
{Segments: map[string]string{"rack": "R2", "zone": "us-east-1a"}},
},
Preferred: nil,
},
Topologies: nil,
},
err: "",
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ast, err := hcl.ParseString(c.hcl)
must.NoError(t, err)
vol, err := csiDecodeVolume(ast)
if c.err == "" {
must.NoError(t, err)
} else {
must.ErrorContains(t, err, c.err)
}
must.Eq(t, c.expected, vol)
})
}
}