github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/client/allocrunner/taskrunner/volume_hook_test.go (about)

     1  package taskrunner
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/client/allocrunner/interfaces"
     7  	"github.com/hashicorp/nomad/client/pluginmanager/csimanager"
     8  	cstructs "github.com/hashicorp/nomad/client/structs"
     9  	"github.com/hashicorp/nomad/client/taskenv"
    10  	"github.com/hashicorp/nomad/helper/testlog"
    11  	"github.com/hashicorp/nomad/nomad/mock"
    12  	"github.com/hashicorp/nomad/nomad/structs"
    13  	"github.com/hashicorp/nomad/plugins/drivers"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestVolumeHook_PartitionMountsByVolume_Works(t *testing.T) {
    18  	mounts := []*structs.VolumeMount{
    19  		{
    20  			Volume:      "foo",
    21  			Destination: "/tmp",
    22  			ReadOnly:    false,
    23  		},
    24  		{
    25  			Volume:      "foo",
    26  			Destination: "/bar",
    27  			ReadOnly:    false,
    28  		},
    29  		{
    30  			Volume:      "baz",
    31  			Destination: "/baz",
    32  			ReadOnly:    false,
    33  		},
    34  	}
    35  
    36  	expected := map[string][]*structs.VolumeMount{
    37  		"foo": {
    38  			{
    39  				Volume:      "foo",
    40  				Destination: "/tmp",
    41  				ReadOnly:    false,
    42  			},
    43  			{
    44  				Volume:      "foo",
    45  				Destination: "/bar",
    46  				ReadOnly:    false,
    47  			},
    48  		},
    49  		"baz": {
    50  			{
    51  				Volume:      "baz",
    52  				Destination: "/baz",
    53  				ReadOnly:    false,
    54  			},
    55  		},
    56  	}
    57  
    58  	// Test with a real collection
    59  
    60  	partitioned := partitionMountsByVolume(mounts)
    61  	require.Equal(t, expected, partitioned)
    62  
    63  	// Test with nil/emptylist
    64  
    65  	partitioned = partitionMountsByVolume(nil)
    66  	require.Equal(t, map[string][]*structs.VolumeMount{}, partitioned)
    67  }
    68  
    69  func TestVolumeHook_prepareCSIVolumes(t *testing.T) {
    70  	req := &interfaces.TaskPrestartRequest{
    71  		Task: &structs.Task{
    72  			VolumeMounts: []*structs.VolumeMount{
    73  				{
    74  					Volume:      "foo",
    75  					Destination: "/bar",
    76  				},
    77  			},
    78  		},
    79  	}
    80  
    81  	volumes := map[string]*structs.VolumeRequest{
    82  		"foo": {
    83  			Type:   "csi",
    84  			Source: "my-test-volume",
    85  		},
    86  	}
    87  
    88  	tr := &TaskRunner{
    89  		allocHookResources: &cstructs.AllocHookResources{
    90  			CSIMounts: map[string]*csimanager.MountInfo{
    91  				"foo": {
    92  					Source: "/mnt/my-test-volume",
    93  				},
    94  			},
    95  		},
    96  	}
    97  
    98  	expected := []*drivers.MountConfig{
    99  		{
   100  			HostPath: "/mnt/my-test-volume",
   101  			TaskPath: "/bar",
   102  		},
   103  	}
   104  
   105  	hook := &volumeHook{
   106  		logger: testlog.HCLogger(t),
   107  		alloc:  structs.MockAlloc(),
   108  		runner: tr,
   109  	}
   110  	mounts, err := hook.prepareCSIVolumes(req, volumes)
   111  	require.NoError(t, err)
   112  	require.Equal(t, expected, mounts)
   113  }
   114  
   115  func TestVolumeHook_Interpolation(t *testing.T) {
   116  
   117  	alloc := mock.Alloc()
   118  	task := alloc.Job.TaskGroups[0].Tasks[0]
   119  	taskEnv := taskenv.NewBuilder(mock.Node(), alloc, task, "global").SetHookEnv("volume",
   120  		map[string]string{
   121  			"PROPAGATION_MODE": "private",
   122  			"VOLUME_ID":        "my-other-volume",
   123  		},
   124  	).Build()
   125  
   126  	mounts := []*structs.VolumeMount{
   127  		{
   128  			Volume:          "foo",
   129  			Destination:     "/tmp",
   130  			ReadOnly:        false,
   131  			PropagationMode: "bidirectional",
   132  		},
   133  		{
   134  			Volume:          "foo",
   135  			Destination:     "/bar-${NOMAD_JOB_NAME}",
   136  			ReadOnly:        false,
   137  			PropagationMode: "bidirectional",
   138  		},
   139  		{
   140  			Volume:          "${VOLUME_ID}",
   141  			Destination:     "/baz",
   142  			ReadOnly:        false,
   143  			PropagationMode: "bidirectional",
   144  		},
   145  		{
   146  			Volume:          "foo",
   147  			Destination:     "/quux",
   148  			ReadOnly:        false,
   149  			PropagationMode: "${PROPAGATION_MODE}",
   150  		},
   151  	}
   152  
   153  	expected := []*structs.VolumeMount{
   154  		{
   155  			Volume:          "foo",
   156  			Destination:     "/tmp",
   157  			ReadOnly:        false,
   158  			PropagationMode: "bidirectional",
   159  		},
   160  		{
   161  			Volume:          "foo",
   162  			Destination:     "/bar-my-job",
   163  			ReadOnly:        false,
   164  			PropagationMode: "bidirectional",
   165  		},
   166  		{
   167  			Volume:          "my-other-volume",
   168  			Destination:     "/baz",
   169  			ReadOnly:        false,
   170  			PropagationMode: "bidirectional",
   171  		},
   172  		{
   173  			Volume:          "foo",
   174  			Destination:     "/quux",
   175  			ReadOnly:        false,
   176  			PropagationMode: "private",
   177  		},
   178  	}
   179  
   180  	interpolateVolumeMounts(mounts, taskEnv)
   181  	require.Equal(t, expected, mounts)
   182  }