mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
64a928a3d4
sed -E -i 's#\bcheck\.C\b#testing.T#g' \
-- "integration-cli/check_test.go" "integration-cli/daemon/daemon.go" "integration-cli/daemon/daemon_swarm.go" "integration-cli/daemon_swarm_hack_test.go" "integration-cli/docker_api_attach_test.go" "integration-cli/docker_api_build_test.go" "integration-cli/docker_api_build_windows_test.go" "integration-cli/docker_api_containers_test.go" "integration-cli/docker_api_containers_windows_test.go" "integration-cli/docker_api_exec_resize_test.go" "integration-cli/docker_api_exec_test.go" "integration-cli/docker_api_images_test.go" "integration-cli/docker_api_inspect_test.go" "integration-cli/docker_api_logs_test.go" "integration-cli/docker_api_network_test.go" "integration-cli/docker_api_stats_test.go" "integration-cli/docker_api_swarm_node_test.go" "integration-cli/docker_api_swarm_service_test.go" "integration-cli/docker_api_swarm_test.go" "integration-cli/docker_api_test.go" "integration-cli/docker_cli_attach_test.go" "integration-cli/docker_cli_attach_unix_test.go" "integration-cli/docker_cli_build_test.go" "integration-cli/docker_cli_build_unix_test.go" "integration-cli/docker_cli_by_digest_test.go" "integration-cli/docker_cli_commit_test.go" "integration-cli/docker_cli_cp_from_container_test.go" "integration-cli/docker_cli_cp_test.go" "integration-cli/docker_cli_cp_to_container_test.go" "integration-cli/docker_cli_cp_to_container_unix_test.go" "integration-cli/docker_cli_cp_utils_test.go" "integration-cli/docker_cli_create_test.go" "integration-cli/docker_cli_daemon_plugins_test.go" "integration-cli/docker_cli_daemon_test.go" "integration-cli/docker_cli_events_test.go" "integration-cli/docker_cli_events_unix_test.go" "integration-cli/docker_cli_exec_test.go" "integration-cli/docker_cli_exec_unix_test.go" "integration-cli/docker_cli_external_volume_driver_unix_test.go" "integration-cli/docker_cli_health_test.go" "integration-cli/docker_cli_history_test.go" "integration-cli/docker_cli_images_test.go" "integration-cli/docker_cli_import_test.go" "integration-cli/docker_cli_info_test.go" "integration-cli/docker_cli_info_unix_test.go" "integration-cli/docker_cli_inspect_test.go" "integration-cli/docker_cli_links_test.go" "integration-cli/docker_cli_login_test.go" "integration-cli/docker_cli_logout_test.go" "integration-cli/docker_cli_logs_test.go" "integration-cli/docker_cli_netmode_test.go" "integration-cli/docker_cli_network_unix_test.go" "integration-cli/docker_cli_plugins_logdriver_test.go" "integration-cli/docker_cli_plugins_test.go" "integration-cli/docker_cli_port_test.go" "integration-cli/docker_cli_proxy_test.go" "integration-cli/docker_cli_prune_unix_test.go" "integration-cli/docker_cli_ps_test.go" "integration-cli/docker_cli_pull_local_test.go" "integration-cli/docker_cli_pull_test.go" "integration-cli/docker_cli_push_test.go" "integration-cli/docker_cli_registry_user_agent_test.go" "integration-cli/docker_cli_restart_test.go" "integration-cli/docker_cli_rmi_test.go" "integration-cli/docker_cli_run_test.go" "integration-cli/docker_cli_run_unix_test.go" "integration-cli/docker_cli_save_load_test.go" "integration-cli/docker_cli_save_load_unix_test.go" "integration-cli/docker_cli_search_test.go" "integration-cli/docker_cli_service_create_test.go" "integration-cli/docker_cli_service_health_test.go" "integration-cli/docker_cli_service_logs_test.go" "integration-cli/docker_cli_service_scale_test.go" "integration-cli/docker_cli_sni_test.go" "integration-cli/docker_cli_start_test.go" "integration-cli/docker_cli_stats_test.go" "integration-cli/docker_cli_swarm_test.go" "integration-cli/docker_cli_swarm_unix_test.go" "integration-cli/docker_cli_top_test.go" "integration-cli/docker_cli_update_unix_test.go" "integration-cli/docker_cli_userns_test.go" "integration-cli/docker_cli_v2_only_test.go" "integration-cli/docker_cli_volume_test.go" "integration-cli/docker_deprecated_api_v124_test.go" "integration-cli/docker_deprecated_api_v124_unix_test.go" "integration-cli/docker_hub_pull_suite_test.go" "integration-cli/docker_utils_test.go" "integration-cli/events_utils_test.go" "integration-cli/fixtures_linux_daemon_test.go" "integration-cli/utils_test.go" "pkg/discovery/discovery_test.go" "pkg/discovery/file/file_test.go" "pkg/discovery/generator_test.go" "pkg/discovery/kv/kv_test.go" "pkg/discovery/memory/memory_test.go" "pkg/discovery/nodes/nodes_test.go"
Signed-off-by: Tibor Vass <tibor@docker.com>
(cherry picked from commit 1d92789b4f
)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
206 lines
6.2 KiB
Go
206 lines
6.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"io"
|
|
"os/exec"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
eventstestutils "github.com/docker/docker/daemon/events/testutils"
|
|
"github.com/go-check/check"
|
|
"github.com/sirupsen/logrus"
|
|
"gotest.tools/assert"
|
|
)
|
|
|
|
// eventMatcher is a function that tries to match an event input.
|
|
// It returns true if the event matches and a map with
|
|
// a set of key/value to identify the match.
|
|
type eventMatcher func(text string) (map[string]string, bool)
|
|
|
|
// eventMatchProcessor is a function to handle an event match.
|
|
// It receives a map of key/value with the information extracted in a match.
|
|
type eventMatchProcessor func(matches map[string]string)
|
|
|
|
// eventObserver runs an events commands and observes its output.
|
|
type eventObserver struct {
|
|
buffer *bytes.Buffer
|
|
command *exec.Cmd
|
|
scanner *bufio.Scanner
|
|
startTime string
|
|
disconnectionError error
|
|
}
|
|
|
|
// newEventObserver creates the observer and initializes the command
|
|
// without running it. Users must call `eventObserver.Start` to start the command.
|
|
func newEventObserver(c *testing.T, args ...string) (*eventObserver, error) {
|
|
since := daemonTime(c).Unix()
|
|
return newEventObserverWithBacklog(c, since, args...)
|
|
}
|
|
|
|
// newEventObserverWithBacklog creates a new observer changing the start time of the backlog to return.
|
|
func newEventObserverWithBacklog(c *testing.T, since int64, args ...string) (*eventObserver, error) {
|
|
startTime := strconv.FormatInt(since, 10)
|
|
cmdArgs := []string{"events", "--since", startTime}
|
|
if len(args) > 0 {
|
|
cmdArgs = append(cmdArgs, args...)
|
|
}
|
|
eventsCmd := exec.Command(dockerBinary, cmdArgs...)
|
|
stdout, err := eventsCmd.StdoutPipe()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &eventObserver{
|
|
buffer: new(bytes.Buffer),
|
|
command: eventsCmd,
|
|
scanner: bufio.NewScanner(stdout),
|
|
startTime: startTime,
|
|
}, nil
|
|
}
|
|
|
|
// Start starts the events command.
|
|
func (e *eventObserver) Start() error {
|
|
return e.command.Start()
|
|
}
|
|
|
|
// Stop stops the events command.
|
|
func (e *eventObserver) Stop() {
|
|
e.command.Process.Kill()
|
|
e.command.Wait()
|
|
}
|
|
|
|
// Match tries to match the events output with a given matcher.
|
|
func (e *eventObserver) Match(match eventMatcher, process eventMatchProcessor) {
|
|
for e.scanner.Scan() {
|
|
text := e.scanner.Text()
|
|
e.buffer.WriteString(text)
|
|
e.buffer.WriteString("\n")
|
|
|
|
if matches, ok := match(text); ok {
|
|
process(matches)
|
|
}
|
|
}
|
|
|
|
err := e.scanner.Err()
|
|
if err == nil {
|
|
err = io.EOF
|
|
}
|
|
|
|
logrus.Debugf("EventObserver scanner loop finished: %v", err)
|
|
e.disconnectionError = err
|
|
}
|
|
|
|
func (e *eventObserver) CheckEventError(c *testing.T, id, event string, match eventMatcher) {
|
|
var foundEvent bool
|
|
scannerOut := e.buffer.String()
|
|
|
|
if e.disconnectionError != nil {
|
|
until := daemonUnixTime(c)
|
|
out, _ := dockerCmd(c, "events", "--since", e.startTime, "--until", until)
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
for _, e := range events {
|
|
if _, ok := match(e); ok {
|
|
foundEvent = true
|
|
break
|
|
}
|
|
}
|
|
scannerOut = out
|
|
}
|
|
if !foundEvent {
|
|
c.Fatalf("failed to observe event `%s` for %s. Disconnection error: %v\nout:\n%v", event, id, e.disconnectionError, scannerOut)
|
|
}
|
|
}
|
|
|
|
// matchEventLine matches a text with the event regular expression.
|
|
// It returns the matches and true if the regular expression matches with the given id and event type.
|
|
// It returns an empty map and false if there is no match.
|
|
func matchEventLine(id, eventType string, actions map[string]chan bool) eventMatcher {
|
|
return func(text string) (map[string]string, bool) {
|
|
matches := eventstestutils.ScanMap(text)
|
|
if len(matches) == 0 {
|
|
return matches, false
|
|
}
|
|
|
|
if matchIDAndEventType(matches, id, eventType) {
|
|
if _, ok := actions[matches["action"]]; ok {
|
|
return matches, true
|
|
}
|
|
}
|
|
return matches, false
|
|
}
|
|
}
|
|
|
|
// processEventMatch closes an action channel when an event line matches the expected action.
|
|
func processEventMatch(actions map[string]chan bool) eventMatchProcessor {
|
|
return func(matches map[string]string) {
|
|
if ch, ok := actions[matches["action"]]; ok {
|
|
ch <- true
|
|
}
|
|
}
|
|
}
|
|
|
|
// parseEventAction parses an event text and returns the action.
|
|
// It fails if the text is not in the event format.
|
|
func parseEventAction(c *testing.T, text string) string {
|
|
matches := eventstestutils.ScanMap(text)
|
|
return matches["action"]
|
|
}
|
|
|
|
// eventActionsByIDAndType returns the actions for a given id and type.
|
|
// It fails if the text is not in the event format.
|
|
func eventActionsByIDAndType(c *testing.T, events []string, id, eventType string) []string {
|
|
var filtered []string
|
|
for _, event := range events {
|
|
matches := eventstestutils.ScanMap(event)
|
|
assert.Assert(c, matches != nil)
|
|
if matchIDAndEventType(matches, id, eventType) {
|
|
filtered = append(filtered, matches["action"])
|
|
}
|
|
}
|
|
return filtered
|
|
}
|
|
|
|
// matchIDAndEventType returns true if an event matches a given id and type.
|
|
// It also resolves names in the event attributes if the id doesn't match.
|
|
func matchIDAndEventType(matches map[string]string, id, eventType string) bool {
|
|
return matchEventID(matches, id) && matches["eventType"] == eventType
|
|
}
|
|
|
|
func matchEventID(matches map[string]string, id string) bool {
|
|
matchID := matches["id"] == id || strings.HasPrefix(matches["id"], id)
|
|
if !matchID && matches["attributes"] != "" {
|
|
// try matching a name in the attributes
|
|
attributes := map[string]string{}
|
|
for _, a := range strings.Split(matches["attributes"], ", ") {
|
|
kv := strings.Split(a, "=")
|
|
attributes[kv[0]] = kv[1]
|
|
}
|
|
matchID = attributes["name"] == id
|
|
}
|
|
return matchID
|
|
}
|
|
|
|
func parseEvents(c *testing.T, out, match string) {
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
for _, event := range events {
|
|
matches := eventstestutils.ScanMap(event)
|
|
matched, err := regexp.MatchString(match, matches["action"])
|
|
assert.NilError(c, err)
|
|
assert.Assert(c, matched, "Matcher: %s did not match %s", match, matches["action"])
|
|
}
|
|
}
|
|
|
|
func parseEventsWithID(c *testing.T, out, match, id string) {
|
|
events := strings.Split(strings.TrimSpace(out), "\n")
|
|
for _, event := range events {
|
|
matches := eventstestutils.ScanMap(event)
|
|
assert.Assert(c, matchEventID(matches, id))
|
|
|
|
matched, err := regexp.MatchString(match, matches["action"])
|
|
assert.NilError(c, err)
|
|
assert.Assert(c, matched, "Matcher: %s did not match %s", match, matches["action"])
|
|
}
|
|
}
|