TestClustersPluginIT.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Licensed to Elasticsearch under one or more contributor
  3. * license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright
  5. * ownership. Elasticsearch licenses this file to you under
  6. * the Apache License, Version 2.0 (the "License"); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.elasticsearch.gradle.testclusters;
  20. import org.elasticsearch.gradle.test.GradleIntegrationTestCase;
  21. import org.gradle.testkit.runner.BuildResult;
  22. import org.gradle.testkit.runner.GradleRunner;
  23. import java.util.Arrays;
  24. public class TestClustersPluginIT extends GradleIntegrationTestCase {
  25. public void testListClusters() {
  26. BuildResult result = getTestClustersRunner("listTestClusters").build();
  27. assertTaskSuccessful(result, ":listTestClusters");
  28. assertOutputContains(
  29. result.getOutput(),
  30. " * myTestCluster:"
  31. );
  32. }
  33. public void testUseClusterByOne() {
  34. BuildResult result = getTestClustersRunner("user1").build();
  35. assertTaskSuccessful(result, ":user1");
  36. assertStartedAndStoppedOnce(result);
  37. }
  38. public void testUseClusterByOneWithDryRun() {
  39. BuildResult result = getTestClustersRunner("--dry-run", "user1").build();
  40. assertNull(result.task(":user1"));
  41. assertNotStarted(result);
  42. }
  43. public void testUseClusterByTwo() {
  44. BuildResult result = getTestClustersRunner("user1", "user2").build();
  45. assertTaskSuccessful(result, ":user1", ":user2");
  46. assertStartedAndStoppedOnce(result);
  47. }
  48. public void testUseClusterByUpToDateTask() {
  49. // Run it once, ignoring the result and again to make sure it's considered up to date.
  50. // Gradle randomly considers tasks without inputs and outputs as as up-to-date or success on the first run
  51. getTestClustersRunner("upToDate1", "upToDate2").build();
  52. BuildResult result = getTestClustersRunner("upToDate1", "upToDate2").build();
  53. assertTaskUpToDate(result, ":upToDate1", ":upToDate2");
  54. assertNotStarted(result);
  55. }
  56. public void testUseClusterBySkippedTask() {
  57. BuildResult result = getTestClustersRunner("skipped1", "skipped2").build();
  58. assertTaskSkipped(result, ":skipped1", ":skipped2");
  59. assertNotStarted(result);
  60. }
  61. public void testUseClusterBySkippedAndWorkingTask() {
  62. BuildResult result = getTestClustersRunner("skipped1", "user1").build();
  63. assertTaskSkipped(result, ":skipped1");
  64. assertTaskSuccessful(result, ":user1");
  65. assertOutputContains(
  66. result.getOutput(),
  67. "> Task :user1",
  68. "Starting `ElasticsearchNode{name='myTestCluster'}`",
  69. "Stopping `ElasticsearchNode{name='myTestCluster'}`"
  70. );
  71. }
  72. public void testMultiProject() {
  73. BuildResult result = GradleRunner.create()
  74. .withProjectDir(getProjectDir("testclusters_multiproject"))
  75. .withArguments("user1", "user2", "-s", "-i", "--parallel", "-Dlocal.repo.path=" + getLocalTestRepoPath())
  76. .withPluginClasspath()
  77. .build();
  78. assertTaskSuccessful(result, ":user1", ":user2");
  79. assertStartedAndStoppedOnce(result);
  80. }
  81. public void testUseClusterByFailingOne() {
  82. BuildResult result = getTestClustersRunner("itAlwaysFails").buildAndFail();
  83. assertTaskFailed(result, ":itAlwaysFails");
  84. assertStartedAndStoppedOnce(result);
  85. assertOutputContains(
  86. result.getOutput(),
  87. "Stopping `ElasticsearchNode{name='myTestCluster'}`, tailLogs: true",
  88. "Execution failed for task ':itAlwaysFails'."
  89. );
  90. }
  91. public void testUseClusterByFailingDependency() {
  92. BuildResult result = getTestClustersRunner("dependsOnFailed").buildAndFail();
  93. assertTaskFailed(result, ":itAlwaysFails");
  94. assertNull(result.task(":dependsOnFailed"));
  95. assertStartedAndStoppedOnce(result);
  96. assertOutputContains(
  97. result.getOutput(),
  98. "Stopping `ElasticsearchNode{name='myTestCluster'}`, tailLogs: true",
  99. "Execution failed for task ':itAlwaysFails'."
  100. );
  101. }
  102. public void testConfigurationLocked() {
  103. BuildResult result = getTestClustersRunner("illegalConfigAlter").buildAndFail();
  104. assertTaskFailed(result, ":illegalConfigAlter");
  105. assertOutputContains(
  106. result.getOutput(),
  107. "Configuration can not be altered, already locked"
  108. );
  109. }
  110. private void assertNotStarted(BuildResult result) {
  111. assertOutputDoesNotContain(
  112. result.getOutput(),
  113. "Starting ",
  114. "Stopping "
  115. );
  116. }
  117. private GradleRunner getTestClustersRunner(String... tasks) {
  118. String[] arguments = Arrays.copyOf(tasks, tasks.length + 3);
  119. arguments[tasks.length] = "-s";
  120. arguments[tasks.length + 1] = "-i";
  121. arguments[tasks.length + 2] = "-Dlocal.repo.path=" + getLocalTestRepoPath();
  122. return GradleRunner.create()
  123. .withProjectDir(getProjectDir("testclusters"))
  124. .withArguments(arguments)
  125. .withPluginClasspath();
  126. }
  127. private void assertStartedAndStoppedOnce(BuildResult result) {
  128. assertOutputOnlyOnce(
  129. result.getOutput(),
  130. "Starting `ElasticsearchNode{name='myTestCluster'}`",
  131. "Stopping `ElasticsearchNode{name='myTestCluster'}`"
  132. );
  133. }
  134. }