Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 4 | "errors" |
| 5 | "fmt" |
| 6 | "io" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 7 | "os" |
| 8 | "os/exec" |
| 9 | "strings" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 10 | "time" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 11 | ) |
| 12 | |
Maros Ondrejicka | 0db1575 | 2022-10-12 22:58:01 +0200 | [diff] [blame] | 13 | const vclTemplate = `vcl { |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 14 | app-socket-api %[1]s/var/run/app_ns_sockets/%[2]s |
Maros Ondrejicka | 0db1575 | 2022-10-12 22:58:01 +0200 | [diff] [blame] | 15 | 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 Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 23 | const networkTopologyDir string = "topo-network/" |
| 24 | const containerTopologyDir string = "topo-containers/" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 25 | |
| 26 | type Stanza struct { |
| 27 | content string |
| 28 | pad int |
| 29 | } |
| 30 | |
| 31 | type ActionResult struct { |
| 32 | Err error |
| 33 | Desc string |
| 34 | ErrOutput string |
| 35 | StdOutput string |
| 36 | } |
| 37 | |
| 38 | type JsonResult struct { |
| 39 | Code int |
| 40 | Desc string |
| 41 | ErrOutput string |
| 42 | StdOutput string |
| 43 | } |
| 44 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 45 | func startServerApp(running chan error, done chan struct{}, env []string) { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 46 | 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 Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 61 | func startClientApp(ipAddress string, env []string, clnCh chan error, clnRes chan string) { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 62 | defer func() { |
| 63 | clnCh <- nil |
| 64 | }() |
| 65 | |
| 66 | nTries := 0 |
| 67 | |
| 68 | for { |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 69 | cmd := exec.Command("iperf3", "-c", ipAddress, "-u", "-l", "1460", "-b", "10g") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 70 | 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 Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 83 | clnRes <- fmt.Sprintf("Client output: %s", o) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 84 | } |
| 85 | break |
| 86 | } |
| 87 | } |
| 88 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 89 | func 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 Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 106 | func startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) { |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 107 | cmd := newCommand([]string{"./http_server", addressPort}, netNs) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 108 | 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 Tehlar | c204c87 | 2022-12-21 08:59:16 +0100 | [diff] [blame] | 118 | func startWget(finished chan error, server_ip, port, query, netNs string) { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 119 | defer func() { |
| 120 | finished <- errors.New("wget error") |
| 121 | }() |
| 122 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 123 | cmd := newCommand([]string{"wget", "--timeout=10", "--no-proxy", "--tries=5", "-O", "/dev/null", server_ip + ":" + port + "/" + query}, |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 124 | netNs) |
| 125 | o, err := cmd.CombinedOutput() |
| 126 | if err != nil { |
Filip Tehlar | c204c87 | 2022-12-21 08:59:16 +0100 | [diff] [blame] | 127 | finished <- fmt.Errorf("wget error: '%v\n\n%s'", err, o) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 128 | return |
Maros Ondrejicka | 2908f8c | 2023-02-02 08:58:04 +0100 | [diff] [blame] | 129 | } else if strings.Contains(string(o), "200 OK") == false { |
| 130 | finished <- fmt.Errorf("wget error: response not 200 OK") |
| 131 | return |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 132 | } |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 133 | finished <- nil |
| 134 | } |
| 135 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 136 | func (c *Stanza) newStanza(name string) *Stanza { |
| 137 | c.append("\n" + name + " {") |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 138 | c.pad += 2 |
| 139 | return c |
| 140 | } |
| 141 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 142 | func (c *Stanza) append(name string) *Stanza { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 143 | c.content += strings.Repeat(" ", c.pad) |
| 144 | c.content += name + "\n" |
| 145 | return c |
| 146 | } |
| 147 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 148 | func (c *Stanza) close() *Stanza { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 149 | c.content += "}\n" |
| 150 | c.pad -= 2 |
| 151 | return c |
| 152 | } |
| 153 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 154 | func (s *Stanza) toString() string { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 155 | return s.content |
| 156 | } |
| 157 | |
Maros Ondrejicka | e7625d0 | 2023-02-28 16:55:01 +0100 | [diff] [blame] | 158 | func (s *Stanza) saveToFile(fileName string) error { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 159 | 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 | } |