Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement terraform based system tests #227

Merged
merged 66 commits into from
Jan 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
75c6c41
Terraform executor as Docker image
Jan 19, 2021
fe54067
Implement docker-compose for tf executor
Jan 19, 2021
4eb1f8b
Enable _dev/deploy for data streams
Jan 20, 2021
2690b65
Fix
Jan 20, 2021
039e39a
Merge branch '89-enable-deploy-everywhere' into 89-terraform-definitions
Jan 20, 2021
b324b83
Gohack: github.com/mtojek/package-spec/code/go
Jan 20, 2021
22c92c9
Install service deployer as static resource
Jan 20, 2021
62a6e85
Cleanup
Jan 20, 2021
7ddd7fe
Define sample test config for AWS SNS
Jan 20, 2021
0f25284
WIP
Jan 20, 2021
6f7e4af
Fix
Jan 20, 2021
6bacf60
Build terraform environment
Jan 21, 2021
deb2c26
Merge branch 'master' into 89-terraform-definitions
Jan 21, 2021
1916b4f
Mount TF_DIR
Jan 21, 2021
6248a86
Set envs in policies
Jan 21, 2021
3ce33c8
Fix: lint
Jan 21, 2021
3b5db75
Fix: build dir (empty value)
Jan 21, 2021
958c43b
Fix: yml file
Jan 21, 2021
ebdf53a
Fix: service name
Jan 21, 2021
dcfb15c
Fix: brackets
Jan 21, 2021
2b8fac7
Fix
Jan 21, 2021
12efcff
Fix: reference issue
Jan 21, 2021
6ae050c
Fix: period
Jan 21, 2021
ba9cd9f
Set time
Jan 21, 2021
b908c9d
Do not print request body to prevent leaks
Jan 21, 2021
c1ded23
Fix: format
Jan 21, 2021
5e26d8a
Sample for EC2 metrics
Jan 25, 2021
0f64098
Fix EC2 system test
Jan 25, 2021
075a2db
Disable SNS test
Jan 25, 2021
14174ee
Pass test run ID
Jan 25, 2021
2d2b18b
Next fixes
Jan 25, 2021
5806a4e
Fix: formatter
Jan 25, 2021
652755e
Try: enable monitoring
Jan 26, 2021
0921c14
Merge branch 'master' into 89-enable-deploy-everywhere
Jan 26, 2021
90d0a1c
Update dependency on package-spec
Jan 26, 2021
be253b7
Address PR comments
Jan 27, 2021
eae2df2
Merge branch '89-enable-deploy-everywhere' into 89-terraform-definitions
Jan 27, 2021
ca8faec
Fix failing test
Jan 27, 2021
3c9b61e
Fix: go.sum
Jan 27, 2021
57ab7ba
Don't set ports
Jan 27, 2021
4b2cc85
Update package-spec
Jan 27, 2021
3cc0cfb
Collect logs for integrations
Jan 27, 2021
d9f5ec3
Use cloud environment
Jan 27, 2021
977cc06
Merge branch 'master' into 89-terraform-definitions
Jan 28, 2021
c990cb7
Fix: go mod tidy
Jan 28, 2021
36bf34b
Update package-spec dependency
Jan 28, 2021
c7ad301
Support session token
Jan 28, 2021
77ff5c9
Terraform service deployer
Jan 28, 2021
7ddc1f4
nit-picks
Jan 28, 2021
8275c7c
Don't connect to the stack network
Jan 28, 2021
b3c0b59
writeTerraformDeployerResources
Jan 28, 2021
c044a01
Link: env-file
Jan 28, 2021
a128259
Link GH issue
Jan 28, 2021
38dd80e
Fix comment
Jan 28, 2021
133bf5d
Next improvements
Jan 28, 2021
f14ad49
nit: separator
Jan 28, 2021
8c116fb
Docs
Jan 28, 2021
ebf857c
Update docs/howto/system_testing.md
mtojek Jan 29, 2021
8871d00
Update docs/howto/system_testing.md
mtojek Jan 29, 2021
b43c3a1
Introductory sentences
Jan 29, 2021
2ea691f
Docker compose, terraform
Jan 29, 2021
5934ad0
More generic
Jan 29, 2021
4d4011f
Dont set hostname
Jan 29, 2021
a97a874
Check if there is a single definition of service deployer
Jan 29, 2021
34097cd
Fix
Jan 29, 2021
e7e8764
Another fix
Jan 29, 2021
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
29 changes: 26 additions & 3 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pipeline {
BASE_DIR="src/github.com/elastic/elastic-package"
JOB_GIT_CREDENTIALS = "f6c7695a-671e-4f4f-a331-acdce44ff9ba"
PIPELINE_LOG_LEVEL='INFO'
AWS_ACCOUNT_SECRET = 'secret/observability-team/ci/elastic-observability-aws-account-auth'
}
options {
timeout(time: 1, unit: 'HOURS')
Expand Down Expand Up @@ -40,16 +41,19 @@ pipeline {
steps {
cleanup()
withMageEnv(){
dir("${BASE_DIR}"){
sh(label: 'Check',script: 'make check')
withCloudTestEnv() {
dir("${BASE_DIR}"){
sh(label: 'Check',script: 'make check')
}
}
}
}
post {
always {
dir("${BASE_DIR}") {
archiveArtifacts(allowEmptyArchive: true, artifacts: 'build/test-results/*.xml')
archiveArtifacts(allowEmptyArchive: true, artifacts: 'build/elastic-stack-dump/logs/*.log')
archiveArtifacts(allowEmptyArchive: true, artifacts: 'build/elastic-stack-dump/stack/logs/*.log')
archiveArtifacts(allowEmptyArchive: true, artifacts: 'build/elastic-stack-dump/check/logs/*.log')
junit(allowEmptyResults: false,
keepLongStdio: true,
testResults: "build/test-results/*.xml")
Expand All @@ -71,3 +75,22 @@ def cleanup(){
}
unstash 'source'
}

def withCloudTestEnv(Closure body) {
def maskedVars = []
// AWS
def aws = getVaultSecret(secret: "${AWS_ACCOUNT_SECRET}").data
if (!aws.containsKey('access_key')) {
error("${AWS_ACCOUNT_SECRET} doesn't contain 'access_key'")
}
if (!aws.containsKey('secret_key')) {
error("${AWS_ACCOUNT_SECRET} doesn't contain 'secret_key'")
}
maskedVars.addAll([
[var: "AWS_ACCESS_KEY_ID", password: aws.access_key],
[var: "AWS_SECRET_ACCESS_KEY", password: aws.secret_key],
])
withEnvMask(vars: maskedVars) {
body()
}
}
82 changes: 66 additions & 16 deletions docs/howto/system_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ Conceptually, running a system test involves the following steps:
1. Depending on the Elastic Package whose data stream is being tested, deploy an instance of the package's integration service.
1. Create a test policy that configures a single data stream for a single package.
1. Assign the test policy to the enrolled Agent.
1. Wait a reasonable amount of time for the Agent to collect data from the
1. Wait a reasonable amount of time for the Agent to collect data from the
integration service and index it into the correct Elasticsearch data stream.
1. Query the first 500 documents based on `@timestamp` for validation.
1. Validate mappings are defined for the fields contained in the indexed documents.
1. Validate that the JSON data types contained `_source` are compatible with
mappings declared for the field.
mappings declared for the field.
1. Delete test artifacts and tear down the instance of the package's integration service.
1. Once all desired data streams have been system tested, tear down the Elastic Stack.

## Limitations

At the moment system tests have limitations. The salient ones are:
* They can only test packages whose integration services can be deployed via Docker Compose. Eventually they will be able to test packages that can be deployed via other means, e.g. a Terraform configuration.
* There isn't a way to do assert that the indexed data matches data from a file (e.g. golden file testing).

## Defining a system test
Expand All @@ -39,21 +38,38 @@ Packages have a specific folder structure (only relevant parts shown).
manifest.yml
```

To define a system test we must define configuration at two levels: the package level and each data stream's level.
To define a system test we must define configuration on at least one level: a package or a data stream's one.

### Package-level configuration

First, we must define the configuration for deploying a package's integration service. As mentioned in the [_Limitations_](#Limitations) section above, only packages whose integration services can be deployed via Docker Compose are supported at the moment.
First, we must define the configuration for deploying a package's integration service. We can define it on either the package level:

```
<package root>/
_dev/
deploy/
docker/
docker-compose.yml
<service deployer>/
<service deployer files>
```

or the data stream's level:

```
<package root>/
data_stream/
<data stream>/
_dev/
deploy/
<service deployer>/
<service deployer files>
```

`<service deployer>` - a name of the supported service deployer: `docker` (Docker Compose service deployer) or `tf` (Terraform service deployer).

### Docker Compose service deployer

The `docker-compose.yml` file defines the integration service(s) for the package. If your package has a logs data stream, the log files from your package's integration service must be written to a volume. For example, the `apache` package has the following definition in it's integration service's `docker-compose.yml` file.
When using the Docker Compose service deployer, the `<service deployer files>` must include a `docker-compose.yml` file.
The `docker-compose.yml` file defines the integration service(s) for the package. If your package has a logs data stream,
the log files from your package's integration service must be written to a volume. For example, the `apache` package has
the following definition in it's integration service's `docker-compose.yml` file.

```
version: '2.3'
Expand All @@ -66,7 +82,43 @@ services:

Here, `SERVICE_LOGS_DIR` is a special keyword. It is something that we will need later.

### Data stream-level configuration
### Terraform service deployer

When using the Terraform service deployer, the `<service deployer files>` must include at least one `*.tf` file.
The `*.tf` files define the infrastructure using the Terraform syntax. The terraform based service can be handy to boot up
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to my comment on the Docker Composer service deployer, I would add an introductory sentence to bridge the gap between what's shown in the sample file structure and this sentence here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

resources using selected cloud provider and use them for testing (e.g. observe and collect metrics).

Sample `main.tf` definition:

```
variable "TEST_RUN_ID" {
default = "detached"
}

provider "aws" {}

resource "aws_instance" "i" {
ami = data.aws_ami.latest-amzn.id
monitoring = true
instance_type = "t1.micro"
tags = {
Name = "elastic-package-test-${var.TEST_RUN_ID}"
}
}

data "aws_ami" "latest-amzn" {
most_recent = true
owners = [ "amazon" ] # AWS
filter {
name = "name"
values = ["amzn2-ami-hvm-*"]
}
}
```

Notice the use of the `TEST_RUN_ID` variable. It contains a unique ID, which can help differentiate resources created in potential concurrent test runs.

### Test case definition

Next, we must define configuration for each data stream that we want to system test.

Expand Down Expand Up @@ -97,10 +149,8 @@ The `data_stream.vars` field corresponds to data stream-level variables for the

Notice the use of the `{{SERVICE_LOGS_DIR}}` placeholder. This corresponds to the `${SERVICE_LOGS_DIR}` variable we saw in the `docker-compose.yml` file earlier. In the above example, the net effect is as if the `/usr/local/apache2/logs/access.log*` files located inside the Apache integration service container become available at the same path from Elastic Agent's perspective.

When a data stream's manifest declares multiple streams with different inputs
you can use the `input` option to select the stream to test. The first stream
whose input type matches the `input` value will be tested. By default, the first
stream declared in the manifest will be tested.
When a data stream's manifest declares multiple streams with different inputs you can use the `input` option to select the stream to test. The first stream
whose input type matches the `input` value will be tested. By default, the first stream declared in the manifest will be tested.

#### Placeholders

Expand Down Expand Up @@ -152,4 +202,4 @@ Finally, when you are done running all system tests, bring down the Elastic Stac

```
elastic-package stack down
```
```
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/elastic/go-elasticsearch/v7 v7.9.0
github.com/elastic/go-licenser v0.3.1
github.com/elastic/go-ucfg v0.8.3
github.com/elastic/package-spec/code/go v0.0.0-20210126144901-46090e1310d3
github.com/elastic/package-spec/code/go v0.0.0-20210127201409-dd08da649371
github.com/go-git/go-billy/v5 v5.0.0
github.com/go-git/go-git/v5 v5.1.0
github.com/go-openapi/strfmt v0.19.6 // indirect
Expand Down
Loading