Files
nomad/command/volume_create_csi_test.go
Allison Larson e16a3339ad Add CSI Volume Sentinel Policy scaffolding (#26438)
* Add ent policy enforcement stubs to CSI Volume create/register

* Wire policy override/warnings through CSI volume register/create

* Add new scope to sentinel apply

* Sanitize CSISecrets & CSIMountOptions

* Add sentinel policy scope to ui

* Update docs for new sentinel scope/policy

* Create new api funcs for CSI endpoints

* fix sentinel csi ui test

* Update sentinel-policy docs

* Add changelog

* Update docs from feedback
2025-08-07 12:03:18 -07:00

63 lines
1.5 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"os"
"testing"
"github.com/hashicorp/cli"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/ci"
"github.com/shoenig/test/must"
)
func TestVolumeCreateCommand_Run(t *testing.T) {
ci.Parallel(t)
srv, client, url := testServer(t, true, nil)
t.Cleanup(srv.Shutdown)
waitForNodes(t, client)
_, err := client.Namespaces().Register(&api.Namespace{Name: "prod"}, nil)
must.NoError(t, err)
ui := cli.NewMockUi()
cmd := &VolumeCreateCommand{
Meta: Meta{Ui: ui},
}
volumeHCL := `
type = "csi"
id = "test-volume"
name = "test-volume"
external_id = "external-test-volume"
plugin_id = "test-plugin"
capacity_min = "1GiB"
capacity_max = "10GiB"
capability {
access_mode = "single-node-reader-only"
attachment_mode = "block-device"
}
`
file, err := os.CreateTemp(t.TempDir(), "csi-volume-test-*.hcl")
must.NoError(t, err)
_, err = file.WriteString(volumeHCL)
must.NoError(t, err)
// Since we can't easily mock the API client to fake a CSI plugin running,
// we'll expect this to fail with a plugin-related error. The flow and
// parsing can still be tested.
args := []string{"-address", url, file.Name()}
code := cmd.Run(args)
must.Eq(t, 1, code)
// Verify error output contains expected message about volume creation
output := ui.ErrorWriter.String()
must.StrContains(t, output, "Error creating volume")
must.StrContains(t, output, "no CSI plugin named: test-plugin could be found")
}