/
net.go
51 lines (44 loc) 路 1.22 KB
/
net.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package utils
import (
"context"
"fmt"
"os"
"time"
"github.com/ipfs/testground/sdk/runtime"
"github.com/ipfs/testground/sdk/sync"
)
// SetupNetwork instructs the sidecar (if enabled) to setup the network for this
// test case.
func SetupNetwork(ctx context.Context, runenv *runtime.RunEnv, watcher *sync.Watcher, writer *sync.Writer) error {
if !runenv.TestSidecar {
return nil
}
// Wait for the network to be initialized.
if err := sync.WaitNetworkInitialized(ctx, runenv, watcher); err != nil {
return err
}
// TODO: just put the unique testplan id inside the runenv?
hostname, err := os.Hostname()
if err != nil {
return err
}
latency := time.Duration(runenv.IntParam("latency_ms")) * time.Millisecond
bandwidth := runenv.IntParam("bandwidth_mb")
_, err = writer.Write(ctx, sync.NetworkSubtree(hostname), &sync.NetworkConfig{
Network: "default",
Enable: true,
Default: sync.LinkShape{
Latency: latency,
Bandwidth: uint64(bandwidth) * 1024 * 1024,
},
State: "network-configured",
})
if err != nil {
return err
}
err = <-watcher.Barrier(ctx, "network-configured", int64(runenv.TestInstanceCount))
if err != nil {
return fmt.Errorf("failed to configure network: %w", err)
}
return nil
}