Skip to content

Commit

Permalink
Undo unintentional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Mar 20, 2023
1 parent 2e3761a commit a39e14e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
4 changes: 2 additions & 2 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ func (Test) All() {

// Unit runs all the unit tests.
func (Test) Unit(ctx context.Context) error {
mg.Deps(Prepare.Env)
mg.Deps(Prepare.Env, Build.TestBinaries)
params := devtools.DefaultGoTestUnitArgs()
return devtools.GoTest(ctx, params)
}

// Coverage takes the coverages report from running all the tests and display the results in the browser.
func (Test) Coverage() error {
mg.Deps(Prepare.Env)
mg.Deps(Prepare.Env, Build.TestBinaries)
return RunGo("tool", "cover", "-html="+filepath.Join(buildDir, "coverage.out"))
}

Expand Down
19 changes: 11 additions & 8 deletions pkg/component/fake/component/comp/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,23 @@ func (s *killAction) Execute(_ context.Context, _ map[string]interface{}) (map[s
}

func newRunningUnit(logger zerolog.Logger, manager *StateManager, unit *client.Unit) (runningUnit, error) {
_, logLevel, config := unit.Expected()
if config.Type == "" {
expected := unit.Expected()
if expected.Config.Type == "" {
return nil, fmt.Errorf("unit config type empty")
}
if unit.Type() == client.UnitTypeOutput {
switch config.Type {
switch expected.Config.Type {
case fakeShipper:
return newFakeShipperOutput(logger, logLevel, unit, config)
return newFakeShipperOutput(
logger, expected.LogLevel, unit, expected.Config)
}
return nil, fmt.Errorf("unknown output unit config type: %s", config.Type)
return nil, fmt.Errorf("unknown output unit config type: %s",
expected.Config.Type)
}
switch config.Type {
switch expected.Config.Type {
case Fake:
return newFakeInput(logger, logLevel, manager, unit, config)
return newFakeInput(logger, expected.LogLevel, manager, unit, expected.Config)
}
return nil, fmt.Errorf("unknown input unit config type: %s", config.Type)
return nil, fmt.Errorf("unknown input unit config type: %s",
expected.Config.Type)
}
44 changes: 29 additions & 15 deletions pkg/component/fake/component/comp/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"crypto/x509"
"errors"
"fmt"
"github.com/elastic/elastic-agent-libs/config"
"os"
"strconv"
"time"
Expand Down Expand Up @@ -87,7 +86,6 @@ func (s *StateManager) Modified(change client.UnitChanged) {
_ = unit.UpdateState(client.UnitStateFailed, fmt.Sprintf("Error: %s", err), nil)
}
return
}

case client.UnitTypeInput:
existingInput, ok := s.inputs[unit.ID()]
Expand Down Expand Up @@ -187,16 +185,17 @@ func (f *fakeShipperOutput) Update(u *client.Unit, triggers client.Trigger) erro
return nil
}

if config.Type == "" {
if expected.Config.Type == "" {
return fmt.Errorf("unit missing config type")
}
if config.Type != fakeShipper {
return fmt.Errorf("unit type changed with the same unit ID: %s", config.Type)
if expected.Config.Type != fakeShipper {
return fmt.Errorf("unit type changed with the same unit ID: %s",
expected.Config.Type)
}

f.stop()
f.cfg = config
f.start(u, config)
f.cfg = expected.Config
f.start(u, expected.Config)

return nil
}
Expand Down Expand Up @@ -298,6 +297,8 @@ type fakeInput struct {
state client.UnitState
stateMsg string

features *proto.Features

canceller context.CancelFunc
killerCanceller context.CancelFunc
}
Expand Down Expand Up @@ -325,6 +326,8 @@ func newFakeInput(logger zerolog.Logger, logLevel client.UnitLogLevel, manager *
unit.RegisterAction(&sendEventAction{i})
logger.Trace().Msg("registering kill action for unit")
unit.RegisterAction(&killAction{i.logger})
logger.Trace().Msg("registering " + ActionRetrieveFeatures + " action for unit")
unit.RegisterAction(&retrieveFeaturesAction{i})

logger.Debug().
Str("state", i.state.String()).
Expand Down Expand Up @@ -369,32 +372,43 @@ func (f *fakeInput) Update(u *client.Unit, triggers client.Trigger) error {
expected := u.Expected()
if expected.State == client.UnitStateStopped {
// agent is requesting this input to stop
f.logger.Debug().Str("state", client.UnitStateStopping.String()).Str("message", stoppingMsg).Msg("updating unit state")
f.logger.Debug().
Str("state", client.UnitStateStopping.String()).
Str("message", stoppingMsg).
Msg("updating unit state")
_ = u.UpdateState(client.UnitStateStopping, stoppingMsg, nil)
f.canceller()
go func() {
<-time.After(1 * time.Second)
f.logger.Debug().Str("state", client.UnitStateStopped.String()).Str("message", stoppedMsg).Msg("updating unit state")
f.logger.Debug().
Str("state", client.UnitStateStopped.String()).
Str("message", stoppedMsg).
Msg("updating unit state")
_ = u.UpdateState(client.UnitStateStopped, stoppedMsg, nil)
}()
return nil
}

if config.Type == "" {
if expected.Config.Type == "" {
return fmt.Errorf("unit missing config type")
}
if config.Type != Fake {
return fmt.Errorf("unit type changed with the same unit ID: %s", config.Type)
if expected.Config.Type != Fake {
return fmt.Errorf("unit type changed with the same unit ID: %s",
expected.Config.Type)
}

f.parseConfig(config)
state, stateMsg, err := getStateFromConfig(config)
f.parseConfig(expected.Config)
state, stateMsg, err := getStateFromConfig(expected.Config)
if err != nil {
return fmt.Errorf("unit config parsing error: %w", err)
}

f.state = state
f.stateMsg = stateMsg
f.logger.Debug().Str("state", f.state.String()).Str("message", f.stateMsg).Msg("updating unit state")
f.logger.Debug().
Str("state", f.state.String()).
Str("message", f.stateMsg).
Msg("updating unit state")
_ = u.UpdateState(f.state, f.stateMsg, nil)

if triggers&client.TriggeredFeatureChange == client.TriggeredFeatureChange {
Expand Down
1 change: 1 addition & 0 deletions pkg/component/fake/component/comp/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// you may not use this file except in compliance with the Elastic License.

//go:build !windows
// +build !windows

package comp

Expand Down
1 change: 0 additions & 1 deletion pkg/component/fake/shipper/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func newRunningUnit(logger zerolog.Logger, manager *stateManager, unit *client.U
}
return nil, fmt.Errorf("unknown input unit config type: %s", expected.Config.Type)
}

return nil, fmt.Errorf("unknown unit type: %+v", unit.Type())
}

Expand Down

0 comments on commit a39e14e

Please sign in to comment.