Skip to content
Closed
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
45 changes: 44 additions & 1 deletion internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,57 @@ func NewTestTools(t *testing.T) *TestTools {
}

// Create a http client with recording capabilities
httpClient, cleanup, err := getHTTPRecoder(t, folder, *UpdateCassettes)
httpClient, cleanup, err := getHTTPRecorder(t, folder, *UpdateCassettes, cassetteMatcher)
require.NoError(t, err)

// Create meta that will be passed in the provider config
m, err := meta.NewMeta(ctx, &meta.Config{
ProviderSchema: nil,
TerraformVersion: "terraform-tests",
HTTPClient: httpClient,
ForceProjectID: "00000000-0000-0000-0000-000000000000",
})
require.NoError(t, err)

if !*UpdateCassettes {
tmp := 0 * time.Second
transport.DefaultWaitRetryInterval = &tmp
}

return &TestTools{
T: t,
Meta: m,
ProviderFactories: map[string]func() (*schema.Provider, error){
"scaleway": func() (*schema.Provider, error) {
return provider.Provider(&provider.Config{Meta: m})(), nil
},
},
Cleanup: cleanup,
}
}

/*
Tested resources that depends on a projectID in the query parameters should use this function
Otherwise, tests will fail because the projectID will not match the ones in the recorded cassette queries
*/
func NewTestToolsWithoutDefaultProjectID(t *testing.T) *TestTools {
t.Helper()

ctx := t.Context()

folder, err := os.Getwd()
if err != nil {
t.Fatalf("cannot detect working directory for testing")
}

httpClient, cleanup, err := getHTTPRecorder(t, folder, *UpdateCassettes, cassetteMatcherWithoutProjectID)
require.NoError(t, err)

m, err := meta.NewMeta(ctx, &meta.Config{
ProviderSchema: nil,
TerraformVersion: "terraform-tests",
HTTPClient: httpClient,
ForceProjectID: "00000000-0000-0000-0000-000000000000",
})
require.NoError(t, err)

Expand Down
30 changes: 27 additions & 3 deletions internal/acctest/vcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,30 @@ func cassetteMatcher(actual *http.Request, expected cassette.Request) bool {
cassetteBodyMatcher(actual, expected)
}

// cassetteMatcherWithoutProjectID strip the project_id from the query to match the query of the recorded cassette
func cassetteMatcherWithoutProjectID(actual *http.Request, expected cassette.Request) bool {
expectedURL, _ := url.Parse(expected.URL)
actualURL := actual.URL
actualURLValues := actualURL.Query()
expectedURLValues := expectedURL.Query()

for _, query := range QueryMatcherIgnore {
actualURLValues.Del(query)
expectedURLValues.Del(query)
}

actualURLValues.Del("project_id")
expectedURLValues.Del("project_id")

actualURL.RawQuery = actualURLValues.Encode()
expectedURL.RawQuery = expectedURLValues.Encode()

return actual.Method == expected.Method &&
actual.URL.Path == expectedURL.Path &&
actualURL.RawQuery == expectedURL.RawQuery &&
cassetteBodyMatcher(actual, expected)
}

func cassetteSensitiveFieldsAnonymizer(i *cassette.Interaction) error {
var jsonBody map[string]interface{}

Expand All @@ -260,13 +284,13 @@ func cassetteSensitiveFieldsAnonymizer(i *cassette.Interaction) error {
return nil
}

// getHTTPRecoder creates a new httpClient that records all HTTP requests in a cassette.
// getHTTPRecorder creates a new httpClient that records all HTTP requests in a cassette.
// This cassette is then replayed whenever tests are executed again. This means that once the
// requests are recorded in the cassette, no more real HTTP requests must be made to run the tests.
//
// It is important to add a `defer cleanup()` so the given cassette files are correctly
// closed and saved after the requests.
func getHTTPRecoder(t *testing.T, pkgFolder string, update bool) (client *http.Client, cleanup func(), err error) {
func getHTTPRecorder(t *testing.T, pkgFolder string, update bool, matcherFunc cassette.MatcherFunc) (client *http.Client, cleanup func(), err error) {
t.Helper()

recorderMode := recorder.ModeReplayOnly
Expand Down Expand Up @@ -297,7 +321,7 @@ func getHTTPRecoder(t *testing.T, pkgFolder string, update bool) (client *http.C
}(r)

// Add custom matcher for requests and cassettes
r.SetMatcher(cassetteMatcher)
r.SetMatcher(matcherFunc)

// Add a filter which removes Authorization headers from all requests:
r.AddHook(func(i *cassette.Interaction) error {
Expand Down
24 changes: 0 additions & 24 deletions internal/services/account/project_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,6 @@ func TestAccDataSourceProject_Default(t *testing.T) {
})
}

func TestAccDataSourceProject_Extract(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()

projectID, projectIDExists := tt.Meta.ScwClient().GetDefaultProjectID()
if !projectIDExists {
t.Skip("no default project ID")
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ProviderFactories: tt.ProviderFactories,
Steps: []resource.TestStep{
{
Config: `data scaleway_account_project "project" {}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.scaleway_account_project.project", "id", projectID),
resource.TestCheckResourceAttrSet("data.scaleway_account_project.project", "name"),
),
},
},
})
}

func TestAccDataSourceProject_List(t *testing.T) {
tt := acctest.NewTestTools(t)
defer tt.Cleanup()
Expand Down
3 changes: 2 additions & 1 deletion internal/services/billing/consumption_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

func TestAccDataSourceConsumption_Basic(t *testing.T) {
tt := acctest.NewTestTools(t)
// somehow the project_id is not set in the query parameters locally when the organization_id is set
tt := acctest.NewTestToolsWithoutDefaultProjectID(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
Expand Down
8 changes: 7 additions & 1 deletion internal/services/container/container_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)
Expand Down Expand Up @@ -58,11 +59,16 @@ func DataSourceContainerRead(ctx context.Context, d *schema.ResourceData, m inte
if !ok {
containerName := d.Get("name").(string)

projectID, _, err := meta.ExtractProjectID(d, m)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to ensure that it is taken into account with a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test would break without the ForceProjectID in meta.Config

if err != nil {
return diag.FromErr(err)
}

res, err := api.ListContainers(&container.ListContainersRequest{
Region: region,
Name: types.ExpandStringPtr(containerName),
NamespaceID: locality.ExpandID(namespaceID),
ProjectID: types.ExpandStringPtr(d.Get("project_id")),
ProjectID: types.ExpandStringPtr(projectID),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down
6 changes: 3 additions & 3 deletions internal/services/container/container_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestAccDataSourceContainer_Basic(t *testing.T) {
tt := acctest.NewTestTools(t)
tt := acctest.NewTestToolsWithoutDefaultProjectID(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -30,7 +30,7 @@ func TestAccDataSourceContainer_Basic(t *testing.T) {
namespace_id = scaleway_container_namespace.main.id
name = scaleway_container.main.name
}

data "scaleway_container" "by_id" {
namespace_id = scaleway_container_namespace.main.id
container_id = scaleway_container.main.id
Expand All @@ -51,7 +51,7 @@ func TestAccDataSourceContainer_Basic(t *testing.T) {
}

func TestAccDataSourceContainer_HealthCheck(t *testing.T) {
tt := acctest.NewTestTools(t)
tt := acctest.NewTestToolsWithoutDefaultProjectID(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
Expand Down
78 changes: 29 additions & 49 deletions internal/services/container/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/acctest"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/container"
containerchecks "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/container/testfuncs"
)

func TestAccContainer_Basic(t *testing.T) {
Expand Down Expand Up @@ -220,51 +219,32 @@ func TestAccContainer_WithIMG(t *testing.T) {
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
resource scaleway_container_namespace main {
name = "%s"
description = "test container"
}
`, containerNamespace),
},
{
Config: fmt.Sprintf(`
resource scaleway_container_namespace main {
name = "%s"
description = "test container"
}
`, containerNamespace),
Check: resource.ComposeTestCheckFunc(
containerchecks.TestConfigContainerNamespace(tt, "scaleway_container_namespace.main"),
),
},
{
Config: fmt.Sprintf(`
resource scaleway_container_namespace main {
name = "%s"
description = "test container"
}

resource scaleway_container main {
name = "my-container-02"
description = "environment variables test"
namespace_id = scaleway_container_namespace.main.id
registry_image = "${scaleway_container_namespace.main.registry_endpoint}/nginx:test"
port = 80
cpu_limit = 140
memory_limit = 256
min_scale = 3
max_scale = 5
timeout = 600
max_concurrency = 80
privacy = "private"
protocol = "h2c"
deploy = true

environment_variables = {
"foo" = "var"
}
}
`, containerNamespace),
resource scaleway_container_namespace main {
name = "%s"
description = "test container"
}

resource scaleway_container main {
name = "my-container-02"
description = "environment variables test"
namespace_id = scaleway_container_namespace.main.id
registry_image = "docker.io/library/nginx:latest"
port = 80
cpu_limit = 140
memory_limit = 256
min_scale = 3
max_scale = 5
timeout = 600
max_concurrency = 80
privacy = "private"
protocol = "h2c"
deploy = true

environment_variables = {
"foo" = "var"
}
}
`, containerNamespace),
Check: resource.ComposeTestCheckFunc(
isContainerPresent(tt, "scaleway_container.main"),
acctest.CheckResourceAttrUUID("scaleway_container.main", "id"),
Expand Down Expand Up @@ -493,7 +473,7 @@ func TestAccContainer_ScalingOption(t *testing.T) {
{
Config: `
resource scaleway_container_namespace main {}

resource scaleway_container main {
namespace_id = scaleway_container_namespace.main.id
deploy = false
Expand All @@ -512,7 +492,7 @@ func TestAccContainer_ScalingOption(t *testing.T) {
{
Config: `
resource scaleway_container_namespace main {}

resource scaleway_container main {
namespace_id = scaleway_container_namespace.main.id
deploy = false
Expand All @@ -534,7 +514,7 @@ func TestAccContainer_ScalingOption(t *testing.T) {
{
Config: `
resource scaleway_container_namespace main {}

resource scaleway_container main {
namespace_id = scaleway_container_namespace.main.id
deploy = false
Expand Down
8 changes: 7 additions & 1 deletion internal/services/container/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/registry"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
Expand Down Expand Up @@ -108,12 +109,17 @@ func ResourceContainerNamespaceCreate(ctx context.Context, d *schema.ResourceDat
return diag.FromErr(err)
}

projectId, _, err := meta.ExtractProjectID(d, m)
if err != nil {
return diag.FromErr(err)
}

createReq := &container.CreateNamespaceRequest{
Description: types.ExpandStringPtr(d.Get("description").(string)),
EnvironmentVariables: types.ExpandMapPtrStringString(d.Get("environment_variables")),
SecretEnvironmentVariables: expandContainerSecrets(d.Get("secret_environment_variables")),
Name: types.ExpandOrGenerateString(d.Get("name").(string), "ns"),
ProjectID: d.Get("project_id").(string),
ProjectID: projectId,
Region: region,
}

Expand Down
8 changes: 7 additions & 1 deletion internal/services/container/namespace_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/datasource"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)
Expand Down Expand Up @@ -43,10 +44,15 @@ func DataSourceContainerNamespaceRead(ctx context.Context, d *schema.ResourceDat
if !ok {
namespaceName := d.Get("name").(string)

projectID, _, err := meta.ExtractProjectID(d, m)
if err != nil {
return diag.FromErr(err)
}

res, err := api.ListNamespaces(&container.ListNamespacesRequest{
Region: region,
Name: types.ExpandStringPtr(namespaceName),
ProjectID: types.ExpandStringPtr(d.Get("project_id")),
ProjectID: types.ExpandStringPtr(projectID),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down
6 changes: 3 additions & 3 deletions internal/services/container/namespace_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestAccDataSourceNamespace_Basic(t *testing.T) {
tt := acctest.NewTestTools(t)
tt := acctest.NewTestToolsWithoutDefaultProjectID(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -21,11 +21,11 @@ func TestAccDataSourceNamespace_Basic(t *testing.T) {
resource "scaleway_container_namespace" "main" {
name = "test-cr-data"
}

data "scaleway_container_namespace" "by_name" {
name = scaleway_container_namespace.main.name
}

data "scaleway_container_namespace" "by_id" {
namespace_id = scaleway_container_namespace.main.id
}
Expand Down
Loading
Loading