From 575910ce81d56fad2dd7b6292051e7410538605b Mon Sep 17 00:00:00 2001 From: Mike Bonnet Date: Oct 28 2019 15:04:47 +0000 Subject: only call "delete()" when there are actually objects to delete Also fix up the tests. --- diff --git a/src/com/redhat/c3i/util/Deployer.groovy b/src/com/redhat/c3i/util/Deployer.groovy index d563fbf..c185112 100644 --- a/src/com/redhat/c3i/util/Deployer.groovy +++ b/src/com/redhat/c3i/util/Deployer.groovy @@ -98,7 +98,6 @@ class Deployer implements Serializable { def selected = script.openshift.selector("all,pvc,configmap,secret", ["app": app]) oldobjs.addAll(selected.objects()) } - def deleted = 0 def now = new Date() def todelete = [] // Delete all objects that are older than 1 hour @@ -109,11 +108,12 @@ class Deployer implements Serializable { def creationTime = df.parse(obj.metadata.creationTimestamp) if (now.getTime() - creationTime.getTime() > (1000 * 60 * age)) { todelete.add("${obj.kind}/${obj.metadata.name}") - deleted += 1 } } script.echo "Deleting:\n"+todelete.join("\n") - script.openshift.delete(todelete.join(' '), "--ignore-not-found=true") - return deleted + if (todelete) { + script.openshift.delete(todelete.join(' '), "--ignore-not-found=true") + } + return todelete.size() } } diff --git a/test/DeployerTest.groovy b/test/DeployerTest.groovy index 6cc57e1..087f094 100644 --- a/test/DeployerTest.groovy +++ b/test/DeployerTest.groovy @@ -49,13 +49,13 @@ class DeployerTest extends BasePipelineTestCPS { helper.registerAllowedMethod('selector', [String.class, Map.class], { new SelectorMock(test: this, objs: [ [name: 'obj1', ts: dateStr] ])}) - helper.registerAllowedMethod('delete', [String.class, String.class, String.class], null) + helper.registerAllowedMethod('delete', [String.class, String.class], null) def cleaned = c3i.cleanup(script: c3i, 'foo') printCallStack() assertEquals(1, helper.methodCallCount('selector')) assertEquals(1, helper.methodCallCount('delete')) assertTrue(helper.callStack.any { call -> - call.methodName == "delete" && call.args == ['test', 'obj1', '--ignore-not-found=true'] + call.methodName == "delete" && call.args == ['test/obj1', '--ignore-not-found=true'] }) assertEquals(1, cleaned) } @@ -66,13 +66,13 @@ class DeployerTest extends BasePipelineTestCPS { helper.registerAllowedMethod('selector', [String.class, Map.class], { new SelectorMock(test: this, objs: [ [name: 'obj1', ts: dateStr] ])}) - helper.registerAllowedMethod('delete', [String.class, String.class, String.class], null) + helper.registerAllowedMethod('delete', [String.class, String.class], null) def cleaned = c3i.cleanup(script: c3i, 'foo', 'bar', 'baz') printCallStack() assertEquals(3, helper.methodCallCount('selector')) - assertEquals(3, helper.methodCallCount('delete')) + assertEquals(1, helper.methodCallCount('delete')) assertTrue(helper.callStack.any { call -> - call.methodName == "delete" && call.args == ['test', 'obj1', '--ignore-not-found=true'] + call.methodName == "delete" && call.args == ['test/obj1 test/obj1 test/obj1', '--ignore-not-found=true'] }) assertEquals(3, cleaned) }