blob: 151567c3fed43fc4612ba46ad1510525a7c5510d [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
Filip Tehlar229f5fc2022-08-09 14:44:47 +00004 "errors"
5 "fmt"
6 "io"
Filip Tehlar229f5fc2022-08-09 14:44:47 +00007 "os"
8 "os/exec"
9 "strings"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000010 "time"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000011)
12
Maros Ondrejicka0db15752022-10-12 22:58:01 +020013const vclTemplate = `vcl {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010014 app-socket-api %[1]s/var/run/app_ns_sockets/%[2]s
Maros Ondrejicka0db15752022-10-12 22:58:01 +020015 app-scope-global
16 app-scope-local
17 namespace-id %[2]s
18 namespace-secret %[2]s
19 use-mq-eventfd
20}
21`
22
Maros Ondrejickae7625d02023-02-28 16:55:01 +010023const networkTopologyDir string = "topo-network/"
24const containerTopologyDir string = "topo-containers/"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000025
26type Stanza struct {
27 content string
28 pad int
29}
30
31type ActionResult struct {
32 Err error
33 Desc string
34 ErrOutput string
35 StdOutput string
36}
37
38type JsonResult struct {
39 Code int
40 Desc string
41 ErrOutput string
42 StdOutput string
43}
44
Maros Ondrejickae7625d02023-02-28 16:55:01 +010045func startServerApp(running chan error, done chan struct{}, env []string) {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000046 cmd := exec.Command("iperf3", "-4", "-s")
47 if env != nil {
48 cmd.Env = env
49 }
50 err := cmd.Start()
51 if err != nil {
52 msg := fmt.Errorf("failed to start iperf server: %v", err)
53 running <- msg
54 return
55 }
56 running <- nil
57 <-done
58 cmd.Process.Kill()
59}
60
Maros Ondrejickae7625d02023-02-28 16:55:01 +010061func startClientApp(ipAddress string, env []string, clnCh chan error, clnRes chan string) {
Filip Tehlar229f5fc2022-08-09 14:44:47 +000062 defer func() {
63 clnCh <- nil
64 }()
65
66 nTries := 0
67
68 for {
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010069 cmd := exec.Command("iperf3", "-c", ipAddress, "-u", "-l", "1460", "-b", "10g")
Filip Tehlar229f5fc2022-08-09 14:44:47 +000070 if env != nil {
71 cmd.Env = env
72 }
73 o, err := cmd.CombinedOutput()
74 if err != nil {
75 if nTries > 5 {
76 clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o)
77 return
78 }
79 time.Sleep(1 * time.Second)
80 nTries++
81 continue
82 } else {
Maros Ondrejicka87531802022-12-19 20:35:27 +010083 clnRes <- fmt.Sprintf("Client output: %s", o)
Filip Tehlar229f5fc2022-08-09 14:44:47 +000084 }
85 break
86 }
87}
88
Filip Tehlar229f5fc2022-08-09 14:44:47 +000089func assertFileSize(f1, f2 string) error {
90 fi1, err := os.Stat(f1)
91 if err != nil {
92 return err
93 }
94
95 fi2, err1 := os.Stat(f2)
96 if err1 != nil {
97 return err1
98 }
99
100 if fi1.Size() != fi2.Size() {
101 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
102 }
103 return nil
104}
105
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000106func startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100107 cmd := newCommand([]string{"./http_server", addressPort}, netNs)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000108 err := cmd.Start()
109 if err != nil {
110 fmt.Println("Failed to start http server")
111 return
112 }
113 running <- struct{}{}
114 <-done
115 cmd.Process.Kill()
116}
117
Filip Tehlarc204c872022-12-21 08:59:16 +0100118func startWget(finished chan error, server_ip, port, query, netNs string) {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000119 defer func() {
120 finished <- errors.New("wget error")
121 }()
122
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100123 cmd := newCommand([]string{"wget", "--timeout=10", "--no-proxy", "--tries=5", "-O", "/dev/null", server_ip + ":" + port + "/" + query},
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000124 netNs)
125 o, err := cmd.CombinedOutput()
126 if err != nil {
Filip Tehlarc204c872022-12-21 08:59:16 +0100127 finished <- fmt.Errorf("wget error: '%v\n\n%s'", err, o)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000128 return
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100129 } else if strings.Contains(string(o), "200 OK") == false {
130 finished <- fmt.Errorf("wget error: response not 200 OK")
131 return
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000132 }
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000133 finished <- nil
134}
135
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100136func (c *Stanza) newStanza(name string) *Stanza {
137 c.append("\n" + name + " {")
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000138 c.pad += 2
139 return c
140}
141
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100142func (c *Stanza) append(name string) *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000143 c.content += strings.Repeat(" ", c.pad)
144 c.content += name + "\n"
145 return c
146}
147
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100148func (c *Stanza) close() *Stanza {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000149 c.content += "}\n"
150 c.pad -= 2
151 return c
152}
153
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100154func (s *Stanza) toString() string {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000155 return s.content
156}
157
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100158func (s *Stanza) saveToFile(fileName string) error {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000159 fo, err := os.Create(fileName)
160 if err != nil {
161 return err
162 }
163 defer fo.Close()
164
165 _, err = io.Copy(fo, strings.NewReader(s.content))
166 return err
167}