Skip to content

Commit aa7eecf

Browse files
authored
Ignore environment variables in tests for existing recordings (#2309)
When creating new recordings for testing, tests that already have recordings fail if the host doesn't match. This happens when recording from environments that don't use the usual local values, as for example in the test case added for serverless in #2307. This can even interfere with any other environment variable set by elastic-package stack shellinit. With this change environment variables are only used to initialize the client when creating new recording files and not in subsequent executions.
1 parent b47c1b6 commit aa7eecf

File tree

2 files changed

+63
-19
lines changed

2 files changed

+63
-19
lines changed

internal/elasticsearch/test/httptest.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
package test
66

77
import (
8+
"errors"
9+
"fmt"
810
"os"
911
"testing"
1012

1113
"github.com/stretchr/testify/require"
14+
"gopkg.in/dnaeon/go-vcr.v3/cassette"
1215
"gopkg.in/dnaeon/go-vcr.v3/recorder"
1316

1417
"github.com/elastic/elastic-package/internal/elasticsearch"
@@ -20,16 +23,10 @@ import (
2023
// elastic-package stack, and records the response.
2124
// Responses are recorded in the directory indicated by serverDataDir.
2225
func NewClient(t *testing.T, recordFileName string) *elasticsearch.Client {
23-
address := os.Getenv(stack.ElasticsearchHostEnv)
24-
if address == "" {
25-
address = "https://127.0.0.1:9200"
26-
}
27-
config, err := elasticsearch.NewConfig(
28-
elasticsearch.OptionWithAddress(address),
29-
elasticsearch.OptionWithPassword(os.Getenv(stack.ElasticsearchPasswordEnv)),
30-
elasticsearch.OptionWithUsername(os.Getenv(stack.ElasticsearchUsernameEnv)),
31-
elasticsearch.OptionWithCertificateAuthority(os.Getenv(stack.CACertificateEnv)),
32-
)
26+
options, err := clientOptionsForRecord(recordFileName)
27+
require.NoError(t, err)
28+
29+
config, err := elasticsearch.NewConfig(options...)
3330
require.NoError(t, err)
3431

3532
rec, err := recorder.NewWithOptions(&recorder.Options{
@@ -51,3 +48,27 @@ func NewClient(t *testing.T, recordFileName string) *elasticsearch.Client {
5148

5249
return client
5350
}
51+
52+
func clientOptionsForRecord(recordFileName string) ([]elasticsearch.ClientOption, error) {
53+
const defaultAddress = "https://127.0.0.1:9200"
54+
_, err := os.Stat(cassette.New(recordFileName).File)
55+
if errors.Is(err, os.ErrNotExist) {
56+
address := os.Getenv(stack.ElasticsearchHostEnv)
57+
if address == "" {
58+
address = defaultAddress
59+
}
60+
return []elasticsearch.ClientOption{
61+
elasticsearch.OptionWithAddress(address),
62+
elasticsearch.OptionWithPassword(os.Getenv(stack.ElasticsearchPasswordEnv)),
63+
elasticsearch.OptionWithUsername(os.Getenv(stack.ElasticsearchUsernameEnv)),
64+
elasticsearch.OptionWithCertificateAuthority(os.Getenv(stack.CACertificateEnv)),
65+
}, nil
66+
}
67+
if err != nil {
68+
return nil, fmt.Errorf("failed to check if record file name exists %s: %w", recordFileName, err)
69+
}
70+
options := []elasticsearch.ClientOption{
71+
elasticsearch.OptionWithAddress(defaultAddress),
72+
}
73+
return options, nil
74+
}

internal/kibana/test/httptest.go

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
package test
66

77
import (
8+
"errors"
9+
"fmt"
810
"net/http"
911
"os"
1012
"testing"
1113

1214
"github.com/stretchr/testify/require"
15+
"gopkg.in/dnaeon/go-vcr.v3/cassette"
1316
"gopkg.in/dnaeon/go-vcr.v3/recorder"
1417

1518
"github.com/elastic/elastic-package/internal/kibana"
@@ -36,20 +39,40 @@ func NewClient(t *testing.T, recordFileName string) *kibana.Client {
3639
return rec.GetDefaultClient()
3740
}
3841

39-
address := os.Getenv(stack.KibanaHostEnv)
40-
if address == "" {
41-
address = "https://127.0.0.1:5601"
42-
}
43-
client, err := kibana.NewClient(
44-
kibana.Address(address),
45-
kibana.Password(os.Getenv(stack.ElasticsearchPasswordEnv)),
46-
kibana.Username(os.Getenv(stack.ElasticsearchUsernameEnv)),
47-
kibana.CertificateAuthority(os.Getenv(stack.CACertificateEnv)),
42+
options, err := clientOptionsForRecord(recordFileName)
43+
require.NoError(t, err)
4844

45+
options = append(options,
4946
kibana.HTTPClientSetup(setupHTTPClient),
5047
kibana.RetryMax(0),
5148
)
49+
50+
client, err := kibana.NewClient(options...)
5251
require.NoError(t, err)
5352

5453
return client
5554
}
55+
56+
func clientOptionsForRecord(recordFileName string) ([]kibana.ClientOption, error) {
57+
const defaultAddress = "https://127.0.0.1:5601"
58+
_, err := os.Stat(cassette.New(recordFileName).File)
59+
if errors.Is(err, os.ErrNotExist) {
60+
address := os.Getenv(stack.KibanaHostEnv)
61+
if address == "" {
62+
address = defaultAddress
63+
}
64+
return []kibana.ClientOption{
65+
kibana.Address(address),
66+
kibana.Password(os.Getenv(stack.ElasticsearchPasswordEnv)),
67+
kibana.Username(os.Getenv(stack.ElasticsearchUsernameEnv)),
68+
kibana.CertificateAuthority(os.Getenv(stack.CACertificateEnv)),
69+
}, nil
70+
}
71+
if err != nil {
72+
return nil, fmt.Errorf("failed to check if record file name exists %s: %w", recordFileName, err)
73+
}
74+
options := []kibana.ClientOption{
75+
kibana.Address(defaultAddress),
76+
}
77+
return options, nil
78+
}

0 commit comments

Comments
 (0)