hs-test: temp skip some tests
[vpp.git] / extras / hs-test / utils.go
1 package main
2
3 import (
4         "fmt"
5         "io"
6         "net/http"
7         "os"
8         "strings"
9         "time"
10 )
11
12 const networkTopologyDir string = "topo-network/"
13 const containerTopologyDir string = "topo-containers/"
14
15 type Stanza struct {
16         content string
17         pad     int
18 }
19
20 type ActionResult struct {
21         Err       error
22         Desc      string
23         ErrOutput string
24         StdOutput string
25 }
26
27 type JsonResult struct {
28         Code      int
29         Desc      string
30         ErrOutput string
31         StdOutput string
32 }
33
34 func assertFileSize(f1, f2 string) error {
35         fi1, err := os.Stat(f1)
36         if err != nil {
37                 return err
38         }
39
40         fi2, err1 := os.Stat(f2)
41         if err1 != nil {
42                 return err1
43         }
44
45         if fi1.Size() != fi2.Size() {
46                 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
47         }
48         return nil
49 }
50
51 func (c *Stanza) newStanza(name string) *Stanza {
52         c.append("\n" + name + " {")
53         c.pad += 2
54         return c
55 }
56
57 func (c *Stanza) append(name string) *Stanza {
58         c.content += strings.Repeat(" ", c.pad)
59         c.content += name + "\n"
60         return c
61 }
62
63 func (c *Stanza) close() *Stanza {
64         c.content += "}\n"
65         c.pad -= 2
66         return c
67 }
68
69 func (s *Stanza) toString() string {
70         return s.content
71 }
72
73 func (s *Stanza) saveToFile(fileName string) error {
74         fo, err := os.Create(fileName)
75         if err != nil {
76                 return err
77         }
78         defer fo.Close()
79
80         _, err = io.Copy(fo, strings.NewReader(s.content))
81         return err
82 }
83
84 // newHttpClient creates [http.Client] with disabled proxy and redirects, it also sets timeout to 30seconds.
85 func newHttpClient() *http.Client {
86         transport := http.DefaultTransport
87         transport.(*http.Transport).Proxy = nil
88         transport.(*http.Transport).DisableKeepAlives = true
89         client := &http.Client{
90                 Transport: transport,
91                 Timeout:   time.Second * 30,
92                 CheckRedirect: func(req *http.Request, via []*http.Request) error {
93                         return http.ErrUseLastResponse
94                 }}
95         return client
96 }