driftctl/pkg/terraform/provider_installer_test.go

183 lines
4.3 KiB
Go
Raw Normal View History

package terraform
import (
"fmt"
"os"
"path"
2021-01-05 13:38:42 +00:00
"runtime"
"testing"
"github.com/cloudskiff/driftctl/mocks"
"github.com/stretchr/testify/assert"
)
func TestProviderInstallerInstallDoesNotExist(t *testing.T) {
assert := assert.New(t)
fakeTmpHome := t.TempDir()
2021-01-05 13:38:42 +00:00
expectedSubFolder := fmt.Sprintf("/.driftctl/plugins/%s_%s", runtime.GOOS, runtime.GOARCH)
config := ProviderConfig{
Key: "aws",
Version: "3.19.0",
}
mockDownloader := mocks.ProviderDownloaderInterface{}
mockDownloader.On("Download", config.GetDownloadUrl(), path.Join(fakeTmpHome, expectedSubFolder)).Return(nil)
installer := ProviderInstaller{
downloader: &mockDownloader,
config: config,
homeDir: fakeTmpHome,
}
providerPath, err := installer.Install()
mockDownloader.AssertExpectations(t)
assert.Nil(err)
assert.Equal(path.Join(fakeTmpHome, expectedSubFolder, config.GetBinaryName()), providerPath)
}
func TestProviderInstallerInstallWithoutHomeDir(t *testing.T) {
assert := assert.New(t)
expectedHomeDir := os.TempDir()
2021-01-05 13:38:42 +00:00
expectedSubFolder := fmt.Sprintf("/.driftctl/plugins/%s_%s", runtime.GOOS, runtime.GOARCH)
config := ProviderConfig{
Key: "aws",
Version: "3.19.0",
}
mockDownloader := mocks.ProviderDownloaderInterface{}
mockDownloader.On("Download", config.GetDownloadUrl(), path.Join(expectedHomeDir, expectedSubFolder)).Return(nil)
installer := ProviderInstaller{
config: config,
downloader: &mockDownloader,
}
providerPath, err := installer.Install()
mockDownloader.AssertExpectations(t)
assert.Nil(err)
assert.Equal(path.Join(expectedHomeDir, expectedSubFolder, config.GetBinaryName()), providerPath)
}
func TestProviderInstallerInstallAlreadyExist(t *testing.T) {
assert := assert.New(t)
fakeTmpHome := t.TempDir()
2021-01-05 13:38:42 +00:00
expectedSubFolder := fmt.Sprintf("/.driftctl/plugins/%s_%s", runtime.GOOS, runtime.GOARCH)
err := os.MkdirAll(path.Join(fakeTmpHome, expectedSubFolder), 0755)
if err != nil {
t.Error(err)
}
config := ProviderConfig{
Key: "aws",
Version: "3.19.0",
}
_, err = os.Create(path.Join(fakeTmpHome, expectedSubFolder, config.GetBinaryName()))
if err != nil {
t.Error(err)
}
mockDownloader := mocks.ProviderDownloaderInterface{}
installer := ProviderInstaller{
downloader: &mockDownloader,
config: config,
homeDir: fakeTmpHome,
}
providerPath, err := installer.Install()
mockDownloader.AssertExpectations(t)
assert.Nil(err)
assert.Equal(path.Join(fakeTmpHome, expectedSubFolder, config.GetBinaryName()), providerPath)
}
func TestProviderInstallerInstallAlreadyExistButIsDirectory(t *testing.T) {
assert := assert.New(t)
fakeTmpHome := t.TempDir()
2021-01-05 13:38:42 +00:00
expectedSubFolder := fmt.Sprintf("/.driftctl/plugins/%s_%s", runtime.GOOS, runtime.GOARCH)
config := ProviderConfig{
Key: "aws",
Version: "3.19.0",
}
invalidDirPath := path.Join(fakeTmpHome, expectedSubFolder, config.GetBinaryName())
err := os.MkdirAll(invalidDirPath, 0755)
if err != nil {
t.Error(err)
}
mockDownloader := mocks.ProviderDownloaderInterface{}
installer := ProviderInstaller{
downloader: &mockDownloader,
config: config,
homeDir: fakeTmpHome,
}
providerPath, err := installer.Install()
mockDownloader.AssertExpectations(t)
assert.Empty(providerPath)
assert.NotNil(err)
assert.Equal(
fmt.Sprintf(
"found directory instead of provider binary in %s",
invalidDirPath,
),
err.Error(),
)
}
// Ensure that if a provider exists with a postfix (_x5) we properly detect it
func TestProviderInstallerInstallPostfixIsHandler(t *testing.T) {
assert := assert.New(t)
fakeTmpHome := t.TempDir()
expectedSubFolder := fmt.Sprintf("/.driftctl/plugins/%s_%s", runtime.GOOS, runtime.GOARCH)
err := os.MkdirAll(path.Join(fakeTmpHome, expectedSubFolder), 0755)
if err != nil {
t.Error(err)
}
config := ProviderConfig{
Key: "aws",
Version: "3.19.0",
}
_, err = os.Create(path.Join(fakeTmpHome, expectedSubFolder, config.GetBinaryName()+"_x5"))
if err != nil {
t.Fatal(err)
}
mockDownloader := mocks.ProviderDownloaderInterface{}
installer := ProviderInstaller{
downloader: &mockDownloader,
config: config,
homeDir: fakeTmpHome,
}
providerPath, err := installer.Install()
mockDownloader.AssertExpectations(t)
assert.Nil(err)
assert.Equal(path.Join(fakeTmpHome, expectedSubFolder, config.GetBinaryName()+"_x5"), providerPath)
}