2020-12-09 15:31:34 +00:00
|
|
|
package supplier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/iac/config"
|
2021-03-16 15:29:32 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/iac/terraform/state/backend"
|
2021-05-03 16:41:52 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/output"
|
2021-01-22 17:06:17 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
2021-03-26 08:44:55 +00:00
|
|
|
"github.com/cloudskiff/driftctl/test/resource"
|
2020-12-09 15:31:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetIACSupplier(t *testing.T) {
|
|
|
|
type args struct {
|
2021-03-16 15:29:32 +00:00
|
|
|
config []config.SupplierConfig
|
|
|
|
options *backend.Options
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
wantErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test unknown supplier",
|
|
|
|
args: args{
|
2021-01-15 11:44:13 +00:00
|
|
|
config: []config.SupplierConfig{
|
|
|
|
{
|
|
|
|
Key: "foobar",
|
|
|
|
},
|
|
|
|
},
|
2021-03-16 15:29:32 +00:00
|
|
|
options: &backend.Options{
|
|
|
|
Headers: map[string]string{},
|
|
|
|
},
|
2021-01-15 11:44:13 +00:00
|
|
|
},
|
|
|
|
wantErr: fmt.Errorf("Unsupported supplier 'foobar'"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test unknown supplier in multiples states",
|
|
|
|
args: args{
|
|
|
|
config: []config.SupplierConfig{
|
|
|
|
{
|
|
|
|
Key: "foobar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "tfstate",
|
|
|
|
Backend: "",
|
|
|
|
Path: "terraform.tfstate",
|
|
|
|
},
|
|
|
|
},
|
2021-03-16 15:29:32 +00:00
|
|
|
options: &backend.Options{
|
|
|
|
Headers: map[string]string{},
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
wantErr: fmt.Errorf("Unsupported supplier 'foobar'"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test valid tfstate://terraform.tfstate",
|
|
|
|
args: args{
|
2021-01-15 11:44:13 +00:00
|
|
|
config: []config.SupplierConfig{
|
|
|
|
{Key: "tfstate", Backend: "", Path: "terraform.tfstate"},
|
|
|
|
},
|
2021-03-16 15:29:32 +00:00
|
|
|
options: &backend.Options{
|
|
|
|
Headers: map[string]string{},
|
|
|
|
},
|
2021-01-15 11:44:13 +00:00
|
|
|
},
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test valid multiples states",
|
|
|
|
args: args{
|
|
|
|
config: []config.SupplierConfig{
|
|
|
|
{Key: "tfstate", Backend: "", Path: "terraform.tfstate"},
|
|
|
|
{Key: "tfstate", Backend: "s3", Path: "terraform.tfstate"},
|
|
|
|
{Key: "tfstate", Backend: "", Path: "terraform2.tfstate"},
|
|
|
|
},
|
2021-03-16 15:29:32 +00:00
|
|
|
options: &backend.Options{
|
|
|
|
Headers: map[string]string{},
|
|
|
|
},
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
wantErr: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-05-03 16:41:52 +00:00
|
|
|
progress := &output.MockProgress{}
|
|
|
|
progress.On("Start").Return().Times(1)
|
|
|
|
|
2021-03-26 08:44:55 +00:00
|
|
|
repo := resource.InitFakeSchemaRepository("aws", "3.19.0")
|
2021-05-03 16:41:52 +00:00
|
|
|
|
|
|
|
_, err := GetIACSupplier(tt.args.config, terraform.NewProviderLibrary(), tt.args.options, progress, repo)
|
2020-12-09 15:31:34 +00:00
|
|
|
if tt.wantErr != nil && err.Error() != tt.wantErr.Error() {
|
|
|
|
t.Errorf("GetIACSupplier() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetSupportedSchemes(t *testing.T) {
|
|
|
|
|
|
|
|
want := []string{
|
|
|
|
"tfstate://",
|
|
|
|
"tfstate+s3://",
|
2021-03-16 15:35:28 +00:00
|
|
|
"tfstate+http://",
|
|
|
|
"tfstate+https://",
|
2021-04-24 21:31:57 +00:00
|
|
|
"tfstate+tfcloud://",
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if got := GetSupportedSchemes(); !reflect.DeepEqual(got, want) {
|
|
|
|
t.Errorf("GetSupportedSchemes() = %v, want %v", got, want)
|
|
|
|
}
|
|
|
|
}
|