|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package mounter |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestLockUnlock(t *testing.T) { |
| 28 | + key := "resource name" |
| 29 | + |
| 30 | + unlock := lock(key) |
| 31 | + defer unlock() |
| 32 | + |
| 33 | + _, loaded := mutexes.Load(key) |
| 34 | + assert.True(t, loaded) |
| 35 | +} |
| 36 | + |
| 37 | +func TestLockLockedResource(t *testing.T) { |
| 38 | + locked := true |
| 39 | + unlock := lock("a") |
| 40 | + go func() { |
| 41 | + time.Sleep(500 * time.Microsecond) |
| 42 | + locked = false |
| 43 | + unlock() |
| 44 | + }() |
| 45 | + |
| 46 | + // try to lock already locked resource |
| 47 | + unlock2 := lock("a") |
| 48 | + defer unlock2() |
| 49 | + if locked { |
| 50 | + assert.Fail(t, "access to locked resource") |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func TestLockDifferentKeys(t *testing.T) { |
| 55 | + unlocka := lock("a") |
| 56 | + unlockb := lock("b") |
| 57 | + unlocka() |
| 58 | + unlockb() |
| 59 | +} |
| 60 | + |
| 61 | +func TestGetRootMappingPath(t *testing.T) { |
| 62 | + testCases := []struct { |
| 63 | + remote string |
| 64 | + expectResult string |
| 65 | + expectError bool |
| 66 | + }{ |
| 67 | + { |
| 68 | + remote: "", |
| 69 | + expectResult: "", |
| 70 | + expectError: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + remote: "hostname", |
| 74 | + expectResult: "", |
| 75 | + expectError: true, |
| 76 | + }, |
| 77 | + { |
| 78 | + remote: "\\\\hostname\\path", |
| 79 | + expectResult: "\\\\hostname\\path", |
| 80 | + expectError: false, |
| 81 | + }, |
| 82 | + { |
| 83 | + remote: "\\\\hostname\\path\\", |
| 84 | + expectResult: "\\\\hostname\\path", |
| 85 | + expectError: false, |
| 86 | + }, |
| 87 | + { |
| 88 | + remote: "\\\\hostname\\path\\subpath", |
| 89 | + expectResult: "\\\\hostname\\path", |
| 90 | + expectError: false, |
| 91 | + }, |
| 92 | + } |
| 93 | + for _, tc := range testCases { |
| 94 | + result, err := getRootMappingPath(tc.remote) |
| 95 | + if tc.expectError && err == nil { |
| 96 | + t.Errorf("Expected error but getRootMappingPath returned a nil error") |
| 97 | + } |
| 98 | + if !tc.expectError { |
| 99 | + if err != nil { |
| 100 | + t.Errorf("Expected no errors but getRootMappingPath returned error: %v", err) |
| 101 | + } |
| 102 | + if tc.expectResult != result { |
| 103 | + t.Errorf("Expected (%s) but getRootMappingPath returned (%s)", tc.expectResult, result) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestRemotePathReferencesCounter(t *testing.T) { |
| 110 | + remotePath1 := "\\\\servername\\share\\subpath\\1" |
| 111 | + remotePath2 := "\\\\servername\\share\\subpath\\2" |
| 112 | + mappingPath, err := getRootMappingPath(remotePath1) |
| 113 | + assert.Nil(t, err) |
| 114 | + |
| 115 | + basePath = os.Getenv("TEMP") + "\\TestMappingPathCounter" |
| 116 | + os.RemoveAll(basePath) |
| 117 | + defer func() { |
| 118 | + // cleanup temp folder |
| 119 | + os.RemoveAll(basePath) |
| 120 | + }() |
| 121 | + |
| 122 | + // by default we have no any files in `mappingPath`. So, `count` should be zero |
| 123 | + assert.Zero(t, getRemotePathReferencesCount(mappingPath)) |
| 124 | + // add reference to `remotePath1`. So, `count` should be equal `1` |
| 125 | + assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath1)) |
| 126 | + assert.Equal(t, 1, getRemotePathReferencesCount(mappingPath)) |
| 127 | + // add reference to `remotePath2`. So, `count` should be equal `2` |
| 128 | + assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath2)) |
| 129 | + assert.Equal(t, 2, getRemotePathReferencesCount(mappingPath)) |
| 130 | + // remove reference to `remotePath1`. So, `count` should be equal `1` |
| 131 | + assert.Nil(t, decrementRemotePathReferencesCount(mappingPath, remotePath1)) |
| 132 | + assert.Equal(t, 1, getRemotePathReferencesCount(mappingPath)) |
| 133 | + // remove reference to `remotePath2`. So, `count` should be equal `0` |
| 134 | + assert.Nil(t, decrementRemotePathReferencesCount(mappingPath, remotePath2)) |
| 135 | + assert.Zero(t, getRemotePathReferencesCount(mappingPath)) |
| 136 | +} |
| 137 | + |
| 138 | +func TestIncementRemotePathReferencesCount(t *testing.T) { |
| 139 | + remotePath := "\\\\servername\\share\\subpath" |
| 140 | + mappingPath, err := getRootMappingPath(remotePath) |
| 141 | + assert.Nil(t, err) |
| 142 | + |
| 143 | + basePath = os.Getenv("TEMP") + "\\TestMappingPathCounter" |
| 144 | + os.RemoveAll(basePath) |
| 145 | + defer func() { |
| 146 | + // cleanup temp folder |
| 147 | + os.RemoveAll(basePath) |
| 148 | + }() |
| 149 | + |
| 150 | + assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath)) |
| 151 | + |
| 152 | + mappingPathContainer := basePath + "\\servername\\share" |
| 153 | + if dir, err := os.Stat(mappingPathContainer); os.IsNotExist(err) || !dir.IsDir() { |
| 154 | + t.Error("mapping file container does not exist") |
| 155 | + } |
| 156 | + |
| 157 | + reference := mappingPathContainer + "\\" + getMd5(remotePath) |
| 158 | + if file, err := os.Stat(reference); os.IsNotExist(err) || file.IsDir() { |
| 159 | + t.Error("reference file does not exist") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +func TestDecrementRemotePathReferencesCount(t *testing.T) { |
| 164 | + remotePath := "\\\\servername\\share\\subpath" |
| 165 | + mappingPath, err := getRootMappingPath(remotePath) |
| 166 | + assert.Nil(t, err) |
| 167 | + |
| 168 | + basePath = os.Getenv("TEMP") + "\\TestMappingPathCounter" |
| 169 | + os.RemoveAll(basePath) |
| 170 | + defer func() { |
| 171 | + // cleanup temp folder |
| 172 | + os.RemoveAll(basePath) |
| 173 | + }() |
| 174 | + |
| 175 | + assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath)) |
| 176 | + assert.Nil(t, decrementRemotePathReferencesCount(mappingPath, remotePath)) |
| 177 | + |
| 178 | + mappingPathContainer := basePath + "\\servername\\share" |
| 179 | + if dir, err := os.Stat(mappingPathContainer); os.IsNotExist(err) || !dir.IsDir() { |
| 180 | + t.Error("mapping file container does not exist") |
| 181 | + } |
| 182 | + |
| 183 | + reference := mappingPathContainer + "\\" + getMd5(remotePath) |
| 184 | + if _, err := os.Stat(reference); os.IsExist(err) { |
| 185 | + t.Error("reference file exists") |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func TestMultiplyCallsOfIncementRemotePathReferencesCount(t *testing.T) { |
| 190 | + remotePath := "\\\\servername\\share\\subpath" |
| 191 | + mappingPath, err := getRootMappingPath(remotePath) |
| 192 | + assert.Nil(t, err) |
| 193 | + |
| 194 | + basePath = os.Getenv("TEMP") + "\\TestMappingPathCounter" |
| 195 | + os.RemoveAll(basePath) |
| 196 | + defer func() { |
| 197 | + // cleanup temp folder |
| 198 | + os.RemoveAll(basePath) |
| 199 | + }() |
| 200 | + |
| 201 | + assert.Zero(t, getRemotePathReferencesCount(mappingPath)) |
| 202 | + assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath)) |
| 203 | + // next calls of `incementMappingPathCount` with the same arguments should be ignored |
| 204 | + assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath)) |
| 205 | + assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath)) |
| 206 | + assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath)) |
| 207 | + assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath)) |
| 208 | + assert.Equal(t, 1, getRemotePathReferencesCount(mappingPath)) |
| 209 | +} |
| 210 | + |
| 211 | +func TestMultiplyCallsOfDecrementRemotePathReferencesCount(t *testing.T) { |
| 212 | + remotePath := "\\\\servername\\share\\subpath" |
| 213 | + mappingPath, err := getRootMappingPath(remotePath) |
| 214 | + assert.Nil(t, err) |
| 215 | + |
| 216 | + basePath = os.Getenv("TEMP") + "\\TestMappingPathCounter" |
| 217 | + os.RemoveAll(basePath) |
| 218 | + defer func() { |
| 219 | + // cleanup temp folder |
| 220 | + os.RemoveAll(basePath) |
| 221 | + }() |
| 222 | + |
| 223 | + assert.Zero(t, getRemotePathReferencesCount(mappingPath)) |
| 224 | + assert.Nil(t, incementRemotePathReferencesCount(mappingPath, remotePath)) |
| 225 | + assert.Nil(t, decrementRemotePathReferencesCount(mappingPath, remotePath)) |
| 226 | + assert.NotNil(t, decrementRemotePathReferencesCount(mappingPath, remotePath)) |
| 227 | +} |
0 commit comments