Skip to content

Commit

Permalink
Fix logging and add test for lifecycle-sidecar
Browse files Browse the repository at this point in the history
  • Loading branch information
lawliet89 committed Nov 12, 2020
1 parent 1fbaddf commit f7f844f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
8 changes: 6 additions & 2 deletions subcommand/lifecycle-sidecar/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (c *Command) Run(args []string) int {
case <-time.After(c.flagSyncPeriod):
continue
case sig := <-c.sigCh:
logger.Info("{} received, shutting down", sig)
logger.Info(fmt.Sprintf("%s received, shutting down", sig))
return 0
}
}
Expand Down Expand Up @@ -165,7 +165,11 @@ func (c *Command) parseConsulFlags() []string {
// interrupt sends os.Interrupt signal to the command
// so it can exit gracefully. This function is needed for tests
func (c *Command) interrupt() {
c.sigCh <- os.Interrupt
c.sendSignal(syscall.SIGINT)
}

func (c *Command) sendSignal(sig os.Signal) {
c.sigCh <- sig
}

func (c *Command) Synopsis() string { return synopsis }
Expand Down
33 changes: 28 additions & 5 deletions subcommand/lifecycle-sidecar/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
"testing"
"time"

Expand All @@ -25,6 +26,28 @@ func TestRun_Defaults(t *testing.T) {
require.Equal(t, "consul", cmd.flagConsulBinary)
}

func TestRun_ExitsCleanlyonSignals(t *testing.T) {
t.Run("SIGINT", testRunSignalHandling(syscall.SIGINT))
t.Run("SIGTERM", testRunSignalHandling(syscall.SIGTERM))
}

func testRunSignalHandling(sig os.Signal) func(*testing.T) {
return func(t *testing.T) {
tmpDir, configFile := createServicesTmpFile(t, servicesRegistration)
defer os.RemoveAll(tmpDir)

ui := cli.NewMockUi()
cmd := Command{
UI: ui,
}
// Run async because we need to kill it when the test is over.
exitChan := runCommandAsynchronously(&cmd, []string{
"-service-config", configFile,
})
defer stopCommand(t, &cmd, exitChan, sig)
}
}

func TestRun_FlagValidation(t *testing.T) {
t.Parallel()
cases := []struct {
Expand Down Expand Up @@ -131,7 +154,7 @@ func TestRun_ServicesRegistration(t *testing.T) {
"-service-config", configFile,
"-sync-period", "100ms",
})
defer stopCommand(t, &cmd, exitChan)
defer stopCommand(t, &cmd, exitChan, syscall.SIGINT)

client, err := api.NewClient(&api.Config{
Address: a.HTTPAddr,
Expand Down Expand Up @@ -171,7 +194,7 @@ func TestRun_ServicesRegistration_ConsulDown(t *testing.T) {
"-service-config", configFile,
"-sync-period", "100ms",
})
defer stopCommand(t, &cmd, exitChan)
defer stopCommand(t, &cmd, exitChan, syscall.SIGINT)

// Start the Consul agent after 500ms.
time.Sleep(500 * time.Millisecond)
Expand Down Expand Up @@ -231,7 +254,7 @@ func TestRun_ConsulCommandFlags(t *testing.T) {
"-ca-file=/ca/file",
"-ca-path=/ca/path",
})
defer stopCommand(t, &cmd, exitChan)
defer stopCommand(t, &cmd, exitChan, syscall.SIGINT)

expectedCommand := []string{
"services",
Expand Down Expand Up @@ -265,9 +288,9 @@ func runCommandAsynchronously(cmd *Command, args []string) chan int {
return exitChan
}

func stopCommand(t *testing.T, cmd *Command, exitChan chan int) {
func stopCommand(t *testing.T, cmd *Command, exitChan chan int, sig os.Signal) {
if len(exitChan) == 0 {
cmd.interrupt()
cmd.sendSignal(sig)
}
select {
case c := <-exitChan:
Expand Down
2 changes: 1 addition & 1 deletion subcommand/sync-catalog/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func (c *Command) Run(args []string) int {

// Interrupted/terminated, gracefully exit
case sig := <-c.sigCh:
c.logger.Info("{} received, shutting down", sig)
c.logger.Info(fmt.Sprintf("%s received, shutting down", sig))
cancelF()
if toConsulCh != nil {
<-toConsulCh
Expand Down
2 changes: 1 addition & 1 deletion subcommand/webhook-cert-manager/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (c *Command) Run(args []string) int {
// all the contexts that have been created by the process.
select {
case sig := <-c.sigCh:
c.logger.Info("{} received, shutting down", sig)
c.logger.Info(fmt.Sprintf("%s received, shutting down", sig))
cancelFunc()
for _, notifier := range notifiers {
notifier.Stop()
Expand Down

0 comments on commit f7f844f

Please sign in to comment.