error-pages/internal/cli/healthcheck/command_test.go

48 lines
1.2 KiB
Go
Raw Normal View History

2021-09-29 15:38:50 +00:00
package healthcheck_test
import (
"errors"
2023-01-29 10:54:56 +00:00
"flag"
2021-09-29 15:38:50 +00:00
"testing"
"github.com/stretchr/testify/assert"
2023-01-29 10:54:56 +00:00
"github.com/urfave/cli/v2"
2021-09-29 15:38:50 +00:00
"github.com/tarampampam/error-pages/internal/cli/healthcheck"
)
type fakeChecker struct{ err error }
func (c *fakeChecker) Check(port uint16) error { return c.err }
func TestProperties(t *testing.T) {
cmd := healthcheck.NewCommand(&fakeChecker{err: nil})
2023-01-29 10:54:56 +00:00
assert.Equal(t, "healthcheck", cmd.Name)
2021-09-29 15:38:50 +00:00
assert.ElementsMatch(t, []string{"chk", "health", "check"}, cmd.Aliases)
2023-01-29 10:54:56 +00:00
assert.NotNil(t, cmd.Action)
2021-09-29 15:38:50 +00:00
}
func TestCommandRun(t *testing.T) {
cmd := healthcheck.NewCommand(&fakeChecker{err: nil})
2023-01-29 10:54:56 +00:00
assert.NoError(t, cmd.Run(cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil)))
2021-09-29 15:38:50 +00:00
}
func TestCommandRunFailed(t *testing.T) {
cmd := healthcheck.NewCommand(&fakeChecker{err: errors.New("foo err")})
2023-01-29 10:54:56 +00:00
assert.ErrorContains(t, cmd.Run(cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil)), "foo err")
2021-09-29 15:38:50 +00:00
}
func TestPortFlagWrongArgument(t *testing.T) {
cmd := healthcheck.NewCommand(&fakeChecker{err: nil})
2023-01-29 10:54:56 +00:00
err := cmd.Run(
cli.NewContext(cli.NewApp(), &flag.FlagSet{}, nil),
"", "-p", "65536",
)
2021-09-29 15:38:50 +00:00
2023-01-29 10:54:56 +00:00
assert.ErrorContains(t, err, "port value out of range")
2021-09-29 15:38:50 +00:00
}