github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/daemon/cluster/convert/volume_test.go (about)

     1  package convert
     2  
     3  import (
     4  	"testing"
     5  
     6  	volumetypes "github.com/Prakhar-Agarwal-byte/moby/api/types/volume"
     7  	swarmapi "github.com/moby/swarmkit/v2/api"
     8  
     9  	"gotest.tools/v3/assert"
    10  )
    11  
    12  func TestTopologyFromGRPC(t *testing.T) {
    13  	nilTopology := topologyFromGRPC(nil)
    14  	assert.DeepEqual(t, nilTopology, volumetypes.Topology{})
    15  
    16  	swarmTop := &swarmapi.Topology{
    17  		Segments: map[string]string{"foo": "bar"},
    18  	}
    19  
    20  	top := topologyFromGRPC(swarmTop)
    21  	assert.DeepEqual(t, top.Segments, swarmTop.Segments)
    22  }
    23  
    24  func TestCapacityRangeFromGRPC(t *testing.T) {
    25  	nilCapacity := capacityRangeFromGRPC(nil)
    26  	assert.Assert(t, nilCapacity == nil)
    27  
    28  	swarmZeroCapacity := &swarmapi.CapacityRange{}
    29  	zeroCapacity := capacityRangeFromGRPC(swarmZeroCapacity)
    30  	assert.Assert(t, zeroCapacity != nil)
    31  	assert.Equal(t, zeroCapacity.RequiredBytes, int64(0))
    32  	assert.Equal(t, zeroCapacity.LimitBytes, int64(0))
    33  
    34  	swarmNonZeroCapacity := &swarmapi.CapacityRange{
    35  		RequiredBytes: 1024,
    36  		LimitBytes:    2048,
    37  	}
    38  	nonZeroCapacity := capacityRangeFromGRPC(swarmNonZeroCapacity)
    39  	assert.Assert(t, nonZeroCapacity != nil)
    40  	assert.Equal(t, nonZeroCapacity.RequiredBytes, int64(1024))
    41  	assert.Equal(t, nonZeroCapacity.LimitBytes, int64(2048))
    42  }
    43  
    44  func TestVolumeAvailabilityFromGRPC(t *testing.T) {
    45  	for _, tc := range []struct {
    46  		name     string
    47  		in       swarmapi.VolumeSpec_VolumeAvailability
    48  		expected volumetypes.Availability
    49  	}{
    50  		{
    51  			name:     "Active",
    52  			in:       swarmapi.VolumeAvailabilityActive,
    53  			expected: volumetypes.AvailabilityActive,
    54  		}, {
    55  			name:     "Pause",
    56  			in:       swarmapi.VolumeAvailabilityPause,
    57  			expected: volumetypes.AvailabilityPause,
    58  		}, {
    59  			name:     "Drain",
    60  			in:       swarmapi.VolumeAvailabilityDrain,
    61  			expected: volumetypes.AvailabilityDrain,
    62  		},
    63  	} {
    64  		tc := tc
    65  		t.Run(tc.name, func(t *testing.T) {
    66  			actual := volumeAvailabilityFromGRPC(tc.in)
    67  			assert.Equal(t, actual, tc.expected)
    68  		})
    69  	}
    70  }
    71  
    72  // TestAccessModeFromGRPC tests that the AccessMode type is correctly converted
    73  func TestAccessModeFromGRPC(t *testing.T) {
    74  	for _, tc := range []struct {
    75  		name     string
    76  		in       *swarmapi.VolumeAccessMode
    77  		expected *volumetypes.AccessMode
    78  	}{
    79  		{
    80  			name: "MountVolume",
    81  			in: &swarmapi.VolumeAccessMode{
    82  				Scope:   swarmapi.VolumeScopeSingleNode,
    83  				Sharing: swarmapi.VolumeSharingNone,
    84  				AccessType: &swarmapi.VolumeAccessMode_Mount{
    85  					Mount: &swarmapi.VolumeAccessMode_MountVolume{
    86  						FsType: "foo",
    87  						// TODO(dperny): maybe don't convert this?
    88  						MountFlags: []string{"one", "two"},
    89  					},
    90  				},
    91  			},
    92  			expected: &volumetypes.AccessMode{
    93  				Scope:   volumetypes.ScopeSingleNode,
    94  				Sharing: volumetypes.SharingNone,
    95  				MountVolume: &volumetypes.TypeMount{
    96  					FsType:     "foo",
    97  					MountFlags: []string{"one", "two"},
    98  				},
    99  			},
   100  		}, {
   101  			name: "BlockVolume",
   102  			in: &swarmapi.VolumeAccessMode{
   103  				Scope:   swarmapi.VolumeScopeSingleNode,
   104  				Sharing: swarmapi.VolumeSharingNone,
   105  				AccessType: &swarmapi.VolumeAccessMode_Block{
   106  					Block: &swarmapi.VolumeAccessMode_BlockVolume{},
   107  				},
   108  			},
   109  			expected: &volumetypes.AccessMode{
   110  				Scope:       volumetypes.ScopeSingleNode,
   111  				Sharing:     volumetypes.SharingNone,
   112  				BlockVolume: &volumetypes.TypeBlock{},
   113  			},
   114  		},
   115  	} {
   116  		tc := tc
   117  		t.Run(tc.name, func(t *testing.T) {
   118  			out := accessModeFromGRPC(tc.in)
   119  			assert.DeepEqual(t, tc.expected, out)
   120  		})
   121  	}
   122  }
   123  
   124  // TestVolumeCreateToGRPC tests that a docker-typed VolumeCreateBody is
   125  // correctly converted to a swarm-typed VolumeSpec.
   126  func TestVolumeCreateToGRPC(t *testing.T) {
   127  	volume := &volumetypes.CreateOptions{
   128  		Driver:     "plug1",
   129  		DriverOpts: map[string]string{"options": "yeah"},
   130  		Labels:     map[string]string{"labeled": "yeah"},
   131  		Name:       "volume1",
   132  	}
   133  
   134  	spec := &volumetypes.ClusterVolumeSpec{
   135  		Group: "gronp",
   136  		AccessMode: &volumetypes.AccessMode{
   137  			Scope:   volumetypes.ScopeMultiNode,
   138  			Sharing: volumetypes.SharingAll,
   139  			MountVolume: &volumetypes.TypeMount{
   140  				FsType:     "foo",
   141  				MountFlags: []string{"one", "two"},
   142  			},
   143  		},
   144  		Secrets: []volumetypes.Secret{
   145  			{Key: "key1", Secret: "secret1"},
   146  			{Key: "key2", Secret: "secret2"},
   147  		},
   148  		AccessibilityRequirements: &volumetypes.TopologyRequirement{
   149  			Requisite: []volumetypes.Topology{
   150  				{Segments: map[string]string{"top1": "yup"}},
   151  				{Segments: map[string]string{"top2": "def"}},
   152  				{Segments: map[string]string{"top3": "nah"}},
   153  			},
   154  			Preferred: []volumetypes.Topology{},
   155  		},
   156  		CapacityRange: &volumetypes.CapacityRange{
   157  			RequiredBytes: 1,
   158  			LimitBytes:    0,
   159  		},
   160  	}
   161  
   162  	volume.ClusterVolumeSpec = spec
   163  
   164  	swarmSpec := VolumeCreateToGRPC(volume)
   165  
   166  	assert.Assert(t, swarmSpec != nil)
   167  	expectedSwarmSpec := &swarmapi.VolumeSpec{
   168  		Annotations: swarmapi.Annotations{
   169  			Name: "volume1",
   170  			Labels: map[string]string{
   171  				"labeled": "yeah",
   172  			},
   173  		},
   174  		Group: "gronp",
   175  		Driver: &swarmapi.Driver{
   176  			Name: "plug1",
   177  			Options: map[string]string{
   178  				"options": "yeah",
   179  			},
   180  		},
   181  		AccessMode: &swarmapi.VolumeAccessMode{
   182  			Scope:   swarmapi.VolumeScopeMultiNode,
   183  			Sharing: swarmapi.VolumeSharingAll,
   184  			AccessType: &swarmapi.VolumeAccessMode_Mount{
   185  				Mount: &swarmapi.VolumeAccessMode_MountVolume{
   186  					FsType:     "foo",
   187  					MountFlags: []string{"one", "two"},
   188  				},
   189  			},
   190  		},
   191  		Secrets: []*swarmapi.VolumeSecret{
   192  			{Key: "key1", Secret: "secret1"},
   193  			{Key: "key2", Secret: "secret2"},
   194  		},
   195  		AccessibilityRequirements: &swarmapi.TopologyRequirement{
   196  			Requisite: []*swarmapi.Topology{
   197  				{Segments: map[string]string{"top1": "yup"}},
   198  				{Segments: map[string]string{"top2": "def"}},
   199  				{Segments: map[string]string{"top3": "nah"}},
   200  			},
   201  			Preferred: nil,
   202  		},
   203  		CapacityRange: &swarmapi.CapacityRange{
   204  			RequiredBytes: 1,
   205  			LimitBytes:    0,
   206  		},
   207  	}
   208  
   209  	assert.DeepEqual(t, swarmSpec, expectedSwarmSpec)
   210  }