Skip to content

Commit 5166432

Browse files
committed
Added env's to be able to configure timouts
Signed-off-by: Boris Popovschi <[email protected]>
1 parent 9e2ff62 commit 5166432

File tree

5 files changed

+57
-10
lines changed

5 files changed

+57
-10
lines changed

HACKING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ You need some external resources in order to run the tests, as described below:
3333
With all of those set up, `make test EXTRAGOARGS=-v` should create a Firecracker
3434
process and run the Linux kernel in a MicroVM.
3535

36+
There is also a possibility to configure timeouts in firecracker-go-sdk,
37+
you can set those env's to customize tests flow:
38+
```
39+
FIRECRACKER_GO_SDK_INIT_TIMEOUT_SECONDS
40+
FIRECRACKER_GO_SDK_REQUEST_TIMEOUT_MILLISECONDS
41+
```
42+
You can set them directly or with a help of buildkite, otherwise default values will be used.
43+
3644
Regenerating the API client
3745
---
3846

firecracker.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ import (
2626
ops "github.com/firecracker-microvm/firecracker-go-sdk/client/operations"
2727
)
2828

29-
const firecrackerRequestTimeout = 500 * time.Millisecond
29+
const (
30+
// env name to make firecracker request timeout configurable
31+
firecrackerRequestTimeoutEnv = "FIRECRACKER_GO_SDK_REQUEST_TIMEOUT_MILLISECONDS"
32+
33+
defaultFirecrackerRequestTimeout = 500
34+
)
3035

3136
// newFirecrackerClient creates a FirecrackerClient
3237
func newFirecrackerClient(socketPath string, logger *logrus.Entry, debug bool) *client.Firecracker {
@@ -51,13 +56,18 @@ func WithOpsClient(opsClient ops.ClientIface) ClientOpt {
5156

5257
// Client is a client for interacting with the Firecracker API
5358
type Client struct {
54-
client *client.Firecracker
59+
client *client.Firecracker
60+
firecrackerRequestTimeout int
61+
firecrackerInitTimeout int
5562
}
5663

5764
// NewClient creates a Client
5865
func NewClient(socketPath string, logger *logrus.Entry, debug bool, opts ...ClientOpt) *Client {
5966
httpClient := newFirecrackerClient(socketPath, logger, debug)
6067
c := &Client{client: httpClient}
68+
c.firecrackerRequestTimeout = envValueOrDefaultInt(firecrackerRequestTimeoutEnv, defaultFirecrackerRequestTimeout)
69+
c.firecrackerInitTimeout = envValueOrDefaultInt(firecrackerInitTimeoutEnv, defaultFirecrackerInitTimeoutSeconds)
70+
6171
for _, opt := range opts {
6272
opt(c)
6373
}
@@ -72,7 +82,7 @@ type PutLoggerOpt func(*ops.PutLoggerParams)
7282
// PutLogger is a wrapper for the swagger generated client to make calling of
7383
// the API easier.
7484
func (f *Client) PutLogger(ctx context.Context, logger *models.Logger, opts ...PutLoggerOpt) (*ops.PutLoggerNoContent, error) {
75-
timeout, cancel := context.WithTimeout(ctx, firecrackerRequestTimeout)
85+
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
7686
defer cancel()
7787

7888
loggerParams := ops.NewPutLoggerParamsWithContext(timeout)
@@ -91,7 +101,7 @@ type PutMachineConfigurationOpt func(*ops.PutMachineConfigurationParams)
91101
// PutMachineConfiguration is a wrapper for the swagger generated client to
92102
// make calling of the API easier.
93103
func (f *Client) PutMachineConfiguration(ctx context.Context, cfg *models.MachineConfiguration, opts ...PutMachineConfigurationOpt) (*ops.PutMachineConfigurationNoContent, error) {
94-
timeout, cancel := context.WithTimeout(ctx, firecrackerRequestTimeout)
104+
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
95105
defer cancel()
96106

97107
mc := ops.NewPutMachineConfigurationParamsWithContext(timeout)
@@ -110,7 +120,7 @@ type PutGuestBootSourceOpt func(*ops.PutGuestBootSourceParams)
110120
// PutGuestBootSource is a wrapper for the swagger generated client to make
111121
// calling of the API easier.
112122
func (f *Client) PutGuestBootSource(ctx context.Context, source *models.BootSource, opts ...PutGuestBootSourceOpt) (*ops.PutGuestBootSourceNoContent, error) {
113-
timeout, cancel := context.WithTimeout(ctx, firecrackerRequestTimeout)
123+
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
114124
defer cancel()
115125

116126
bootSource := ops.NewPutGuestBootSourceParamsWithContext(timeout)
@@ -129,7 +139,7 @@ type PutGuestNetworkInterfaceByIDOpt func(*ops.PutGuestNetworkInterfaceByIDParam
129139
// PutGuestNetworkInterfaceByID is a wrapper for the swagger generated client
130140
// to make calling of the API easier.
131141
func (f *Client) PutGuestNetworkInterfaceByID(ctx context.Context, ifaceID string, ifaceCfg *models.NetworkInterface, opts ...PutGuestNetworkInterfaceByIDOpt) (*ops.PutGuestNetworkInterfaceByIDNoContent, error) {
132-
timeout, cancel := context.WithTimeout(ctx, firecrackerRequestTimeout)
142+
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
133143
defer cancel()
134144

135145
cfg := ops.NewPutGuestNetworkInterfaceByIDParamsWithContext(timeout)
@@ -149,7 +159,7 @@ type PatchGuestNetworkInterfaceByIDOpt func(*ops.PatchGuestNetworkInterfaceByIDP
149159
// PatchGuestNetworkInterfaceByID is a wrapper for the swagger generated client to make calling of the
150160
// API easier.
151161
func (f *Client) PatchGuestNetworkInterfaceByID(ctx context.Context, ifaceID string, ifaceCfg *models.PartialNetworkInterface, opts ...PatchGuestNetworkInterfaceByIDOpt) (*ops.PatchGuestNetworkInterfaceByIDNoContent, error) {
152-
timeout, cancel := context.WithTimeout(ctx, firecrackerRequestTimeout)
162+
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)*time.Millisecond)
153163
defer cancel()
154164

155165
cfg := ops.NewPatchGuestNetworkInterfaceByIDParamsWithContext(timeout)
@@ -170,7 +180,7 @@ type PutGuestDriveByIDOpt func(*ops.PutGuestDriveByIDParams)
170180
// PutGuestDriveByID is a wrapper for the swagger generated client to make
171181
// calling of the API easier.
172182
func (f *Client) PutGuestDriveByID(ctx context.Context, driveID string, drive *models.Drive, opts ...PutGuestDriveByIDOpt) (*ops.PutGuestDriveByIDNoContent, error) {
173-
timeout, cancel := context.WithTimeout(ctx, 250*time.Millisecond)
183+
timeout, cancel := context.WithTimeout(ctx, time.Duration(f.firecrackerRequestTimeout)/2*time.Millisecond)
174184
defer cancel()
175185

176186
params := ops.NewPutGuestDriveByIDParamsWithContext(timeout)
@@ -275,7 +285,7 @@ type GetMachineConfigurationOpt func(*ops.GetMachineConfigurationParams)
275285
// calling of the API easier.
276286
func (f *Client) GetMachineConfiguration(opts ...GetMachineConfigurationOpt) (*ops.GetMachineConfigurationOK, error) {
277287
p := ops.NewGetMachineConfigurationParams()
278-
p.SetTimeout(firecrackerRequestTimeout)
288+
p.SetTimeout(time.Duration(f.firecrackerRequestTimeout) * time.Millisecond)
279289
for _, opt := range opts {
280290
opt(p)
281291
}

machine.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ const (
4242

4343
// as specified in http://man7.org/linux/man-pages/man8/ip-netns.8.html
4444
defaultNetNSDir = "/var/run/netns"
45+
46+
// env name to make firecracker init timeout configurable
47+
firecrackerInitTimeoutEnv = "FIRECRACKER_GO_SDK_INIT_TIMEOUT_SECONDS"
48+
49+
defaultFirecrackerInitTimeoutSeconds = 3
4550
)
4651

4752
// ErrAlreadyStarted signifies that the Machine has already started and cannot
@@ -492,7 +497,7 @@ func (m *Machine) startVMM(ctx context.Context) error {
492497
}()
493498

494499
// Wait for firecracker to initialize:
495-
err = m.waitForSocket(3*time.Second, errCh)
500+
err = m.waitForSocket(time.Duration(m.client.firecrackerInitTimeout)*time.Second, errCh)
496501
if err != nil {
497502
err = errors.Wrapf(err, "Firecracker did not create API socket %s", m.Cfg.SocketPath)
498503
m.fatalErr = err

utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package firecracker
22

33
import (
44
"context"
5+
"os"
6+
"strconv"
57
"time"
68
)
79

@@ -27,3 +29,13 @@ func waitForAliveVMM(ctx context.Context, client *Client) error {
2729
}
2830
}
2931
}
32+
33+
// envValueOrDefaultInt check if env value exists and returns it or returns default value
34+
// provided as a second param to this function
35+
func envValueOrDefaultInt(envName string, def int) int {
36+
envVal, err := strconv.Atoi(os.Getenv(envName))
37+
if envVal == 0 || err != nil {
38+
envVal = def
39+
}
40+
return envVal
41+
}

utils_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package firecracker
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestEnvValueOrDefaultInt(t *testing.T) {
10+
defaultVal := 500
11+
assert.Equal(t, defaultVal, envValueOrDefaultInt("UNEXISTS_ENV", defaultVal))
12+
}

0 commit comments

Comments
 (0)