Skip to content

Commit

Permalink
Update package_type validation for xray_binary_manager_repos
Browse files Browse the repository at this point in the history
Update test to match

Increase test timeout in makefile
  • Loading branch information
alexhung committed Dec 5, 2024
1 parent 3b13059 commit 71abaf6
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 137 deletions.
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ attach:

acceptance: fmt
export TF_ACC=true && \
go test -cover -coverprofile=coverage.txt -ldflags="-X '${PKG_VERSION_PATH}/provider.Version=${NEXT_VERSION}-test'" -v -p 1 -parallel 20 -timeout 35m ./pkg/...
go test -cover -coverprofile=coverage.txt -ldflags="-X '${PKG_VERSION_PATH}/provider.Version=${NEXT_VERSION}-test'" -v -p 1 -parallel 20 -timeout 45m ./pkg/...

# To generate coverage.txt run `make acceptance` first
coverage:
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/binary_manager_repos.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ resource "xray_binary_manager_repos" "my-indexed-repos" {
Required:

- `name` (String) Name of the repository
- `package_type` (String) Artifactory package type. Valid value: Alpine, Bower, Cargo, Composer, Conan, Conda, Cran, Debian, Docker, Generic, Go, Gradle, Huggingface, Ivy, Maven, Npm, Nuget, Oci, Pypi, Rpm, Rubygems, Sbt, Terraformbe
- `package_type` (String) Artifactory package type. Valid value: Alpine Linux, Bower, Cargo, Composer, CocoaPods, Conan, Conda, CRAN, Debian, Docker, Gems, Generic, Go, Gradle, HuggingFaceML, Ivy, Maven, npm, NuGet, OCI, Pypi, RPM, SBT, TerraformBackend
- `type` (String) Repository type. Valid value: local, remote, federated


Expand Down
36 changes: 28 additions & 8 deletions pkg/xray/resource/resource_xray_binary_manager_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
utilfw "github.com/jfrog/terraform-provider-shared/util/fw"
validatorfw_string "github.com/jfrog/terraform-provider-shared/validator/fw/string"
"github.com/samber/lo"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)

const BinaryManagerReposEndpoint = "xray/api/v1/binMgr/{id}/repos"
Expand Down Expand Up @@ -145,10 +143,32 @@ type BinaryManagerRepoAPIModel struct {
PackageType string `json:"pkg_type"`
}

var validTitledPackageTypes = lo.Map(validPackageTypes, func(packageType string, _ int) string {
caser := cases.Title(language.AmericanEnglish, cases.NoLower)
return caser.String(packageType)
})
var validBinMgrPackageTypes = []string{
"Alpine Linux",
"Bower",
"Cargo",
"Composer",
"CocoaPods",
"Conan",
"Conda",
"CRAN",
"Debian",
"Docker",
"Gems",
"Generic",
"Go",
"Gradle",
"HuggingFaceML",
"Ivy",
"Maven",
"npm",
"NuGet",
"OCI",
"Pypi",
"RPM",
"SBT",
"TerraformBackend",
}

func (r *BinaryManagerReposResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Expand Down Expand Up @@ -190,9 +210,9 @@ func (r *BinaryManagerReposResource) Schema(ctx context.Context, req resource.Sc
"package_type": schema.StringAttribute{
Required: true,
Validators: []validator.String{
stringvalidator.OneOf(validTitledPackageTypes...),
stringvalidator.OneOf(validBinMgrPackageTypes...),
},
Description: fmt.Sprintf("Artifactory package type. Valid value: %s", strings.Join(validTitledPackageTypes, ", ")),
Description: fmt.Sprintf("Artifactory package type. Valid value: %s", strings.Join(validBinMgrPackageTypes, ", ")),
},
},
},
Expand Down
282 changes: 155 additions & 127 deletions pkg/xray/resource/resource_xray_binary_manager_repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package xray_test

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand All @@ -12,146 +13,173 @@ import (
)

func TestAccBinaryManagerRepos_full(t *testing.T) {
_, fqrn, resourceName := testutil.MkNames("test-bin-mgr-repos", "xray_binary_manager_repos")
_, _, repo1Name := testutil.MkNames("test-local-generic-repo", "artifactory_local_generic_repository")
_, _, repo2Name := testutil.MkNames("test-local-docker-repo", "artifactory_local_docker_v2_repository")
_, _, repo3Name := testutil.MkNames("test-local-npm-repo", "artifactory_local_npm_repository")

const template = `
resource "artifactory_local_generic_repository" "{{ .repo1 }}" {
key = "{{ .repo1 }}"
xray_index = true
lifecycle {
ignore_changes = ["project_key"]
}
}
resource "artifactory_local_docker_v2_repository" "{{ .repo2 }}" {
key = "{{ .repo2 }}"
xray_index = false
lifecycle {
ignore_changes = ["project_key"]
}
}
testCases := []struct {
packageType string
resourceType string
}{
{packageType: "Alpine Linux", resourceType: "artifactory_local_alpine_repository"},
{packageType: "Bower", resourceType: "artifactory_local_bower_repository"},
{packageType: "Cargo", resourceType: "artifactory_local_cargo_repository"},
{packageType: "Composer", resourceType: "artifactory_local_composer_repository"},
{packageType: "CocoaPods", resourceType: "artifactory_local_cocoapods_repository"},
{packageType: "Conan", resourceType: "artifactory_local_conan_repository"},
{packageType: "Conda", resourceType: "artifactory_local_conda_repository"},
{packageType: "CRAN", resourceType: "artifactory_local_cran_repository"},
{packageType: "Debian", resourceType: "artifactory_local_debian_repository"},
{packageType: "Docker", resourceType: "artifactory_local_docker_v2_repository"},
{packageType: "Gems", resourceType: "artifactory_local_gems_repository"},
{packageType: "Generic", resourceType: "artifactory_local_generic_repository"},
{packageType: "Go", resourceType: "artifactory_local_go_repository"},
{packageType: "Gradle", resourceType: "artifactory_local_gradle_repository"},
{packageType: "HuggingFaceML", resourceType: "artifactory_local_huggingfaceml_repository"},
{packageType: "Ivy", resourceType: "artifactory_local_ivy_repository"},
{packageType: "Maven", resourceType: "artifactory_local_maven_repository"},
{packageType: "npm", resourceType: "artifactory_local_npm_repository"},
{packageType: "NuGet", resourceType: "artifactory_local_nuget_repository"},
{packageType: "OCI", resourceType: "artifactory_local_oci_repository"},
{packageType: "Pypi", resourceType: "artifactory_local_pypi_repository"},
{packageType: "RPM", resourceType: "artifactory_local_rpm_repository"},
{packageType: "SBT", resourceType: "artifactory_local_sbt_repository"},
{packageType: "TerraformBackend", resourceType: "artifactory_local_terraformbackend_repository"},
}

resource "xray_binary_manager_repos" "{{ .name }}" {
id = "default"
indexed_repos = [
{
name = artifactory_local_generic_repository.{{ .repo1 }}.key
type = "local"
package_type = "Generic"
for _, testCase := range testCases {
t.Run(testCase.packageType, func(t *testing.T) {
_, fqrn, resourceName := testutil.MkNames("test-bin-mgr-repos", "xray_binary_manager_repos")
_, _, repo1Name := testutil.MkNames(fmt.Sprintf("test-%s-local", strings.ReplaceAll(strings.ToLower(testCase.packageType), " ", "")), testCase.resourceType)
_, _, repo2Name := testutil.MkNames("test-local-generic-repo", "artifactory_local_generic_repository")

const template = `
resource "{{ .resourceType }}" "{{ .repo1 }}" {
key = "{{ .repo1 }}"
xray_index = true
project_key = "default"
{{if eq .packageType "Debian"}}
index_compression_formats = ["bz2"]
{{end}}
lifecycle {
ignore_changes = ["project_key"]
}
}
]
}
`
testData := map[string]string{
"name": resourceName,
"repo1": repo1Name,
"repo2": repo2Name,
}
resource "xray_binary_manager_repos" "{{ .name }}" {
id = "default"
indexed_repos = [
{
name = {{ .resourceType }}.{{ .repo1 }}.key
type = "local"
package_type = "{{ .packageType }}"
}
]
}
`

config := util.ExecuteTemplate("TestAccBinaryManagerRepos_full", template, testData)
testData := map[string]string{
"name": resourceName,
"repo1": repo1Name,
"resourceType": testCase.resourceType,
"packageType": testCase.packageType,
}

const updateTemplate = `
resource "artifactory_local_generic_repository" "{{ .repo1 }}" {
key = "{{ .repo1 }}"
xray_index = true
config := util.ExecuteTemplate("TestAccBinaryManagerRepos_full", template, testData)

lifecycle {
ignore_changes = ["project_key"]
}
}
const updateTemplate = `
resource "{{ .resourceType }}" "{{ .repo1 }}" {
key = "{{ .repo1 }}"
xray_index = true
project_key = "default"
{{if eq .packageType "Debian"}}
index_compression_formats = ["bz2"]
{{end}}
resource "artifactory_local_docker_v2_repository" "{{ .repo2 }}" {
key = "{{ .repo2 }}"
xray_index = true
lifecycle {
ignore_changes = ["project_key"]
}
}
lifecycle {
ignore_changes = ["project_key"]
}
}
resource "artifactory_local_generic_repository" "{{ .repo2 }}" {
key = "{{ .repo2 }}"
xray_index = true
project_key = "default"
resource "artifactory_local_npm_repository" "{{ .repo3 }}" {
key = "{{ .repo3 }}"
lifecycle {
ignore_changes = ["project_key"]
}
}
lifecycle {
ignore_changes = ["project_key"]
resource "xray_binary_manager_repos" "{{ .name }}" {
id = "default"
indexed_repos = [
{
name = {{ .resourceType }}.{{ .repo1 }}.key
type = "local"
package_type = "{{ .packageType }}"
},
{
name = artifactory_local_generic_repository.{{ .repo2 }}.key
type = "local"
package_type = "Generic"
}
]
}
`
updatedTestData := map[string]string{
"name": resourceName,
"repo1": repo1Name,
"repo2": repo2Name,
"resourceType": testCase.resourceType,
"packageType": testCase.packageType,
}
}
resource "xray_binary_manager_repos" "{{ .name }}" {
id = "default"
indexed_repos = [
{
name = artifactory_local_generic_repository.{{ .repo1 }}.key
type = "local"
package_type = "Generic"
updatedConfig := util.ExecuteTemplate("TestAccBinaryManagerRepos_full", updateTemplate, updatedTestData)

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
ExternalProviders: map[string]resource.ExternalProvider{
"artifactory": {
Source: "jfrog/artifactory",
},
},
{
name = artifactory_local_docker_v2_repository.{{ .repo2 }}.key
type = "local"
package_type = "Docker"
}
]
}
`
updatedTestData := map[string]string{
"name": resourceName,
"repo1": repo1Name,
"repo2": repo2Name,
"repo3": repo3Name,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "id", "default"),
resource.TestCheckResourceAttr(fqrn, "indexed_repos.#", "1"),
resource.TestCheckResourceAttr(fqrn, "indexed_repos.0.name", repo1Name),
resource.TestCheckResourceAttr(fqrn, "indexed_repos.0.type", "local"),
resource.TestCheckResourceAttr(fqrn, "indexed_repos.0.package_type", testCase.packageType),
),
},
{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "id", "default"),
resource.TestCheckResourceAttr(fqrn, "indexed_repos.#", "2"),
resource.TestCheckTypeSetElemNestedAttrs(fqrn, "indexed_repos.*", map[string]string{
"name": repo1Name,
"type": "local",
"package_type": testCase.packageType,
}),
resource.TestCheckTypeSetElemNestedAttrs(fqrn, "indexed_repos.*", map[string]string{
"name": repo2Name,
"type": "local",
"package_type": "Generic",
}),
),
ConfigPlanChecks: testutil.ConfigPlanChecks("xray_binary_manager_repos"),
},
{
ResourceName: fqrn,
ImportState: true,
ImportStateId: resourceName,
ImportStateVerify: true,
ImportStateVerifyIdentifierAttribute: "id",
},
},
})
})
}
updatedConfig := util.ExecuteTemplate("TestAccBinaryManagerRepos_full", updateTemplate, updatedTestData)

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
ExternalProviders: map[string]resource.ExternalProvider{
"artifactory": {
Source: "jfrog/artifactory",
},
},
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "id", "default"),
resource.TestCheckResourceAttr(fqrn, "indexed_repos.#", "1"),
resource.TestCheckResourceAttr(fqrn, "indexed_repos.0.name", repo1Name),
resource.TestCheckResourceAttr(fqrn, "indexed_repos.0.type", "local"),
resource.TestCheckResourceAttr(fqrn, "indexed_repos.0.package_type", "Generic"),
),
},
{
Config: updatedConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fqrn, "id", "default"),
resource.TestCheckResourceAttr(fqrn, "indexed_repos.#", "2"),
resource.TestCheckTypeSetElemNestedAttrs(fqrn, "indexed_repos.*", map[string]string{
"name": repo1Name,
"type": "local",
"package_type": "Generic",
}),
resource.TestCheckTypeSetElemNestedAttrs(fqrn, "indexed_repos.*", map[string]string{
"name": repo2Name,
"type": "local",
"package_type": "Docker",
}),
),
ConfigPlanChecks: testutil.ConfigPlanChecks("xray_binary_manager_repos"),
},
{
ResourceName: fqrn,
ImportState: true,
ImportStateId: resourceName,
ImportStateVerify: true,
ImportStateVerifyIdentifierAttribute: "id",
},
},
})
}

func TestAccBinaryManagerRepos_project_full(t *testing.T) {
Expand Down

0 comments on commit 71abaf6

Please sign in to comment.