Skip to content

Commit

Permalink
chore: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
levkohimins committed Feb 26, 2025
1 parent 64c13d0 commit 9f2d735
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 9 deletions.
8 changes: 8 additions & 0 deletions test/fixtures/provider-cache/network-mirror/apps/app1/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
terraform {
required_providers {
aws = {
source = "example.com/hashicorp/aws"
version = "5.58.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally empty
28 changes: 20 additions & 8 deletions test/integration_serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestTerragruntProviderCacheWithNetworkMirror(t *testing.T) {
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureProviderCacheNetworkMirror)
rootPath := util.JoinPath(tmpEnvPath, testFixtureProviderCacheNetworkMirror)

appPath := filepath.Join(rootPath, "app")
appsPath := filepath.Join(rootPath, "apps")
providersNetkworMirrorPath := filepath.Join(rootPath, "providers-network-mirror")
providersFilesystemMirrorPath := filepath.Join(rootPath, "providers-filesystem-mirror")

Expand All @@ -122,6 +122,16 @@ func TestTerragruntProviderCacheWithNetworkMirror(t *testing.T) {
netowrkProvider.CreateMirror(t, providersNetkworMirrorPath)

filesystemProvider := FakeProvider{
RegistryName: "example.com",
Namespace: "hashicorp",
Name: "aws",
Version: "5.58.0",
PlatformOS: runtime.GOOS,
PlatformArch: runtime.GOARCH,
}
filesystemProvider.CreateMirror(t, providersFilesystemMirrorPath)

filesystemProvider = FakeProvider{
RegistryName: "example.com",
Namespace: "hashicorp",
Name: "azurerm",
Expand Down Expand Up @@ -169,7 +179,7 @@ func TestTerragruntProviderCacheWithNetworkMirror(t *testing.T) {
FilesystemMirrorMethods: []test.CLIConfigProviderInstallationFilesystemMirror{
{
Path: providersFilesystemMirrorPath,
Include: []string{"example.com/hashicorp/azurerm"},
Include: []string{"example.com/hashicorp/azurerm", "example.com/hashicorp/aws"},
},
},
NetworkMirrorMethods: []test.CLIConfigProviderInstallationNetworkMirror{
Expand All @@ -181,16 +191,18 @@ func TestTerragruntProviderCacheWithNetworkMirror(t *testing.T) {
}
test.CreateCLIConfig(t, cliConfigFilename, cliConfigSettings)

helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt run-all init --terragrunt-provider-cache --terragrunt-provider-cache-registry-names example.com --terragrunt-provider-cache-registry-names registry.opentofu.org --terragrunt-provider-cache-registry-names registry.terraform.io --terragrunt-provider-cache-dir %s --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir %s", providerCacheDir, appPath))
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt run-all init --terragrunt-provider-cache --terragrunt-provider-cache-registry-names example.com --terragrunt-provider-cache-registry-names registry.opentofu.org --terragrunt-provider-cache-registry-names registry.terraform.io --terragrunt-provider-cache-dir %s --terragrunt-log-level trace --terragrunt-non-interactive --terragrunt-working-dir %s", providerCacheDir, appsPath))

expectedProviderInstallation := `provider_installation { "filesystem_mirror" { path = "%s" include = ["example.com/hashicorp/azurerm"] exclude = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "network_mirror" { url = "%s" exclude = ["example.com/hashicorp/azurerm", "example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "filesystem_mirror" { path = "%s" include = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "direct" { exclude = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } }`
expectedProviderInstallation := `provider_installation { "filesystem_mirror" { path = "%s" include = ["example.com/hashicorp/azurerm", "example.com/hashicorp/aws"] exclude = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "network_mirror" { url = "%s" exclude = ["example.com/hashicorp/azurerm", "example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "filesystem_mirror" { path = "%s" include = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } "direct" { exclude = ["example.com/*/*", "registry.opentofu.org/*/*", "registry.terraform.io/*/*"] } }`
expectedProviderInstallation = fmt.Sprintf(strings.Join(strings.Fields(expectedProviderInstallation), " "), providersFilesystemMirrorPath, networkMirrorURL.String(), providerCacheDir)

terraformrcBytes, err := os.ReadFile(filepath.Join(appPath, ".terraformrc"))
require.NoError(t, err)
terraformrc := strings.Join(strings.Fields(string(terraformrcBytes)), " ")
for _, filename := range []string{"app0/.terraformrc", "app1/.terraformrc"} {
terraformrcBytes, err := os.ReadFile(filepath.Join(appsPath, filename))
require.NoError(t, err)
terraformrc := strings.Join(strings.Fields(string(terraformrcBytes)), " ")

assert.Contains(t, terraformrc, expectedProviderInstallation, "%s\n\n%s", terraformrc, expectedProviderInstallation)
assert.Contains(t, terraformrc, expectedProviderInstallation, "%s\n\n%s", terraformrc, expectedProviderInstallation)
}
}

func TestTerragruntInputsFromDependency(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion tf/cliconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (cfg *Config) Clone() *Config {

if cfg.ProviderInstallation != nil {
providerInstallation = &ProviderInstallation{
Methods: cfg.ProviderInstallation.Methods,
Methods: cfg.ProviderInstallation.Methods.Clone(),
}
}

Expand Down
67 changes: 67 additions & 0 deletions tf/cliconfig/provider_installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ func (methods ProviderInstallationMethods) Merge(withMethods ...ProviderInstalla
return mergedMethods
}

func (methods ProviderInstallationMethods) Clone() ProviderInstallationMethods {
var cloned = make(ProviderInstallationMethods, len(methods))

for i, method := range methods {
cloned[i] = method.Clone()
}

return cloned
}

// ProviderInstallationMethod is an interface type representing the different installation path types and represents an installation method block inside a provider_installation block. The concrete implementations of this interface are:
//
// ProviderInstallationDirect: install from the provider's origin registry
Expand All @@ -56,6 +66,7 @@ type ProviderInstallationMethod interface {
RemoveInclude(addrs []string)
RemoveExclude(addrs []string)
Merge(with ProviderInstallationMethod) bool
Clone() ProviderInstallationMethod
}

type ProviderInstallationDirect struct {
Expand All @@ -80,6 +91,24 @@ func NewProviderInstallationDirect(include, exclude []string) *ProviderInstallat
return res
}

func (method *ProviderInstallationDirect) Clone() ProviderInstallationMethod {
cloned := &ProviderInstallationDirect{
Name: method.Name,
}

if method.Include != nil {
include := *method.Include
cloned.Include = &include
}

if method.Exclude != nil {
exclude := *method.Exclude
cloned.Exclude = &exclude
}

return cloned
}

func (method *ProviderInstallationDirect) Merge(with ProviderInstallationMethod) bool {
if with, ok := with.(*ProviderInstallationDirect); ok {
if with.Exclude != nil {
Expand Down Expand Up @@ -174,6 +203,25 @@ func NewProviderInstallationFilesystemMirror(path string, include, exclude []str
return res
}

func (method *ProviderInstallationFilesystemMirror) Clone() ProviderInstallationMethod {
cloned := &ProviderInstallationFilesystemMirror{
Name: method.Name,
Path: method.Path,
}

if method.Include != nil {
include := *method.Include
cloned.Include = &include
}

if method.Exclude != nil {
exclude := *method.Exclude
cloned.Exclude = &exclude
}

return cloned
}

func (method *ProviderInstallationFilesystemMirror) Merge(with ProviderInstallationMethod) bool {
if with, ok := with.(*ProviderInstallationFilesystemMirror); ok && method.Path == with.Path {
if with.Exclude != nil {
Expand Down Expand Up @@ -268,6 +316,25 @@ func NewProviderInstallationNetworkMirror(url string, include, exclude []string)
return res
}

func (method *ProviderInstallationNetworkMirror) Clone() ProviderInstallationMethod {
cloned := &ProviderInstallationNetworkMirror{
Name: method.Name,
URL: method.URL,
}

if method.Include != nil {
include := *method.Include
cloned.Include = &include
}

if method.Exclude != nil {
exclude := *method.Exclude
cloned.Exclude = &exclude
}

return cloned
}

func (method *ProviderInstallationNetworkMirror) Merge(with ProviderInstallationMethod) bool {
if with, ok := with.(*ProviderInstallationNetworkMirror); ok && method.URL == with.URL {
if with.Exclude != nil {
Expand Down

0 comments on commit 9f2d735

Please sign in to comment.