Skip to content

Commit

Permalink
notify about deactivated SWA reporting (#261)
Browse files Browse the repository at this point in the history
* report deactivated SWA reporting

* Update Utils.groovy

* add tests

* add test cases

* use hamcrest matchers in tests

* check for missing call to web analytics

* Update UtilsTest.groovy
  • Loading branch information
CCFenner committed Aug 29, 2018
1 parent 23b4cd3 commit 4425629
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 53 deletions.
3 changes: 2 additions & 1 deletion src/com/sap/piper/Utils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ def generateSha1Inline(input) {
void pushToSWA(Map parameters, Map config) {
try {
//allow opt-out via configuration
if (!config.collectTelemetryData) {
if (!config?.collectTelemetryData) {
echo "[${parameters.get('step')}] Telemetry Report to SWA disabled!"
return
}

Expand Down
78 changes: 31 additions & 47 deletions test/groovy/CloudFoundryDeployTest.groovy
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!groovy
import groovy.json.JsonSlurperClassic
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand All @@ -15,8 +14,11 @@ import util.JenkinsStepRule
import util.JenkinsWriteFileRule
import util.Rules

import static org.junit.Assert.assertTrue
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertThat

import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.hasEntry
import static org.hamcrest.Matchers.containsString

class CloudFoundryDeployTest extends BasePiperTest {

Expand Down Expand Up @@ -59,10 +61,8 @@ class CloudFoundryDeployTest extends BasePiperTest {
})
}


@Test
void testNoTool() throws Exception {

nullScript.commonPipelineEnvironment.configuration = [
general: [
camSystemRole: 'testRole',
Expand All @@ -86,13 +86,12 @@ class CloudFoundryDeployTest extends BasePiperTest {
deployTool: '',
stageName: 'acceptance',
])

assertTrue(jlr.log.contains('[cloudFoundryDeploy] General parameters: deployTool=, deployType=standard, cfApiEndpoint=https://api.cf.eu10.hana.ondemand.com, cfOrg=testOrg, cfSpace=testSpace, cfCredentialsId=myCreds, deployUser=testUser'))
// asserts
assertThat(jlr.log, containsString('[cloudFoundryDeploy] General parameters: deployTool=, deployType=standard, cfApiEndpoint=https://api.cf.eu10.hana.ondemand.com, cfOrg=testOrg, cfSpace=testSpace, cfCredentialsId=myCreds, deployUser=testUser'))
}

@Test
void testNotAvailableTool() throws Exception {

nullScript.commonPipelineEnvironment.configuration = [
general: [
cfCredentialsId: 'myCreds'
Expand All @@ -115,13 +114,12 @@ class CloudFoundryDeployTest extends BasePiperTest {
deployTool: 'notAvailable',
stageName: 'acceptance'
])

assertTrue(jlr.log.contains('[cloudFoundryDeploy] General parameters: deployTool=notAvailable, deployType=standard, cfApiEndpoint=https://api.cf.eu10.hana.ondemand.com, cfOrg=testOrg, cfSpace=testSpace, cfCredentialsId=myCreds, deployUser=testUser'))
// asserts
assertThat(jlr.log, containsString('[cloudFoundryDeploy] General parameters: deployTool=notAvailable, deployType=standard, cfApiEndpoint=https://api.cf.eu10.hana.ondemand.com, cfOrg=testOrg, cfSpace=testSpace, cfCredentialsId=myCreds, deployUser=testUser'))
}

@Test
void testCfNativeWithAppName() {

jsr.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
Expand All @@ -132,19 +130,16 @@ class CloudFoundryDeployTest extends BasePiperTest {
cfAppName: 'testAppName',
cfManifest: 'test.yml'
])

assertEquals('s4sdk/docker-cf-cli', jedr.dockerParams.dockerImage)
assertEquals('/home/piper', jedr.dockerParams.dockerWorkspace)
assertEquals('200', jedr.dockerParams.dockerEnvVars.STATUS_CODE.toString())


assertTrue(jscr.shell[1].contains('cf login -u "test_cf" -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"'))
assertTrue(jscr.shell[1].contains('cf push "testAppName" -f "test.yml"'))
// asserts
assertThat(jedr.dockerParams, hasEntry('dockerImage', 's4sdk/docker-cf-cli'))
assertThat(jedr.dockerParams, hasEntry('dockerWorkspace', '/home/piper'))
assertThat(jedr.dockerParams.dockerEnvVars, hasEntry('STATUS_CODE', "${200}"))
assertThat(jscr.shell, hasItem(containsString('cf login -u "test_cf" -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"')))
assertThat(jscr.shell, hasItem(containsString('cf push "testAppName" -f "test.yml"')))
}

@Test
void testCfNativeWithAppNameCustomApi() {

jsr.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
Expand All @@ -156,13 +151,12 @@ class CloudFoundryDeployTest extends BasePiperTest {
cfAppName: 'testAppName',
cfManifest: 'test.yml'
])

assertTrue(jscr.shell[1].contains('cf login -u "test_cf" -p \'********\' -a https://customApi -o "testOrg" -s "testSpace"'))
// asserts
assertThat(jscr.shell, hasItem(containsString('cf login -u "test_cf" -p \'********\' -a https://customApi -o "testOrg" -s "testSpace"')))
}

@Test
void testCfNativeWithAppNameCompatible() {

jsr.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
Expand All @@ -175,21 +169,17 @@ class CloudFoundryDeployTest extends BasePiperTest {
manifest: 'test.yml'
]
])

assertEquals('s4sdk/docker-cf-cli', jedr.dockerParams.dockerImage)
assertEquals('/home/piper', jedr.dockerParams.dockerWorkspace)
assertEquals('200', jedr.dockerParams.dockerEnvVars.STATUS_CODE.toString())


assertTrue(jscr.shell[1].contains('cf login -u "test_cf" -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"'))
assertTrue(jscr.shell[1].contains('cf push "testAppName" -f "test.yml"'))
// asserts
assertThat(jedr.dockerParams, hasEntry('dockerImage', 's4sdk/docker-cf-cli'))
assertThat(jedr.dockerParams, hasEntry('dockerWorkspace', '/home/piper'))
assertThat(jedr.dockerParams.dockerEnvVars, hasEntry('STATUS_CODE', "${200}"))
assertThat(jscr.shell, hasItem(containsString('cf login -u "test_cf" -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"')))
assertThat(jscr.shell, hasItem(containsString('cf push "testAppName" -f "test.yml"')))
}

@Test
void testCfNativeAppNameFromManifest() {

helper.registerAllowedMethod('fileExists', [String.class], { s -> return true })

helper.registerAllowedMethod("readYaml", [Map], { Map m ->
if(m.text) {
return new Yaml().load(m.text)
Expand All @@ -211,17 +201,14 @@ class CloudFoundryDeployTest extends BasePiperTest {
cfCredentialsId: 'test_cfCredentialsId',
cfManifest: 'test.yml'
])

assertTrue(jscr.shell[1].contains('cf login -u "test_cf" -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"'))
assertTrue(jscr.shell[1].contains('cf push -f "test.yml"'))

// asserts
assertThat(jscr.shell, hasItem(containsString('cf login -u "test_cf" -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"')))
assertThat(jscr.shell, hasItem(containsString('cf push -f "test.yml"')))
}

@Test
void testCfNativeWithoutAppName() {

helper.registerAllowedMethod('fileExists', [String.class], { s -> return true })

helper.registerAllowedMethod("readYaml", [Map], { Map m ->
if(m.text) {
return new Yaml().load(m.text)
Expand Down Expand Up @@ -250,7 +237,6 @@ class CloudFoundryDeployTest extends BasePiperTest {

@Test
void testMta() {

jsr.step.cloudFoundryDeploy([
script: nullScript,
juStabUtils: utils,
Expand All @@ -260,12 +246,10 @@ class CloudFoundryDeployTest extends BasePiperTest {
deployTool: 'mtaDeployPlugin',
mtaPath: 'target/test.mtar'
])

assertEquals('s4sdk/docker-cf-cli', jedr.dockerParams.dockerImage)
assertEquals('/home/piper', jedr.dockerParams.dockerWorkspace)

assertTrue(jscr.shell[0].contains('cf login -u test_cf -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"'))
assertTrue(jscr.shell[0].contains("cf deploy target/test.mtar -f".toString()))
// asserts
assertThat(jedr.dockerParams, hasEntry('dockerImage', 's4sdk/docker-cf-cli'))
assertThat(jedr.dockerParams, hasEntry('dockerWorkspace', '/home/piper'))
assertThat(jscr.shell, hasItem(containsString('cf login -u test_cf -p \'********\' -a https://api.cf.eu10.hana.ondemand.com -o "testOrg" -s "testSpace"')))
assertThat(jscr.shell, hasItem(containsString('cf deploy target/test.mtar -f')))
}

}
62 changes: 57 additions & 5 deletions test/groovy/com/sap/piper/UtilsTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,36 @@ package com.sap.piper
import org.junit.Rule
import org.junit.Before
import org.junit.Test
import static org.junit.Assert.assertThat
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain

import com.sap.piper.Utils
import static org.hamcrest.Matchers.containsString
import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.is
import static org.hamcrest.Matchers.not

import util.JenkinsLoggingRule
import util.JenkinsShellCallRule
import util.BasePiperTest
import util.Rules

import com.sap.piper.Utils

class UtilsTest {
class UtilsTest extends BasePiperTest {
private ExpectedException thrown = ExpectedException.none()
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
private JenkinsShellCallRule jscr = new JenkinsShellCallRule(this)

@Rule
public ExpectedException thrown = new ExpectedException().none()
public RuleChain rules = Rules
.getCommonRules(this)
.around(thrown)
.around(jscr)
.around(jlr)

private utils = new Utils()
private parameters


@Before
void setup() {

Expand Down Expand Up @@ -45,4 +61,40 @@ class UtilsTest {

assert utils.getMandatoryParameter(parameters, 'test', null) == 'value'
}

@Test
void testSWAReporting() {
utils.env = [BUILD_URL: 'something', JOB_URL: 'nothing']
utils.pushToSWA([step: 'anything'], [collectTelemetryData: true])
// asserts
assertThat(jscr.shell, hasItem(containsString('curl -G -v "https://webanalytics.cfapps.eu10.hana.ondemand.com/tracker/log"')))
assertThat(jscr.shell, hasItem(containsString('action_name=Piper Library OS')))
assertThat(jscr.shell, hasItem(containsString('custom3=anything')))
assertThat(jscr.shell, hasItem(containsString('custom5=`echo -n \'something\' | sha1sum | sed \'s/ -//\'`')))
}

@Test
void testDisabledSWAReporting() {
utils.env = [BUILD_URL: 'something', JOB_URL: 'nothing']
utils.pushToSWA([step: 'anything'], [collectTelemetryData: false])
// asserts
assertThat(jlr.log, containsString('[anything] Telemetry Report to SWA disabled!'))
assertThat(jscr.shell, not(hasItem(containsString('https://webanalytics.cfapps.eu10.hana.ondemand.com'))))
}

@Test
void testImplicitlyDisabledSWAReporting() {
utils.env = [BUILD_URL: 'something', JOB_URL: 'nothing']
utils.pushToSWA([step: 'anything'], null)
// asserts
assertThat(jlr.log, containsString('[anything] Telemetry Report to SWA disabled!'))
}

@Test
void testImplicitlyDisabledSWAReporting2() {
utils.env = [BUILD_URL: 'something', JOB_URL: 'nothing']
utils.pushToSWA([step: 'anything'], [:])
// asserts
assertThat(jlr.log, containsString('[anything] Telemetry Report to SWA disabled!'))
}
}

0 comments on commit 4425629

Please sign in to comment.