Documentation ¶
Overview ¶
Package testutil provides utilities for testing the network package.
Index ¶
- Constants
- func GetDockerPID(dockerName string) (int64, error)
- func GetShortTestName(proto, subtest string) string
- func RunDockerServer(t testing.TB, serverName, dockerPath string, env []string, ...) error
- func RunHostServer(t *testing.T, command []string, env []string, serverStartRegex *regexp.Regexp) bool
- func WithPCAP(t *testing.T, port string, suffix string, alwaysSave bool) io.Writer
- type PatternScanner
Examples ¶
Constants ¶
const ( TLSDisabled = false TLSEnabled = true )
Constants to represent whether the connection should be encrypted with TLSEnabled.
const ( // DefaultTimeout is the default timeout for running a server. DefaultTimeout = time.Minute )
Variables ¶
This section is empty.
Functions ¶
func GetDockerPID ¶
GetDockerPID returns the PID of a docker container.
func GetShortTestName ¶
GetShortTestName generates a suffix in the form "<protocol>-<subtest name>" to be passed to WithPCAP.
func RunDockerServer ¶
func RunDockerServer(t testing.TB, serverName, dockerPath string, env []string, serverStartRegex *regexp.Regexp, timeout time.Duration, retryCount int) error
RunDockerServer is a template for running a protocols server in a docker. - serverName is a friendly name of the server we are setting (AMQP, mongo, etc.). - dockerPath is the path for the docker-compose. - env is any environment variable required for running the server. - serverStartRegex is a regex to be matched on the server logs to ensure it started correctly.
func RunHostServer ¶
func RunHostServer(t *testing.T, command []string, env []string, serverStartRegex *regexp.Regexp) bool
RunHostServer is a template for running a command on the Host. - command is the path for the command to execute. - env is any environment variable required for running the server. - serverStartRegex is a regex to be matched on the server logs to ensure it started correctly. return true on success
func WithPCAP ¶
WithPCAP runs tcpdump for the duration of the test.
It returns an `io.Writer` to be used as a KeyLogWriter in tls.Config to be able, to decrypt the TLS traffic in the resulting PCAP. The resulting PCAPs and keylog files will be saved in `/tmp/test_pcaps`
Unless alwaysSave is true, WithPCAP will only save the resulting PCAP if the test fails.
Example (Plaintext) ¶
package main import ( "net/http" "testing" "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil" ) func main() { t := &testing.T{} // Run tcpdump alongside the test and only save the resulting // PCAP if the test failed. We don't need the keylog writer // here so we discard it. _ = testutil.WithPCAP(t, "80", testutil.GetShortTestName("HTTP", "simple request"), false) client := &http.Client{} resp, err := client.Get("http://httpbin.org/status/200") if err != nil { t.Errorf("error while making request") } if err = resp.Body.Close(); err != nil { t.Errorf("error while closing request body") } }
Output:
Example (Tls) ¶
package main import ( "crypto/tls" "net/http" "testing" "github.com/DataDog/datadog-agent/pkg/network/protocols/testutil" ) func main() { t := &testing.T{} // Run tcpdump alongside the test and always save the resulting PCAP. klw := testutil.WithPCAP(t, "443", testutil.GetShortTestName("HTTP", "simple request - TLS"), true) client := &http.Client{ Transport: &http.Transport{ TLSClientConfig: &tls.Config{ InsecureSkipVerify: true, KeyLogWriter: klw, }, }, } resp, err := client.Get("https://httpbin.org/status/200") if err != nil { t.Errorf("error while making request") } if err = resp.Body.Close(); err != nil { t.Errorf("error while closing request body") } }
Output:
Types ¶
type PatternScanner ¶
type PatternScanner struct { // Once we've found the correct log, we should notify the caller. DoneChan chan struct{} // contains filtered or unexported fields }
PatternScanner is a helper to scan logs for a given pattern.
func NewScanner ¶
func NewScanner(pattern *regexp.Regexp, doneChan chan struct{}) *PatternScanner
NewScanner returns a new instance of PatternScanner.
func (*PatternScanner) PrintLogs ¶
func (ps *PatternScanner) PrintLogs(t testing.TB)
PrintLogs writes the captured logs into the test logger.