Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(d/column): fix failing d/column test and tidy up #548

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 37 additions & 44 deletions honeycombio/data_source_column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,68 @@ import (
"context"
"fmt"
"regexp"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"

honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client"
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper/test"
)

func TestAccDataSourceHoneycombioColumn_basic(t *testing.T) {
ctx := context.Background()
c := testAccClient(t)
dataset := testAccDataset()

testColumns := []honeycombio.Column{
{
KeyName: acctest.RandString(4) + "_test_column3",
Description: "test column3",
Type: honeycombio.ToPtr(honeycombio.ColumnType("float")),
},
}

for i, column := range testColumns {
col, err := c.Columns.Create(ctx, dataset, &column)
require.NoError(t, err)
// update ID for removal later
testColumns[i].ID = col.ID

}
//nolint:errcheck
col, err := c.Columns.Create(ctx, dataset, &honeycombio.Column{
KeyName: test.RandomStringWithPrefix("test.", 10),
Description: test.RandomString(20),
Type: honeycombio.ToPtr(honeycombio.ColumnTypeFloat),
})
require.NoError(t, err)
t.Cleanup(func() {
// remove Columns at the of the test run
for _, col := range testColumns {
c.Columns.Delete(ctx, dataset, col.ID)
}
c.Columns.Delete(ctx, dataset, col.ID)
})

resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ProtoV5ProviderFactories: testAccProtoV5ProviderFactory,
Steps: []resource.TestStep{
// match by name and return a single column with the right type
{
Config: testAccDataSourceColumnConfig([]string{"dataset = \"" + testAccDataset() + "\"", "name = \"" + testColumns[0].KeyName + "\""}),
Check: resource.TestCheckResourceAttr("data.honeycombio_column.test", "type", "float"),
},
// test a failed match
{
Config: testAccDataSourceColumnConfig([]string{"dataset = \"" + testAccDataset() + "\"", "name = \"test_column5\""}),
ExpectError: regexp.MustCompile("404 Not Found"),
Config: fmt.Sprintf(`
data "honeycombio_column" "test" {
dataset = "%s"
name = "%s"
}`, dataset, col.KeyName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.honeycombio_column.test", "name", col.KeyName),
resource.TestCheckResourceAttr("data.honeycombio_column.test", "description", col.Description),
resource.TestCheckResourceAttr("data.honeycombio_column.test", "type", "float"),
resource.TestCheckResourceAttr("data.honeycombio_column.test", "hidden", "false"),
resource.TestCheckResourceAttrSet("data.honeycombio_column.test", "last_written_at"),
resource.TestCheckResourceAttrSet("data.honeycombio_column.test", "created_at"),
resource.TestCheckResourceAttrSet("data.honeycombio_column.test", "updated_at"),
),
PlanOnly: true,
},
},
})
}

func testAccDataSourceColumnConfig(filters []string) string {
return fmt.Sprintf(`
resource.Test(t, resource.TestCase{
PreCheck: testAccPreCheck(t),
ProtoV5ProviderFactories: testAccProtoV5ProviderFactory,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
data "honeycombio_column" "test" {
%s
}

output "type" {
value = data.honeycombio_column.test.type
}

output "description" {
value = data.honeycombio_column.test.description
}
`, strings.Join(filters, "\n"))
dataset = "%s"
name = "does-not-exist"
}`, dataset),
PlanOnly: true,
ExpectError: regexp.MustCompile(`(?i)not found`),
},
},
})
}
106 changes: 64 additions & 42 deletions honeycombio/data_source_columns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,35 @@ package honeycombio
import (
"context"
"fmt"
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

honeycombio "github.com/honeycombio/terraform-provider-honeycombio/client"
"github.com/honeycombio/terraform-provider-honeycombio/internal/helper/test"
)

func TestAccDataSourceHoneycombioColumns_basic(t *testing.T) {
ctx := context.Background()
c := testAccClient(t)
dataset := testAccDataset()

testprefix := acctest.RandString(4)

testColumns := []honeycombio.Column{
{
KeyName: testprefix + "_test_column1",
Description: "test column1",
},
{
KeyName: testprefix + "_test_column2",
Description: "test column2",
},
}

for i, column := range testColumns {
col, err := c.Columns.Create(ctx, dataset, &column)
const numColumns = 5
testFilterPrefix := test.RandomStringWithPrefix("test.", 5)
testColumns := make([]*honeycombio.Column, 0, numColumns)
for range numColumns {
col, err := c.Columns.Create(ctx, dataset, &honeycombio.Column{
KeyName: test.RandomStringWithPrefix(testFilterPrefix+".", 10),
Description: test.RandomString(20),
Type: honeycombio.ToPtr(honeycombio.ColumnTypeFloat),
})
require.NoError(t, err)
// update ID for removal later
testColumns[i].ID = col.ID
testColumns = append(testColumns, col)
}
//nolint:errcheck
t.Cleanup(func() {
// remove Columns at the of the test run
for _, col := range testColumns {
c.Columns.Delete(ctx, dataset, col.ID)
}
Expand All @@ -51,32 +42,63 @@ func TestAccDataSourceHoneycombioColumns_basic(t *testing.T) {
ProtoV5ProviderFactories: testAccProtoV5ProviderFactory,
Steps: []resource.TestStep{
{
Config: testAccDataSourceColumnsConfig([]string{"dataset = \"" + testAccDataset() + "\""}),
Check: resource.ComposeTestCheckFunc(
testCheckOutputContains("names", testColumns[0].KeyName),
),
},
{
Config: testAccDataSourceColumnsConfig([]string{"dataset = \"" + testAccDataset() + "\"", "starts_with = \"" + testprefix + "\""}),
Check: resource.TestCheckResourceAttr("data.honeycombio_columns.test", "names.#", "2"),
},
{
Config: testAccDataSourceColumnsConfig([]string{"dataset = \"" + testAccDataset() + "\"", "starts_with = \"foo\""}),
Check: resource.ComposeTestCheckFunc(
testCheckOutputDoesNotContain("names", testColumns[0].KeyName),
Config: fmt.Sprintf(`
data "honeycombio_columns" "all" {
dataset = "%[1]s"
}

data "honeycombio_columns" "filtered" {
dataset = "%[1]s"
starts_with = "%[2]s"
}

data "honeycombio_columns" "none" {
dataset = "%[1]s"
starts_with = "does-not-exist"
}

output "all" {
value = data.honeycombio_columns.all.names
}`, dataset, testFilterPrefix),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAllOutputContains(testColumns[0].KeyName),
testCheckAllOutputContains(testColumns[1].KeyName),
testCheckAllOutputContains(testColumns[2].KeyName),
testCheckAllOutputContains(testColumns[3].KeyName),
testCheckAllOutputContains(testColumns[4].KeyName),
resource.TestCheckResourceAttr("data.honeycombio_columns.filtered",
"names.#",
fmt.Sprintf("%d", numColumns),
),
resource.TestCheckResourceAttr("data.honeycombio_columns.none",
"names.#",
"0",
),
),
PlanOnly: true,
},
},
})
}

func testAccDataSourceColumnsConfig(filters []string) string {
return fmt.Sprintf(`
data "honeycombio_columns" "test" {
%s
}
func testCheckAllOutputContains(contains string) resource.TestCheckFunc {
return func(s *terraform.State) error {
const name = "all"

output "names" {
value = data.honeycombio_columns.test.names
}`, strings.Join(filters, "\n"))
ms := s.RootModule()
rs, ok := ms.Outputs[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

output := rs.Value.([]interface{})

for _, value := range output {
if value.(string) == contains {
return nil
}
}

return fmt.Errorf("Output '%s' did not contain %#v, got %#v", name, contains, output)
}
}
65 changes: 0 additions & 65 deletions honeycombio/helpers_test.go

This file was deleted.