|
| 1 | +package systemtests |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/Sirupsen/logrus" |
| 9 | +) |
| 10 | + |
| 11 | +type container struct { |
| 12 | + node *node |
| 13 | + containerID string |
| 14 | + name string |
| 15 | + eth0 string |
| 16 | +} |
| 17 | + |
| 18 | +func newContainer(node *node, containerID, name string) (*container, error) { |
| 19 | + cont := &container{node: node, containerID: containerID, name: name} |
| 20 | + |
| 21 | + out, err := cont.getIPAddr("eth0") |
| 22 | + if err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + cont.eth0 = out |
| 26 | + |
| 27 | + return cont, nil |
| 28 | +} |
| 29 | + |
| 30 | +func (c *container) String() string { |
| 31 | + return fmt.Sprintf("(container: %s (name: %q ip: %s host: %s))", c.containerID, c.name, c.eth0, c.node.Name()) |
| 32 | +} |
| 33 | + |
| 34 | +func (c *container) checkPingFailure(ipaddr string) error { |
| 35 | + logrus.Infof("Expecting ping failure from %v to %s", c, ipaddr) |
| 36 | + if err := c.checkPing(ipaddr); err == nil { |
| 37 | + return fmt.Errorf("Ping succeeded when expected to fail from %v to %s", c, ipaddr) |
| 38 | + } |
| 39 | + |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +func (c *container) checkPing(ipaddr string) error { |
| 44 | + logrus.Infof("Checking ping from %v to %s", c, ipaddr) |
| 45 | + out, err := c.exec("ping -c 1 " + ipaddr) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + if err != nil || strings.Contains(out, "0 received, 100% packet loss") { |
| 51 | + logrus.Errorf("Ping from %v to %s FAILED: %q - %v", c, ipaddr, out, err) |
| 52 | + return fmt.Errorf("Ping failed from %v to %s: %q - %v", c, ipaddr, out, err) |
| 53 | + } |
| 54 | + |
| 55 | + logrus.Infof("Ping from %v to %s SUCCEEDED", c, ipaddr) |
| 56 | + return nil |
| 57 | +} |
| 58 | + |
| 59 | +func (c *container) getIPAddr(dev string) (string, error) { |
| 60 | + out, err := c.exec(fmt.Sprintf("ip addr show dev %s | grep inet | head -1", dev)) |
| 61 | + if err != nil { |
| 62 | + logrus.Errorf("Failed to get IP for container %q", c.containerID) |
| 63 | + logrus.Println(out) |
| 64 | + } |
| 65 | + |
| 66 | + parts := regexp.MustCompile(`\s+`).Split(strings.TrimSpace(out), -1) |
| 67 | + if len(parts) < 2 { |
| 68 | + return "", fmt.Errorf("Invalid output from container %q: %s", c.containerID, out) |
| 69 | + } |
| 70 | + |
| 71 | + parts = strings.Split(parts[1], "/") |
| 72 | + out = strings.TrimSpace(parts[0]) |
| 73 | + return out, err |
| 74 | +} |
| 75 | + |
| 76 | +func (c *container) exec(args string) (string, error) { |
| 77 | + return c.node.runCommand(fmt.Sprintf("docker exec %s %s", c.containerID, args)) |
| 78 | +} |
| 79 | + |
| 80 | +func (c *container) execBG(args string) (string, error) { |
| 81 | + return c.node.runCommand(fmt.Sprintf("docker exec -d %s %s", c.containerID, args)) |
| 82 | +} |
| 83 | + |
| 84 | +func (c *container) dockerCmd(arg string) error { |
| 85 | + out, err := c.node.runCommand(fmt.Sprintf("docker %s %s", arg, c.containerID)) |
| 86 | + if err != nil { |
| 87 | + logrus.Println(out) |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func (c *container) start() error { |
| 95 | + logrus.Infof("Starting container %s on %s", c.containerID, c.node.Name()) |
| 96 | + return c.dockerCmd("start") |
| 97 | +} |
| 98 | + |
| 99 | +func (c *container) stop() error { |
| 100 | + logrus.Infof("Stopping container %s on %s", c.containerID, c.node.Name()) |
| 101 | + return c.dockerCmd("stop") |
| 102 | +} |
| 103 | + |
| 104 | +func (c *container) rm() error { |
| 105 | + logrus.Infof("Removing container %s on %s", c.containerID, c.node.Name()) |
| 106 | + c.dockerCmd("kill -s 9") |
| 107 | + return c.dockerCmd("rm -f") |
| 108 | +} |
| 109 | + |
| 110 | +func (c *container) startListener(port int, protocol string) error { |
| 111 | + var protoStr string |
| 112 | + |
| 113 | + if protocol == "udp" { |
| 114 | + protoStr = "-u" |
| 115 | + } |
| 116 | + |
| 117 | + logrus.Infof("Starting a %s listener on %v port %d", protocol, c, port) |
| 118 | + _, err := c.execBG(fmt.Sprintf("nc -lk %s -p %v -e /bin/true", protoStr, port)) |
| 119 | + return err |
| 120 | +} |
| 121 | + |
| 122 | +func (c *container) checkConnection(ipaddr, protocol string, port int) error { |
| 123 | + var protoStr string |
| 124 | + |
| 125 | + if protocol == "udp" { |
| 126 | + protoStr = "-u" |
| 127 | + } |
| 128 | + |
| 129 | + logrus.Infof("Checking connection from %v to ip %s on port %d", c, ipaddr, port) |
| 130 | + |
| 131 | + _, err := c.exec(fmt.Sprintf("nc -z -n -v -w 1 %s %s %v", protoStr, ipaddr, port)) |
| 132 | + if err != nil { |
| 133 | + logrus.Errorf("Connection from %v to ip %s on port %d FAILED", c, ipaddr, port) |
| 134 | + } else { |
| 135 | + logrus.Infof("Connection from %v to ip %s on port %d SUCCEEDED", c, ipaddr, port) |
| 136 | + } |
| 137 | + |
| 138 | + return err |
| 139 | +} |
| 140 | + |
| 141 | +func (c *container) checkNoConnection(ipaddr, protocol string, port int) error { |
| 142 | + logrus.Infof("Expecting connection to fail from %v to %s on port %d", c, ipaddr, port) |
| 143 | + |
| 144 | + if err := c.checkConnection(ipaddr, protocol, port); err != nil { |
| 145 | + return nil |
| 146 | + } |
| 147 | + |
| 148 | + return fmt.Errorf("Connection SUCCEEDED on port %d from %s from %v when it should have FAILED.", port, ipaddr, c) |
| 149 | +} |
0 commit comments