-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache error responses for cloudfoundry apps metadata (#19181)
Cache error responses when requesting Cloud Foundry apps metadata to avoid hitting continuously the API when there are missing applications.
- Loading branch information
Showing
9 changed files
with
142 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
x-pack/libbeat/common/cloudfoundry/cache_integration_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
// +build integration | ||
// +build cloudfoundry | ||
|
||
package cloudfoundry | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/cloudfoundry-community/go-cfclient" | ||
|
||
"github.com/elastic/beats/v7/libbeat/common" | ||
"github.com/elastic/beats/v7/libbeat/logp" | ||
cftest "github.com/elastic/beats/v7/x-pack/libbeat/common/cloudfoundry/test" | ||
) | ||
|
||
func TestGetApps(t *testing.T) { | ||
var conf Config | ||
err := common.MustNewConfigFrom(cftest.GetConfigFromEnv(t)).Unpack(&conf) | ||
require.NoError(t, err) | ||
|
||
log := logp.NewLogger("cloudfoundry") | ||
hub := NewHub(&conf, "filebeat", log) | ||
|
||
client, err := hub.Client() | ||
require.NoError(t, err) | ||
apps, err := client.(*clientCacheWrap).client.(*cfclient.Client).ListApps() | ||
require.NoError(t, err) | ||
|
||
t.Logf("%d applications available", len(apps)) | ||
|
||
t.Run("request one of the available applications", func(t *testing.T) { | ||
if len(apps) == 0 { | ||
t.Skip("no apps in account?") | ||
} | ||
client, err := hub.Client() | ||
require.NoError(t, err) | ||
|
||
guid := apps[0].Guid | ||
app, err := client.GetAppByGuid(guid) | ||
assert.Equal(t, guid, app.Guid) | ||
assert.NoError(t, err) | ||
}) | ||
|
||
t.Run("handle error when application is not available", func(t *testing.T) { | ||
client, err := hub.Client() | ||
require.NoError(t, err) | ||
|
||
testNotExists := func(t *testing.T) { | ||
app, err := client.GetAppByGuid("notexists") | ||
assert.Nil(t, app) | ||
assert.True(t, cfclient.IsAppNotFoundError(err)) | ||
} | ||
|
||
var firstTimeDuration time.Duration | ||
t.Run("first call", func(t *testing.T) { | ||
startTime := time.Now() | ||
testNotExists(t) | ||
firstTimeDuration = time.Now().Sub(startTime) | ||
}) | ||
|
||
t.Run("second call, in cache, faster, same response", func(t *testing.T) { | ||
for i := 0; i < 10; i++ { | ||
startTime := time.Now() | ||
testNotExists(t) | ||
require.True(t, firstTimeDuration > time.Now().Sub(startTime)) | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters