go.ligato.io/vpp-agent/v3@v3.5.0/tests/e2e/e2etest/commands.go (about) 1 // Copyright (c) 2020 Pantheon.tech 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package e2etest 16 17 import ( 18 "fmt" 19 "net" 20 "regexp" 21 "strconv" 22 23 "github.com/go-errors/errors" 24 ) 25 26 // DNSRecordType represent types of records associated with domain name in DNS server 27 type DNSRecordType int 28 29 const ( 30 A DNSRecordType = iota 31 AAAA 32 ) 33 34 var ( 35 dnsRecordTypeNames = map[DNSRecordType]string{ 36 A: "A", 37 AAAA: "AAAA", 38 } 39 digRecordRegexps = map[DNSRecordType]*regexp.Regexp{ 40 A: regexp.MustCompile(`\tA\t([^\n]*)\n`), 41 AAAA: regexp.MustCompile(`\tAAAA\t([^\n]*)\n`), 42 } 43 pingRegexp = regexp.MustCompile("\n([0-9]+) packets transmitted, ([0-9]+) packets received, ([0-9]+)% packet loss") 44 ) 45 46 // Dig calls linux tool "dig" that query DNS server for domain name (queryDomain) and return records associated 47 // of given type (requestedInfo) associated with the domain name. 48 func (c *ContainerRuntime) Dig(dnsServer net.IP, queryDomain string, requestedInfo DNSRecordType) ([]net.IP, error) { 49 c.ctx.t.Helper() 50 51 // call dig in container 52 args := []string{fmt.Sprintf("@%s", dnsServer), // target DNS server 53 "+time=1", // minimize max request time (for request that don't have answer) 54 "+tries=1", // minimize retries (try count = initial request + retries) (for request that don't have answers) 55 "-t", dnsRecordTypeNames[requestedInfo], // requested record type 56 queryDomain, 57 } 58 stdout, _, err := c.ExecCmd("dig", args...) 59 if err != nil { 60 return nil, errors.Errorf("execution of linux command dig failed due to: %v", err) 61 } 62 63 // parse output of dig linux command 64 ipAddresses := make([]net.IP, 0) 65 for _, match := range digRecordRegexps[requestedInfo].FindAllSubmatch([]byte(stdout), -1) { 66 ipAddressStr := string(match[1]) 67 ipAddress := net.ParseIP(ipAddressStr) 68 if ipAddress == nil { 69 return nil, errors.Errorf("can't parse %s record value %s as ip address. Probably regular "+ 70 "expression matching issue for dig output:\n %s", dnsRecordTypeNames[requestedInfo], 71 ipAddressStr, stdout) 72 } 73 c.ctx.Logger.Printf("Linux dig command got for queried domain %s an %s record %s", 74 queryDomain, dnsRecordTypeNames[requestedInfo], ipAddressStr) 75 76 ipAddresses = append(ipAddresses, ipAddress) 77 } 78 return ipAddresses, nil 79 } 80 81 // PingAsCallback can be used to ping repeatedly inside the assertions "Eventually" 82 // and "Consistently" from Omega. 83 func (c *ContainerRuntime) PingAsCallback(destAddress string, opts ...PingOptModifier) func() error { 84 return func() error { 85 return c.Ping(destAddress, opts...) 86 } 87 } 88 89 // Ping <destAddress> from inside of the container. 90 func (c *ContainerRuntime) Ping(destAddress string, opts ...PingOptModifier) error { 91 c.ctx.t.Helper() 92 93 ping := NewPingOpts(opts...) 94 args := append(ping.args(), destAddress) 95 96 stdout, _, err := c.ExecCmd("ping", args...) 97 if err != nil { 98 return err 99 } 100 101 matches := pingRegexp.FindStringSubmatch(stdout) 102 sent, recv, loss, err := parsePingOutput(stdout, matches) 103 if err != nil { 104 return err 105 } 106 c.ctx.Logger.Printf("Linux ping from %s container to %s: sent=%d, received=%d, loss=%d%%", 107 c.logIdentity, destAddress, sent, recv, loss) 108 109 if sent == 0 || loss > ping.AllowedLoss { 110 return fmt.Errorf("failed to ping '%s': %s", destAddress, matches[0]) 111 } 112 return nil 113 } 114 115 func parsePingOutput(output string, matches []string) (sent int, recv int, loss int, err error) { 116 if len(matches) != 4 { 117 err = fmt.Errorf("unexpected output from ping: %s", output) 118 return 119 } 120 sent, err = strconv.Atoi(matches[1]) 121 if err != nil { 122 err = fmt.Errorf("failed to parse the sent packet count: %v", err) 123 return 124 } 125 recv, err = strconv.Atoi(matches[2]) 126 if err != nil { 127 err = fmt.Errorf("failed to parse the received packet count: %v", err) 128 return 129 } 130 loss, err = strconv.Atoi(matches[3]) 131 if err != nil { 132 err = fmt.Errorf("failed to parse the loss percentage: %v", err) 133 return 134 } 135 return 136 }