| 201 | { |
| 202 | public: |
| 203 | TestCSIPlugin( |
| 204 | const Option<string>& _apiVersion, |
| 205 | const string& _endpoint, |
| 206 | const string& _workDir, |
| 207 | const Bytes& _availableCapacity, |
| 208 | const hashmap<string, string>& _createParameters, |
| 209 | const hashmap<string, string>& _volumeMetadata, |
| 210 | const hashmap<string, Bytes>& _volumes, |
| 211 | bool _volumeIdPath) |
| 212 | : apiVersion(_apiVersion), |
| 213 | endpoint(_endpoint), |
| 214 | workDir(_workDir), |
| 215 | availableCapacity(_availableCapacity), |
| 216 | createParameters(_createParameters.begin(), _createParameters.end()), |
| 217 | volumeMetadata(_volumeMetadata.begin(), _volumeMetadata.end()), |
| 218 | volumeIdPath(_volumeIdPath) |
| 219 | { |
| 220 | // Construct the default mount volume capability. |
| 221 | defaultVolumeCapability.mutable_mount(); |
| 222 | defaultVolumeCapability.mutable_access_mode() |
| 223 | ->set_mode(VolumeCapability::AccessMode::SINGLE_NODE_WRITER); |
| 224 | |
| 225 | Bytes usedCapacity(0); |
| 226 | |
| 227 | // Scan for created volumes. |
| 228 | // |
| 229 | // TODO(jieyu): Consider not using CHECKs here. |
| 230 | Try<list<string>> paths = fs::list(path::join(workDir, "*-*")); |
| 231 | foreach (const string& path, CHECK_NOTERROR(paths)) { |
| 232 | Try<VolumeInfo> createdVolume = CHECK_NOTERROR(parseVolumePath(path)); |
| 233 | volumes.put(createdVolume->id, createdVolume.get()); |
| 234 | usedCapacity += createdVolume->capacity; |
| 235 | } |
| 236 | |
| 237 | // Create preprovisioned volumes if they have not existed yet. |
| 238 | foreachpair (const string& name, const Bytes& capacity, _volumes) { |
| 239 | Option<VolumeInfo> found = findVolumeByName(name); |
| 240 | |
| 241 | if (found.isSome()) { |
| 242 | CHECK_EQ(found->capacity, capacity) |
| 243 | << "Expected preprovisioned volume '" << name << "' to be " |
| 244 | << capacity << " but found " << found->capacity << " instead"; |
| 245 | |
| 246 | usedCapacity -= found->capacity; |
| 247 | continue; |
| 248 | } |
| 249 | |
| 250 | |
| 251 | VolumeInfo volumeInfo = |
| 252 | createVolumeInfo(capacity, name, volumeMetadata); |
| 253 | |
| 254 | Try<Nothing> mkdir = os::mkdir(volumeInfo.path); |
| 255 | CHECK_SOME(mkdir) |
| 256 | << "Failed to create directory for preprovisioned volume '" << name |
| 257 | << "': " << mkdir.error(); |
| 258 | |
| 259 | volumes.put(volumeInfo.id, std::move(volumeInfo)); |
| 260 | } |