Skip to content

Commit

Permalink
bus: cleanup client
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Nov 14, 2024
1 parent 75de934 commit ce2033f
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 61 deletions.
2 changes: 1 addition & 1 deletion autopilot/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (ap *Autopilot) blockUntilEnabled(interrupt <-chan time.Time) (enabled, int

for {
autopilot, err := ap.bus.Autopilot(ap.shutdownCtx)
if err != nil {
if err != nil && !errors.Is(err, context.Canceled) {
ap.logger.Errorf("unable to fetch autopilot from the bus, err: %v", err)
}

Expand Down
64 changes: 31 additions & 33 deletions bus/client/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,45 @@ import (
"go.sia.tech/renterd/api"
)

var (
enable = true
disable = false
)
type UpdateAutopilotOption func(*api.UpdateAutopilotRequest)

// Autopilot returns the autopilot.
func (c *Client) Autopilot(ctx context.Context) (ap api.Autopilot, err error) {
err = c.c.WithContext(ctx).GET("/autopilot", &ap)
return
func WithAutopilotEnabled(enabled bool) UpdateAutopilotOption {
return func(req *api.UpdateAutopilotRequest) {
req.Enabled = &enabled
}
}

// DisableAutopilot disables the autopilot.
func (c *Client) DisableAutopilot(ctx context.Context) error {
return c.updateAutopilot(ctx, &disable, nil, nil, nil)
func WithContractsConfig(cfg api.ContractsConfig) UpdateAutopilotOption {
return func(req *api.UpdateAutopilotRequest) {
req.Contracts = &cfg
}
}

// EnableAutopilot enables the autopilot.
func (c *Client) EnableAutopilot(ctx context.Context) error {
return c.updateAutopilot(ctx, &enable, nil, nil, nil)
func WithCurrentPeriod(currentPeriod uint64) UpdateAutopilotOption {
return func(req *api.UpdateAutopilotRequest) {
req.CurrentPeriod = &currentPeriod
}
}

// UpdateCurrentPeriod updates the autopilot's current period in the bus.
func (c *Client) UpdateCurrentPeriod(ctx context.Context, currentPeriod uint64) error {
return c.updateAutopilot(ctx, nil, nil, nil, &currentPeriod)
func WithHostsConfig(cfg api.HostsConfig) UpdateAutopilotOption {
return func(req *api.UpdateAutopilotRequest) {
req.Hosts = &cfg
}
}

// UpdateContractsConfig updates the autopilot's contract configuration in the bus.
func (c *Client) UpdateContractsConfig(ctx context.Context, cfg api.ContractsConfig) error {
return c.updateAutopilot(ctx, nil, &cfg, nil, nil)
// Autopilot returns the autopilot.
func (c *Client) Autopilot(ctx context.Context) (ap api.Autopilot, err error) {
err = c.c.WithContext(ctx).GET("/autopilot", &ap)
return
}

// UpdateHostsConfig updates the autopilot's hosts configuration in the bus.
func (c *Client) UpdateHostsConfig(ctx context.Context, cfg api.HostsConfig) error {
return c.updateAutopilot(ctx, nil, nil, &cfg, nil)
// UpdateAutopilot updates the autopilot.
func (c *Client) UpdateAutopilot(ctx context.Context, opts ...UpdateAutopilotOption) error {
var req api.UpdateAutopilotRequest
for _, opt := range opts {
opt(&req)
}
return c.c.WithContext(ctx).PUT("/autopilot", req)
}

func (c *Client) updateAutopilot(ctx context.Context, enabled *bool, contracts *api.ContractsConfig, hosts *api.HostsConfig, currentPeriod *uint64) error {
return c.c.WithContext(ctx).PUT("/autopilot", api.UpdateAutopilotRequest{
Enabled: enabled,
Contracts: contracts,
CurrentPeriod: currentPeriod,
Hosts: hosts,
})
// UpdateCurrentPeriod updates the current period.
func (c *Client) UpdateCurrentPeriod(ctx context.Context, currentPeriod uint64) error {
return c.UpdateAutopilot(ctx, WithCurrentPeriod(currentPeriod))
}
37 changes: 19 additions & 18 deletions internal/test/e2e/autopilot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"go.sia.tech/renterd/api"
"go.sia.tech/renterd/bus/client"
"go.sia.tech/renterd/internal/test"
"go.sia.tech/renterd/internal/utils"
)
Expand All @@ -31,38 +32,38 @@ func TestAutopilot(t *testing.T) {
t.Fatalf("hosts config should be defaulted, got %v", ap.Hosts)
}

// assert hosts config is validated
hosts := ap.Hosts
hosts.MaxDowntimeHours = 99*365*24 + 1 // exceed by one
if err := b.UpdateHostsConfig(context.Background(), hosts); !utils.IsErr(err, api.ErrMaxDowntimeHoursTooHigh) {
// assert h config is validated
h := ap.Hosts
h.MaxDowntimeHours = 99*365*24 + 1 // exceed by one
if err := b.UpdateAutopilot(context.Background(), client.WithHostsConfig(h)); !utils.IsErr(err, api.ErrMaxDowntimeHoursTooHigh) {
t.Fatal("unexpected", err)
}
hosts.MaxDowntimeHours = 99 * 365 * 24 // allowed max
tt.OK(b.UpdateHostsConfig(context.Background(), hosts))
h.MaxDowntimeHours = 99 * 365 * 24 // allowed max
tt.OK(b.UpdateAutopilot(context.Background(), client.WithHostsConfig(h)))

hosts.MinProtocolVersion = "not a version"
if err := b.UpdateHostsConfig(context.Background(), hosts); !utils.IsErr(err, api.ErrInvalidReleaseVersion) {
h.MinProtocolVersion = "not a version"
if err := b.UpdateAutopilot(context.Background(), client.WithHostsConfig(h)); !utils.IsErr(err, api.ErrInvalidReleaseVersion) {
t.Fatal("unexpected")
}

// assert contracts config is validated
contracts := ap.Contracts
contracts.Period = 0 // invalid period
if err := b.UpdateContractsConfig(context.Background(), contracts); err == nil || !strings.Contains(err.Error(), "period must be greater than 0") {
// assert c config is validated
c := ap.Contracts
c.Period = 0 // invalid period
if err := b.UpdateAutopilot(context.Background(), client.WithContractsConfig(c)); err == nil || !strings.Contains(err.Error(), "period must be greater than 0") {
t.Fatal("unexpected", err)
}
contracts.Period = 1 // valid period
contracts.RenewWindow = 0 // invalid renew window
if err := b.UpdateContractsConfig(context.Background(), contracts); err == nil || !strings.Contains(err.Error(), "renewWindow must be greater than 0") {
c.Period = 1 // valid period
c.RenewWindow = 0 // invalid renew window
if err := b.UpdateAutopilot(context.Background(), client.WithContractsConfig(c)); err == nil || !strings.Contains(err.Error(), "renewWindow must be greater than 0") {
t.Fatal("unexpected", err)
}
contracts.RenewWindow = 1 // valid renew window
if err := b.UpdateContractsConfig(context.Background(), contracts); err != nil {
c.RenewWindow = 1 // valid renew window
if err := b.UpdateAutopilot(context.Background(), client.WithContractsConfig(c)); err != nil {
t.Fatal(err)
}

// assert we can disable the autopilot
tt.OK(b.DisableAutopilot(context.Background()))
tt.OK(b.UpdateAutopilot(context.Background(), client.WithAutopilotEnabled(false)))
ap, err = b.Autopilot(context.Background())
tt.OK(err)
if ap.Enabled {
Expand Down
9 changes: 6 additions & 3 deletions internal/test/e2e/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"go.sia.tech/renterd/api"
"go.sia.tech/renterd/autopilot"
"go.sia.tech/renterd/bus"
"go.sia.tech/renterd/bus/client"
"go.sia.tech/renterd/config"
"go.sia.tech/renterd/internal/test"
"go.sia.tech/renterd/internal/utils"
Expand Down Expand Up @@ -452,9 +453,11 @@ func newTestCluster(t *testing.T, opts testClusterOptions) *TestCluster {
// Update the autopilot to use test settings
if !opts.skipSettingAutopilot {
tt.OKAll(
busClient.UpdateContractsConfig(ctx, apConfig.Contracts),
busClient.UpdateHostsConfig(ctx, apConfig.Hosts),
busClient.EnableAutopilot(ctx),
busClient.UpdateAutopilot(ctx,
client.WithContractsConfig(apConfig.Contracts),
client.WithHostsConfig(apConfig.Hosts),
client.WithAutopilotEnabled(true),
),
)
}

Expand Down
12 changes: 8 additions & 4 deletions internal/test/e2e/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"go.sia.tech/renterd/alerts"
"go.sia.tech/renterd/api"
"go.sia.tech/renterd/autopilot/contractor"
"go.sia.tech/renterd/bus/client"
"go.sia.tech/renterd/config"
"go.sia.tech/renterd/internal/test"
"go.sia.tech/renterd/internal/utils"
Expand Down Expand Up @@ -2253,10 +2254,13 @@ func TestWalletFormUnconfirmed(t *testing.T) {
}

// enable the autopilot by configuring it
tt.OKAll(
b.UpdateContractsConfig(context.Background(), test.AutopilotConfig.Contracts),
b.UpdateHostsConfig(context.Background(), test.AutopilotConfig.Hosts),
b.EnableAutopilot(context.Background()),
tt.OK(
b.UpdateAutopilot(
context.Background(),
client.WithContractsConfig(test.AutopilotConfig.Contracts),
client.WithHostsConfig(test.AutopilotConfig.Hosts),
client.WithAutopilotEnabled(true),
),
)

// wait for a contract to form
Expand Down
3 changes: 2 additions & 1 deletion internal/test/e2e/contracts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"go.sia.tech/core/types"
"go.sia.tech/renterd/api"
"go.sia.tech/renterd/bus/client"
"go.sia.tech/renterd/internal/test"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func TestFormContract(t *testing.T) {
// renew the contract we formed
contracts := ap.Contracts
contracts.Amount = 1
tt.OK(b.UpdateContractsConfig(context.Background(), contracts))
tt.OK(b.UpdateAutopilot(context.Background(), client.WithContractsConfig(contracts)))

// assert the contract gets renewed and thus maintained
var renewalID types.FileContractID
Expand Down
3 changes: 2 additions & 1 deletion internal/test/e2e/gouging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

rhpv2 "go.sia.tech/core/rhp/v2"
"go.sia.tech/renterd/api"
"go.sia.tech/renterd/bus/client"
"go.sia.tech/renterd/internal/test"
"lukechampine.com/frand"
)
Expand Down Expand Up @@ -153,7 +154,7 @@ func TestHostMinVersion(t *testing.T) {
// set min version to a high value
hosts := test.AutopilotConfig.Hosts
hosts.MinProtocolVersion = "99.99.99"
tt.OK(cluster.Bus.UpdateHostsConfig(context.Background(), hosts))
tt.OK(cluster.Bus.UpdateAutopilot(context.Background(), client.WithHostsConfig(hosts)))

// contracts in set should drop to 0
tt.Retry(100, 100*time.Millisecond, func() error {
Expand Down

0 comments on commit ce2033f

Please sign in to comment.