blob: 9dd2dbd73c81f82416b1dd9718ff7e8a65b21c40 [file] [log] [blame]
Adrian Villin4677d922024-06-14 09:32:39 +02001package hst
Filip Tehlar229f5fc2022-08-09 14:44:47 +00002
3import (
Filip Tehlar229f5fc2022-08-09 14:44:47 +00004 "fmt"
5 "io"
Matus Fabian82ad9662024-06-04 19:00:00 +02006 "net"
Matus Fabian5409d332024-05-28 13:39:13 +02007 "net/http"
Matus Fabian8ca6ce62024-06-20 17:08:26 +02008 "net/http/httputil"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00009 "os"
Matus Fabian8792e5c2024-08-14 12:38:20 +020010 "os/exec"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000011 "strings"
Matus Fabian5409d332024-05-28 13:39:13 +020012 "time"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000013)
14
Maros Ondrejickae7625d02023-02-28 16:55:01 +010015const networkTopologyDir string = "topo-network/"
16const containerTopologyDir string = "topo-containers/"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000017
18type Stanza struct {
19 content string
20 pad int
21}
22
23type ActionResult struct {
24 Err error
25 Desc string
26 ErrOutput string
27 StdOutput string
28}
29
30type JsonResult struct {
31 Code int
32 Desc string
33 ErrOutput string
34 StdOutput string
35}
36
Adrian Villin4677d922024-06-14 09:32:39 +020037func AssertFileSize(f1, f2 string) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000038 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 Villin4677d922024-06-14 09:32:39 +020054func (c *Stanza) NewStanza(name string) *Stanza {
55 c.Append("\n" + name + " {")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000056 c.pad += 2
57 return c
58}
59
Adrian Villin4677d922024-06-14 09:32:39 +020060func (c *Stanza) Append(name string) *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000061 c.content += strings.Repeat(" ", c.pad)
62 c.content += name + "\n"
63 return c
64}
65
Adrian Villin4677d922024-06-14 09:32:39 +020066func (c *Stanza) Close() *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000067 c.content += "}\n"
68 c.pad -= 2
69 return c
70}
71
Adrian Villin4677d922024-06-14 09:32:39 +020072func (s *Stanza) ToString() string {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000073 return s.content
74}
75
Adrian Villin4677d922024-06-14 09:32:39 +020076func (s *Stanza) SaveToFile(fileName string) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000077 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 Fabian5409d332024-05-28 13:39:13 +020086
Adrian Villin4677d922024-06-14 09:32:39 +020087// NewHttpClient creates [http.Client] with disabled proxy and redirects, it also sets timeout to 30seconds.
88func NewHttpClient() *http.Client {
Matus Fabian5409d332024-05-28 13:39:13 +020089 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 Fabian82ad9662024-06-04 19:00:00 +0200100
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200101func 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 Villin4677d922024-06-14 09:32:39 +0200109func TcpSendReceive(address, data string) (string, error) {
Matus Fabian82ad9662024-06-04 19:00:00 +0200110 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 Fabian8792e5c2024-08-14 12:38:20 +0200130
131/*
132RunCurlContainer execute curl command with given args.
133Container with name "curl" must be available.
134Curl runs in verbose mode and progress meter switch off by default.
135*/
136func (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/*
149CollectNginxLogs save access and error logs to the test execution directory.
150Nginx 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
155where LogPrefix is set to nginxContainer.Name
156*/
157func (s *HstSuite) CollectNginxLogs(containerName string) {
158 nginxContainer := s.GetContainerByName(containerName)
Matus Fabian56f5bd92024-08-22 09:35:59 +0200159 targetDir := nginxContainer.Suite.getLogDirPath()
Matus Fabian8792e5c2024-08-14 12:38:20 +0200160 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/*
170CollectEnvoyLogs save access logs to the test execution directory.
171Envoy access log path need to be set following way:
172<default-work-dir>/{{.LogPrefix}}-access.log
173where LogPrefix is set to envoyContainer.Name
174*/
175func (s *HstSuite) CollectEnvoyLogs(containerName string) {
176 envoyContainer := s.GetContainerByName(containerName)
Matus Fabian56f5bd92024-08-22 09:35:59 +0200177 targetDir := envoyContainer.Suite.getLogDirPath()
Matus Fabian8792e5c2024-08-14 12:38:20 +0200178 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}