Skip to content

Commit

Permalink
refactor DownloadRemoteModule func and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patilpankaj212 committed Jan 21, 2021
1 parent f784302 commit 19128c9
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 34 deletions.
71 changes: 42 additions & 29 deletions pkg/iac-providers/terraform/commons/remote-module-download.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
hclConfigs "github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/registry"
"github.com/hashicorp/terraform/registry/regsrc"
"github.com/hashicorp/terraform/registry/response"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -53,6 +54,44 @@ func (r *RemoteModuleInstaller) DownloadRemoteModule(requiredVersion hclConfigs.
return "", err
}

// get the version to download
versionToDownload, err := getVersionToDownload(moduleVersions, requiredVersion, module)
if err != nil {
zap.S().Error("error while fetching the version to download,", zap.Error(err))
return "", err
}

// get the source location for the matched version
sourceLocation, err := regClient.ModuleLocation(module, versionToDownload.String())
if err != nil {
zap.S().Errorf("error while getting the source location for module: %s, at registry: %s", module.String(), module.Host().Display())
return "", err
}

downloadLocation, err := r.DownloadModule(sourceLocation, destPath)
if err != nil {
zap.S().Errorf("error while downloading module: %s, with source location: %s", module.String(), sourceLocation)
return "", nil
}

if module.RawSubmodule != "" {
// Append the user's requested subdirectory
downloadLocation = filepath.Join(downloadLocation, module.RawSubmodule)
}

return downloadLocation, nil
}

// helper func to compare and update the version
func getGreaterVersion(latestVersion *version.Version, currentVersion *version.Version) *version.Version {
if latestVersion == nil || currentVersion.GreaterThan(latestVersion) {
latestVersion = currentVersion
}
return latestVersion
}

// helper func to get the module version to download
func getVersionToDownload(moduleVersions *response.ModuleVersions, requiredVersion hclConfigs.VersionConstraint, module *regsrc.Module) (*version.Version, error) {
// terraform init command pulls all the available versions of a module,
// and downloads the latest non pre-release (unless a pre-release version was
// specified in tf file) version, if a version constraint is not provided in the tf file.
Expand Down Expand Up @@ -95,43 +134,17 @@ func (r *RemoteModuleInstaller) DownloadRemoteModule(requiredVersion hclConfigs.
}

if latestVersion == nil {
return "", fmt.Errorf("no versions for module: %s, found at registry: %s", module.String(), module.Host().Display())
return nil, fmt.Errorf("no versions for module: %s, found at registry: %s", module.String(), module.Host().Display())
}

if requiredVersion.Required != nil && latestMatch == nil {
return "", fmt.Errorf("no versions matching: %s, for module: %s, found at registry: %s, latest version found: %s", requiredVersion.Required.String(), module.String(), module.Host().Display(), latestVersion.String())
return nil, fmt.Errorf("no versions matching: %s, for module: %s, found at registry: %s, latest version found: %s", requiredVersion.Required.String(), module.String(), module.Host().Display(), latestVersion.String())
}

versionToDownload = latestVersion
if latestMatch != nil {
versionToDownload = latestMatch
}

// get the source location for the matched version
sourceLocation, err := regClient.ModuleLocation(module, versionToDownload.String())
if err != nil {
zap.S().Errorf("error while getting the source location for module: %s, at registry: %s", module.String(), module.Host().Display())
return "", err
}

downloadLocation, err := r.DownloadModule(sourceLocation, destPath)
if err != nil {
zap.S().Errorf("error while downloading module: %s, with source location: %s", module.String(), sourceLocation)
return "", nil
}

if module.RawSubmodule != "" {
// Append the user's requested subdirectory to any subdirectory that
// was implied by any of the nested layers we expanded within go-getter.
downloadLocation = filepath.Join(downloadLocation, module.RawSubmodule)
}

return downloadLocation, nil
}

func getGreaterVersion(latestVersion *version.Version, currentVersion *version.Version) *version.Version {
if latestVersion == nil || currentVersion.GreaterThan(latestVersion) {
latestVersion = currentVersion
}
return latestVersion
return versionToDownload, nil
}
148 changes: 143 additions & 5 deletions pkg/iac-providers/terraform/commons/remote-module-download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ import (
"log"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/accurics/terrascan/pkg/downloader"
"github.com/accurics/terrascan/pkg/utils"
version "github.com/hashicorp/go-version"
hclConfigs "github.com/hashicorp/terraform/configs"
"github.com/hashicorp/terraform/registry/regsrc"
"github.com/hashicorp/terraform/registry/response"
)

func TestRemoteModuleInstallerDownloadRemoteModule(t *testing.T) {
Expand All @@ -40,6 +42,8 @@ func TestRemoteModuleInstallerDownloadRemoteModule(t *testing.T) {
testConstraintsSecurityGroup, _ := version.NewConstraint("3.17.0")
testModuleSecurityGroup, _ := regsrc.ParseModuleSource("terraform-aws-modules/security-group/aws")
testModuleInvalidProvider, _ := regsrc.ParseModuleSource("terraform-aws-modules/testgroup/testprovider")
testModuleRdsWithRawSubModule, _ := regsrc.ParseModuleSource("terraform-aws-modules/security-group/aws//db_subnet_group")
testRawSubModuleName := "db_subnet_group"

type fields struct {
cache InstalledCache
Expand All @@ -50,11 +54,13 @@ func TestRemoteModuleInstallerDownloadRemoteModule(t *testing.T) {
module *regsrc.Module
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
name string
fields fields
args args
want string
wantErr bool
hasRawSubModule bool
rawSubModule string
}{
{
name: "remote module download with valid module and version",
Expand Down Expand Up @@ -92,6 +98,19 @@ func TestRemoteModuleInstallerDownloadRemoteModule(t *testing.T) {
},
wantErr: true,
},
{
name: "remote module download with raw sub module",
fields: fields{
cache: make(map[string]string),
downloader: downloader.NewDownloader(),
},
args: args{
requiredVersion: hclConfigs.VersionConstraint{},
module: testModuleRdsWithRawSubModule,
},
hasRawSubModule: true,
rawSubModule: testRawSubModuleName,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -105,6 +124,10 @@ func TestRemoteModuleInstallerDownloadRemoteModule(t *testing.T) {
tt.want = ""
}

if tt.hasRawSubModule {
tt.want = tt.want + string(os.PathSeparator) + tt.rawSubModule
}

defer os.RemoveAll(testDir)
got, err := r.DownloadRemoteModule(tt.args.requiredVersion, testDir, tt.args.module)
if (err != nil) != tt.wantErr {
Expand All @@ -117,3 +140,118 @@ func TestRemoteModuleInstallerDownloadRemoteModule(t *testing.T) {
})
}
}

func TestGetVersionToDownload(t *testing.T) {
source := "terraform-aws-modules/security-group/aws"
testModule, _ := regsrc.ParseModuleSource(source)
testVersionConstraint, _ := version.NewConstraint("3.17.0")
testVersionConstraintMatching, _ := version.NewConstraint("1.2.0")
testVersion, _ := version.NewVersion("1.2.0")
testVersions := []*response.ModuleVersion{
{
Version: "1.0.0",
},
{
Version: "1.1.0",
},
{
Version: "1.2.0",
},
}

type args struct {
moduleVersions *response.ModuleVersions
requiredVersion hclConfigs.VersionConstraint
module *regsrc.Module
}
tests := []struct {
name string
args args
want *version.Version
wantErr bool
}{
{
name: "invalid version",
args: args{
moduleVersions: &response.ModuleVersions{
Modules: []*response.ModuleProviderVersions{
{
Source: "",
Versions: []*response.ModuleVersion{
{},
},
},
},
},
module: testModule,
requiredVersion: hclConfigs.VersionConstraint{
Required: testVersionConstraint,
},
},
wantErr: true,
},
{
name: "no matching versions",
args: args{
moduleVersions: &response.ModuleVersions{
Modules: []*response.ModuleProviderVersions{
{
Source: "source",
Versions: testVersions,
},
},
},
module: testModule,
requiredVersion: hclConfigs.VersionConstraint{
Required: testVersionConstraint,
},
},
wantErr: true,
},
{
name: "no required version specified",
args: args{
moduleVersions: &response.ModuleVersions{
Modules: []*response.ModuleProviderVersions{
{
Source: "source",
Versions: testVersions,
},
},
},
module: testModule,
},
want: testVersion,
},
{
name: "required version specified",
args: args{
moduleVersions: &response.ModuleVersions{
Modules: []*response.ModuleProviderVersions{
{
Source: "source",
Versions: testVersions,
},
},
},
module: testModule,
requiredVersion: hclConfigs.VersionConstraint{
Required: testVersionConstraintMatching,
},
},
want: testVersion,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getVersionToDownload(tt.args.moduleVersions, tt.args.requiredVersion, tt.args.module)
if (err != nil) != tt.wantErr {
t.Errorf("getVersionToDownload() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getVersionToDownload() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 19128c9

Please sign in to comment.