|
| 1 | +package com.eficode.devstack.container.impl |
| 2 | + |
| 3 | +import com.eficode.devstack.DevStackSpec |
| 4 | +import com.eficode.devstack.util.DirectorySyncer |
| 5 | +import de.gesellix.docker.remote.api.ContainerInspectResponse |
| 6 | +import de.gesellix.docker.remote.api.MountPoint |
| 7 | +import org.slf4j.LoggerFactory |
| 8 | + |
| 9 | + |
| 10 | +class DirectorySyncerTest extends DevStackSpec { |
| 11 | + |
| 12 | + |
| 13 | + def setupSpec() { |
| 14 | + |
| 15 | + DevStackSpec.log = LoggerFactory.getLogger(this.class) |
| 16 | + |
| 17 | + cleanupContainerNames = ["DirectorySyncer", "DirectorySyncer1", "DirectorySyncer2", "DirectorySyncer-companion", "DirectorySyncer1-companion"] |
| 18 | + cleanupContainerPorts = [] |
| 19 | + |
| 20 | + disableCleanup = false |
| 21 | + |
| 22 | + |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + Boolean volumeExists(String volumeName) { |
| 27 | + |
| 28 | + Boolean result = dockerClient.volumes().content?.volumes?.any { it.name == volumeName } |
| 29 | + return result |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + def "Test createSyncToVolume"() { |
| 34 | + |
| 35 | + setup: |
| 36 | + log.info("Testing createSyncToVolume") |
| 37 | + File srcDir1 = File.createTempDir("srcDir1") |
| 38 | + log.debug("\tCreated Engine local temp dir:" + srcDir1.canonicalPath) |
| 39 | + File srcDir2 = File.createTempDir("srcDir2") |
| 40 | + log.debug("\tCreated Engine local temp dir:" + srcDir2.canonicalPath) |
| 41 | + |
| 42 | + String uniqueVolumeName = "syncVolume" + System.currentTimeMillis().toString().takeRight(3) |
| 43 | + !volumeExists(uniqueVolumeName) ?: dockerClient.rmVolume(uniqueVolumeName) |
| 44 | + log.debug("\tWill use sync to Docker volume:" + uniqueVolumeName) |
| 45 | + |
| 46 | + |
| 47 | + when: "When creating syncer" |
| 48 | + |
| 49 | + assert !volumeExists(uniqueVolumeName): "Destination volume already exists" |
| 50 | + DirectorySyncer syncer = DirectorySyncer.createSyncToVolume([srcDir1.canonicalPath, srcDir2.canonicalPath], uniqueVolumeName, "-avh --delete", dockerRemoteHost, dockerCertPath ) |
| 51 | + log.info("\tCreated sync container: ${syncer.containerName} (${syncer.shortId})") |
| 52 | + ContainerInspectResponse containerInspect = syncer.inspectContainer() |
| 53 | + |
| 54 | + |
| 55 | + then: "I should have the two bind mounts and one volume mount" |
| 56 | + assert syncer.running: "Syncer container is not running" |
| 57 | + log.debug("\tContainer is running") |
| 58 | + assert containerInspect.mounts.any { it.destination == "/mnt/src/${srcDir1.name}".toString() && it.RW == false } |
| 59 | + log.debug("\tContainer has mounted the first src dir") |
| 60 | + assert containerInspect.mounts.any { it.destination == "/mnt/src/${srcDir2.name}".toString() && it.RW == false } |
| 61 | + log.debug("\tContainer has mounted the second src dir") |
| 62 | + assert containerInspect.mounts.any { it.type == MountPoint.Type.Volume && it.RW } |
| 63 | + assert dockerClient.getVolumesWithName(uniqueVolumeName).size(): "Destination volume was not created" |
| 64 | + log.debug("\tContainer has mounted the expected destination volume") |
| 65 | + |
| 66 | + when: "Creating files in src directories" |
| 67 | + File srcFile1 = File.createTempFile("srcFile1", "temp", srcDir1) |
| 68 | + srcFile1.text = System.currentTimeMillis() |
| 69 | + log.debug("\tCreated file \"${srcFile1.name}\" in first src dir") |
| 70 | + File srcFile2 = File.createTempFile("srcFile2", "temp", srcDir2) |
| 71 | + srcFile2.text = System.currentTimeMillis() + new Random().nextInt() |
| 72 | + log.debug("\tCreated file \"${srcFile2.name}\" in second src dir") |
| 73 | + |
| 74 | + then: "The sync container should see new source files, and sync them" |
| 75 | + syncer.runBashCommandInContainer("cat /mnt/src/${srcDir1.name}/${srcFile1.name}").toString().contains(srcFile1.text) |
| 76 | + syncer.runBashCommandInContainer("cat /mnt/src/${srcDir2.name}/${srcFile2.name}").toString().contains(srcFile2.text) |
| 77 | + log.debug("\tContainer sees the source files") |
| 78 | + sleep(2000)//Wait for sync |
| 79 | + syncer.runBashCommandInContainer("cat /mnt/dest/${srcFile1.name}").toString().contains(srcFile1.text) |
| 80 | + syncer.runBashCommandInContainer("cat /mnt/dest/${srcFile2.name}").toString().contains(srcFile2.text) |
| 81 | + log.debug("\tContainer successfully synced the files to destination dir") |
| 82 | + |
| 83 | + when: "Creating a recursive file" |
| 84 | + File recursiveFile = new File(srcDir1.canonicalPath + "/subDir/subFile.temp").createParentDirectories() |
| 85 | + recursiveFile.createNewFile() |
| 86 | + recursiveFile.text = System.nanoTime() |
| 87 | + log.info("\tCreate recursive file:" + recursiveFile.canonicalPath) |
| 88 | + |
| 89 | + then: "The sync container should see the new source file, and sync it to a new recursive dir" |
| 90 | + sleep(2000) |
| 91 | + syncer.runBashCommandInContainer("cat /mnt/dest/subDir/subFile.temp").toString().contains(recursiveFile.text) |
| 92 | + log.info("\t\tFile was successfully synced") |
| 93 | + |
| 94 | + |
| 95 | + /** |
| 96 | + inotify does not appear to successfully detect deletions |
| 97 | + Files will however be deleted once a create/update is detected |
| 98 | + */ |
| 99 | + when: "Deleting first source file and updating second source file" |
| 100 | + assert srcFile1.delete(): "Error deleting source file:" + srcFile1.canonicalPath |
| 101 | + srcFile2.text = "UPDATED FILE" |
| 102 | + log.debug("\tUpdating AND deleting src files") |
| 103 | + |
| 104 | + then: "The file should be removed from destination dir" |
| 105 | + sleep(2000) |
| 106 | + !syncer.runBashCommandInContainer("cat /mnt/dest/${srcFile1.name} && echo Status: \$?").toString().containsIgnoreCase("Status: 0") |
| 107 | + syncer.runBashCommandInContainer("cat /mnt/dest/${srcFile2.name}").toString().containsIgnoreCase(srcFile2.text) |
| 108 | + log.debug("\t\tContainer successfully synced the changes") |
| 109 | + |
| 110 | + when:"Creating a new container and attaching it to the synced volume" |
| 111 | + AlpineContainer secondContainer = new AlpineContainer(dockerRemoteHost, dockerCertPath) |
| 112 | + secondContainer.containerName = syncer.containerName + "-companion" |
| 113 | + secondContainer.prepareVolumeMount(uniqueVolumeName, "/mnt/syncDir", false) |
| 114 | + secondContainer.createSleepyContainer() |
| 115 | + |
| 116 | + |
| 117 | + then:"The second container should see the still remaining synced files" |
| 118 | + assert secondContainer.startContainer() : "Error creating/staring second container" |
| 119 | + log.info("\tCreated an started second container ${secondContainer.shortId}") |
| 120 | + assert secondContainer.mounts.any { it.type == MountPoint.Type.Volume && it.RW == true} : "Second container did not mount the shared volume" |
| 121 | + log.info("\tSecond container was attached to volume:" + uniqueVolumeName) |
| 122 | + log.info("\tChecking that second container can access synced file:" + " /mnt/syncDir/${srcFile2.name}" ) |
| 123 | + assert secondContainer.runBashCommandInContainer("cat /mnt/syncDir/${srcFile2.name}").toString().containsIgnoreCase(srcFile2.text) : "Error reading synced file in second container:" + " /mnt/syncDir/${srcFile2.name}" |
| 124 | + |
| 125 | + log.info("\tChecking that second container can access recursive synced file") |
| 126 | + assert secondContainer.runBashCommandInContainer("cat /mnt/syncDir/subDir/subFile.temp").toString().contains(recursiveFile.text) |
| 127 | + log.info("\t\tContainer can access that file") |
| 128 | + cleanup: |
| 129 | + assert syncer.stopAndRemoveContainer() |
| 130 | + assert secondContainer?.stopAndRemoveContainer() |
| 131 | + srcDir1.deleteDir() |
| 132 | + srcDir2.deleteDir() |
| 133 | + dockerClient.rmVolume(containerInspect.mounts.find { it.type == MountPoint.Type.Volume }.name) |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | +} |
0 commit comments