import groovy.json.JsonOutput import hudson.init.InitMilestone import hudson.model.BooleanParameterDefinition import hudson.model.BooleanParameterValue import hudson.model.ParametersAction import hudson.model.ParametersDefinitionProperty import hudson.security.FullControlOnceLoggedInAuthorizationStrategy import hudson.security.HudsonPrivateSecurityRealm import hudson.security.ACL import jenkins.model.Jenkins import java.util.concurrent.TimeUnit // Trusted administrator bootstrap, only in a new temporary home. Never log secrets. def j = Jenkins.get() def realm = new HudsonPrivateSecurityRealm(false) realm.createAccount('capacity-test', UUID.randomUUID().toString() + UUID.randomUUID().toString()) j.setSecurityRealm(realm) def authorization = new FullControlOnceLoggedInAuthorizationStrategy() authorization.setAllowAnonymousRead(false) j.setAuthorizationStrategy(authorization) j.setNumExecutors(1) j.setQuietPeriod(0) j.save() def root = new File('/tmp/ssw590') def state = [phase: 'initializing', builds: []] def writeState = { def temporary = new File(root, 'status.next') temporary.text = JsonOutput.toJson(state) java.nio.file.Files.move(temporary.toPath(), new File(root, 'status.json').toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING, java.nio.file.StandardCopyOption.ATOMIC_MOVE) } writeState() Thread.startDaemon('ssw590-disposable-test') { // The trusted bootstrap worker needs the same system identity as the init hook. def securityContext = ACL.as2(ACL.SYSTEM2) try { while (j.getInitLevel() != InitMilestone.COMPLETED) { Thread.sleep(1000) } state.plugins = j.pluginManager.plugins.collect { [id: it.shortName, version: it.version, active: it.isActive()] } state.failedPlugins = j.pluginManager.failedPlugins.collect { it.name } def requested = new File('/opt/ssw590-input/plugins.txt').readLines().findAll { it.trim() } requested.each { entry -> def (id, version) = entry.trim().split(':', 2) def p = j.pluginManager.getPlugin(id) if (p == null || !p.isActive() || p.version != version) { throw new IllegalStateException('Requested plugin not active at expected version: ' + id) } } if (state.failedPlugins || state.plugins.any { !it.active }) { throw new IllegalStateException('Plugin load failure; inspect recorded inventory') } // Access plugin classes only after checking that they loaded. def jobClass = j.pluginManager.uberClassLoader.loadClass('org.jenkinsci.plugins.workflow.job.WorkflowJob') def definitionClass = j.pluginManager.uberClassLoader.loadClass('org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition') def job = j.createProject(jobClass, 'capacity-pricing') job.addProperty(new ParametersDefinitionProperty( new BooleanParameterDefinition('BREAK_CALCULATION', false, 'Disposable failure exercise'))) job.setDefinition(definitionClass.getConstructor(String.class, Boolean.TYPE).newInstance( new File('/opt/ssw590-input/fixtures/Jenkinsfile').text, true)) job.save() state.phase = 'ready' writeState() // Host must observe >=60 seconds of healthy HTTP AFTER plugin checks first. while (!new File(root, 'start-jobs').exists()) { Thread.sleep(1000) } state.phase = 'pipelines' writeState() def cases = [[name: 'pass', broken: false, expected: 'SUCCESS'], [name: 'intentional-failure', broken: true, expected: 'FAILURE'], [name: 'repair', broken: false, expected: 'SUCCESS']] for (testCase in cases) { def future = job.scheduleBuild2(0, new ParametersAction( new BooleanParameterValue('BREAK_CALCULATION', testCase.broken))) if (future == null) { throw new IllegalStateException('Build could not be scheduled') } def build = future.get(180, TimeUnit.SECONDS) // Only these known fixture-job logs are copied, never controller logs. def console = build.getLog(500).join('\n') + '\n' new File(root, testCase.name + '-console.txt').text = console def observed = build.result.toString() state.builds << [name: testCase.name, number: build.number, expected: testCase.expected, actual: observed] writeState() if (observed != testCase.expected) { throw new IllegalStateException('Unexpected fixture build result: ' + testCase.name) } if (!console.contains('Ran 2 tests')) { throw new IllegalStateException('Unit tests did not actually execute') } if (testCase.broken && (!console.contains('13 != 30') || !console.contains('FAILED (failures=1)'))) { throw new IllegalStateException('Failure was not the intended calculation assertion') } } state.phase = 'complete' writeState() } catch (Throwable failure) { state.phase = 'failed' // Fixed harness error text/type; do not dump arbitrary stack traces or secrets. state.errorType = failure.class.name writeState() } finally { securityContext.close() } }