From c01a7399c1c07c9f8f795794db380fbfb924bbef Mon Sep 17 00:00:00 2001 From: Michal Kovarik Date: Nov 06 2019 14:10:45 +0000 Subject: [PATCH 1/3] Pipeline-as-a-service get variables function. Function to get metadata about pipeline. Metadata are stored inside configmap in namespace of pipeline. --- diff --git a/vars/pipelineaas.groovy b/vars/pipelineaas.groovy new file mode 100644 index 0000000..c5c28ca --- /dev/null +++ b/vars/pipelineaas.groovy @@ -0,0 +1,27 @@ +// Functions to get variables from Pipeline-as-a-service. +// Michal Kovarik (mkovarik@redhat.com), 2019-11-06 + +/** + * Get variables from Pipeline-as-a-service. + * @param args.script The script calling the method. + * @param args.pipeline_id A unique {@code String} used to identify pipeline. + * @param args.namespace Namespace which requested pipeline or namespace with pipeline. + * @return A map with pipeline variables. + */ +def getVars(Map args) { + def pipelineNamespace + def data + def originNamespace = "" + if (args.containsKey('namespace')) { + originNamespace = args.namespace + } + args.script.openshift.withCluster() { + args.script.openshift.withProject(originNamespace) { + pipelineNamespace = args.script.openshift.selector('configmap', "${args.pipeline_id}").object().data.PIPELINE_NAMESPACE + } + args.script.openshift.withProject(pipelineNamespace){ + data = args.script.openshift.selector('configmap', "${args.pipeline_id}").object().data + } + } + return data +} From 4df4e2f70f8e9acab9c23b72c6f90c1106b2bd78 Mon Sep 17 00:00:00 2001 From: Michal Kovarik Date: Nov 06 2019 14:13:07 +0000 Subject: [PATCH 2/3] Add script for destroy pipeline --- diff --git a/vars/pipelineaas.groovy b/vars/pipelineaas.groovy index c5c28ca..5289861 100644 --- a/vars/pipelineaas.groovy +++ b/vars/pipelineaas.groovy @@ -25,3 +25,29 @@ def getVars(Map args) { } return data } + +/** + * Destroy Pipeline-as-a-service project. + * @param args.script The script calling the method. + * @param args.pipeline_id A unique {@code String} used to identify pipeline. + * @param args.namespace Namespace which requested pipeline or namespace with pipeline. + * @return A map with pipeline variables. + */ +def destroy(Map args) { + vars = getVars(args) + args.script.openshift.withCluster() { + if (vars.C3IAAS == "true") { + echo "Deleting project ${vars.PIPELINE_NAMESPACE}" + args.script.openshift.selector('project', vars.PIPELINE_NAMESPACE).delete() + } + else { + args.script.openshift.withProject(vars.PIPELINE_NAMESPACE) { + echo "Deleting services..." + def toBeDeleted = args.script.openshift.selector('all,pvc,cm,secret,sa', ["c3i.redhat.com/pipeline": args.pipeline_id]) + echo "Deleting ${toBeDeleted.names()}" + toBeDeleted.delete() + } + } + args.script.openshift.selector('configmap', args.pipeline_id).delete() + } +} From e2233b7dacebf5be5831568051ecd28d73d0e3d8 Mon Sep 17 00:00:00 2001 From: Michal Kovarik Date: Nov 08 2019 06:33:47 +0000 Subject: [PATCH 3/3] Tests for Pipelineaas --- diff --git a/test/PipelineaasTest.groovy b/test/PipelineaasTest.groovy new file mode 100644 index 0000000..3437d7d --- /dev/null +++ b/test/PipelineaasTest.groovy @@ -0,0 +1,74 @@ +import org.junit.* +import com.lesfurets.jenkins.unit.cps.BasePipelineTestCPS +import static groovy.test.GroovyAssert.* + +class PipelineaasTest extends BasePipelineTestCPS implements Serializable { + def pipelineaas + + @Before + void setUp() { + super.setUp() + pipelineaas = loadScript('vars/pipelineaas.groovy') + helper.registerAllowedMethod('withCluster', [Closure.class], null) + helper.registerAllowedMethod('withProject', [String.class, Closure.class], null) + // for getVars + helper.registerAllowedMethod('selector', [String.class, String.class], { new SelectorMock(test: this, objs: [ + [data: ['PIPELINE_NAMESPACE': "testnamespace"]] + ])}) + // for destroy + helper.registerAllowedMethod('selector', [String.class, Map.class], { new SelectorMock(test: this)}) + + binding.setVariable('script', pipelineaas) + binding.setVariable('openshift', pipelineaas) + } + + @Test + void testGetVars() { + pipelineaas.getVars(script: pipelineaas, pipeline_id: 'abc-12', namespace: 'originnamespace') + printCallStack() + assertEquals(1, helper.methodCallCount("withCluster")) + assertEquals(2, helper.methodCallCount("withProject")) + def withProjectCalls = helper.callStack.findAll {call -> call.methodName == 'withProject'} + assertEquals('originnamespace', withProjectCalls[0].args[0]) + assertEquals('testnamespace', withProjectCalls[1].args[0]) + } + + @Test + void testGetVarsDefaultNamespace() { + pipelineaas.getVars(script: pipelineaas, pipeline_id: 'abc-12') + printCallStack() + assertEquals(1, helper.methodCallCount("withCluster")) + assertEquals(2, helper.methodCallCount("withProject")) + def withProjectCalls = helper.callStack.findAll {call -> call.methodName == 'withProject'} + assertEquals('', withProjectCalls[0].args[0]) + assertEquals('testnamespace', withProjectCalls[1].args[0]) + } + + @Test + void testDestroyC3IAAS() { + helper.registerAllowedMethod('getVars', [Map.class], {['C3IAAS': "true"]}) + pipelineaas.destroy(script: pipelineaas, pipeline_id: 'abc-12') + printCallStack() + assertTrue(helper.callStack.any { call -> + call.methodName == 'selector' && call.args[0] == 'project' + }) + assertEquals(1, helper.methodCallCount("withCluster")) + assertEquals(0, helper.methodCallCount("withProject")) + assertEquals(2, helper.methodCallCount("delete")) + } + + @Test + void testDestroyNamespace() { + helper.registerAllowedMethod('getVars', [Map.class], {['C3IAAS': "false"]}) + pipelineaas.destroy(script: pipelineaas, pipeline_id: 'abc-12') + printCallStack() + assertTrue(helper.callStack.any { call -> + call.methodName == 'selector' && call.args[1] == ["c3i.redhat.com/pipeline": "abc-12"] + }) + assertEquals(1, helper.methodCallCount("withCluster")) + assertEquals(1, helper.methodCallCount("withProject")) + assertEquals(2, helper.methodCallCount("delete")) + } + + +} diff --git a/test/SelectorMock.groovy b/test/SelectorMock.groovy index 33311c9..d9deda1 100644 --- a/test/SelectorMock.groovy +++ b/test/SelectorMock.groovy @@ -1,7 +1,7 @@ import com.lesfurets.jenkins.unit.BasePipelineTest import static groovy.test.GroovyAssert.* -class SelectorMock { +class SelectorMock implements Serializable { BasePipelineTest test List objs @@ -25,7 +25,8 @@ class SelectorMock { status: [ phase: it.phase ?: 'Unknown', conditions: it.conditions ?: [] - ] + ], + data: it.data ?: [] ] } } @@ -46,6 +47,11 @@ class SelectorMock { return this } + def delete() { + test.helper.registerMethodCall(this, getDepth(), 'delete') + return this + } + def narrow(kind) { test.helper.registerMethodCall(this, getDepth(), 'narrow', kind) return new SelectorMock(test: test, objs: objs.findAll { it.kind == kind }) diff --git a/vars/pipelineaas.groovy b/vars/pipelineaas.groovy index 5289861..4aa79ed 100644 --- a/vars/pipelineaas.groovy +++ b/vars/pipelineaas.groovy @@ -12,7 +12,7 @@ def getVars(Map args) { def pipelineNamespace def data def originNamespace = "" - if (args.containsKey('namespace')) { + if (args.namespace) { originNamespace = args.namespace } args.script.openshift.withCluster() {