forked from Checkmarx/kics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28e75b8
commit b29f0f3
Showing
4 changed files
with
187 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//go:build !dev | ||
// +build !dev | ||
|
||
package terraformer | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestImport(t *testing.T) { | ||
type args struct { | ||
terraformerPath string | ||
} | ||
tests := []struct { | ||
name string | ||
runTerraformer func(pathOptions *Path, destination string) (string, error) | ||
args args | ||
want string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test import aws", | ||
runTerraformer: func(pathOptions *Path, destination string) (string, error) { | ||
return "", nil | ||
}, | ||
args: args{ | ||
terraformerPath: "aws:subnet:eu-west-2", | ||
}, | ||
want: "kics-extract-terraformer", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "test import gcp", | ||
runTerraformer: func(pathOptions *Path, destination string) (string, error) { | ||
return "", nil | ||
}, | ||
args: args{ | ||
terraformerPath: "gcp:instances:us-west4:cosmic-1234", | ||
}, | ||
want: "kics-extract-terraformer", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "test import azure", | ||
runTerraformer: func(pathOptions *Path, destination string) (string, error) { | ||
return "", nil | ||
}, | ||
args: args{ | ||
terraformerPath: "azure:storage_account", | ||
}, | ||
want: "kics-extract-terraformer", | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "test import path error", | ||
runTerraformer: func(pathOptions *Path, destination string) (string, error) { | ||
return "", nil | ||
}, | ||
args: args{ | ||
terraformerPath: "aws", | ||
}, | ||
want: ".", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
runTerraformerFunc = tt.runTerraformer | ||
saveTerraformerOutputFunc = func(destination, output string) error { return nil } | ||
got, err := Import(tt.args.terraformerPath, "") | ||
os.RemoveAll(got) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Import() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !strings.Contains(filepath.Base(got), filepath.Base(tt.want)) { | ||
t.Errorf("Import() = %v, want %v", filepath.Base(got), filepath.Base(tt.want)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//go:build !dev | ||
// +build !dev | ||
|
||
package terraformer | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
// save saves the terraformer command output in the destination folder | ||
func save(destination, output string, statErr error) { | ||
if statErr != nil { | ||
if err := os.MkdirAll(destination, os.ModePerm); err != nil { | ||
log.Error().Msgf("failed to mkdir: %s", err) | ||
} | ||
} | ||
|
||
filePath := filepath.Join(destination, "terraformer-output.txt") | ||
filepath.Clean(filePath) | ||
|
||
f, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm) | ||
|
||
if err != nil { | ||
log.Error().Msgf("failed to open file '%s': %s", filePath, err) | ||
} | ||
|
||
defer func(f *os.File) { | ||
err = f.Close() | ||
if err != nil { | ||
log.Err(err).Msgf("failed to close file: %s", filePath) | ||
} | ||
}(f) | ||
|
||
if _, err = f.WriteString(output); err != nil { | ||
log.Error().Msgf("failed to write file '%s': %s", filePath, err) | ||
} | ||
} | ||
|
||
// cleanUnwantedFiles deletes the output files from the destination folder | ||
func cleanUnwantedFiles(destination string) { | ||
err := filepath.Walk(destination, func(path string, info fs.FileInfo, err error) error { | ||
if info != nil { | ||
deleteOutputFile(path, info) | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
log.Err(err).Msg("failed to clean unwanted files") | ||
} | ||
} | ||
|
||
// deleteOutputFile deletes a 'outputs.tf' file | ||
func deleteOutputFile(path string, output fs.FileInfo) { | ||
if output.Name() == "outputs.tf" { | ||
err := os.Remove(path) | ||
if err != nil { | ||
log.Err(err).Msgf("failed to remove outputs.tf in path %s", path) | ||
} | ||
} | ||
} |