#30 Pipeline-as-a-service integration
Closed 5 years ago by mkovarik. Opened 5 years ago by mkovarik.
mkovarik/c3i-library pipelineaas  into  master

@@ -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"))

+     }

+ 

+ 

+ }

file modified
+8 -2
@@ -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 @@ 

                 status: [

                            phase: it.phase ?: 'Unknown',

                            conditions: it.conditions ?: []

-                        ]

+                        ],

+                data: it.data ?: []

              ]

          }

      }
@@ -46,6 +47,11 @@ 

          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 })

@@ -0,0 +1,53 @@ 

+ // 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.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

+ }

+ 

+ /**

+  * 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()

+   }

+ }

Function for getting metadata of pipeline as a service and destroying pipeline.

This can just be: if (args.namespace) {

This looks great! Can you write some tests for these new functions? You can find examples of test cases in the test directory. You may need to call other Jenkins steps via the args.script reference, like args.script.echo, for them to be testable. Let me know if you run into any issues.

1 new commit added

  • WIP: Tests for Pipelineaas
5 years ago

3 new commits added

  • Tests for Pipelineaas
  • Add script for destroy pipeline
  • Pipeline-as-a-service get variables function.
5 years ago

3 new commits added

  • Tests for Pipelineaas
  • Add script for destroy pipeline
  • Pipeline-as-a-service get variables function.
5 years ago

Pull-Request has been closed by mkovarik

5 years ago