|
| 1 | +package gcl |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + obstestruntime "github.com/openshift/cluster-logging-operator/test/runtime/observability" |
| 9 | + |
| 10 | + . "github.com/onsi/ginkgo/v2" |
| 11 | + . "github.com/onsi/gomega" |
| 12 | + obs "github.com/openshift/cluster-logging-operator/api/observability/v1" |
| 13 | + "github.com/openshift/cluster-logging-operator/internal/constants" |
| 14 | + internalgcl "github.com/openshift/cluster-logging-operator/internal/generator/vector/output/gcl" |
| 15 | + "github.com/openshift/cluster-logging-operator/internal/runtime" |
| 16 | + "github.com/openshift/cluster-logging-operator/test/client" |
| 17 | + "github.com/openshift/cluster-logging-operator/test/framework/functional" |
| 18 | + gclhelper "github.com/openshift/cluster-logging-operator/test/helpers/gcl" |
| 19 | + "github.com/openshift/cluster-logging-operator/test/helpers/types" |
| 20 | + "github.com/openshift/cluster-logging-operator/test/matchers" |
| 21 | +) |
| 22 | + |
| 23 | +var _ = Describe("Forwarding to GCP Cloud Logging", func() { |
| 24 | + var ( |
| 25 | + framework *functional.CollectorFunctionalFramework |
| 26 | + ) |
| 27 | + |
| 28 | + setup := func(inputType obs.InputType, testOptions ...client.TestOption) { |
| 29 | + framework = functional.NewCollectorFunctionalFramework(testOptions...) |
| 30 | + |
| 31 | + saJSON, err := gclhelper.GenerateFakeServiceAccountJSON( |
| 32 | + fmt.Sprintf("https://%s/token", gclhelper.GCLDomain)) |
| 33 | + Expect(err).To(BeNil()) |
| 34 | + |
| 35 | + secret := runtime.NewSecret(framework.Namespace, gclhelper.SecretName, |
| 36 | + map[string][]byte{ |
| 37 | + internalgcl.GoogleApplicationCredentialsKey: saJSON, |
| 38 | + }, |
| 39 | + ) |
| 40 | + framework.Secrets = append(framework.Secrets, secret) |
| 41 | + |
| 42 | + obstestruntime.NewClusterLogForwarderBuilder(framework.Forwarder). |
| 43 | + FromInput(inputType). |
| 44 | + ToGoogleCloudLoggingOutput() |
| 45 | + Expect(framework.DeployWithVisitor( |
| 46 | + gclhelper.NewMockoonVisitor(framework), |
| 47 | + )).To(BeNil()) |
| 48 | + } |
| 49 | + |
| 50 | + AfterEach(func() { |
| 51 | + framework.Cleanup() |
| 52 | + }) |
| 53 | + |
| 54 | + Context("application logs", func() { |
| 55 | + BeforeEach(func() { |
| 56 | + setup(obs.InputTypeApplication) |
| 57 | + }) |
| 58 | + |
| 59 | + It("should accept application logs", func() { |
| 60 | + timestamp := "2020-11-04T18:13:59.061892+00:00" |
| 61 | + nanoTime, _ := time.Parse(time.RFC3339Nano, timestamp) |
| 62 | + message := "This is my new test message" |
| 63 | + appLogTemplate := functional.NewApplicationLogTemplate() |
| 64 | + appLogTemplate.TimestampLegacy = nanoTime |
| 65 | + appLogTemplate.Message = message |
| 66 | + appLogTemplate.Level = "**optional**" |
| 67 | + appLogTemplate.Kubernetes.PodName = framework.Pod.Name |
| 68 | + appLogTemplate.Kubernetes.ContainerName = constants.CollectorName |
| 69 | + appLogTemplate.Kubernetes.NamespaceName = framework.Namespace |
| 70 | + |
| 71 | + applicationLogLine := functional.NewCRIOLogMessage(timestamp, message, false) |
| 72 | + Expect(framework.WriteMessagesToApplicationLog(applicationLogLine, 3)).To(BeNil()) |
| 73 | + time.Sleep(30 * time.Second) |
| 74 | + |
| 75 | + collectorLog, err := framework.ReadCollectorLogs() |
| 76 | + Expect(err).To(BeNil()) |
| 77 | + Expect(collectorLog).ToNot(ContainSubstring("error sending request")) |
| 78 | + |
| 79 | + appLogs, err := gclhelper.ReadApplicationLog(framework.Namespace, framework.Name) |
| 80 | + Expect(err).To(BeNil()) |
| 81 | + Expect(appLogs).ToNot(BeNil()) |
| 82 | + Expect(appLogs).To(HaveLen(3)) |
| 83 | + for i := range 3 { |
| 84 | + Expect(appLogs[i]).To(matchers.FitLogFormatTemplate(appLogTemplate)) |
| 85 | + } |
| 86 | + }) |
| 87 | + }) |
| 88 | + |
| 89 | + Context("infrastructure logs", func() { |
| 90 | + BeforeEach(func() { |
| 91 | + setup(obs.InputTypeInfrastructure, client.UseInfraNamespaceTestOption) |
| 92 | + }) |
| 93 | + |
| 94 | + It("should accept infrastructure container logs", func() { |
| 95 | + infraLogTemplate := functional.NewContainerInfrastructureLogTemplate() |
| 96 | + infraLogTemplate.Level = "**optional**" |
| 97 | + infraLogTemplate.Kubernetes.ContainerStream = "**optional**" |
| 98 | + |
| 99 | + Expect(framework.WritesInfraContainerLogs(3)).To(BeNil()) |
| 100 | + time.Sleep(30 * time.Second) |
| 101 | + |
| 102 | + collectorLog, err := framework.ReadCollectorLogs() |
| 103 | + Expect(err).To(BeNil()) |
| 104 | + Expect(collectorLog).ToNot(ContainSubstring("error sending request")) |
| 105 | + |
| 106 | + infraLogs, err := gclhelper.ReadApplicationLog(framework.Namespace, framework.Name) |
| 107 | + Expect(err).To(BeNil()) |
| 108 | + Expect(infraLogs).ToNot(BeNil()) |
| 109 | + Expect(infraLogs).To(HaveLen(3)) |
| 110 | + for i := range 3 { |
| 111 | + Expect(infraLogs[i]).To(matchers.FitLogFormatTemplate(infraLogTemplate)) |
| 112 | + } |
| 113 | + }) |
| 114 | + }) |
| 115 | + |
| 116 | + Context("audit logs", func() { |
| 117 | + BeforeEach(func() { |
| 118 | + setup(obs.InputTypeAudit) |
| 119 | + }) |
| 120 | + |
| 121 | + It("should accept audit logs", func() { |
| 122 | + Expect(framework.WriteK8sAuditLog(1)).To(BeNil()) |
| 123 | + time.Sleep(30 * time.Second) |
| 124 | + |
| 125 | + collectorLog, err := framework.ReadCollectorLogs() |
| 126 | + Expect(err).To(BeNil()) |
| 127 | + Expect(collectorLog).ToNot(ContainSubstring("error sending request")) |
| 128 | + |
| 129 | + rawEntries, err := gclhelper.ReadRawLogEntries(framework.Namespace, framework.Name) |
| 130 | + Expect(err).To(BeNil()) |
| 131 | + Expect(rawEntries).ToNot(BeEmpty()) |
| 132 | + |
| 133 | + var auditLogs []types.AuditLogCommon |
| 134 | + jsonString := fmt.Sprintf("[%s]", strings.Join(rawEntries, ",")) |
| 135 | + Expect(types.ParseLogsFrom(jsonString, &auditLogs, false)).To(Succeed()) |
| 136 | + Expect(auditLogs).To(HaveLen(1)) |
| 137 | + Expect(auditLogs[0].LogType).To(Equal(string(obs.InputTypeAudit))) |
| 138 | + Expect(auditLogs[0].Timestamp).ToNot(BeZero()) |
| 139 | + }) |
| 140 | + }) |
| 141 | +}) |
0 commit comments