|
| 1 | +package com.rundeck.plugins.ansible.util |
| 2 | + |
| 3 | +import com.dtolabs.rundeck.core.common.Framework |
| 4 | +import com.dtolabs.rundeck.core.utils.PropertyLookup |
| 5 | +import spock.lang.Specification |
| 6 | +import spock.lang.TempDir |
| 7 | + |
| 8 | +import java.nio.file.Files |
| 9 | + |
| 10 | +class AnsibleUtilSpec extends Specification{ |
| 11 | + |
| 12 | + @TempDir |
| 13 | + File tempDir |
| 14 | + |
| 15 | + def "createTemporaryFile should create a temporary file with the correct properties"() { |
| 16 | + given: "Input parameters for the temporary file" |
| 17 | + String prefix = inputPrefix |
| 18 | + String suffix = ".txt" |
| 19 | + String data = "test data" |
| 20 | + String path = tempDir.absolutePath |
| 21 | + |
| 22 | + when: "The method is called" |
| 23 | + File result = AnsibleUtil.createTemporaryFile(prefix, suffix, data, path) |
| 24 | + |
| 25 | + then: "The file is created in the specified path with the correct content" |
| 26 | + result.exists() |
| 27 | + result.parent == tempDir.absolutePath |
| 28 | + result.name.startsWith(expectedPrefix) |
| 29 | + result.name.endsWith(suffix) |
| 30 | + new String(Files.readAllBytes(result.toPath())) == data |
| 31 | + |
| 32 | + where: |
| 33 | + inputPrefix || expectedPrefix |
| 34 | + "my-prefix" || "my-prefix" |
| 35 | + "" || "ansible-runner" |
| 36 | + } |
| 37 | + |
| 38 | + def "getCustomTmpPathDir should return the correct tmp path based on framework properties"() { |
| 39 | + given: "A mock Framework and PropertyLookup" |
| 40 | + def framework = Mock(Framework) |
| 41 | + def propertyLookup = Mock(PropertyLookup) |
| 42 | + framework.getPropertyLookup() >> propertyLookup |
| 43 | + |
| 44 | + and: "A specific value for framework.tmp.dir property" |
| 45 | + propertyLookup.getProperty("framework.tmp.dir") >> frameworkTmpDir |
| 46 | + |
| 47 | + and: "A system property for java.io.tmpdir" |
| 48 | + System.setProperty("java.io.tmpdir", systemTmpDir) |
| 49 | + |
| 50 | + when: "The method is called" |
| 51 | + def result = AnsibleUtil.getCustomTmpPathDir(framework) |
| 52 | + |
| 53 | + then: "The expected tmp path is returned" |
| 54 | + result == expectedTmpDir |
| 55 | + |
| 56 | + where: |
| 57 | + frameworkTmpDir | systemTmpDir || expectedTmpDir |
| 58 | + "custom/tmp/dir" | "/default/tmp" || "custom/tmp/dir" // Case where framework.tmp.dir is set |
| 59 | + "" | "/default/tmp" || "/default/tmp" // Case where framework.tmp.dir is empty |
| 60 | + null | "/default/tmp" || "/default/tmp" // Case where framework.tmp.dir is null |
| 61 | + } |
| 62 | +} |
| 63 | + |
0 commit comments