|
| 1 | +// Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +// license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright |
| 4 | +// ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +// the Apache License, Version 2.0 (the "License"); you may |
| 6 | +// not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +import org.junit.Before |
| 19 | +import org.junit.Test |
| 20 | +import static org.junit.Assert.assertFalse |
| 21 | +import static org.junit.Assert.assertEquals |
| 22 | +import static org.junit.Assert.assertTrue |
| 23 | + |
| 24 | +class GetGitMatchingGroupStepTests extends ApmBasePipelineTest { |
| 25 | + String scriptName = 'vars/getGitMatchingGroup.groovy' |
| 26 | + |
| 27 | + def realData = '''CHANGELOG.next.asciidoc |
| 28 | +metricbeat/docs/modules/zookeeper.asciidoc |
| 29 | +metricbeat/docs/modules/zookeeper/connection.asciidoc |
| 30 | +metricbeat/docs/modules_list.asciidoc |
| 31 | +metricbeat/module/zookeeper/_meta/docs.asciidoc |
| 32 | +metricbeat/module/zookeeper/connection/_meta/docs.asciidoc |
| 33 | +metricbeat/module/zookeeper/connection/_meta/fields.yml |
| 34 | +metricbeat/module/zookeeper/connection/connection.go |
| 35 | +metricbeat/module/zookeeper/fields.go |
| 36 | +metricbeat/module/zookeeper/mntr/_meta/docs.asciidoc |
| 37 | +metricbeat/module/zookeeper/server/_meta/docs.asciidoc'''.stripMargin().stripIndent() |
| 38 | + |
| 39 | + @Override |
| 40 | + @Before |
| 41 | + void setUp() throws Exception { |
| 42 | + super.setUp() |
| 43 | + env.CHANGE_TARGET = 'foo' |
| 44 | + env.GIT_BASE_COMMIT = 'bar' |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void test_without_pattern_parameter() throws Exception { |
| 49 | + def script = loadScript(scriptName) |
| 50 | + try { |
| 51 | + script.call() |
| 52 | + } catch(e){ |
| 53 | + //NOOP |
| 54 | + } |
| 55 | + printCallStack() |
| 56 | + assertTrue(assertMethodCallContainsPattern('error', 'Missing pattern argument.')) |
| 57 | + assertJobStatusFailure() |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void test_without_env_variables() throws Exception { |
| 62 | + def script = loadScript(scriptName) |
| 63 | + def result = true |
| 64 | + env.remove('CHANGE_TARGET') |
| 65 | + def module = script.call(pattern: 'foo') |
| 66 | + printCallStack() |
| 67 | + assertEquals('', module) |
| 68 | + assertTrue(assertMethodCallContainsPattern('log', 'CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.')) |
| 69 | + assertJobStatusSuccess() |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void test_simple_match() throws Exception { |
| 74 | + def script = loadScript(scriptName) |
| 75 | + def changeset = 'foo/bar/file.txt' |
| 76 | + helper.registerAllowedMethod('readFile', [String.class], { return changeset }) |
| 77 | + def module = script.call(pattern: '([^\\/]+)\\/.*') |
| 78 | + assertEquals('foo', module) |
| 79 | + assertJobStatusSuccess() |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void test_simple_match_with_previous_commit_env_variable() throws Exception { |
| 84 | + env.GIT_PREVIOUS_COMMIT = "foo-1" |
| 85 | + env.remove('CHANGE_TARGET') |
| 86 | + def script = loadScript(scriptName) |
| 87 | + def changeset = 'foo/bar/file.txt' |
| 88 | + helper.registerAllowedMethod('readFile', [String.class], { return changeset }) |
| 89 | + def module = script.call(pattern: '([^\\/]+)\\/.*') |
| 90 | + printCallStack() |
| 91 | + assertEquals('foo', module) |
| 92 | + assertJobStatusSuccess() |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void test_multiple_match() throws Exception { |
| 97 | + def script = loadScript(scriptName) |
| 98 | + def changeset = '''foo/bar/file.txt |
| 99 | +foo/bar/subfolder'''.stripMargin().stripIndent() |
| 100 | + helper.registerAllowedMethod('readFile', [String.class], { return changeset }) |
| 101 | + def module = script.call(pattern: '([^\\/]+)\\/.*') |
| 102 | + assertEquals('foo', module) |
| 103 | + assertJobStatusSuccess() |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void test_multiple_match_2() throws Exception { |
| 108 | + def script = loadScript(scriptName) |
| 109 | + def changeset = '''filebeat/README.md |
| 110 | +filebeat/Dockerfile |
| 111 | +filebeat/docs/faq.asciidoc |
| 112 | +filebeat/autodiscover/builder/hints/config.go'''.stripMargin().stripIndent() |
| 113 | + helper.registerAllowedMethod('readFile', [String.class], { return changeset }) |
| 114 | + def module = script.call(pattern: '([^\\/]+)\\/.*') |
| 115 | + assertEquals('filebeat', module) |
| 116 | + assertJobStatusSuccess() |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + void test_multiple_without_match() throws Exception { |
| 121 | + def script = loadScript(scriptName) |
| 122 | + def changeset = '''foo/bar/file.txt |
| 123 | +bar/foo/subfolder'''.stripMargin().stripIndent() |
| 124 | + helper.registerAllowedMethod('readFile', [String.class], { return changeset }) |
| 125 | + def module = script.call(pattern: '([^\\/]+)\\/.*') |
| 126 | + assertEquals('', module) |
| 127 | + assertTrue(assertMethodCallContainsPattern('log', 'not found')) |
| 128 | + assertJobStatusSuccess() |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void test_simple_unmatch() throws Exception { |
| 133 | + def script = loadScript(scriptName) |
| 134 | + def changeset = 'foo/anotherfolder/file.txt' |
| 135 | + helper.registerAllowedMethod('readFile', [String.class], { return changeset }) |
| 136 | + def module = script.call(pattern: '^unknown.txt') |
| 137 | + printCallStack() |
| 138 | + assertEquals('', module) |
| 139 | + assertTrue(assertMethodCallContainsPattern('log', 'not found with regex ^unknown.txt')) |
| 140 | + assertJobStatusSuccess() |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void test_windows() throws Exception { |
| 145 | + def script = loadScript(scriptName) |
| 146 | + helper.registerAllowedMethod('isUnix', [], { false }) |
| 147 | + try { |
| 148 | + script.call() |
| 149 | + } catch(e){ |
| 150 | + //NOOP |
| 151 | + } |
| 152 | + printCallStack() |
| 153 | + assertTrue(assertMethodCallContainsPattern('error', 'windows is not supported yet.')) |
| 154 | + assertJobStatusFailure() |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void test_without_change_request_env_variable() throws Exception { |
| 159 | + env.GIT_PREVIOUS_COMMIT = "foo-1" |
| 160 | + env.remove('CHANGE_TARGET') |
| 161 | + def script = loadScript(scriptName) |
| 162 | + def changeset = 'foo/bar/file.txt' |
| 163 | + helper.registerAllowedMethod('readFile', [String.class], { return changeset }) |
| 164 | + helper.registerAllowedMethod('sh', [Map.class], { m -> |
| 165 | + assertFalse(m.script.contains('origin/')) |
| 166 | + }) |
| 167 | + def module = script.call(pattern: '([^\\/]+)\\/.*') |
| 168 | + printCallStack() |
| 169 | + assertEquals('foo', module) |
| 170 | + assertJobStatusSuccess() |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void test_with_empty_change_target_env_variable() throws Exception { |
| 175 | + env.CHANGE_TARGET = " " |
| 176 | + def script = loadScript(scriptName) |
| 177 | + def changeset = 'foo/anotherfolder/file.txt' |
| 178 | + helper.registerAllowedMethod('readFile', [String.class], { return changeset }) |
| 179 | + helper.registerAllowedMethod('sh', [Map.class], { m -> |
| 180 | + assertFalse(m.script.contains('origin/')) |
| 181 | + }) |
| 182 | + def module = script.call(pattern: 'foo') |
| 183 | + printCallStack() |
| 184 | + assertEquals('', module) |
| 185 | + assertJobStatusSuccess() |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + void test_with_from_parameter() throws Exception { |
| 190 | + def script = loadScript(scriptName) |
| 191 | + def changeset = 'foo/anotherfolder/file.txt' |
| 192 | + helper.registerAllowedMethod('readFile', [String.class], { return changeset }) |
| 193 | + def module = script.call(pattern: '([^\\/]+)\\/.*', from: 'something') |
| 194 | + printCallStack() |
| 195 | + assertEquals('foo', module) |
| 196 | + assertTrue(assertMethodCallContainsPattern('sh', 'something...bar')) |
| 197 | + assertJobStatusSuccess() |
| 198 | + } |
| 199 | + |
| 200 | + @Test |
| 201 | + void test_with_from_and_to_parameters() throws Exception { |
| 202 | + def script = loadScript(scriptName) |
| 203 | + def changeset = 'foo/bar/file.txt' |
| 204 | + helper.registerAllowedMethod('readFile', [String.class], { return changeset }) |
| 205 | + helper.registerAllowedMethod('sh', [Map.class], { m -> |
| 206 | + assertFalse(m.script.contains('origin/')) |
| 207 | + }) |
| 208 | + def module = script.call(pattern: '([^\\/]+)\\/.*', from: 'something', to: 'else') |
| 209 | + printCallStack() |
| 210 | + assertEquals('foo', module) |
| 211 | + assertTrue(assertMethodCallContainsPattern('sh', 'something...else')) |
| 212 | + assertJobStatusSuccess() |
| 213 | + } |
| 214 | + |
| 215 | + @Test |
| 216 | + void test_with_empty_values_for_from_and_to_parameters() throws Exception { |
| 217 | + def script = loadScript(scriptName) |
| 218 | + def module = script.call(pattern: '^foo/.*/file.txt', from: '', to: '') |
| 219 | + printCallStack() |
| 220 | + assertEquals('', module) |
| 221 | + assertTrue(assertMethodCallContainsPattern('log', 'CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.')) |
| 222 | + assertJobStatusSuccess() |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + void test_with_empty_value_for_to_parameter() throws Exception { |
| 227 | + def script = loadScript(scriptName) |
| 228 | + def module = script.call(pattern: '^foo/.*/file.txt', to: '') |
| 229 | + printCallStack() |
| 230 | + assertEquals('', module) |
| 231 | + assertTrue(assertMethodCallContainsPattern('log', 'CHANGE_TARGET or GIT_PREVIOUS_COMMIT and GIT_BASE_COMMIT env variables are required to evaluate the changes.')) |
| 232 | + assertJobStatusSuccess() |
| 233 | + } |
| 234 | + |
| 235 | + @Test |
| 236 | + void test_multiple_match_with_real_data_with_exclude() throws Exception { |
| 237 | + def script = loadScript(scriptName) |
| 238 | + helper.registerAllowedMethod('readFile', [String.class], { return realData }) |
| 239 | + def module = script.call(pattern: '.*\\/module\\/([^\\/]+)\\/.*', exclude: '(.*\\/docs\\/.*|.*\\.asciidoc)' ) |
| 240 | + assertEquals('zookeeper', module) |
| 241 | + assertJobStatusSuccess() |
| 242 | + } |
| 243 | + |
| 244 | + @Test |
| 245 | + void test_multiple_match_with_real_data_without_exclude() throws Exception { |
| 246 | + def script = loadScript(scriptName) |
| 247 | + helper.registerAllowedMethod('readFile', [String.class], { return realData }) |
| 248 | + def module = script.call(pattern: '.*\\/module\\/([^\\/]+)\\/.*') |
| 249 | + assertEquals('', module) |
| 250 | + assertJobStatusSuccess() |
| 251 | + } |
| 252 | + |
| 253 | + @Test |
| 254 | + void test_is_excluded() throws Exception { |
| 255 | + def script = loadScript(scriptName) |
| 256 | + assertFalse(script.isExcluded('', '')) |
| 257 | + assertTrue(script.isExcluded('metricbeat/docs/modules/zookeeper.asciidoc', '(.*\\/docs\\/.*|.*\\.asciidoc)')) |
| 258 | + assertFalse(script.isExcluded('metricbeat/zookeeper.asciido', '(.*\\/docs\\/.*|.*\\.asciidoc)')) |
| 259 | + } |
| 260 | +} |
0 commit comments