From ae25b0d3fa5d0aad95d94fc926e78cadfcde7712 Mon Sep 17 00:00:00 2001 From: Pradeep Murugesan Date: Thu, 17 Oct 2019 23:54:19 +0530 Subject: [PATCH] feat(kumactl) add support for deleting traffic-log Implemented the support for deleting traffic-log. Check if the the given traffic-log exists and then delete. * returns an error in case of no traffic-log with the given name. Fix #279 --- app/kumactl/cmd/delete/delete.go | 2 + .../cmd/delete/delete_trafficlog_test.go | 174 ++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 app/kumactl/cmd/delete/delete_trafficlog_test.go diff --git a/app/kumactl/cmd/delete/delete.go b/app/kumactl/cmd/delete/delete.go index d0e8c1781006..ab8a2a1830b0 100644 --- a/app/kumactl/cmd/delete/delete.go +++ b/app/kumactl/cmd/delete/delete.go @@ -34,6 +34,8 @@ func NewDeleteCmd(pctx *kumactl_cmd.RootContext) *cobra.Command { resourceType = model.ResourceType(mesh.DataplaneType) case "proxytemplate": resourceType = model.ResourceType(mesh.ProxyTemplateType) + case "traffic-log": + resourceType = model.ResourceType(mesh.TrafficLogType) default: return errors.Errorf("unknown resource type: %s. Allowed types: mesh, dataplane, proxytemplate, traffic-log, traffic-permission", resourceTypeArg) diff --git a/app/kumactl/cmd/delete/delete_trafficlog_test.go b/app/kumactl/cmd/delete/delete_trafficlog_test.go new file mode 100644 index 000000000000..deb4f7c3f9e9 --- /dev/null +++ b/app/kumactl/cmd/delete/delete_trafficlog_test.go @@ -0,0 +1,174 @@ +package delete_test + +import ( + "bytes" + "context" + "github.com/Kong/kuma/app/kumactl/cmd" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + "github.com/spf13/cobra" + "path/filepath" + "time" + + mesh_proto "github.com/Kong/kuma/api/mesh/v1alpha1" + kumactl_cmd "github.com/Kong/kuma/app/kumactl/pkg/cmd" + config_proto "github.com/Kong/kuma/pkg/config/app/kumactl/v1alpha1" + mesh_core "github.com/Kong/kuma/pkg/core/resources/apis/mesh" + core_model "github.com/Kong/kuma/pkg/core/resources/model" + core_store "github.com/Kong/kuma/pkg/core/resources/store" + memory_resources "github.com/Kong/kuma/pkg/plugins/resources/memory" + + test_model "github.com/Kong/kuma/pkg/test/resources/model" +) + +var _ = Describe("kumactl delete trafficlog", func() { + + trafficLoggingResources := []*mesh_core.TrafficLogResource{ + { + Spec: mesh_proto.TrafficLog{ + Rules: []*mesh_proto.TrafficLog_Rule{ + { + Sources: []*mesh_proto.Selector{ + { + Match: map[string]string{ + "service": "web1", + "version": "1.0", + }, + }, + }, + Destinations: []*mesh_proto.Selector{ + { + Match: map[string]string{ + "service": "backend1", + "env": "dev", + }, + }, + }, + Conf: &mesh_proto.TrafficLog_Rule_Conf{ + Backend: "file", + }, + }, + }, + }, + Meta: &test_model.ResourceMeta{ + Mesh: "web1-to-backend1", + Name: "web1-to-backend1", + Namespace: "default", + }, + }, + { + Spec: mesh_proto.TrafficLog{ + Rules: []*mesh_proto.TrafficLog_Rule{ + { + Sources: []*mesh_proto.Selector{ + { + Match: map[string]string{ + "service": "web2", + "version": "1.0", + }, + }, + }, + Destinations: []*mesh_proto.Selector{ + { + Match: map[string]string{ + "service": "backend2", + "env": "dev", + }, + }, + }, + Conf: &mesh_proto.TrafficLog_Rule_Conf{ + Backend: "logstash", + }, + }, + }, + }, + Meta: &test_model.ResourceMeta{ + Mesh: "web2-to-backend2", + Name: "web2-to-backend2", + Namespace: "default", + }, + }, + } + + Describe("delete trafficlog", func() { + + var rootCtx *kumactl_cmd.RootContext + var rootCmd *cobra.Command + var outbuf, errbuf *bytes.Buffer + var store core_store.ResourceStore + + BeforeEach(func() { + // setup + rootCtx = &kumactl_cmd.RootContext{ + Runtime: kumactl_cmd.RootRuntime{ + Now: func() time.Time { return time.Now() }, + NewResourceStore: func(*config_proto.ControlPlaneCoordinates_ApiServer) (core_store.ResourceStore, error) { + return store, nil + }, + }, + } + + store = memory_resources.NewStore() + + for _, ds := range trafficLoggingResources { + err := store.Create(context.Background(), ds, core_store.CreateBy(core_model.MetaToResourceKey(ds.GetMeta()))) + Expect(err).ToNot(HaveOccurred()) + } + + rootCmd = cmd.NewRootCmd(rootCtx) + outbuf = &bytes.Buffer{} + errbuf = &bytes.Buffer{} + rootCmd.SetOut(outbuf) + rootCmd.SetErr(errbuf) + }) + + Describe("Delete TrafficLog", func() { + + It("should throw an error in case of a non existing trafficlog", func() { + // given + rootCmd.SetArgs([]string{ + "--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"), + "delete", "traffic-log", "some-non-existing-trafficlog"}) + + // when + err := rootCmd.Execute() + + // then + Expect(err).To(HaveOccurred()) + // and + Expect(err.Error()).To(Equal("there is no TrafficLog with name \"some-non-existing-trafficlog\"")) + // and + Expect(outbuf.String()).To(Equal("Error: there is no TrafficLog with name \"some-non-existing-trafficlog\"\n")) + // and + Expect(errbuf.Bytes()).To(BeEmpty()) + }) + + It("should delete the trafficlog if exists", func() { + + // given + rootCmd.SetArgs([]string{ + "--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"), + "delete", "traffic-log", "web1-to-backend1"}) + + // when + err := rootCmd.Execute() + + // then + Expect(err).ToNot(HaveOccurred()) + + // and + list := &mesh_core.TrafficLogResourceList{} + err = store.List(context.Background(), list, core_store.ListByNamespace("default")) + Expect(err).ToNot(HaveOccurred()) + Expect(list.Items).To(HaveLen(1)) + // and + Expect(errbuf.String()).To(BeEmpty()) + Expect(errbuf.String()).To(BeEmpty()) + // and + Expect(outbuf.String()).To(Equal("deleted TrafficLog \"web1-to-backend1\"\n")) + }) + + }) + }) + +})