Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
lgfa29 committed Jul 11, 2023
1 parent 111bc59 commit 824b07f
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 1 deletion.
223 changes: 223 additions & 0 deletions nomad/resource_csi_volume_registration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package nomad

import (
"errors"
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestResourceCSIVolumeRegistration_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
Providers: testProviders,
PreCheck: func() {
testAccPreCheck(t)
testCheckCSIPluginAvailable(t, "hostpath-plugin0")
},
Steps: []resource.TestStep{
{
Config: `
resource "nomad_csi_volume_registration" "test" {
plugin_id = "hostpath-plugin0"
name = "tf-test"
volume_id = "tf-test"
external_id = "67209253-1f73-11ee-abf1-0242ac110002"
capability {
access_mode = "single-node-reader-only"
attachment_mode = "file-system"
}
capability {
access_mode = "single-node-writer"
attachment_mode = "file-system"
}
mount_options {
fs_type = "ext4"
mount_flags = ["ro", "noatime"]
}
secrets = {
secret = "value"
}
parameters = {
param = "param-value"
}
topology_request {
required {
topology {
segments = {
rack = "R1"
"topology.hostpath.csi/node" = "node-0"
}
}
topology {
segments = {
rack = "R2"
}
}
}
}
}
`,
Check: func(s *terraform.State) error {
resourceState := s.Modules[0].Resources["nomad_csi_volume_registration.test"]
if resourceState == nil {
return errors.New("resource not found in state")
}

instanceState := resourceState.Primary
if instanceState == nil {
return errors.New("resource has no primary instance")
}

if instanceState.ID != "tf-test" {
return fmt.Errorf("expected ID to be tf-test, got %s", instanceState.ID)
}

expected := map[string]string{
"namespace": "default",
"volume_id": "tf-test",
"name": "tf-test",
"plugin_id": "hostpath-plugin0",
"external_id": "67209253-1f73-11ee-abf1-0242ac110002",
"capability.#": "2",
"capability.0.access_mode": "single-node-reader-only",
"capability.0.attachment_mode": "file-system",
"capability.1.access_mode": "single-node-writer",
"capability.1.attachment_mode": "file-system",
"mount_options.#": "1",
"mount_options.0.fs_type": "ext4",
"mount_options.0.mount_flags.0": "ro",
"mount_options.0.mount_flags.1": "noatime",
"secrets.%": "1",
"secrets.secret": "value",
"parameters.%": "1",
"parameters.param": "param-value",
"topology_request.#": "1",
"topology_request.0.required.#": "1",
"topology_request.0.required.0.topology.#": "2",
"topology_request.0.required.0.topology.0.segments.rack": "R1",
"topology_request.0.required.0.topology.0.segments.topology.hostpath.csi/node": "node-0",
"topology_request.0.required.0.topology.1.segments.rack": "R2",
}
for k, v := range expected {
got := instanceState.Attributes[k]
if got != v {
return fmt.Errorf("expected %s to be %s, got %s", k, v, got)
}
}

client := testProvider.Meta().(ProviderConfig).client
volume, _, err := client.CSIVolumes().Info(instanceState.ID, nil)
if err != nil {
return fmt.Errorf("failed to read volume %s: %v", instanceState.ID, err)
}

if volume.Name != expected["name"] {
return fmt.Errorf("expected Name to be %s, got: %s", expected["name"], volume.Name)
}
if volume.Namespace != expected["namespace"] {
return fmt.Errorf("expected Namespace to be %s, got: %s", expected["namespace"], volume.Namespace)
}
if volume.PluginID != expected["plugin_id"] {
return fmt.Errorf("expected PluginID to be %s, got: %s", expected["plugin_id"], volume.PluginID)
}

expectedMountOptions := &api.CSIMountOptions{
FSType: "ext4",
// mount flags may contain secrets, so they are not
// returned by the Nomad API, but check if they are at
// least set.
MountFlags: []string{"[REDACTED]"},
}
if diff := cmp.Diff(expectedMountOptions, volume.MountOptions); diff != "" {
t.Errorf("MountOptions mismatch (-want +got):\n%s", diff)
}

expectedCapabilities := []*api.CSIVolumeCapability{
{
AccessMode: api.CSIVolumeAccessModeSingleNodeWriter,
AttachmentMode: api.CSIVolumeAttachmentModeFilesystem,
},
{
AccessMode: api.CSIVolumeAccessModeSingleNodeReader,
AttachmentMode: api.CSIVolumeAttachmentModeFilesystem,
},
}
if diff := cmp.Diff(expectedCapabilities, volume.RequestedCapabilities); diff != "" {
t.Errorf("RequestedCapabilities mismatch (-want +got):\n%s", diff)
}

expectedTopologyRequest := &api.CSITopologyRequest{
Required: []*api.CSITopology{
{
Segments: map[string]string{
"topology.hostpath.csi/node": "node-0",
"rack": "R1",
},
},
{
Segments: map[string]string{
"rack": "R2",
},
},
},
}
if diff := cmp.Diff(expectedTopologyRequest, volume.RequestedTopologies); diff != "" {
t.Errorf("RequestedTopologies mismatch (-want +got):\n%s", diff)
}

expectedTopologies := []*api.CSITopology{
{
Segments: map[string]string{
"rack": "R1",
"topology.hostpath.csi/node": "node-0",
},
},
{
Segments: map[string]string{
"rack": "R2",
},
},
}
if diff := cmp.Diff(expectedTopologies, volume.Topologies); diff != "" {
t.Errorf("Topologies mismatch (-want +got):\n%s", diff)
}

return nil
},
},
},

CheckDestroy: func(s *terraform.State) error {
for _, s := range s.Modules[0].Resources {
if s.Type != "nomad_csi_volume_registration" {
continue
}
if s.Primary == nil {
continue
}
client := testProvider.Meta().(ProviderConfig).client
volume, _, err := client.CSIVolumes().Info(s.Primary.ID, nil)
if err != nil && strings.Contains(err.Error(), "404") || volume == nil {
continue
}
return fmt.Errorf("volume %q has not been deregistered.", volume.ID)
}
return nil
},
})
}
2 changes: 1 addition & 1 deletion nomad/resource_job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2991,7 +2991,7 @@ job "foo-service-with-deployment" {
update {
min_healthy_time = "1s"
healthy_deadline = "2s"
progress_deadline = "3s"
progress_deadline = "10s"
}
task "sleep" {
driver = "raw_exec"
Expand Down

0 comments on commit 824b07f

Please sign in to comment.