Skip to content

Commit 30da015

Browse files
committed
Don't copy the inputs file to the output directory if the output directory already has a copy (add corresponding unit tests to)
1 parent 5a9adf3 commit 30da015

File tree

7 files changed

+86
-10
lines changed

7 files changed

+86
-10
lines changed

kubernetes/internal/create-weblogic-domain.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,12 @@ function createYamlFiles {
350350
# Create a directory for this domain's output files
351351
mkdir -p ${domainOutputDir}
352352

353-
# Save a copy of the inputs yaml file there
354-
cp ${valuesInputFile} "${domainOutputDir}/create-weblogic-domain-inputs.yaml"
353+
# Make sure the output directory has a copy of the inputs file.
354+
# The user can either pre-create the output directory, put the inputs
355+
# file there, and create the domain from it, or the user can put the
356+
# inputs file some place else and let this script create the output directory
357+
# (if needed) and copy the inputs file there.
358+
copyInputsFileToOutputDirectory ${valuesInputFile} "${domainOutputDir}/create-weblogic-domain-inputs.yaml"
355359

356360
domainPVOutput="${domainOutputDir}/weblogic-domain-persistent-volume.yaml"
357361
domainPVCOutput="${domainOutputDir}/weblogic-domain-persistent-volume-claim.yaml"

kubernetes/internal/create-weblogic-operator.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,12 @@ function createYamlFiles {
349349
# Create a directory for this operator's output files
350350
mkdir -p ${oprOutputDir}
351351

352-
# Save a copy of the inputs yaml file there
353-
cp ${valuesInputFile} "${oprOutputDir}/create-weblogic-operator-inputs.yaml"
352+
# Make sure the output directory has a copy of the inputs file.
353+
# The user can either pre-create the output directory, put the inputs
354+
# file there, and create the operator from it, or the user can put the
355+
# inputs file some place else and let this script create the output directory
356+
# (if needed) and copy the inputs file there.
357+
copyInputsFileToOutputDirectory ${valuesInputFile} "${oprOutputDir}/create-weblogic-operator-inputs.yaml"
354358

355359
# Generate the yaml to create the WebLogic operator
356360
oprOutput="${oprOutputDir}/weblogic-operator.yaml"

kubernetes/internal/utility.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,24 @@ function internalValidateGeneratedYamlFilesDoNotExist {
293293
done
294294
}
295295

296+
# Copy the inputs file from the command line into the output directory
297+
# for the domain/operator unless the output directory already has an
298+
# inputs file and the file is the same as the one from the commandline.
299+
# $1 the inputs file from the command line
300+
# $2 the file in the output directory that needs to be made the same as $1
301+
function copyInputsFileToOutputDirectory {
302+
local from=$1
303+
local to=$2
304+
local doCopy="true"
305+
if [ -f "${to}" ]; then
306+
local difference=`diff ${from} ${to}`
307+
if [ -z "${difference}" ]; then
308+
# the output file already exists and is the same as the inputs file.
309+
# don't make a copy.
310+
doCopy="false"
311+
fi
312+
fi
313+
if [ "${doCopy}" = "true" ]; then
314+
cp ${from} ${to}
315+
fi
316+
}

src/test/java/oracle/kubernetes/operator/create/CreateDomainInputsFileTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,30 @@ public void createDomainWithCompletedDefaultsInputsFile_usesSpecifiedInputsFileA
7878
.domainUID("test-domain-uid")
7979
.weblogicDomainStoragePath("/scratch/k8s_dir/domain1");
8080
assertThat(execCreateDomain(userProjects.getPath(), inputs), succeedsAndPrints("Completed"));
81+
assertThatOnlyTheExpectedGeneratedYamlFilesExist(inputs);
82+
}
83+
84+
@Test
85+
public void createDomainFromPreCreatedInputsFileInPreCreatedOutputDirectory_usesSpecifiedInputsFileAndSucceedsAndGeneratesExpectedYamlFiles() throws Exception {
86+
// customize the domain uid and weblogic storage path so that we have a valid inputs file
87+
CreateDomainInputs inputs =
88+
readDefaultInputsFile()
89+
.domainUID("test-domain-uid")
90+
.weblogicDomainStoragePath("/scratch/k8s_dir/domain1");
91+
// pre-create the output directory and the inputs file in the output directory, then
92+
// use that inputs file to create the domain
93+
DomainFiles domainFiles = new DomainFiles(userProjects.getPath(), inputs);
94+
Files.createDirectories(domainFiles.getWeblogicDomainPath());
95+
assertThat(
96+
execCreateDomain(
97+
userProjects.getPath(),
98+
inputs,
99+
domainFiles.getCreateWeblogicDomainInputsYamlPath()),
100+
succeedsAndPrints("Completed"));
101+
assertThatOnlyTheExpectedGeneratedYamlFilesExist(inputs);
102+
}
81103

104+
private void assertThatOnlyTheExpectedGeneratedYamlFilesExist(CreateDomainInputs inputs) throws Exception {
82105
// Make sure the generated directory has the correct list of files
83106
DomainFiles domainFiles = new DomainFiles(userProjects.getPath(), inputs);
84107
List<Path> expectedFiles = domainFiles.getExpectedContents(true); // include the directory too

src/test/java/oracle/kubernetes/operator/create/CreateOperatorInputsFileTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ public void createOperatorWithSpecifiedInputsFile_usesSpecifiedInputsFileAndSucc
7777
assertThatOnlyTheExpectedGeneratedYamlFilesExist(inputs);
7878
}
7979

80+
81+
@Test
82+
public void createDomainFromPreCreatedInputsFileInPreCreatedOutputDirectory_usesSpecifiedInputsFileAndSucceedsAndGeneratesExpectedYamlFiles() throws Exception {
83+
// customize the namespace name so that we can tell that it generated the yaml files based on this inputs instead of the default one
84+
CreateOperatorInputs inputs = readDefaultInputsFile().namespace("weblogic-operator-2");
85+
// pre-create the output directory and the inputs file in the output directory, then
86+
// use that inputs file to create the operator
87+
OperatorFiles operatorFiles = new OperatorFiles(userProjects.getPath(), inputs);
88+
Files.createDirectories(operatorFiles.getWeblogicOperatorPath());
89+
assertThat(
90+
execCreateOperator(
91+
userProjects.getPath(),
92+
inputs,
93+
operatorFiles.getCreateWeblogicOperatorInputsYamlPath()),
94+
succeedsAndPrints("Completed"));
95+
assertThatOnlyTheExpectedGeneratedYamlFilesExist(inputs);
96+
}
97+
8098
private void assertThatOnlyTheExpectedGeneratedYamlFilesExist(CreateOperatorInputs inputs) throws Exception {
8199
// Make sure the generated directory has the correct list of files
82100
OperatorFiles operatorFiles = new OperatorFiles(userProjects.getPath(), inputs);

src/test/java/oracle/kubernetes/operator/create/ExecCreateDomain.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ public class ExecCreateDomain {
1414
public static final String CREATE_SCRIPT = "src/test/scripts/unit-test-create-weblogic-domain.sh";
1515

1616
public static ExecResult execCreateDomain(Path userProjectsPath, CreateDomainInputs inputs) throws Exception {
17-
Path p = getInputsYamlPath(userProjectsPath);
18-
newYaml().dump(inputs, Files.newBufferedWriter(p));
19-
return execCreateDomain(" -g -o " + userProjectsPath.toString() + " -i " + p.toString());
17+
return execCreateDomain(userProjectsPath, inputs, getInputsYamlPath(userProjectsPath));
18+
}
19+
20+
public static ExecResult execCreateDomain(Path userProjectsPath, CreateDomainInputs inputs, Path inputsYamlPath) throws Exception {
21+
newYaml().dump(inputs, Files.newBufferedWriter(inputsYamlPath));
22+
return execCreateDomain(" -g -o " + userProjectsPath.toString() + " -i " + inputsYamlPath.toString());
2023
}
2124

2225
public static ExecResult execCreateDomain(String options) throws Exception {

src/test/java/oracle/kubernetes/operator/create/ExecCreateOperator.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@ public class ExecCreateOperator {
1414
public static final String CREATE_SCRIPT = "src/test/scripts/unit-test-create-weblogic-operator.sh";
1515

1616
public static ExecResult execCreateOperator(Path userProjectsPath, CreateOperatorInputs inputs) throws Exception {
17-
Path p = getInputsYamlPath(userProjectsPath);
18-
newYaml().dump(inputs, Files.newBufferedWriter(p));
19-
return execCreateOperator(" -g -o " + userProjectsPath.toString() + " -i " + p.toString());
17+
return execCreateOperator(userProjectsPath, inputs, getInputsYamlPath(userProjectsPath));
18+
}
19+
20+
public static ExecResult execCreateOperator(Path userProjectsPath, CreateOperatorInputs inputs, Path inputsYamlPath) throws Exception {
21+
newYaml().dump(inputs, Files.newBufferedWriter(inputsYamlPath));
22+
return execCreateOperator(" -g -o " + userProjectsPath.toString() + " -i " + inputsYamlPath.toString());
2023
}
2124

2225
public static ExecResult execCreateOperator(String options) throws Exception {

0 commit comments

Comments
 (0)