This repository has been archived by the owner on Oct 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: metricbeat step * fix:tests * feat: use resources for scripts and configuration files * fix: rever change on testMissingArgument * docs: update REAMDE
- Loading branch information
1 parent
cf48f5d
commit 103d530
Showing
11 changed files
with
517 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
metricbeat.modules: | ||
- module: system | ||
metricsets: | ||
- cpu # CPU usage | ||
- load # CPU load averages | ||
- memory # Memory usage | ||
- network # Network IO | ||
- process # Per process metrics | ||
- process_summary # Process summary | ||
- uptime # System Uptime | ||
- socket_summary # Socket summary | ||
#- core # Per CPU core usage | ||
#- diskio # Disk IO | ||
#- filesystem # File system usage for each mountpoint | ||
#- fsstat # File system summary metrics | ||
#- raid # Raid | ||
#- socket # Sockets and connection info (linux only) | ||
#- service # systemd service information | ||
enabled: true | ||
period: 10s | ||
processes: ['.*'] | ||
|
||
# Configure the metric types that are included by these metricsets. | ||
cpu.metrics: ["percentages", "normalized_percentages"] # The other available option is ticks. | ||
core.metrics: ["percentages"] # The other available option is ticks. | ||
|
||
processors: | ||
- add_host_metadata: ~ | ||
- add_cloud_metadata: ~ | ||
- add_docker_metadata: ~ | ||
- add_kubernetes_metadata: ~ | ||
output.elasticsearch: | ||
hosts: ["${ES_URL}"] | ||
username: "${ES_USERNAME}" | ||
password: "${ES_PASSWORD}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env bash | ||
set -eu | ||
docker run \ | ||
--detach \ | ||
-v "${CONFIG_PATH}:/usr/share/metricbeat/metricbeat.yml" \ | ||
-u 0:0 \ | ||
--mount type=bind,source=/proc,target=/hostfs/proc,readonly \ | ||
--mount type=bind,source=/sys/fs/cgroup,target=/hostfs/sys/fs/cgroup,readonly \ | ||
--mount type=bind,source=/,target=/hostfs,readonly \ | ||
--net=host \ | ||
-e ES_URL="${ES_URL}" \ | ||
-e ES_USERNAME="${ES_USERNAME}" \ | ||
-e ES_PASSWORD="${ES_PASSWORD}" \ | ||
"${DOCKER_IMAGE}" \ | ||
--strict.perms=false \ | ||
-environment container \ | ||
-E http.enabled=true \ | ||
-e -system.hostfs=/hostfs > docker_id |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
// 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.assertTrue | ||
import static org.junit.Assert.assertFalse | ||
|
||
class MetricbeatStepTests extends ApmBasePipelineTest { | ||
// test resources file uses this value as filename | ||
String nodeName = 'worker-0676d01d9601f8191' | ||
String jsonConfig = "metricbeat_container_" + nodeName + ".json" | ||
String resources = "target/test-classes" | ||
|
||
@Override | ||
@Before | ||
void setUp() throws Exception { | ||
super.setUp() | ||
script = loadScript('vars/metricbeat.groovy') | ||
env.NODE_NAME = nodeName | ||
env.WORKSPACE = "metricbeatTest" | ||
helper.registerAllowedMethod('writeFile', [Map.class], { m -> | ||
(new File("${resources}/${m.file}")).withWriter('UTF-8') { writer -> | ||
writer.write(m.text) | ||
} | ||
}) | ||
helper.registerAllowedMethod('writeJSON', [Map.class], { m -> | ||
def script = loadScript('vars/toJSON.groovy') | ||
def json = script.call(m.json) | ||
(new File("${resources}/${m.file}")).withWriter('UTF-8') { writer -> | ||
writer.write(json.toString()) | ||
} | ||
}) | ||
helper.registerAllowedMethod('readJSON', [Map.class], { m -> | ||
def jsonSlurper = new groovy.json.JsonSlurperClassic() | ||
File f = new File("${resources}/${m.file}") | ||
jsonText = f.getText() | ||
return jsonSlurper.parseText(jsonText) | ||
}) | ||
helper.registerAllowedMethod('sh', [Map.class], { m -> | ||
def ret = "OK" | ||
if(m.script.contains('docker run')){ | ||
ret = 'fooID' | ||
} | ||
return ret | ||
}) | ||
helper.registerAllowedMethod('pwd', [], { 'metricbeatTest' }) | ||
helper.registerAllowedMethod('isBuildFailure', [], { false }) | ||
helper.registerAllowedMethod('readFile', [Map.class], { 'fooID' }) | ||
} | ||
|
||
@Test | ||
void test() throws Exception { | ||
helper.registerAllowedMethod('fileExists', [String.class], { false }) | ||
printCallStack(){ | ||
script.call(es_secret: 'foo') | ||
} | ||
assertTrue(assertMethodCallContainsPattern('writeFile', "file=metricbeatTest/metricbeat_conf.yml")) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'run_metricbeat.sh')) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void testNoSecret() throws Exception { | ||
helper.registerAllowedMethod('fileExists', [String.class], { false }) | ||
testError('metricbeat: The parameter es_secret is mandatory.'){ | ||
script.call() | ||
} | ||
} | ||
|
||
@Test | ||
void testClosure() throws Exception { | ||
def id = "fooID" | ||
def workdir = "metricbeatTest_1" | ||
def config = "bar.xml" | ||
def image = "foo:latest" | ||
|
||
helper.registerAllowedMethod('fileExists', [String.class], { f -> | ||
if(f == "${workdir}/${config}"){ | ||
return false | ||
} else { | ||
return true | ||
} | ||
}) | ||
|
||
printCallStack(){ | ||
script.call( | ||
es_secret: 'foo', | ||
config: config, | ||
image: image, | ||
workdir: workdir, | ||
timeout: "30", | ||
){ | ||
print("OK") | ||
} | ||
} | ||
|
||
assertTrue(assertMethodCallContainsPattern('writeFile', "file=${workdir}/${config}")) | ||
assertTrue(assertMethodCallContainsPattern('readJSON', "file=${workdir}/${jsonConfig}")) | ||
assertTrue(assertMethodCallContainsPattern('sh', "docker stop --time 30 ${id}")) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test(expected = Exception.class) | ||
void testClosureError() throws Exception { | ||
def id = "fooID" | ||
def output = "foo.log" | ||
def workdir = "metricbeatTest_1" | ||
def config = "bar.xml" | ||
def image = "foo:latest" | ||
|
||
helper.registerAllowedMethod('fileExists', [String.class], { f -> | ||
if(f == "${workdir}/${config}"){ | ||
return false | ||
} else { | ||
return true | ||
} | ||
}) | ||
|
||
try { | ||
script.call( | ||
es_secret: 'foo', | ||
config: config, | ||
image: image, | ||
workdir: workdir, | ||
timeout: "30", | ||
){ | ||
throw new Exception('Ooops!!') | ||
} | ||
} finally { | ||
printCallStack() | ||
assertTrue(assertMethodCallContainsPattern('writeFile', "file=${workdir}/${config}")) | ||
assertTrue(assertMethodCallContainsPattern('readJSON', "file=${workdir}/${jsonConfig}")) | ||
assertTrue(assertMethodCallContainsPattern('sh', 'run_metricbeat.sh')) | ||
assertTrue(assertMethodCallContainsPattern('sh', "docker stop --time 30 ${id}")) | ||
} | ||
} | ||
|
||
@Test | ||
void testConfigurationExists() throws Exception { | ||
printCallStack(){ | ||
script.call(es_secret: 'foo') | ||
} | ||
assertTrue(assertMethodCallContainsPattern('sh', 'run_metricbeat.sh')) | ||
assertFalse(assertMethodCallContainsPattern('writeFile', 'file=metricbeat_conf.yml')) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void testStop() throws Exception { | ||
def id = "fooID" | ||
def output = "foo.log" | ||
def workdir = "metricbeatTest" | ||
|
||
printCallStack(){ | ||
script.stop( | ||
workdir: workdir, | ||
) | ||
} | ||
assertTrue(assertMethodCallContainsPattern('readJSON', "file=${workdir}/${jsonConfig}")) | ||
assertTrue(assertMethodCallContainsPattern('sh', "docker stop --time 30 ${id}")) | ||
assertJobStatusSuccess() | ||
} | ||
|
||
@Test | ||
void testConfigFileNotExists() throws Exception { | ||
helper.registerAllowedMethod('fileExists', [String.class], { false }) | ||
|
||
def workdir = "metricbeatTest_1" | ||
|
||
printCallStack(){ | ||
script.stop(workdir: workdir) | ||
} | ||
assertTrue(assertMethodCallContainsPattern('log', "There is no configuration file to stop metricbeat.")) | ||
assertJobStatusSuccess() | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/test/resources/metricbeatTest/metricbeat_container_worker-0676d01d9601f8191.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"id": "fooID", | ||
"config": "bar.xml", | ||
"image": "foo: latest", | ||
"workdir": "fooDir", | ||
"timeout": "30" | ||
} |
Empty file.
8 changes: 8 additions & 0 deletions
8
src/test/resources/metricbeatTest_2/metricbeat_container_worker-0676d01d9601f8191.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"id": "fooID", | ||
"config": "bar.xml", | ||
"image": "foo: latest", | ||
"workdir": "fooDir", | ||
"timeout": "30", | ||
"archiveOnlyOnFail": "true" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.