|
| 1 | +/* |
| 2 | + Copyright (c) 2020 Docker Inc. |
| 3 | +
|
| 4 | + Permission is hereby granted, free of charge, to any person |
| 5 | + obtaining a copy of this software and associated documentation |
| 6 | + files (the "Software"), to deal in the Software without |
| 7 | + restriction, including without limitation the rights to use, copy, |
| 8 | + modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | + of the Software, and to permit persons to whom the Software is |
| 10 | + furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | + The above copyright notice and this permission notice shall be |
| 13 | + included in all copies or substantial portions of the Software. |
| 14 | +
|
| 15 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | + EXPRESS OR IMPLIED, |
| 17 | + INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | + IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | + HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 | + DAMAGES OR OTHER LIABILITY, |
| 22 | + WHETHER IN AN ACTION OF CONTRACT, |
| 23 | + TORT OR OTHERWISE, |
| 24 | + ARISING FROM, OUT OF OR IN CONNECTION WITH |
| 25 | + THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | +*/ |
| 27 | + |
| 28 | +package convert |
| 29 | + |
| 30 | +import ( |
| 31 | + "testing" |
| 32 | + |
| 33 | + "github.com/compose-spec/compose-go/types" |
| 34 | + "gotest.tools/assert" |
| 35 | + |
| 36 | + "github.com/docker/api/errdefs" |
| 37 | +) |
| 38 | + |
| 39 | +const ( |
| 40 | + storageAccountNameKey = "storage_account_name" |
| 41 | + storageAccountKeyKey = "storage_account_key" |
| 42 | + shareNameKey = "share_name" |
| 43 | +) |
| 44 | + |
| 45 | +func TestGetRunVolumes(t *testing.T) { |
| 46 | + volumeStrings := []string{ |
| 47 | + "myuser1:mykey1@myshare1/my/path/to/target1", |
| 48 | + "myuser2:mykey2@myshare2/my/path/to/target2", |
| 49 | + "myuser3:mykey3@mydefaultsharename", // Use default placement at '/run/volumes/<share_name>' |
| 50 | + } |
| 51 | + var goldenVolumeConfigs = map[string]types.VolumeConfig{ |
| 52 | + "volume-0": { |
| 53 | + Name: "volume-0", |
| 54 | + Driver: "azure_file", |
| 55 | + DriverOpts: map[string]string{ |
| 56 | + storageAccountNameKey: "myuser1", |
| 57 | + storageAccountKeyKey: "mykey1", |
| 58 | + shareNameKey: "myshare1", |
| 59 | + }, |
| 60 | + }, |
| 61 | + "volume-1": { |
| 62 | + Name: "volume-1", |
| 63 | + Driver: "azure_file", |
| 64 | + DriverOpts: map[string]string{ |
| 65 | + storageAccountNameKey: "myuser2", |
| 66 | + storageAccountKeyKey: "mykey2", |
| 67 | + shareNameKey: "myshare2", |
| 68 | + }, |
| 69 | + }, |
| 70 | + "volume-2": { |
| 71 | + Name: "volume-2", |
| 72 | + Driver: "azure_file", |
| 73 | + DriverOpts: map[string]string{ |
| 74 | + storageAccountNameKey: "myuser3", |
| 75 | + storageAccountKeyKey: "mykey3", |
| 76 | + shareNameKey: "mydefaultsharename", |
| 77 | + }, |
| 78 | + }, |
| 79 | + } |
| 80 | + goldenServiceVolumeConfigs := []types.ServiceVolumeConfig{ |
| 81 | + { |
| 82 | + Type: "azure_file", |
| 83 | + Source: "volume-0", |
| 84 | + Target: "/my/path/to/target1", |
| 85 | + }, |
| 86 | + { |
| 87 | + Type: "azure_file", |
| 88 | + Source: "volume-1", |
| 89 | + Target: "/my/path/to/target2", |
| 90 | + }, |
| 91 | + { |
| 92 | + Type: "azure_file", |
| 93 | + Source: "volume-2", |
| 94 | + Target: "/run/volumes/mydefaultsharename", |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + volumeConfigs, serviceVolumeConfigs, err := GetRunVolumes(volumeStrings) |
| 99 | + assert.NilError(t, err) |
| 100 | + for k, v := range volumeConfigs { |
| 101 | + assert.DeepEqual(t, goldenVolumeConfigs[k], v) |
| 102 | + } |
| 103 | + for i, v := range serviceVolumeConfigs { |
| 104 | + assert.DeepEqual(t, goldenServiceVolumeConfigs[i], v) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestGetRunVolumesMissingFileShare(t *testing.T) { |
| 109 | + _, _, err := GetRunVolumes([]string{"myuser:mykey@"}) |
| 110 | + assert.Equal(t, true, errdefs.IsErrParsingFailed(err)) |
| 111 | + assert.ErrorContains(t, err, "does not include a storage file share") |
| 112 | +} |
| 113 | + |
| 114 | +func TestGetRunVolumesMissingUser(t *testing.T) { |
| 115 | + _, _, err := GetRunVolumes([]string{":mykey@myshare"}) |
| 116 | + assert.Equal(t, true, errdefs.IsErrParsingFailed(err)) |
| 117 | + assert.ErrorContains(t, err, "does not include a storage username") |
| 118 | +} |
| 119 | + |
| 120 | +func TestGetRunVolumesMissingKey(t *testing.T) { |
| 121 | + _, _, err := GetRunVolumes([]string{"userwithnokey:@myshare"}) |
| 122 | + assert.Equal(t, true, errdefs.IsErrParsingFailed(err)) |
| 123 | + assert.ErrorContains(t, err, "does not include a storage key") |
| 124 | + |
| 125 | + _, _, err = GetRunVolumes([]string{"userwithnokeytoo@myshare"}) |
| 126 | + assert.Equal(t, true, errdefs.IsErrParsingFailed(err)) |
| 127 | + assert.ErrorContains(t, err, "does not include a storage key") |
| 128 | +} |
| 129 | + |
| 130 | +func TestGetRunVolumesNoShare(t *testing.T) { |
| 131 | + _, _, err := GetRunVolumes([]string{"noshare"}) |
| 132 | + assert.Equal(t, true, errdefs.IsErrParsingFailed(err)) |
| 133 | + assert.ErrorContains(t, err, "no share specified") |
| 134 | +} |
0 commit comments