|
| 1 | +package helpers |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/onsi/ginkgo/v2" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | + obs "github.com/openshift/cluster-logging-operator/api/observability/v1" |
| 7 | +) |
| 8 | + |
| 9 | +var _ = Describe("helpers functions", func() { |
| 10 | + Context("#GenerateQuotedPathSegmentArrayStr", func() { |
| 11 | + It("should generate array of path segments and flat path for single value", func() { |
| 12 | + pathExpression := []obs.FieldPath{`.kubernetes.labels.foo`} |
| 13 | + expectedArrayPath := `[["kubernetes","labels","foo"]]` |
| 14 | + expectedFlatPath := `["kubernetes_labels_foo"]` |
| 15 | + arrayPath, flatPath := GenerateQuotedPathSegmentArrayStr(pathExpression) |
| 16 | + Expect(arrayPath).To(Equal(expectedArrayPath)) |
| 17 | + Expect(flatPath).To(Equal(expectedFlatPath)) |
| 18 | + }) |
| 19 | + It("should generate array of path segments and flat path for multiple value", func() { |
| 20 | + pathExpression := []obs.FieldPath{`.kubernetes.labels.foo`, `.kubernetes.labels.bar`} |
| 21 | + expectedArrayPath := `[["kubernetes","labels","foo"],["kubernetes","labels","bar"]]` |
| 22 | + expectedFlatPath := `["kubernetes_labels_foo","kubernetes_labels_bar"]` |
| 23 | + arrayPath, flatPath := GenerateQuotedPathSegmentArrayStr(pathExpression) |
| 24 | + Expect(arrayPath).To(Equal(expectedArrayPath)) |
| 25 | + Expect(flatPath).To(Equal(expectedFlatPath)) |
| 26 | + }) |
| 27 | + It("should generate array of path segments and escaped flat path", func() { |
| 28 | + pathExpression := []obs.FieldPath{`.kubernetes.labels."bar/baz0-9.test"`} |
| 29 | + expectedArrayPath := `[["kubernetes","labels","bar/baz0-9.test"]]` |
| 30 | + expectedFlatPath := `["kubernetes_labels_bar_baz0-9_test"]` |
| 31 | + arrayPath, flatPath := GenerateQuotedPathSegmentArrayStr(pathExpression) |
| 32 | + Expect(arrayPath).To(Equal(expectedArrayPath)) |
| 33 | + Expect(flatPath).To(Equal(expectedFlatPath)) |
| 34 | + }) |
| 35 | + It("should generate array of path segments and escaped flat path", func() { |
| 36 | + pathExpression := []obs.FieldPath{`.foo.bar."foo.bar.baz-ok".foo123."bar/baz0-9.test"`, `.foo.bar`} |
| 37 | + expectedArrayPath := `[["foo","bar","foo.bar.baz-ok","foo123","bar/baz0-9.test"],["foo","bar"]]` |
| 38 | + expectedFlatPath := `["foo_bar_foo_bar_baz-ok_foo123_bar_baz0-9_test","foo_bar"]` |
| 39 | + arrayPath, flatPath := GenerateQuotedPathSegmentArrayStr(pathExpression) |
| 40 | + Expect(arrayPath).To(Equal(expectedArrayPath)) |
| 41 | + Expect(flatPath).To(Equal(expectedFlatPath)) |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + DescribeTable("#SplitPath generates correct array of path segments", func(path string, expectedArray []string) { |
| 46 | + Expect(SplitPath(path)).To(Equal(expectedArray)) |
| 47 | + }, |
| 48 | + Entry("with single segment", `.foo`, []string{"foo"}), |
| 49 | + Entry("with 2 segments", `.foo.bar`, []string{"foo", "bar"}), |
| 50 | + Entry("with first segment in quotes", `."@foobar"`, []string{`"@foobar"`}), |
| 51 | + Entry("with 1 quoted segment and one with quotes", `.foo."bar111-22/333"`, []string{"foo", `"bar111-22/333"`}), |
| 52 | + Entry("with 2 non quoted segments and one quoted segment ", `.foo.bar."baz111-22/333"`, []string{"foo", "bar", `"baz111-22/333"`}), |
| 53 | + Entry("with multiple quoted and unquoted segments", `.foo."@some"."d.f.g.o111-22/333".foo_bar`, []string{"foo", `"@some"`, `"d.f.g.o111-22/333"`, "foo_bar"})) |
| 54 | + |
| 55 | + DescribeTable("#QuotePathSegments generates array with path segments quoted", func(pathSegments []string, expectedArray []string) { |
| 56 | + Expect(QuotePathSegments(pathSegments)).To(Equal(expectedArray)) |
| 57 | + }, |
| 58 | + Entry("single value", []string{"foo"}, []string{`"foo"`}), |
| 59 | + Entry("multiple value", []string{"foo", "bar.zip", `"foo-bar"`}, []string{`"foo"`, `"bar.zip"`, `"foo-bar"`}), |
| 60 | + ) |
| 61 | +}) |
0 commit comments