-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Perform MVS command method (#90)
* remove 'WithResult' postfix Signed-off-by: Anatoli Kalbasin <[email protected]> * implement PerformMvsCommand method Signed-off-by: Anatoli Kalbasin <[email protected]> * add tests Signed-off-by: Anatoli Kalbasin <[email protected]> * update license Signed-off-by: Anatoli Kalbasin <[email protected]> * add docs Signed-off-by: Anatoli Kalbasin <[email protected]> --------- Signed-off-by: Anatoli Kalbasin <[email protected]>
- Loading branch information
1 parent
620a967
commit 95f7d6c
Showing
11 changed files
with
350 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/kotlin/org/zowe/zdevops/classic/steps/PerformMvsCommandStep.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (c) 2024 IBA Group. | ||
* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* IBA Group | ||
* Zowe Community | ||
*/ | ||
|
||
package org.zowe.zdevops.classic.steps | ||
|
||
import hudson.Extension | ||
import hudson.Launcher | ||
import hudson.model.AbstractBuild | ||
import hudson.model.BuildListener | ||
import hudson.util.FormValidation | ||
import org.kohsuke.stapler.DataBoundConstructor | ||
import org.kohsuke.stapler.QueryParameter | ||
import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection | ||
import org.zowe.zdevops.Messages | ||
import org.zowe.zdevops.classic.AbstractBuildStep | ||
import org.zowe.zdevops.logic.performMvsCommand | ||
import org.zowe.zdevops.utils.validateFieldIsNotEmpty | ||
|
||
|
||
/** | ||
* A Jenkins Pipeline step for performing a MVS command on a z/OS system via freestyle job. | ||
*/ | ||
class PerformMvsCommandStep | ||
/** | ||
* Data-bound constructor for the {@code PerformMvsCommandStep} step. | ||
* | ||
* @param connectionName The name of the z/OS connection to be used for executing the MVS command. | ||
* @param command The MVS command to be executed. | ||
*/ | ||
@DataBoundConstructor | ||
constructor( | ||
connectionName: String, | ||
val command: String, | ||
) : AbstractBuildStep(connectionName) { | ||
|
||
/** | ||
* Performs the MVS command execution step within a Jenkins Pipeline build. | ||
* | ||
* @param build The current Jenkins build. | ||
* @param launcher The build launcher. | ||
* @param listener The build listener. | ||
* @param zosConnection The z/OS connection to execute the MVS command. | ||
*/ | ||
override fun perform( | ||
build: AbstractBuild<*, *>, | ||
launcher: Launcher, | ||
listener: BuildListener, | ||
zosConnection: ZOSConnection | ||
) { | ||
performMvsCommand(zosConnection, listener, command) | ||
} | ||
|
||
|
||
/** | ||
* Descriptor for the {@code PerformMvsCommandStep} step. | ||
* | ||
* This descriptor provides information about the step and makes it available for use | ||
* within Jenkins Pipelines. | ||
*/ | ||
@Extension | ||
class DescriptorImpl : Companion.DefaultBuildDescriptor(Messages.zdevops_classic_performMvsCommandStep_display_name()) { | ||
|
||
/** | ||
* Performs form validation for the 'command' parameter to ensure it is not empty. | ||
* | ||
* @param command The MVS command field value to validate. | ||
* @return A {@link FormValidation} object indicating whether the field is valid or contains an error. | ||
*/ | ||
fun doCheckCommand(@QueryParameter command: String): FormValidation? { | ||
return validateFieldIsNotEmpty(command) | ||
} | ||
|
||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/kotlin/org/zowe/zdevops/declarative/jobs/PerformMvsCommandDeclarative.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (c) 2024 IBA Group. | ||
* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* IBA Group | ||
* Zowe Community | ||
*/ | ||
|
||
package org.zowe.zdevops.declarative.jobs | ||
|
||
import hudson.AbortException | ||
import hudson.EnvVars | ||
import hudson.Extension | ||
import hudson.FilePath | ||
import hudson.model.TaskListener | ||
import org.kohsuke.stapler.DataBoundConstructor | ||
import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection | ||
import org.zowe.zdevops.declarative.AbstractZosmfActionWithResult | ||
import org.zowe.zdevops.logic.performMvsCommand | ||
|
||
/** | ||
* Class that represents an action to perform an MVS command with a result in a declarative pipeline. | ||
* This class extends {@code AbstractZosmfActionWithResult} and is designed to execute an MVS command | ||
* via Zowe z/OSMF and return the command's output. | ||
* | ||
* @param command the MVS command to be executed. | ||
*/ | ||
class PerformMvsCommandDeclarative | ||
@DataBoundConstructor | ||
constructor( | ||
val command: String, | ||
) : AbstractZosmfActionWithResult() { | ||
|
||
override fun run( | ||
workspace: FilePath, | ||
listener: TaskListener, | ||
envVars: EnvVars, | ||
zoweZOSConnection: ZOSConnection | ||
): String { | ||
return performMvsCommand(zoweZOSConnection, listener, command) | ||
?: throw AbortException("MVS command execution returned an empty result") | ||
} | ||
|
||
@Extension | ||
class DescriptorImpl : Companion.DefaultStepDescriptor(functionName = "performMvsCommand") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/kotlin/org/zowe/zdevops/logic/PerformMvsCommandOperation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) 2024 IBA Group. | ||
* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* IBA Group | ||
* Zowe Community | ||
*/ | ||
|
||
package org.zowe.zdevops.logic | ||
|
||
import hudson.AbortException | ||
import hudson.model.TaskListener | ||
import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection | ||
import org.zowe.kotlinsdk.zowe.client.sdk.zosconsole.ConsoleResponse | ||
import org.zowe.kotlinsdk.zowe.client.sdk.zosconsole.IssueCommand | ||
|
||
/** | ||
* Executes an MVS command on a z/OS system using the provided z/OS connection. | ||
* | ||
* This function allows you to send an MVS command to a z/OS system, and capture the response | ||
* | ||
* @param zosConnection The z/OS connection through which the MVS command will be executed. | ||
* @param listener The Jenkins build listener for logging and monitoring the execution. | ||
* @param command The MVS command to be executed. | ||
* @return the command output. | ||
* @throws AbortException if the MVS command execution fails, with the error message indicating | ||
* the reason for the failure. | ||
*/ | ||
fun performMvsCommand( | ||
zosConnection: ZOSConnection, | ||
listener: TaskListener, | ||
command: String, | ||
): String? { | ||
listener.logger.println("[Perform MVS command] - Issuing command : $command") | ||
val commandResponseObj: ConsoleResponse | ||
try { | ||
commandResponseObj = IssueCommand(zosConnection).issueSimple(command) | ||
listener.logger.println(commandResponseObj.commandResponse) | ||
} catch (ex: Exception) { | ||
listener.logger.println("[Perform MVS command] - MVS command execution failed") | ||
throw ex | ||
} | ||
listener.logger.println("[Perform MVS command] - The command has been successfully executed") | ||
return commandResponseObj.commandResponse | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/resources/org/zowe/zdevops/classic/steps/PerformMvsCommandStep/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<?jelly escape-by-default='true'?> | ||
|
||
<jelly:jelly xmlns:jelly="jelly:core" xmlns:form="/lib/form"> | ||
<form:entry field="connectionName" title="${%zdevops.classic.connection.title}"> | ||
<form:select/> | ||
</form:entry> | ||
<form:entry field="command" title="${%zdevops.classic.command.title}"> | ||
<form:textbox/> | ||
</form:entry> | ||
</jelly:jelly> |
2 changes: 2 additions & 0 deletions
2
src/main/resources/org/zowe/zdevops/classic/steps/PerformMvsCommandStep/config.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
zdevops.classic.connection.title=z/OS connection | ||
zdevops.classic.command.title=MVS command to be executed |
98 changes: 98 additions & 0 deletions
98
src/test/kotlin/org/zowe/zdevops/declarative/jobs/PerformMvsCommandDeclarativeSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (c) 2024 IBA Group. | ||
* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* IBA Group | ||
* Zowe Community | ||
*/ | ||
|
||
package org.zowe.zdevops.classic.steps | ||
|
||
import hudson.EnvVars | ||
import hudson.FilePath | ||
import io.kotest.assertions.assertSoftly | ||
import io.kotest.core.spec.style.ShouldSpec | ||
import io.kotest.engine.spec.tempdir | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.string.shouldContain | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.spyk | ||
import okhttp3.mockwebserver.MockResponse | ||
import okhttp3.mockwebserver.MockWebServer | ||
import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection | ||
import org.zowe.zdevops.MOCK_SERVER_HOST | ||
import org.zowe.zdevops.MockResponseDispatcher | ||
import org.zowe.zdevops.MockServerFactory | ||
import org.zowe.zdevops.declarative.jobs.PerformMvsCommandDeclarative | ||
import java.io.File | ||
import java.io.PrintStream | ||
import java.nio.file.Paths | ||
|
||
class PerformMvsCommandDeclarativeSpec : ShouldSpec({ | ||
lateinit var mockServer: MockWebServer | ||
lateinit var responseDispatcher: MockResponseDispatcher | ||
val mockServerFactory = MockServerFactory() | ||
|
||
beforeSpec { | ||
mockServer = mockServerFactory.startMockServer(MOCK_SERVER_HOST) | ||
responseDispatcher = mockServerFactory.responseDispatcher | ||
} | ||
afterSpec { | ||
mockServerFactory.stopMockServer() | ||
} | ||
context("declarative/jobs module: PerformMvsCommandStep") { | ||
val zosConnection = ZOSConnection(mockServer.hostName, mockServer.port.toString(), "test", "test", "https") | ||
val trashDir = tempdir() | ||
val trashDirWithInternal = Paths.get(trashDir.absolutePath, "test_name").toString() | ||
val workspace = FilePath(File(trashDirWithInternal)) | ||
val env = EnvVars() | ||
|
||
afterEach { | ||
responseDispatcher.removeAllEndpoints() | ||
} | ||
should("perform PerformMvsCommandDeclarative operation and return its result") { | ||
var isPreExecuteStage = false | ||
var isCommandExecuted = false | ||
val expectedPartialMvsDisplayActiveCommandOutput = "CNZ4105I 12.56.27 DISPLAY ACTIVITY" | ||
val taskListener = object : TestBuildListener() { | ||
override fun getLogger(): PrintStream { | ||
val logger = mockk<PrintStream>() | ||
every { | ||
logger.println(any<String>()) | ||
} answers { | ||
if (firstArg<String>().contains("Issuing command")) { | ||
isPreExecuteStage = true | ||
} else if (firstArg<String>().contains("The command has been successfully executed")) { | ||
isCommandExecuted = true | ||
} | ||
} | ||
return logger | ||
} | ||
} | ||
|
||
responseDispatcher.injectEndpoint( | ||
this.testCase.name.testName, | ||
{ it?.requestLine?.contains("PUT /zosmf/restconsoles/consoles/") ?: false }, | ||
{ MockResponse() | ||
.setResponseCode(200) | ||
.setBody(responseDispatcher.readMockJson("displayActiveASCommandOutput") ?: "") } | ||
) | ||
|
||
val performMvsCommandInst = spyk( | ||
PerformMvsCommandDeclarative("D A,L") | ||
) | ||
val mvsCommandResult = performMvsCommandInst.run(workspace, taskListener, env, zosConnection) | ||
|
||
assertSoftly { mvsCommandResult shouldContain expectedPartialMvsDisplayActiveCommandOutput } | ||
assertSoftly { isPreExecuteStage shouldBe true } | ||
assertSoftly { isCommandExecuted shouldBe true } | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"cmd-response-key": "C1977656", | ||
"cmd-response-url": "https://192.168.1.1:10443/zosmf/restconsoles/consoles/defcn/solmsgs/C1977656", | ||
"cmd-response-uri": "/zosmf/restconsoles/consoles/defcn/solmsgs/C1977656", | ||
"cmd-response": " CNZ4105I 12.56.27 DISPLAY ACTIVITY 546\r JOBS M/S TS USERS SYSAS INITS ACTIVE/MAX VTAM OAS\r 00020 00040 00004 00034 00025 00002/00040 00041\r ..." | ||
} |