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

56 lines
1.2 KiB
Go
Raw Normal View History

2021-09-29 15:38:50 +00:00
package healthcheck_test
import (
2024-07-03 14:12:13 +00:00
"context"
2021-09-29 15:38:50 +00:00
"testing"
"github.com/stretchr/testify/assert"
2024-07-03 14:12:13 +00:00
"github.com/stretchr/testify/require"
2023-01-29 10:54:56 +00:00
"gh.tarampamp.am/error-pages/internal/cli/healthcheck"
2024-07-03 14:12:13 +00:00
"gh.tarampamp.am/error-pages/internal/logger"
2021-09-29 15:38:50 +00:00
)
2024-07-03 14:12:13 +00:00
func TestNewCommand(t *testing.T) {
t.Parallel()
2021-09-29 15:38:50 +00:00
2024-07-03 14:12:13 +00:00
var cmd = healthcheck.NewCommand(logger.NewNop(), nil)
2021-09-29 15:38:50 +00:00
2023-01-29 10:54:56 +00:00
assert.Equal(t, "healthcheck", cmd.Name)
2024-07-03 14:12:13 +00:00
assert.Equal(t, []string{"chk", "health", "check"}, cmd.Aliases)
}
type fakeHealthChecker struct {
t *testing.T
wantAddress string
giveErr error
2021-09-29 15:38:50 +00:00
}
2024-07-03 14:12:13 +00:00
func (m *fakeHealthChecker) Check(_ context.Context, addr string) error {
assert.Equal(m.t, m.wantAddress, addr)
2021-09-29 15:38:50 +00:00
2024-07-03 14:12:13 +00:00
return m.giveErr
2021-09-29 15:38:50 +00:00
}
2024-07-03 14:12:13 +00:00
func TestCommand_RunSuccess(t *testing.T) {
var cmd = healthcheck.NewCommand(logger.NewNop(), &fakeHealthChecker{
t: t,
wantAddress: "http://127.0.0.1:1234",
})
2021-09-29 15:38:50 +00:00
2024-07-03 14:12:13 +00:00
require.NoError(t, cmd.Run(context.Background(), []string{"", "--port", "1234"}))
2021-09-29 15:38:50 +00:00
}
2024-07-03 14:12:13 +00:00
func TestCommand_RunFail(t *testing.T) {
cmd := healthcheck.NewCommand(logger.NewNop(), &fakeHealthChecker{
t: t,
wantAddress: "http://127.0.0.1:4321",
giveErr: assert.AnError,
})
2021-09-29 15:38:50 +00:00
2024-07-03 14:12:13 +00:00
assert.ErrorIs(t,
cmd.Run(context.Background(), []string{"", "--port", "4321"}),
assert.AnError,
2023-01-29 10:54:56 +00:00
)
2021-09-29 15:38:50 +00:00
}