Skip to content

Commit

Permalink
Clean up acceptance test setup
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhung committed Sep 3, 2024
1 parent 84a7543 commit 85e1b42
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 74 deletions.
10 changes: 4 additions & 6 deletions pkg/distribution/resource_release_bundle_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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"),
),
Expand Down Expand Up @@ -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"),
),
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down
2 changes: 0 additions & 2 deletions pkg/distribution/resource_signing_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down Expand Up @@ -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{
{
Expand Down
1 change: 0 additions & 1 deletion pkg/distribution/resource_vault_signing_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down
65 changes: 0 additions & 65 deletions pkg/distribution/util_test.go
Original file line number Diff line number Diff line change
@@ -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()()

Expand Down

0 comments on commit 85e1b42

Please sign in to comment.