Skip to content

Commit

Permalink
Add test for sync-catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
lawliet89 committed Nov 12, 2020
1 parent f7f844f commit e47ff66
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
6 changes: 5 additions & 1 deletion subcommand/sync-catalog/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,11 @@ func (c *Command) Help() 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) validateFlags() error {
Expand Down
49 changes: 39 additions & 10 deletions subcommand/sync-catalog/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package synccatalog

import (
"context"
"os"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -70,7 +72,7 @@ func TestRun_Defaults_SyncsConsulServiceToK8s(t *testing.T) {
exitChan := runCommandAsynchronously(&cmd, []string{
"-http-addr", testServer.HTTPAddr,
})
defer stopCommand(t, &cmd, exitChan)
defer stopCommand(t, &cmd, exitChan, syscall.SIGINT)

retry.Run(t, func(r *retry.R) {
serviceList, err := k8s.CoreV1().Services(metav1.NamespaceDefault).List(context.Background(), metav1.ListOptions{})
Expand All @@ -81,6 +83,33 @@ func TestRun_Defaults_SyncsConsulServiceToK8s(t *testing.T) {
})
}

// Test that the command exits cleanly on signals
func TestRun_ExitCleanlyOnSignals(t *testing.T) {
t.Run("SIGINT", testSignalHandling(syscall.SIGINT))
t.Run("SIGTERM", testSignalHandling(syscall.SIGTERM))
}

func testSignalHandling(sig os.Signal) func(*testing.T) {
k8s, testServer := completeSetup(t)
defer testServer.Stop()

// Run the command.
ui := cli.NewMockUi()
cmd := Command{
UI: ui,
clientset: k8s,
logger: hclog.New(&hclog.LoggerOptions{
Name: t.Name(),
Level: hclog.Debug,
}),
}

exitChan := runCommandAsynchronously(&cmd, []string{
"-http-addr", testServer.HTTPAddr,
})
defer stopCommand(t, &cmd, exitChan, sig)
}

// Test that when -add-k8s-namespace-suffix flag is used
// k8s namespaces are appended to the service names synced to Consul
func TestRun_ToConsulWithAddK8SNamespaceSuffix(t *testing.T) {
Expand Down Expand Up @@ -116,7 +145,7 @@ func TestRun_ToConsulWithAddK8SNamespaceSuffix(t *testing.T) {
"-consul-write-interval", "100ms",
"-add-k8s-namespace-suffix",
})
defer stopCommand(t, &cmd, exitChan)
defer stopCommand(t, &cmd, exitChan, syscall.SIGINT)

retry.Run(t, func(r *retry.R) {
services, _, err := consulClient.Catalog().Services(nil)
Expand Down Expand Up @@ -167,14 +196,14 @@ func TestCommand_Run_ToConsulChangeAddK8SNamespaceSuffixToTrue(t *testing.T) {
require.Contains(r, services, "foo")
})

stopCommand(t, &cmd, exitChan)
stopCommand(t, &cmd, exitChan, syscall.SIGINT)

// restart sync with -add-k8s-namespace-suffix
exitChan = runCommandAsynchronously(&cmd, []string{
"-consul-write-interval", "100ms",
"-add-k8s-namespace-suffix",
})
defer stopCommand(t, &cmd, exitChan)
defer stopCommand(t, &cmd, exitChan, syscall.SIGINT)

// check that the name of the service is now namespaced
retry.Run(t, func(r *retry.R) {
Expand Down Expand Up @@ -223,7 +252,7 @@ func TestCommand_Run_ToConsulTwoServicesSameNameDifferentNamespace(t *testing.T)
"-consul-write-interval", "100ms",
"-add-k8s-namespace-suffix",
})
defer stopCommand(t, &cmd, exitChan)
defer stopCommand(t, &cmd, exitChan, syscall.SIGINT)

// check that the name of the service is namespaced
retry.Run(t, func(r *retry.R) {
Expand Down Expand Up @@ -333,7 +362,7 @@ func TestRun_ToConsulAllowDenyLists(t *testing.T) {
}),
}
exitChan := runCommandAsynchronously(&cmd, flags)
defer stopCommand(tt, &cmd, exitChan)
defer stopCommand(tt, &cmd, exitChan, syscall.SIGINT)

retry.Run(tt, func(r *retry.R) {
svcs, _, err := consulClient.Catalog().Services(nil)
Expand Down Expand Up @@ -488,7 +517,7 @@ func TestRun_ToConsulChangingFlags(t *testing.T) {
require.Equal(r, instances[0].ServiceName, svcName)
}
})
stopCommand(tt, &firstCmd, exitChan)
stopCommand(tt, &firstCmd, exitChan, syscall.SIGINT)
}
tt.Log("first command run complete")

Expand All @@ -504,7 +533,7 @@ func TestRun_ToConsulChangingFlags(t *testing.T) {
}),
}
exitChan := runCommandAsynchronously(&secondCmd, append(commonArgs, c.SecondRunFlags...))
defer stopCommand(tt, &secondCmd, exitChan)
defer stopCommand(tt, &secondCmd, exitChan, syscall.SIGINT)

// Wait until the expected services are synced and the old ones
// deleted.
Expand Down Expand Up @@ -559,9 +588,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

0 comments on commit e47ff66

Please sign in to comment.