Skip to content

Commit

Permalink
client: better support for nondefault api hosts (#393)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cewkrupa authored Nov 14, 2023
1 parent 1009fcb commit 5068dc5
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
23 changes: 20 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<your API key> HONEYCOMB_DATASET=<dataset> ./scripts/setup-testsuite-dataset
```

Finally, **run the acceptance tests** by passing the API key and dataset as environment variables:

Expand Down
11 changes: 8 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -81,6 +82,10 @@ func DefaultConfig() *Config {
c.APIKey = os.Getenv(LegacyAPIKeyEnv)
}

if c.APIUrl == "" {
c.APIUrl = DefaultAPIHost
}

return c
}

Expand Down
7 changes: 4 additions & 3 deletions client/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"fmt"
"net/http"
"testing"

Expand All @@ -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")
})
Expand All @@ -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")
})
Expand All @@ -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))
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/burn_alert_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/trigger_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provider
import (
"context"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions scripts/setup-testsuite-dataset
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 5068dc5

Please sign in to comment.