From 85e1b42a5bc1a298ba190cd7086f8f37bea2802f Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Tue, 3 Sep 2024 10:37:04 -0700 Subject: [PATCH] Clean up acceptance test setup --- .../resource_release_bundle_v1_test.go | 10 ++- pkg/distribution/resource_signing_key_test.go | 2 - .../resource_vault_signing_key_test.go | 1 - pkg/distribution/util_test.go | 65 ------------------- 4 files changed, 4 insertions(+), 74 deletions(-) diff --git a/pkg/distribution/resource_release_bundle_v1_test.go b/pkg/distribution/resource_release_bundle_v1_test.go index d8fa979..05afd69 100644 --- a/pkg/distribution/resource_release_bundle_v1_test.go +++ b/pkg/distribution/resource_release_bundle_v1_test.go @@ -10,6 +10,8 @@ import ( "github.com/jfrog/terraform-provider-shared/util" ) +// TestAccReleaseBundleV1_full would only be successful when executed against Artifactory instance +// that has Distribution enabled (i.e. has edge node(s) configured) func TestAccReleaseBundleV1_full(t *testing.T) { _, fqrn, resourceName := testutil.MkNames("test-release-bundle-v1", "distribution_release_bundle_v1") @@ -99,7 +101,6 @@ func TestAccReleaseBundleV1_full(t *testing.T) { updatedConfig := util.ExecuteTemplate("TestAccReleaseBundleV1_full", updatedTemplate, testData) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProviders(), Steps: []resource.TestStep{ { @@ -128,7 +129,7 @@ func TestAccReleaseBundleV1_full(t *testing.T) { resource.TestCheckResourceAttrSet(fqrn, "state"), resource.TestCheckResourceAttrSet(fqrn, "created"), resource.TestCheckResourceAttrSet(fqrn, "created_by"), - resource.TestCheckResourceAttr(fqrn, "artifacts.#", "8"), + resource.TestCheckResourceAttr(fqrn, "artifacts.#", "4"), resource.TestCheckResourceAttrSet(fqrn, "artifacts_size"), resource.TestCheckResourceAttrSet(fqrn, "archived"), ), @@ -169,7 +170,7 @@ func TestAccReleaseBundleV1_full(t *testing.T) { resource.TestCheckResourceAttrSet(fqrn, "state"), resource.TestCheckResourceAttrSet(fqrn, "created"), resource.TestCheckResourceAttrSet(fqrn, "created_by"), - resource.TestCheckResourceAttr(fqrn, "artifacts.#", "8"), + resource.TestCheckResourceAttr(fqrn, "artifacts.#", "4"), resource.TestCheckResourceAttrSet(fqrn, "artifacts_size"), resource.TestCheckResourceAttrSet(fqrn, "archived"), ), @@ -242,7 +243,6 @@ func TestAccReleaseBundleV1_invalid_name(t *testing.T) { config := util.ExecuteTemplate("TestAccReleaseBundleV1_full", template, testData) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProviders(), Steps: []resource.TestStep{ { @@ -311,7 +311,6 @@ func TestAccReleaseBundleV1_invalid_version(t *testing.T) { config := util.ExecuteTemplate("TestAccReleaseBundleV1_full", template, testData) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProviders(), Steps: []resource.TestStep{ { @@ -380,7 +379,6 @@ func TestAccReleaseBundleV1_invalid_query_name(t *testing.T) { config := util.ExecuteTemplate("TestAccReleaseBundleV1_full", template, testData) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProviders(), Steps: []resource.TestStep{ { diff --git a/pkg/distribution/resource_signing_key_test.go b/pkg/distribution/resource_signing_key_test.go index ca06e90..91b9e5c 100644 --- a/pkg/distribution/resource_signing_key_test.go +++ b/pkg/distribution/resource_signing_key_test.go @@ -168,7 +168,6 @@ EOF updatedConfig := util.ExecuteTemplate("TestAccSigningKey_gpg_full", template, updatedTestData) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProviders(), Steps: []resource.TestStep{ { @@ -320,7 +319,6 @@ EOF updatedConfig := util.ExecuteTemplate("TestAccSigningKey_pgp_full", template, updatedTestData) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProviders(), Steps: []resource.TestStep{ { diff --git a/pkg/distribution/resource_vault_signing_key_test.go b/pkg/distribution/resource_vault_signing_key_test.go index 43d003d..10daed3 100644 --- a/pkg/distribution/resource_vault_signing_key_test.go +++ b/pkg/distribution/resource_vault_signing_key_test.go @@ -55,7 +55,6 @@ func TestAccVaultSigningKey_full(t *testing.T) { config := util.ExecuteTemplate("TestAccVaultSigningKey_gpg_full", template, testData) resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, ProtoV6ProviderFactories: testAccProviders(), Steps: []resource.TestStep{ { diff --git a/pkg/distribution/util_test.go b/pkg/distribution/util_test.go index cd0a15d..caa04c7 100644 --- a/pkg/distribution/util_test.go +++ b/pkg/distribution/util_test.go @@ -1,80 +1,15 @@ package distribution_test import ( - "os" - "sync" - "testing" - - "github.com/go-resty/resty/v2" "github.com/hashicorp/terraform-plugin-framework/provider" "github.com/hashicorp/terraform-plugin-framework/providerserver" "github.com/hashicorp/terraform-plugin-go/tfprotov6" "github.com/jfrog/terraform-provider-distribution/pkg/distribution" - "github.com/jfrog/terraform-provider-shared/client" ) // TestProvider PreCheck(t) must be called before using this provider instance. var TestProvider provider.Provider -// testAccProviderConfigure ensures Provider is only configured once -// -// The PreCheck(t) function is invoked for every test and this prevents -// extraneous reconfiguration to the same values each time. However, this does -// not prevent reconfiguration that may happen should the address of -// Provider be errantly reused in ProviderFactories. -var testAccProviderConfigure sync.Once - -// testAccPreCheck This function should be present in every acceptance test. -func testAccPreCheck(t *testing.T) { - // Since we are outside the scope of the Terraform configuration we must - // call Configure() to properly initialize the provider configuration. - testAccProviderConfigure.Do(func() { - restyClient := getTestResty(t) - - platformUrl := getPlatformUrl(t) - // Set custom base URL so repos that relies on it will work - // https://www.jfrog.com/confluence/display/JFROG/Artifactory+REST+API#ArtifactoryRESTAPI-UpdateCustomURLBase - _, err := restyClient.R(). - SetBody(platformUrl). - SetHeader("Content-Type", "text/plain"). - Put("/artifactory/api/system/configuration/baseUrl") - if err != nil { - t.Fatalf("failed to set custom base URL: %v", err) - } - }) -} - -func getTestResty(t *testing.T) *resty.Client { - var ok bool - - platformUrl := getPlatformUrl(t) - - restyClient, err := client.Build(platformUrl, "") - if err != nil { - t.Fatal(err) - } - - var accessToken string - if accessToken, ok = os.LookupEnv("JFROG_ACCESS_TOKEN"); !ok { - t.Fatal("JFROG_ACCESS_TOKEN must be set for acceptance tests") - } - restyClient, err = client.AddAuth(restyClient, "", accessToken) - if err != nil { - t.Fatal(err) - } - - return restyClient -} - -func getPlatformUrl(t *testing.T) string { - platformUrl, ok := os.LookupEnv("JFROG_URL") - if !ok { - t.Fatal("JFROG_URL must be set for acceptance tests") - } - - return platformUrl -} - func testAccProviders() map[string]func() (tfprotov6.ProviderServer, error) { TestProvider = distribution.NewProvider()()