From 5068dc5f6eca073c7b858e71a336a90f0786bb96 Mon Sep 17 00:00:00 2001 From: Elliott Krupa Date: Tue, 14 Nov 2023 17:40:13 -0500 Subject: [PATCH] client: better support for nondefault api hosts (#393) This PR updates the client to support non-default API hosts by looking at the `HONEYCOMB_API_URL` environment variable. Additionally, it: - Brings CONTRIBUTING.md up to speed with the current state of the tests - Updates `scripts/setup-testsuite-dataset` with all the columns the tests need ## How to verify that this has the expected result CI tests should still pass as expected. --- CONTRIBUTING.md | 23 ++++++++++++++++--- client/client.go | 11 ++++++--- client/errors_test.go | 7 +++--- internal/provider/burn_alert_resource_test.go | 11 +++++++++ internal/provider/trigger_resource_test.go | 11 +++++++++ scripts/setup-testsuite-dataset | 3 +++ 6 files changed, 57 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 35a0a3e3..653e9307 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -24,11 +24,28 @@ Hashicorp has a tool to preview documentation. Visit [registry.terraform.io/tool ### Running the test -Most of the tests are acceptance tests, which will call real APIs. To run the tests you'll need to have access to a Honeycomb account. If not, you can create a new free team. +Most of the tests are acceptance tests, which will call real APIs. To run the tests you'll need to have access to a Honeycomb account/team. If not, you can create a new free team to run the majority of the tests. -First, **create an API key**. Initially you'll have to check all permissions, but _Send Events_ and _Create Datasets_ can be disabled once setup is done. +Some tests, such as those for SLOs and those for the Query Data API require an Enterprise team. -Next, **initialize the dataset**. The helper script [setup-testsuite-dataset](scripts/setup-testsuite-dataset) will create the dataset and required columns that are used in the tests. +Additionally, the test for a Slack recipient requires that the Slack authorization be [set up with the team ahead of time](https://docs.honeycomb.io/working-with-your-data/triggers/#slack) + +First, **create an API key**. This key should have the following permissions: +- Create Datasets +- Manage Queries and Columns +- Run Queries +- Manage Public Boards +- Manage SLOs +- Manage Triggers +- Manage Recipients +- Manage Markers + +Next, **initialize the dataset**. The helper script [setup-testsuite-dataset](scripts/setup-testsuite-dataset) will create the dataset and required columns that are used in the tests. +You will need to use Bash 4+ to run this script. + +```sh +HONEYCOMB_API_KEY= HONEYCOMB_DATASET= ./scripts/setup-testsuite-dataset +``` Finally, **run the acceptance tests** by passing the API key and dataset as environment variables: diff --git a/client/client.go b/client/client.go index 918e35fe..9eb8fef4 100644 --- a/client/client.go +++ b/client/client.go @@ -22,8 +22,9 @@ import ( ) const ( - DefaultAPIHost = "https://api.honeycomb.io" - DefaultAPIKeyEnv = "HONEYCOMB_API_KEY" + DefaultAPIHost = "https://api.honeycomb.io" + DefaultAPIHostEnv = "HONEYCOMB_API_HOST" + DefaultAPIKeyEnv = "HONEYCOMB_API_KEY" // Deprecated: use DefaultAPIKeyEnv instead. To be removed in v1.0 LegacyAPIKeyEnv = "HONEYCOMBIO_API_KEY" defaultUserAgent = "go-honeycombio" @@ -70,7 +71,7 @@ type Client struct { func DefaultConfig() *Config { c := &Config{ APIKey: os.Getenv(DefaultAPIKeyEnv), - APIUrl: DefaultAPIHost, + APIUrl: os.Getenv(DefaultAPIHostEnv), Debug: false, HTTPClient: cleanhttp.DefaultPooledClient(), UserAgent: defaultUserAgent, @@ -81,6 +82,10 @@ func DefaultConfig() *Config { c.APIKey = os.Getenv(LegacyAPIKeyEnv) } + if c.APIUrl == "" { + c.APIUrl = DefaultAPIHost + } + return c } diff --git a/client/errors_test.go b/client/errors_test.go index 7b24bda8..7e9a473a 100644 --- a/client/errors_test.go +++ b/client/errors_test.go @@ -2,6 +2,7 @@ package client import ( "context" + "fmt" "net/http" "testing" @@ -19,7 +20,7 @@ func TestClient_ParseDetailedError(t *testing.T) { require.Error(t, err) assert.ErrorAs(t, err, &de) assert.Equal(t, de.Status, http.StatusBadRequest) - assert.Equal(t, de.Type, "https://api.honeycomb.io/problems/unparseable") + assert.Equal(t, de.Type, fmt.Sprintf("%s/problems/unparseable", c.apiURL)) assert.Equal(t, de.Title, "The request body could not be parsed.") assert.Equal(t, de.Message, "could not parse request body") }) @@ -29,7 +30,7 @@ func TestClient_ParseDetailedError(t *testing.T) { require.Error(t, err) assert.ErrorAs(t, err, &de) assert.Equal(t, de.Status, http.StatusNotFound) - assert.Equal(t, de.Type, "https://api.honeycomb.io/problems/not-found") + assert.Equal(t, de.Type, fmt.Sprintf("%s/problems/not-found", c.apiURL)) assert.Equal(t, de.Title, "The requested resource cannot be found.") assert.Equal(t, de.Message, "Dataset not found") }) @@ -40,7 +41,7 @@ func TestClient_ParseDetailedError(t *testing.T) { require.Error(t, err) assert.ErrorAs(t, err, &de) assert.Equal(t, http.StatusUnprocessableEntity, de.Status) - assert.Equal(t, "https://api.honeycomb.io/problems/validation-failed", de.Type) + assert.Equal(t, fmt.Sprintf("%s/problems/validation-failed", c.apiURL), de.Type) assert.Equal(t, "The provided input is invalid.", de.Title) assert.Equal(t, "The provided input is invalid.", de.Message) assert.Equal(t, 1, len(de.Details)) diff --git a/internal/provider/burn_alert_resource_test.go b/internal/provider/burn_alert_resource_test.go index 1ba57de8..4b465c3d 100644 --- a/internal/provider/burn_alert_resource_test.go +++ b/internal/provider/burn_alert_resource_test.go @@ -3,6 +3,7 @@ package provider import ( "context" "fmt" + "os" "testing" "github.com/hashicorp/terraform-plugin-testing/helper/acctest" @@ -65,6 +66,16 @@ func TestAcc_BurnAlertResourceUpgradeFromVersion015(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccEnsureBurnAlertExists(t, "honeycombio_burn_alert.test"), ), + // These tests pull in older versions of the provider that don't + // support setting the API host easily. We'll skip them for now + // if we have a non-default API host. + SkipFunc: func() (bool, error) { + apiHost := os.Getenv(client.DefaultAPIHostEnv) + if apiHost == "" { + return false, nil + } + return apiHost != client.DefaultAPIHost, nil + }, }, { ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory, diff --git a/internal/provider/trigger_resource_test.go b/internal/provider/trigger_resource_test.go index 2ecb8343..1065dc7e 100644 --- a/internal/provider/trigger_resource_test.go +++ b/internal/provider/trigger_resource_test.go @@ -3,6 +3,7 @@ package provider import ( "context" "fmt" + "os" "testing" "github.com/stretchr/testify/require" @@ -68,6 +69,16 @@ func TestAcc_TriggerResourceUpgradeFromVersion014(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccEnsureTriggerExists(t, "honeycombio_trigger.test"), ), + // These tests pull in older versions of the provider that don't + // support setting the API host easily. We'll skip them for now + // if we have a non-default API host. + SkipFunc: func() (bool, error) { + apiHost := os.Getenv(client.DefaultAPIHostEnv) + if apiHost == "" { + return false, nil + } + return apiHost != client.DefaultAPIHost, nil + }, }, { ProtoV5ProviderFactories: testAccProtoV5MuxServerFactory, diff --git a/scripts/setup-testsuite-dataset b/scripts/setup-testsuite-dataset index df871535..0c83ca1c 100755 --- a/scripts/setup-testsuite-dataset +++ b/scripts/setup-testsuite-dataset @@ -11,11 +11,14 @@ DATASETS_API="${HONEYCOMB_API}/1/datasets" declare -A REQUIRED_COLUMNS=( [trace.parent_id]=string [trace.trace_id]=string + [trace.span_id]=string [app.tenant]=string [app.error]=string [column_1]=string [column_2]=string [duration_ms]=float + [name]=string + [service_name]=string ) log () {