Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions docs/howto/script_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ a stack, starting agents and services and validating results.
- `get_policy [-profile <profile>] [-timeout <duration>] <policy_name>`: print the details for a policy

- agent commands:
- `install_agent [-profile <profile>] [-timeout <duration>] [<network_name_label>]`: install an Elastic Agent policy, setting the environment variable named in the positional argument
- `install_agent [-profile <profile>] [-timeout <duration>] [-container_name <container_name_label>] [-network_name <network_name_label>]`: install an Elastic Agent policy, setting the environment variables named in the container_name and network_name arguments
- `uninstall_agent [-profile <profile>] [-timeout <duration>]`: remove an installed Elastic Agent policy

- package commands:
Expand Down Expand Up @@ -110,7 +110,7 @@ As an example, a basic system test could be expressed as follows.
use_stack -profile ${CONFIG_PROFILES}/${PROFILE}

# Install an agent.
install_agent -profile ${CONFIG_PROFILES}/${PROFILE} NETWORK_NAME
install_agent -profile ${CONFIG_PROFILES}/${PROFILE} -network_name NETWORK_NAME

# Bring up a docker container.
#
Expand Down
20 changes: 11 additions & 9 deletions internal/testrunner/script/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"flag"
"fmt"
"path/filepath"
"time"

"github.com/rogpeppe/go-internal/testscript"
Expand Down Expand Up @@ -47,14 +48,11 @@ func installAgent(ts *testscript.TestScript, neg bool, args []string) {
flg := flag.NewFlagSet("install", flag.ContinueOnError)
profName := flg.String("profile", "default", "profile name")
timeout := flg.Duration("timeout", 0, "timeout (zero or lower indicates no timeout)")
containerNameLabel := flg.String("container_name", "", "environment variable name to place container name in")
networkNameLabel := flg.String("network_name", "", "environment variable name to place network name in")
ts.Check(flg.Parse(args))
if flg.NArg() != 0 && flg.NArg() != 1 {
ts.Fatalf("usage: install_agent [-profile <profile>] [-timeout <duration>] [<network_name_label>]")
}

var networkNameLabel string
if flg.NArg() == 1 {
networkNameLabel = flg.Arg(0)
if flg.NArg() != 0 {
ts.Fatalf("usage: install_agent [-profile <profile>] [-timeout <duration>] [-container_name <container_name_label>] [-network_name <network_name_label>]")
}

stk, ok := stacks[*profName]
Expand Down Expand Up @@ -120,8 +118,12 @@ func installAgent(ts *testscript.TestScript, neg bool, args []string) {
// ELASTIC_PACKAGE_CA_CERT is set. ¯\_(ツ)_/¯
installed.deployed, err = dep.SetUp(ctx, info)
ts.Check(decoratedWith("setting up agent", err))
if networkNameLabel != "" {
ts.Setenv(networkNameLabel, installed.deployed.Info().NetworkName)
depInfo := installed.deployed.Info()
if *networkNameLabel != "" {
ts.Setenv(*networkNameLabel, depInfo.NetworkName)
}
if *containerNameLabel != "" {
ts.Setenv(*containerNameLabel, fmt.Sprintf("elastic-package-agent-%s-%s-%s-%s-1", filepath.Base(pkgRoot), ds, info.Test.RunID, depInfo.Name))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mrodm Is this a reasonable way to reconstruct this? I did not see any direct representation of the container name for the container running the agent.

I expect that the suffix will ~always be "-1" since the run ID is a random value and we would not expect to have collisions between running agents, even in the case that a script started two or more agents (something that I would imagine would be vanishingly rare in itself).

}
polID := installed.deployed.Info().Policy.ID
ts.Check(decoratedWith("getting kibana agent", doKibanaAgent(ctx, stk.kibana, func(a kibana.Agent) (bool, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ use_stack -profile ${CONFIG_PROFILES}/${PROFILE}
cmpenv stdout want_use.json

# Install an agent.
install_agent -profile ${CONFIG_PROFILES}/${PROFILE} NETWORK_NAME
install_agent -profile ${CONFIG_PROFILES}/${PROFILE} -container_name CONTAINER_NAME -network_name NETWORK_NAME
! stderr .
cmp stdout want_agent_up.text
# Check the container name is valid.
exec echo ${CONTAINER_NAME}
stdout 'elastic-package-agent-with_script-first-[0-9]+-elastic-agent-1'
# Check the network name is valid.
exec echo ${NETWORK_NAME}
stdout 'elastic-package-agent-with_script-first-[0-9]+_default'

# Demonstrate we can interact with the running agent container.
exec docker exec ${CONTAINER_NAME} ls
stdout elastic-agent

# Bring up a docker container and check that we get the expected output.
docker_up -profile ${CONFIG_PROFILES}/${PROFILE} -network ${NETWORK_NAME} test-hits
! stderr .
Expand Down