Skip to content

Commit

Permalink
Fix tests comments
Browse files Browse the repository at this point in the history
1) change disableScheduledAndDeleteProject method to receive params
2) change api version to the last version.
3) the original test is added in the where and if it matches with false.
Additional: Add groovy docs
Update ProjectUtils.groovy
  • Loading branch information
avelasquezr committed Mar 13, 2024
1 parent 48bb689 commit bcc68a2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,11 @@ class ExecutionSpec extends BaseContainer {
jsonValue(response4.body()).executions.size() >= 1
}
cleanup:
(2..4).each {disableScheduledAndDeleteProject("${projectNameSuffix}-${it}")}
(2..4).each {disableScheduledAndDeleteProject("${projectNameSuffix}-${it}", [
"project.disable.schedule": "true",
"project.later.schedule.enable": "false",
"project.disable.executions": "true"
])}
}

def "executions-running when project is disabled"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProjectUtils {
* @param client The RdClient object used to interact with the Rundeck API.
* @throws RuntimeException If failed to create a project.
*/
public static def createProjectsWithJobsScheduled = (String suffixProjectName, int projectsCount, int jobsCountsPerProject, RdClient client) -> {
static void createProjectsWithJobsScheduled(String suffixProjectName, int projectsCount, int jobsCountsPerProject, RdClient client) {
(1..projectsCount).each {it ->
def projectName = "${suffixProjectName}-${it}".toString()
def getProject = client.doGet("/project/${projectName}")
Expand All @@ -31,4 +31,33 @@ class ProjectUtils {
}
}

/**
* Retrieves the count of running executions for a given project, waiting until the count exceeds a specified value or a timeout occurs.
*
* @param projectName The name of the project to query. Must not be null.
* @param valueMoreThan The threshold value for the count of running executions. Must be greater than zero.
* @param client The RdClient instance used to make HTTP requests. Must not be null.
* @return True if the count of running executions exceeds the specified value within the timeout period, false otherwise.
* @throws RuntimeException if fetching running executions fails or if a timeout occurs.
*/
static def projectCountExecutions = (String projectName, int valueMoreThan, RdClient client) -> {
def startTime = System.currentTimeMillis()
def timeout = 10000
def pollingInterval = 1000
while (true) {
def response = client.doGet("/project/${projectName}/executions/running?includePostponed=true")
if (!response.successful) {
throw new RuntimeException("Failed to get running executions: ${response.body().string()}")
}
def valueCount = client.jsonValue(response.body(), Map).paging.count
if (valueCount > valueMoreThan) {
return Boolean.TRUE
}
if (System.currentTimeMillis() - startTime > timeout) {
throw new RuntimeException("Timeout: No running executions found within ${timeout} milliseconds.")
}
sleep pollingInterval
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -303,17 +303,39 @@ abstract class BaseContainer extends Specification implements ClientProvider {
}
}

/**
* Deletes the specified project.
* This method sends a DELETE request to remove the project with the given name.
* If the deletion operation fails, a RuntimeException is thrown.
*
* @param projectName the name of the project to be deleted. Must not be null.
* @throws RuntimeException if the project deletion fails.
* The exception contains a detailed message obtained from the server's response.
*/
void deleteProject(String projectName) {
def response = client.doDelete("/project/${projectName}")
if (!response.successful) {
throw new RuntimeException("Failed to delete project: ${response.body().string()}")
}
}

void disableScheduledAndDeleteProject(String projectName) {
def responseDisable = client.doPutWithJsonBody("/project/${projectName}/config",
["project.name": projectName, "project.disable.schedule": "true", "project.later.schedule.enable": "false",
"project.disable.executions": "true"])
/**
* Disables scheduled executions for a specific project and then deletes the project.
* This method first makes a PUT request to update the project's configuration,
* specifically to disable all scheduled executions. If this operation is successful,
* it proceeds to delete the project with a DELETE request. If any of the operations fail,
* a RuntimeException is thrown.
*
* @param projectName the name of the project to be disabled and deleted. Must not be null.
* @param body a map containing the configuration to be updated in the project before deletion.
* Specifically, this map should include the necessary properties to disable
* scheduled executions. The exact contents of the map will depend on the client API and
* the project configuration.
* @throws RuntimeException if disabling scheduled executions or deleting the project fails.
* The exception contains a detailed message obtained from the server's response.
*/
void disableScheduledAndDeleteProject(String projectName, Map body) {
def responseDisable = client.doPutWithJsonBody("/project/${projectName}/config", body)
if (!responseDisable.successful) {
throw new RuntimeException("Failed to disable scheduled execution: ${responseDisable.body().string()}")
}
Expand All @@ -328,6 +350,14 @@ abstract class BaseContainer extends Specification implements ClientProvider {
startEnvironment()
}

/**
* Pauses the execution for a specified number of seconds.
* This method utilizes the sleep function to pause the current thread for the given duration.
* If the thread is interrupted while sleeping, it catches the InterruptedException and logs the error.
*
* @param seconds the number of seconds to pause the execution. This value should be positive.
* @throws IllegalArgumentException if the `seconds` parameter is negative, as `Duration.ofSeconds` cannot process negative values.
*/
void hold(int seconds) {
try {
sleep Duration.ofSeconds(seconds).toMillis()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2721,7 +2721,7 @@ class MenuControllerSpec extends RundeckHibernateSpec implements ControllerUnitT
}

params.project = 'project1,project2,project3'
request.api_version = 35
request.api_version = 47

when:
def result = controller.apiExecutionsRunningv14()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ class ApiProjectSelectInterceptorSpec extends Specification implements Intercept

where:
project | controller | action | match
'xProject' | 'menu' | 'apiExecutionsRunningv14' | false
'*' | 'menu' | 'apiExecutionsRunningv14' | false
'***' | 'menu' | 'apiExecutionsRunningv14' | false
'*Project' | 'menu' | 'apiExecutionsRunningv14' | false
'*Project' | 'anyMenu' | 'anyAction' | true
}

void "excluded project"() {
given:
params.project = 'project1'
request.requestURI >> '/api/15/executions/running'
request.requestURI >> '/api/47/executions/running'
request.getMethod() >> 'GET'

when:
Expand Down

0 comments on commit bcc68a2

Please sign in to comment.