|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +//go:build integration |
| 6 | + |
| 7 | +package integration |
| 8 | + |
| 9 | +import ( |
| 10 | + "bytes" |
| 11 | + "context" |
| 12 | + "fmt" |
| 13 | + "net/http/httptest" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | + "text/template" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/gofrs/uuid/v5" |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | + |
| 23 | + "github.com/elastic/beats/v7/libbeat/tests/integration" |
| 24 | + "github.com/elastic/beats/v7/x-pack/filebeat/input/azureblobstorage/mock" |
| 25 | + "github.com/elastic/beats/v7/x-pack/otel/oteltest" |
| 26 | + "github.com/elastic/beats/v7/x-pack/otel/oteltestcol" |
| 27 | + "github.com/elastic/elastic-agent-libs/testing/estools" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + azureBlobTestAccountName = "beatsblobnew" |
| 32 | + azureBlobTestAccountKey = "7pfLm1betGiRyyABEM/RFrLYlafLZHbLtGhB52LkWVeBxE7la9mIvk6YYAbQKYE/f0GdhiaOZeV8+AStsAdr/Q==" |
| 33 | + azureBlobTestContainer = "beatscontainer" |
| 34 | + azureBlobTestBlob = "ata.json" |
| 35 | + azureBlobTestMessage = "iPhone 9" |
| 36 | +) |
| 37 | + |
| 38 | +func startAzureBlobMockStorageServer(t *testing.T) string { |
| 39 | + t.Helper() |
| 40 | + |
| 41 | + srv := httptest.NewServer(mock.AzureStorageServer()) |
| 42 | + t.Cleanup(srv.Close) |
| 43 | + |
| 44 | + return srv.URL + "/" |
| 45 | +} |
| 46 | + |
| 47 | +func TestAzureBlobStorageInputOTelE2E(t *testing.T) { |
| 48 | + integration.EnsureESIsRunning(t) |
| 49 | + |
| 50 | + storageURL := startAzureBlobMockStorageServer(t) |
| 51 | + otelHome := t.TempDir() |
| 52 | + |
| 53 | + host := integration.GetESURL(t, "http") |
| 54 | + user := host.User.Username() |
| 55 | + password, _ := host.User.Password() |
| 56 | + |
| 57 | + otelNamespace := strings.ReplaceAll(uuid.Must(uuid.NewV4()).String(), "-", "") |
| 58 | + fbNamespace := strings.ReplaceAll(uuid.Must(uuid.NewV4()).String(), "-", "") |
| 59 | + |
| 60 | + otelIndex := "logs-integration-" + otelNamespace |
| 61 | + fbIndex := "logs-integration-" + fbNamespace |
| 62 | + |
| 63 | + type options struct { |
| 64 | + Index string |
| 65 | + ESURL string |
| 66 | + Username string |
| 67 | + Password string |
| 68 | + StorageURL string |
| 69 | + PathHome string |
| 70 | + AccountName string |
| 71 | + AccountKey string |
| 72 | + } |
| 73 | + |
| 74 | + filebeatConfig := `filebeat.inputs: |
| 75 | +- type: azure-blob-storage |
| 76 | + id: azure-blob-storage-input-e2e |
| 77 | + account_name: {{ .AccountName }} |
| 78 | + storage_url: {{ .StorageURL }} |
| 79 | + auth: |
| 80 | + shared_credentials: |
| 81 | + account_key: {{ .AccountKey }} |
| 82 | + poll: false |
| 83 | + max_workers: 1 |
| 84 | + containers: |
| 85 | + - name: ` + azureBlobTestContainer + ` |
| 86 | + file_selectors: |
| 87 | + - regex: '^` + azureBlobTestBlob + `$' |
| 88 | +
|
| 89 | +output: |
| 90 | + elasticsearch: |
| 91 | + hosts: |
| 92 | + - {{ .ESURL }} |
| 93 | + username: {{ .Username }} |
| 94 | + password: {{ .Password }} |
| 95 | + index: {{ .Index }} |
| 96 | +
|
| 97 | +queue.mem.flush.timeout: 0s |
| 98 | +setup.template.enabled: false |
| 99 | +processors: |
| 100 | + - add_host_metadata: ~ |
| 101 | + - add_cloud_metadata: ~ |
| 102 | + - add_docker_metadata: ~ |
| 103 | + - add_kubernetes_metadata: ~ |
| 104 | +` |
| 105 | + |
| 106 | + otelConfig := otelElasticsearchExporterYAML + `receivers: |
| 107 | + filebeatreceiver: |
| 108 | + filebeat: |
| 109 | + inputs: |
| 110 | + - type: azure-blob-storage |
| 111 | + id: azure-blob-storage-input-e2e |
| 112 | + account_name: {{ .AccountName }} |
| 113 | + storage_url: {{ .StorageURL }} |
| 114 | + auth: |
| 115 | + shared_credentials: |
| 116 | + account_key: {{ .AccountKey }} |
| 117 | + poll: false |
| 118 | + max_workers: 1 |
| 119 | + containers: |
| 120 | + - name: ` + azureBlobTestContainer + ` |
| 121 | + file_selectors: |
| 122 | + - regex: '^` + azureBlobTestBlob + `$' |
| 123 | + processors: |
| 124 | + - add_host_metadata: ~ |
| 125 | + - add_cloud_metadata: ~ |
| 126 | + - add_docker_metadata: ~ |
| 127 | + - add_kubernetes_metadata: ~ |
| 128 | + queue.mem.flush.timeout: 0s |
| 129 | + setup.template.enabled: false |
| 130 | + path.home: {{ .PathHome }} |
| 131 | +` + otelElasticsearchServiceYAML |
| 132 | + |
| 133 | + optionsValue := options{ |
| 134 | + ESURL: fmt.Sprintf("%s://%s", host.Scheme, host.Host), |
| 135 | + Username: user, |
| 136 | + Password: password, |
| 137 | + StorageURL: storageURL, |
| 138 | + PathHome: otelHome, |
| 139 | + AccountName: azureBlobTestAccountName, |
| 140 | + AccountKey: azureBlobTestAccountKey, |
| 141 | + } |
| 142 | + |
| 143 | + var configBuffer bytes.Buffer |
| 144 | + optionsValue.Index = otelIndex |
| 145 | + require.NoError(t, template.Must(template.New("config").Parse(otelConfig)).Execute(&configBuffer, optionsValue)) |
| 146 | + |
| 147 | + oteltestcol.New(t, configBuffer.String()) |
| 148 | + |
| 149 | + configBuffer.Reset() |
| 150 | + |
| 151 | + optionsValue.Index = fbIndex |
| 152 | + require.NoError(t, template.Must(template.New("config").Parse(filebeatConfig)).Execute(&configBuffer, optionsValue)) |
| 153 | + |
| 154 | + filebeat := integration.NewBeat( |
| 155 | + t, |
| 156 | + "filebeat", |
| 157 | + "../../filebeat.test", |
| 158 | + ) |
| 159 | + filebeat.WriteConfigFile(configBuffer.String()) |
| 160 | + filebeat.Start() |
| 161 | + defer filebeat.Stop() |
| 162 | + |
| 163 | + filebeat.WaitLogsContains( |
| 164 | + "filebeat start running", |
| 165 | + 20*time.Second, |
| 166 | + "filebeat did not run", |
| 167 | + ) |
| 168 | + |
| 169 | + es := integration.GetESClient(t, "http") |
| 170 | + |
| 171 | + t.Cleanup(func() { |
| 172 | + deleteDataStreamsFromES(t, es, []string{otelIndex, fbIndex}) |
| 173 | + }) |
| 174 | + |
| 175 | + rawQuery := otelE2ERawQueryForInputTypeAndMessage("azure-blob-storage", azureBlobTestMessage) |
| 176 | + |
| 177 | + var filebeatDocs estools.Documents |
| 178 | + var otelDocs estools.Documents |
| 179 | + var err error |
| 180 | + |
| 181 | + require.EventuallyWithTf(t, |
| 182 | + func(ct *assert.CollectT) { |
| 183 | + findCtx, findCancel := context.WithTimeout(t.Context(), 900*time.Millisecond) |
| 184 | + defer findCancel() |
| 185 | + |
| 186 | + otelDocs, err = estools.PerformQueryForRawQuery(findCtx, rawQuery, ".ds-"+otelIndex+"*", es) |
| 187 | + assert.NoError(ct, err) |
| 188 | + assert.GreaterOrEqual(ct, otelDocs.Hits.Total.Value, 1, "expected at least 1 otel document, got %d", otelDocs.Hits.Total.Value) |
| 189 | + |
| 190 | + filebeatDocs, err = estools.PerformQueryForRawQuery(findCtx, rawQuery, ".ds-"+fbIndex+"*", es) |
| 191 | + assert.NoError(ct, err) |
| 192 | + assert.GreaterOrEqual(ct, filebeatDocs.Hits.Total.Value, 1, "expected at least 1 filebeat document, got %d", filebeatDocs.Hits.Total.Value) |
| 193 | + }, |
| 194 | + 3*time.Minute, 1*time.Second, "expected at least 1 document for both filebeat and otel modes") |
| 195 | + |
| 196 | + filebeatDoc := filebeatDocs.Hits.Hits[0].Source |
| 197 | + otelDoc := otelDocs.Hits.Hits[0].Source |
| 198 | + ignoredFields := []string{ |
| 199 | + "@timestamp", |
| 200 | + "agent.ephemeral_id", |
| 201 | + "agent.id", |
| 202 | + } |
| 203 | + |
| 204 | + oteltest.AssertMapsEqual(t, filebeatDoc, otelDoc, ignoredFields, "expected documents to be equal") |
| 205 | +} |
0 commit comments