Skip to content

Commit

Permalink
Merge pull request #5 from dnnrly/chore/coverage
Browse files Browse the repository at this point in the history
Increase test code coverage
  • Loading branch information
dnnrly authored Apr 2, 2022
2 parents 89ea1b5 + 3f91a4d commit e95d5c6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
8 changes: 7 additions & 1 deletion cmd/wait-for/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ func main() {
logger = waitfor.NullLogger
}

err := waitfor.WaitOn(configFile, fs, logger, timeoutParam, flag.Args(), waitfor.SupportedWaiters)
config, err := waitfor.OpenConfig(configFile, timeoutParam, fs)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}

err = waitfor.WaitOn(config, logger, flag.Args(), waitfor.SupportedWaiters)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

"gopkg.in/yaml.v3"
yaml "gopkg.in/yaml.v3"
)

// DefautTimeout is the amount of time to wait for target before failing
Expand Down
10 changes: 3 additions & 7 deletions waitfor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ var SupportedWaiters = map[string]WaiterFunc{

// WaitOn implements waiting for many targets, using the location of config file provided with named targets to wait until
// all of those targets are responding as expected
func WaitOn(configFile string, fs afero.Fs, logger Logger, timeoutParam string, targets []string, waiters map[string]WaiterFunc) error {
config, err := openConfig(configFile, timeoutParam, fs)
if err != nil {
return err
}
func WaitOn(config *Config, logger Logger, targets []string, waiters map[string]WaiterFunc) error {

for _, target := range targets {
if !config.GotTarget(target) {
Expand All @@ -42,15 +38,15 @@ func WaitOn(configFile string, fs afero.Fs, logger Logger, timeoutParam string,
}
}
filtered := config.Filter(targets)
err = waitOnTargets(logger, filtered.Targets, waiters)
err := waitOnTargets(logger, filtered.Targets, waiters)
if err != nil {
return err
}

return nil
}

func openConfig(configFile, defaultTimeout string, fs afero.Fs) (*Config, error) {
func OpenConfig(configFile, defaultTimeout string, fs afero.Fs) (*Config, error) {
var config *Config
if configFile == "" {
config = NewConfig()
Expand Down
19 changes: 7 additions & 12 deletions waitfor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Test_isSuccess(t *testing.T) {
func TestOpenConfig_errorOnFileOpenFailure(t *testing.T) {
mockFS := afero.NewMemMapFs()

config, err := openConfig("./wait-for.yaml", "", afero.NewReadOnlyFs(mockFS))
config, err := OpenConfig("./wait-for.yaml", "", afero.NewReadOnlyFs(mockFS))
assert.Error(t, err)
assert.Nil(t, config)
}
Expand All @@ -34,7 +34,7 @@ func TestOpenConfig_errorOnFileParsingFailure(t *testing.T) {
mockFS := afero.NewMemMapFs()
_ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte("this isn't yaml!"), 0444)

config, err := openConfig("./wait-for.yaml", "", afero.NewReadOnlyFs(mockFS))
config, err := OpenConfig("./wait-for.yaml", "", afero.NewReadOnlyFs(mockFS))
assert.Error(t, err)
assert.Nil(t, config)
}
Expand All @@ -43,7 +43,7 @@ func TestOpenConfig_errorOnParsingDefaultTimeout(t *testing.T) {
mockFS := afero.NewMemMapFs()
_ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte(defaultConfigYaml()), 0444)

config, err := openConfig("./wait-for.yaml", "invalid duration", afero.NewReadOnlyFs(mockFS))
config, err := OpenConfig("./wait-for.yaml", "invalid duration", afero.NewReadOnlyFs(mockFS))
assert.Error(t, err)
assert.Nil(t, config)
}
Expand All @@ -52,24 +52,19 @@ func TestOpenConfig_defaultTimeoutCanBeSet(t *testing.T) {
mockFS := afero.NewMemMapFs()
_ = afero.WriteFile(mockFS, "./wait-for.yaml", []byte(defaultConfigYaml()), 0444)

config, err := openConfig("./wait-for.yaml", "19s", afero.NewReadOnlyFs(mockFS))
config, err := OpenConfig("./wait-for.yaml", "19s", afero.NewReadOnlyFs(mockFS))
assert.NoError(t, err)
assert.NotNil(t, config)
assert.Equal(t, time.Second*19, config.DefaultTimeout)
}

func TestRun_errorsOnConfigFileFailure(t *testing.T) {
err := WaitOn("non-existent", afero.NewMemMapFs(), NullLogger, "invalid", []string{"http://localhost"}, map[string]WaiterFunc{})
func TestWaitOn_errorsInvalidTarget(t *testing.T) {
err := WaitOn(NewConfig(), NullLogger, []string{"localhost"}, map[string]WaiterFunc{})
assert.Error(t, err)
}

func TestRun_errorsOnParseFailure(t *testing.T) {
err := WaitOn("", afero.NewMemMapFs(), NullLogger, "invalid", []string{"http://localhost"}, map[string]WaiterFunc{})
assert.Error(t, err)
}

func TestRun_errorsOnConfigFailure(t *testing.T) {
err := WaitOn("", afero.NewMemMapFs(), NullLogger, "invalid", []string{"localhost"}, map[string]WaiterFunc{})
err := WaitOn(NewConfig(), NullLogger, []string{"http://localhost"}, map[string]WaiterFunc{})
assert.Error(t, err)
}

Expand Down

0 comments on commit e95d5c6

Please sign in to comment.