From 45381f9a65e76389967bcbc13c023924b809f3a3 Mon Sep 17 00:00:00 2001 From: Xinyi Wang Date: Fri, 23 Aug 2024 16:05:57 -0700 Subject: [PATCH] add test to cover the filter scenario --- .../subcommand/sync-catalog/command.go | 6 +- .../subcommand/sync-catalog/command_test.go | 82 +++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/control-plane/subcommand/sync-catalog/command.go b/control-plane/subcommand/sync-catalog/command.go index 3481b54355..da5a19789a 100644 --- a/control-plane/subcommand/sync-catalog/command.go +++ b/control-plane/subcommand/sync-catalog/command.go @@ -263,11 +263,11 @@ func (c *Command) Run(args []string) int { if c.flagPurgeK8SServices { consulClient, err := consul.NewClientFromConnMgr(consulConfig, c.connMgr) if err != nil { - c.logger.Error(fmt.Sprintf("unable to instantiate consul client: %s", err)) + c.UI.Error(fmt.Sprintf("unable to instantiate consul client: %s", err)) return 1 } if err := c.removeAllK8SServicesFromConsulNode(consulClient, c.flagConsulNodeName); err != nil { - c.logger.Error(fmt.Sprintf("unable to remove all K8S services: %s", err)) + c.UI.Error(fmt.Sprintf("unable to remove all K8S services: %s", err)) return 1 } return 0 @@ -476,7 +476,7 @@ func (c *Command) removeAllK8SServicesFromConsulNode(consulClient *api.Client, n if err = <-errChan; err != nil { return err } - c.logger.Info("All K8S services were deregistered from Consul") + c.UI.Info("All K8S services were deregistered from Consul") return nil } diff --git a/control-plane/subcommand/sync-catalog/command_test.go b/control-plane/subcommand/sync-catalog/command_test.go index 5b2768a9d2..65548d6f27 100644 --- a/control-plane/subcommand/sync-catalog/command_test.go +++ b/control-plane/subcommand/sync-catalog/command_test.go @@ -648,6 +648,88 @@ func TestRemoveAllK8SServicesFromConsul(t *testing.T) { }) } +// Test services could be de-registered from Consul with filter. +func TestRemoveAllK8SServicesFromConsulWithFilter(t *testing.T) { + t.Parallel() + + k8s, testClient := completeSetup(t) + consulClient := testClient.APIClient + + // Create a mock reader to simulate user input + input := "y\n" + reader, writer, err := os.Pipe() + require.NoError(t, err) + oldStdin := os.Stdin + os.Stdin = reader + defer func() { os.Stdin = oldStdin }() + + // Write the simulated user input to the mock reader + go func() { + defer writer.Close() + _, err := writer.WriteString(input) + require.NoError(t, err) + }() + + // Run the command. + ui := cli.NewMockUi() + cmd := Command{ + UI: ui, + clientset: k8s, + logger: hclog.New(&hclog.LoggerOptions{ + Name: t.Name(), + Level: hclog.Debug, + }), + flagAllowK8sNamespacesList: []string{"*"}, + connMgr: testClient.Watcher, + } + + // create two services in k8s + _, err = k8s.CoreV1().Services("bar").Create(context.Background(), lbService("foo", "1.1.1.1"), metav1.CreateOptions{}) + require.NoError(t, err) + _, err = k8s.CoreV1().Services("baz").Create(context.Background(), lbService("foo", "2.2.2.2"), metav1.CreateOptions{}) + require.NoError(t, err) + _, err = k8s.CoreV1().Services("bat").Create(context.Background(), lbService("foo", "3.3.3.3"), metav1.CreateOptions{}) + require.NoError(t, err) + + longRunningChan := runCommandAsynchronously(&cmd, []string{ + "-addresses", "127.0.0.1", + "-http-port", strconv.Itoa(testClient.Cfg.HTTPPort), + "-consul-write-interval", "100ms", + "-add-k8s-namespace-suffix", + }) + defer stopCommand(t, &cmd, longRunningChan) + + // check that the name of the service is namespaced + retry.Run(t, func(r *retry.R) { + svc, _, err := consulClient.Catalog().Service("foo-bar", "k8s", nil) + require.NoError(r, err) + require.Len(r, svc, 1) + require.Equal(r, "1.1.1.1", svc[0].ServiceAddress) + svc, _, err = consulClient.Catalog().Service("foo-baz", "k8s", nil) + require.NoError(r, err) + require.Len(r, svc, 1) + require.Equal(r, "2.2.2.2", svc[0].ServiceAddress) + svc, _, err = consulClient.Catalog().Service("foo-bat", "k8s", nil) + require.NoError(r, err) + require.Len(r, svc, 1) + require.Equal(r, "3.3.3.3", svc[0].ServiceAddress) + }) + + exitChan := runCommandAsynchronously(&cmd, []string{ + "-addresses", "127.0.0.1", + "-http-port", strconv.Itoa(testClient.Cfg.HTTPPort), + "-purge-k8s-services=true", + "-filter=baz in ID", + }) + stopCommand(t, &cmd, exitChan) + + retry.Run(t, func(r *retry.R) { + serviceList, _, err := consulClient.Catalog().NodeServiceList("k8s-sync", &api.QueryOptions{AllowStale: false}) + require.NoError(r, err) + require.Len(r, serviceList.Services, 2) + }) +} + // Set up test consul agent and fake kubernetes cluster client. func completeSetup(t *testing.T) (*fake.Clientset, *test.TestServerClient) { k8s := fake.NewSimpleClientset()