Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
2.0 pipeline steps for beats (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1v authored Sep 14, 2020
1 parent ff8871a commit 5d6a10d
Show file tree
Hide file tree
Showing 15 changed files with 1,224 additions and 1 deletion.
6 changes: 5 additions & 1 deletion local/configs/jenkins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ unclassified:
globalConfigName: username
globalConfigEmail: username@example.com
jobs:
- file: "/var/pipeline-library/src/test/resources/folders/it.dsl"
- file: "/var/pipeline-library/src/test/resources/folders/beats.dsl"
- file: "/var/pipeline-library/src/test/resources/folders/getBuildInfoJsonFiles.dsl"
- file: "/var/pipeline-library/src/test/resources/folders/it.dsl"
- file: "/var/pipeline-library/src/test/resources/folders/timeout.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/beats/beatsStages.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/beats/beatsWhen.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/cancelPreviousRunningBuilds.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/cmd.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/dockerLogin.dsl"
Expand Down
35 changes: 35 additions & 0 deletions src/co/elastic/beats/BeatsFunction.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package co.elastic.beats

/**
Base class to implement specific functions for the beats 2.0 pipeline.
*/
class BeatsFunction {
/** object to access to pipeline steps */
public steps

public BeatsFunction(Map args){
this.steps = args.steps
}

/**
This method should be overwritten by the target pipeline.
*/
protected run(Map args){ }
}
1 change: 1 addition & 0 deletions src/test/groovy/ApmBasePipelineTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ class ApmBasePipelineTest extends DeclarativePipelineTest {
return script.call(m)
})
helper.registerAllowedMethod('base64encode', [Map.class], { return "YWRtaW46YWRtaW4xMjMK" })
helper.registerAllowedMethod('beatsWhen', [Map.class], null)
helper.registerAllowedMethod('cancelPreviousRunningBuilds', [Map.class], null)
helper.registerAllowedMethod('cmd', [Map.class], { m ->
def script = loadScript('vars/cmd.groovy')
Expand Down
183 changes: 183 additions & 0 deletions src/test/groovy/BeatsStagesStepTests.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import org.junit.Before
import org.junit.After
import org.junit.Test
import static org.junit.Assert.assertFalse
import static org.junit.Assert.assertTrue
import co.elastic.mock.beats.RunCommand

class BeatsStagesStepTests extends ApmBasePipelineTest {
String scriptName = 'vars/beatsStages.groovy'

@Override
@Before
void setUp() throws Exception {
super.setUp()
}

@Test
void test_with_no_data() throws Exception {
def script = loadScript(scriptName)
try {
script.call()
} catch (e) {
// NOOP
}
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'project param is required'))
assertJobStatusFailure()
}

@Test
void test_with_no_project() throws Exception {
def script = loadScript(scriptName)
try {
script.call(project: 'foo')
} catch (e) {
// NOOP
}
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'content param is required'))
assertJobStatusFailure()
}

@Test
void test_with_no_platform() throws Exception {
def script = loadScript(scriptName)
try {
script.call(project: 'foo', content: [:], function: new RunCommand(steps: this))
} catch (e) {
// NOOP
}
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'platform entry in the content is required'))
assertJobStatusFailure()
}

@Test
void test_with_no_function() throws Exception {
def script = loadScript(scriptName)
try {
script.call(project: 'foo', content: [:])
} catch (e) {
// NOOP
}
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'function param is required'))
assertJobStatusFailure()
}

@Test
void test_simple() throws Exception {
def script = loadScript(scriptName)
def ret = script.call(project: 'foo', content: [
"platform" : [ "linux && ubuntu-16" ],
"stages": [
"simple" : [
"mage" : [ "foo" ]
]
]
], function: new RunCommand(steps: this))
printCallStack()
assertTrue(ret.size() == 1)
assertTrue(assertMethodCallContainsPattern('log', 'stage: foo-simple'))
assertJobStatusSuccess()
}

@Test
void test_multiple() throws Exception {
def script = loadScript(scriptName)
def ret = script.call(project: 'foo', content: [
"platform" : [ "linux && ubuntu-16" ],
"stages": [
"simple" : [
"make" : [ "foo" ]
],
"multi" : [
"mage" : [ "foo" ],
"platforms" : [ 'windows-2019', 'windows-2016' ]
]
]
], function: new RunCommand(steps: this))
printCallStack()
assertTrue(ret.size() == 3)
assertTrue(assertMethodCallContainsPattern('log', 'stage: foo-simple'))
assertTrue(assertMethodCallContainsPattern('log', 'stage: foo-multi-windows-2019'))
assertTrue(assertMethodCallContainsPattern('log', 'stage: foo-multi-windows-2016'))
assertJobStatusSuccess()
}

@Test
void test_multiple_when_without_match() throws Exception {
def script = loadScript(scriptName)
helper.registerAllowedMethod('beatsWhen', [Map.class], {return false})
def ret = script.call(project: 'foo', content: [
"platform" : [ "linux && ubuntu-16" ],
"stages": [
"simple" : [
"make" : [ "foo" ]
],
"multi" : [
"make" : [ "foo" ],
"platforms" : [ 'windows-2019' ]
],
"multi-when" : [
"mage" : [ "foo" ],
"platforms" : [ 'windows-2016' ],
"when" : [
"comments" : [ "/test auditbeat for windows" ]
]
]
]
], function: new RunCommand(steps: this))
printCallStack()
assertTrue(ret.size() == 2)
assertFalse(assertMethodCallContainsPattern('log', 'stage: foo-multi-when'))
assertJobStatusSuccess()
}

@Test
void test_multiple_when_with_match() throws Exception {
def script = loadScript(scriptName)
helper.registerAllowedMethod('beatsWhen', [Map.class], { return true })
def ret = script.call(project: 'foo', content: [
"platform" : [ "linux && ubuntu-16" ],
"stages": [
"simple" : [
"make" : [ "foo" ]
],
"multi" : [
"mage" : [ "foo" ],
"platforms" : [ 'windows-2019' ]
],
"multi-when" : [
"mage" : [ "foo" ],
"platforms" : [ 'windows-2016' ],
"when" : [
"comments" : [ "/test auditbeat for windows" ]
]
]
]
], function: new RunCommand(steps: this))
printCallStack()
assertTrue(ret.size() == 3)
assertTrue(assertMethodCallContainsPattern('log', 'stage: foo-multi-when'))
assertJobStatusSuccess()
}
}
Loading

0 comments on commit 5d6a10d

Please sign in to comment.