Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions local/configs/jenkins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ jobs:
- file: "/var/pipeline-library/src/test/resources/jobs/cancelPreviousRunningBuilds.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/dockerLogin.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/downstream.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/getBeatsModule.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/getBuildInfoJsonFiles/abort.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/getBuildInfoJsonFiles/connectionRefused.dsl"
- file: "/var/pipeline-library/src/test/resources/jobs/getBuildInfoJsonFiles/error.dsl"
Expand Down
260 changes: 260 additions & 0 deletions src/test/groovy/GetBeatsModuleStepTests.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
// 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.Test
import static org.junit.Assert.assertFalse
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue

class GetBeatsModuleStepTests extends ApmBasePipelineTest {
String scriptName = 'vars/getBeatsModule.groovy'

def realData = '''CHANGELOG.next.asciidoc
metricbeat/docs/modules/zookeeper.asciidoc
metricbeat/docs/modules/zookeeper/connection.asciidoc
metricbeat/docs/modules_list.asciidoc
metricbeat/module/zookeeper/_meta/docs.asciidoc
metricbeat/module/zookeeper/connection/_meta/docs.asciidoc
metricbeat/module/zookeeper/connection/_meta/fields.yml
metricbeat/module/zookeeper/connection/connection.go
metricbeat/module/zookeeper/fields.go
metricbeat/module/zookeeper/mntr/_meta/docs.asciidoc
metricbeat/module/zookeeper/server/_meta/docs.asciidoc'''.stripMargin().stripIndent()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changeset from elastic/beats#17043


@Override
@Before
void setUp() throws Exception {
super.setUp()
env.CHANGE_TARGET = 'foo'
env.GIT_BASE_COMMIT = 'bar'
}

@Test
void test_without_pattern_parameter() throws Exception {
def script = loadScript(scriptName)
try {
script.call()
} catch(e){
//NOOP
}
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'getBeatsModule: Missing pattern argument.'))
assertJobStatusFailure()
}

@Test
void test_without_env_variables() throws Exception {
def script = loadScript(scriptName)
def result = true
env.remove('CHANGE_TARGET')
def module = script.call(pattern: 'foo')
printCallStack()
assertEquals('', module)
assertTrue(assertMethodCallContainsPattern('log', 'getBeatsModule: CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.'))
assertJobStatusSuccess()
}

@Test
void test_simple_match() throws Exception {
def script = loadScript(scriptName)
def changeset = 'foo/bar/file.txt'
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
def module = script.call(pattern: '([^\\/]+)\\/.*')
assertEquals('foo', module)
assertJobStatusSuccess()
}

@Test
void test_simple_match_with_previous_commit_env_variable() throws Exception {
env.GIT_PREVIOUS_COMMIT = "foo-1"
env.remove('CHANGE_TARGET')
def script = loadScript(scriptName)
def changeset = 'foo/bar/file.txt'
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
def module = script.call(pattern: '([^\\/]+)\\/.*')
printCallStack()
assertEquals('foo', module)
assertJobStatusSuccess()
}

@Test
void test_multiple_match() throws Exception {
def script = loadScript(scriptName)
def changeset = '''foo/bar/file.txt
foo/bar/subfolder'''.stripMargin().stripIndent()
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
def module = script.call(pattern: '([^\\/]+)\\/.*')
assertEquals('foo', module)
assertJobStatusSuccess()
}

@Test
void test_multiple_match_2() throws Exception {
def script = loadScript(scriptName)
def changeset = '''filebeat/README.md
filebeat/Dockerfile
filebeat/docs/faq.asciidoc
filebeat/autodiscover/builder/hints/config.go'''.stripMargin().stripIndent()
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
def module = script.call(pattern: '([^\\/]+)\\/.*')
assertEquals('filebeat', module)
assertJobStatusSuccess()
}

@Test
void test_multiple_without_match() throws Exception {
def script = loadScript(scriptName)
def changeset = '''foo/bar/file.txt
bar/foo/subfolder'''.stripMargin().stripIndent()
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
def module = script.call(pattern: '([^\\/]+)\\/.*')
assertEquals('', module)
assertTrue(assertMethodCallContainsPattern('log', 'getBeatsModule: not found'))
assertJobStatusSuccess()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the regexp return 2 matches

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as long as they don't have a folder name in common there is no match.


@Test
void test_simple_unmatch() throws Exception {
def script = loadScript(scriptName)
def changeset = 'foo/anotherfolder/file.txt'
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
def module = script.call(pattern: '^unknown.txt')
printCallStack()
assertEquals('', module)
assertTrue(assertMethodCallContainsPattern('log', 'getBeatsModule: not found with regex ^unknown.txt'))
assertJobStatusSuccess()
}

@Test
void test_windows() throws Exception {
def script = loadScript(scriptName)
helper.registerAllowedMethod('isUnix', [], { false })
try {
script.call()
} catch(e){
//NOOP
}
printCallStack()
assertTrue(assertMethodCallContainsPattern('error', 'getBeatsModule: windows is not supported yet.'))
assertJobStatusFailure()
}

@Test
void test_without_change_request_env_variable() throws Exception {
env.GIT_PREVIOUS_COMMIT = "foo-1"
env.remove('CHANGE_TARGET')
def script = loadScript(scriptName)
def changeset = 'foo/bar/file.txt'
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
helper.registerAllowedMethod('sh', [Map.class], { m ->
assertFalse(m.script.contains('origin/'))
})
def module = script.call(pattern: '([^\\/]+)\\/.*')
printCallStack()
assertEquals('foo', module)
assertJobStatusSuccess()
}

@Test
void test_with_empty_change_target_env_variable() throws Exception {
env.CHANGE_TARGET = " "
def script = loadScript(scriptName)
def changeset = 'foo/anotherfolder/file.txt'
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
helper.registerAllowedMethod('sh', [Map.class], { m ->
assertFalse(m.script.contains('origin/'))
})
def module = script.call(pattern: 'foo')
printCallStack()
assertEquals('', module)
assertJobStatusSuccess()
}

@Test
void test_with_from_parameter() throws Exception {
def script = loadScript(scriptName)
def changeset = 'foo/anotherfolder/file.txt'
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
def module = script.call(pattern: '([^\\/]+)\\/.*', from: 'something')
printCallStack()
assertEquals('foo', module)
assertTrue(assertMethodCallContainsPattern('sh', 'something...bar'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bar confused me until I realized that it is GIT_BASE_COMIT

assertJobStatusSuccess()
}

@Test
void test_with_from_and_to_parameters() throws Exception {
def script = loadScript(scriptName)
def changeset = 'foo/bar/file.txt'
helper.registerAllowedMethod('readFile', [String.class], { return changeset })
helper.registerAllowedMethod('sh', [Map.class], { m ->
assertFalse(m.script.contains('origin/'))
})
def module = script.call(pattern: '([^\\/]+)\\/.*', from: 'something', to: 'else')
printCallStack()
assertEquals('foo', module)
assertTrue(assertMethodCallContainsPattern('sh', 'something...else'))
assertJobStatusSuccess()
}

@Test
void test_with_empty_values_for_from_and_to_parameters() throws Exception {
def script = loadScript(scriptName)
def module = script.call(pattern: '^foo/.*/file.txt', from: '', to: '')
printCallStack()
assertEquals('', module)
assertTrue(assertMethodCallContainsPattern('log', 'getBeatsModule: CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.'))
assertJobStatusSuccess()
}

@Test
void test_with_empty_value_for_to_parameter() throws Exception {
def script = loadScript(scriptName)
def module = script.call(pattern: '^foo/.*/file.txt', to: '')
printCallStack()
assertEquals('', module)
assertTrue(assertMethodCallContainsPattern('log', 'getBeatsModule: CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.'))
assertJobStatusSuccess()
}

@Test
void test_multiple_match_with_real_data_with_exclude() throws Exception {
def script = loadScript(scriptName)
helper.registerAllowedMethod('readFile', [String.class], { return realData })
def module = script.call(pattern: '.*\\/module\\/([^\\/]+)\\/.*', exclude: '(.*\\/docs\\/.*|.*\\.asciidoc)' )
assertEquals('zookeeper', module)
assertJobStatusSuccess()
}

@Test
void test_multiple_match_with_real_data_without_exclude() throws Exception {
def script = loadScript(scriptName)
helper.registerAllowedMethod('readFile', [String.class], { return realData })
def module = script.call(pattern: '.*\\/module\\/([^\\/]+)\\/.*')
assertEquals('', module)
assertJobStatusSuccess()
}

@Test
void test_is_excluded() throws Exception {
def script = loadScript(scriptName)
assertFalse(script.isExcluded('', ''))
assertTrue(script.isExcluded('metricbeat/docs/modules/zookeeper.asciidoc', '(.*\\/docs\\/.*|.*\\.asciidoc)'))
assertFalse(script.isExcluded('metricbeat/zookeeper.asciido', '(.*\\/docs\\/.*|.*\\.asciidoc)'))
}
}
69 changes: 69 additions & 0 deletions src/test/resources/jobs/getBeatsModule.dsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
NAME = 'it/getBeatsModule'
DSL = '''pipeline {
agent any
environment {
PIPELINE_LOG_LEVEL = 'DEBUG'
}
stages {
stage('checkout') {
steps {
gitCheckout(credentialsId: '2a9602aa-ab9f-4e52-baf3-b71ca88469c7-UserAndToken',
repo: 'https://github.com/elastic/apm-pipeline-library.git',
branch: 'master',
basedir: 'sub-folder',
shallow: false)
}
}
stage('match') {
steps {
dir('sub-folder') {
script {
def module = getBeatsModule(pattern: '^\\\\.ci\\\\/([^\\\\/]+)\\\\/.*',
from: '347185bd7e2b402ba8f6befa5ef4428ad417fbbc',
to: '4d9fc25d258622c767ec4d38df38520647cc7dda')
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ChangeSet from #516

whenFalse(module.equals('jobs')){
error("Expected module name 'jobs'")
}
}
}
}
}
stage('no_match') {
steps {
dir('sub-folder') {
script {
def module = getBeatsModule(pattern: '^\\\\ci\\\\/([^\\\\/]+)\\\\/.*',
from: '347185bd7e2b402ba8f6befa5ef4428ad417fbbc',
to: '4d9fc25d258622c767ec4d38df38520647cc7dda')
whenFalse(module.equals('')){
error("Expected module name ''")
}
}
}
}
}
stage('match_with_exclude') {
steps {
dir('sub-folder') {
script {
def module = getBeatsModule(pattern: '^test-infra\\\\/([^\\\\/]+)\\\\/.*',
from: 'b0de59d0ec1e2ae52103a238a2e9cb5b0d7fd9b8',
to: '641fd600836abafa51def05260d63fab6eed4707',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changeset from #524

exclude: '^resources.*')
whenFalse(module.equals('apm-ci')){
error("Expected module name 'apm-ci'")
}
}
}
}
}
}
}'''

pipelineJob(NAME) {
definition {
cps {
script(DSL.stripIndent())
}
}
}
Loading