Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1 | package hst |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 2 | |
| 3 | import ( |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 4 | "fmt" |
| 5 | "io" |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 6 | "net" |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 7 | "net/http" |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 8 | "net/http/httputil" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 9 | "os" |
Matus Fabian | 8792e5c | 2024-08-14 12:38:20 +0200 | [diff] [blame] | 10 | "os/exec" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 11 | "strings" |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 12 | "time" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 13 | ) |
| 14 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 15 | const networkTopologyDir string = "topo-network/" |
| 16 | const containerTopologyDir string = "topo-containers/" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 17 | |
| 18 | type Stanza struct { |
| 19 | content string |
| 20 | pad int |
| 21 | } |
| 22 | |
| 23 | type ActionResult struct { |
| 24 | Err error |
| 25 | Desc string |
| 26 | ErrOutput string |
| 27 | StdOutput string |
| 28 | } |
| 29 | |
| 30 | type JsonResult struct { |
| 31 | Code int |
| 32 | Desc string |
| 33 | ErrOutput string |
| 34 | StdOutput string |
| 35 | } |
| 36 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 37 | func AssertFileSize(f1, f2 string) error { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 38 | fi1, err := os.Stat(f1) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | fi2, err1 := os.Stat(f2) |
| 44 | if err1 != nil { |
| 45 | return err1 |
| 46 | } |
| 47 | |
| 48 | if fi1.Size() != fi2.Size() { |
| 49 | return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size()) |
| 50 | } |
| 51 | return nil |
| 52 | } |
| 53 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 54 | func (c *Stanza) NewStanza(name string) *Stanza { |
| 55 | c.Append("\n" + name + " {") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 56 | c.pad += 2 |
| 57 | return c |
| 58 | } |
| 59 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 60 | func (c *Stanza) Append(name string) *Stanza { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 61 | c.content += strings.Repeat(" ", c.pad) |
| 62 | c.content += name + "\n" |
| 63 | return c |
| 64 | } |
| 65 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 66 | func (c *Stanza) Close() *Stanza { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 67 | c.content += "}\n" |
| 68 | c.pad -= 2 |
| 69 | return c |
| 70 | } |
| 71 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 72 | func (s *Stanza) ToString() string { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 73 | return s.content |
| 74 | } |
| 75 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 76 | func (s *Stanza) SaveToFile(fileName string) error { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 77 | fo, err := os.Create(fileName) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | defer fo.Close() |
| 82 | |
| 83 | _, err = io.Copy(fo, strings.NewReader(s.content)) |
| 84 | return err |
| 85 | } |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 86 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 87 | // NewHttpClient creates [http.Client] with disabled proxy and redirects, it also sets timeout to 30seconds. |
| 88 | func NewHttpClient() *http.Client { |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 89 | transport := http.DefaultTransport |
| 90 | transport.(*http.Transport).Proxy = nil |
| 91 | transport.(*http.Transport).DisableKeepAlives = true |
| 92 | client := &http.Client{ |
| 93 | Transport: transport, |
| 94 | Timeout: time.Second * 30, |
| 95 | CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 96 | return http.ErrUseLastResponse |
| 97 | }} |
| 98 | return client |
| 99 | } |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 100 | |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 101 | func DumpHttpResp(resp *http.Response, body bool) string { |
| 102 | dump, err := httputil.DumpResponse(resp, body) |
| 103 | if err != nil { |
| 104 | return "" |
| 105 | } |
| 106 | return string(dump) |
| 107 | } |
| 108 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 109 | func TcpSendReceive(address, data string) (string, error) { |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 110 | conn, err := net.DialTimeout("tcp", address, time.Second*30) |
| 111 | if err != nil { |
| 112 | return "", err |
| 113 | } |
| 114 | defer conn.Close() |
| 115 | err = conn.SetDeadline(time.Now().Add(time.Second * 30)) |
| 116 | if err != nil { |
| 117 | return "", err |
| 118 | } |
| 119 | _, err = conn.Write([]byte(data)) |
| 120 | if err != nil { |
| 121 | return "", err |
| 122 | } |
| 123 | reply := make([]byte, 1024) |
| 124 | _, err = conn.Read(reply) |
| 125 | if err != nil { |
| 126 | return "", err |
| 127 | } |
| 128 | return string(reply), nil |
| 129 | } |
Matus Fabian | 8792e5c | 2024-08-14 12:38:20 +0200 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | RunCurlContainer execute curl command with given args. |
| 133 | Container with name "curl" must be available. |
| 134 | Curl runs in verbose mode and progress meter switch off by default. |
| 135 | */ |
| 136 | func (s *HstSuite) RunCurlContainer(args string) (string, string) { |
| 137 | curlCont := s.GetContainerByName("curl") |
| 138 | cmd := fmt.Sprintf("curl -v -s %s", args) |
| 139 | s.Log(cmd) |
| 140 | curlCont.ExtraRunningArgs = cmd |
| 141 | curlCont.Run() |
| 142 | stdout, stderr := curlCont.GetOutput() |
| 143 | s.Log(stderr) |
| 144 | s.Log(stdout) |
| 145 | return stdout, stderr |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | CollectNginxLogs save access and error logs to the test execution directory. |
| 150 | Nginx logging need to be set following way: |
| 151 | |
| 152 | - error_log <default-work-dir>/{{.LogPrefix}}-error.log; |
| 153 | - access_log <default-work-dir>/{{.LogPrefix}}-access.log; |
| 154 | |
| 155 | where LogPrefix is set to nginxContainer.Name |
| 156 | */ |
| 157 | func (s *HstSuite) CollectNginxLogs(containerName string) { |
| 158 | nginxContainer := s.GetContainerByName(containerName) |
Matus Fabian | 56f5bd9 | 2024-08-22 09:35:59 +0200 | [diff] [blame] | 159 | targetDir := nginxContainer.Suite.getLogDirPath() |
Matus Fabian | 8792e5c | 2024-08-14 12:38:20 +0200 | [diff] [blame] | 160 | source := nginxContainer.GetHostWorkDir() + "/" + nginxContainer.Name + "-" |
| 161 | cmd := exec.Command("cp", "-t", targetDir, source+"error.log", source+"access.log") |
| 162 | s.Log(cmd.String()) |
| 163 | err := cmd.Run() |
| 164 | if err != nil { |
| 165 | s.Log(fmt.Sprint(err)) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | CollectEnvoyLogs save access logs to the test execution directory. |
| 171 | Envoy access log path need to be set following way: |
| 172 | <default-work-dir>/{{.LogPrefix}}-access.log |
| 173 | where LogPrefix is set to envoyContainer.Name |
| 174 | */ |
| 175 | func (s *HstSuite) CollectEnvoyLogs(containerName string) { |
| 176 | envoyContainer := s.GetContainerByName(containerName) |
Matus Fabian | 56f5bd9 | 2024-08-22 09:35:59 +0200 | [diff] [blame] | 177 | targetDir := envoyContainer.Suite.getLogDirPath() |
Matus Fabian | 8792e5c | 2024-08-14 12:38:20 +0200 | [diff] [blame] | 178 | source := envoyContainer.GetHostWorkDir() + "/" + envoyContainer.Name + "-" |
| 179 | cmd := exec.Command("cp", "-t", targetDir, source+"access.log") |
| 180 | s.Log(cmd.String()) |
| 181 | err := cmd.Run() |
| 182 | if err != nil { |
| 183 | s.Log(fmt.Sprint(err)) |
| 184 | } |
| 185 | } |