hs-test: log external apps
[vpp.git] / extras / hs-test / utils.go
1 package main
2
3 import (
4         "fmt"
5         "io"
6         "os"
7         "strings"
8 )
9
10 const networkTopologyDir string = "topo-network/"
11 const containerTopologyDir string = "topo-containers/"
12
13 type Stanza struct {
14         content string
15         pad     int
16 }
17
18 type ActionResult struct {
19         Err       error
20         Desc      string
21         ErrOutput string
22         StdOutput string
23 }
24
25 type JsonResult struct {
26         Code      int
27         Desc      string
28         ErrOutput string
29         StdOutput string
30 }
31
32 func assertFileSize(f1, f2 string) error {
33         fi1, err := os.Stat(f1)
34         if err != nil {
35                 return err
36         }
37
38         fi2, err1 := os.Stat(f2)
39         if err1 != nil {
40                 return err1
41         }
42
43         if fi1.Size() != fi2.Size() {
44                 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
45         }
46         return nil
47 }
48
49 func (c *Stanza) newStanza(name string) *Stanza {
50         c.append("\n" + name + " {")
51         c.pad += 2
52         return c
53 }
54
55 func (c *Stanza) append(name string) *Stanza {
56         c.content += strings.Repeat(" ", c.pad)
57         c.content += name + "\n"
58         return c
59 }
60
61 func (c *Stanza) close() *Stanza {
62         c.content += "}\n"
63         c.pad -= 2
64         return c
65 }
66
67 func (s *Stanza) toString() string {
68         return s.content
69 }
70
71 func (s *Stanza) saveToFile(fileName string) error {
72         fo, err := os.Create(fileName)
73         if err != nil {
74                 return err
75         }
76         defer fo.Close()
77
78         _, err = io.Copy(fo, strings.NewReader(s.content))
79         return err
80 }