2020-12-09 15:31:34 +00:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
2021-01-05 13:38:42 +00:00
|
|
|
"runtime"
|
2020-12-09 15:31:34 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/mocks"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-02-08 15:42:35 +00:00
|
|
|
func TestProviderInstallerInstallDoesNotExist(t *testing.T) {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
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)
|
2021-02-08 15:42:35 +00:00
|
|
|
|
|
|
|
config := ProviderConfig{
|
|
|
|
Key: "aws",
|
|
|
|
Version: "3.19.0",
|
|
|
|
Postfix: "x5",
|
|
|
|
}
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
mockDownloader := mocks.ProviderDownloaderInterface{}
|
2021-02-08 15:42:35 +00:00
|
|
|
mockDownloader.On("Download", config.GetDownloadUrl(), path.Join(fakeTmpHome, expectedSubFolder)).Return(nil)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
installer := ProviderInstaller{
|
|
|
|
downloader: &mockDownloader,
|
2021-02-08 15:42:35 +00:00
|
|
|
config: config,
|
2020-12-09 15:31:34 +00:00
|
|
|
homeDir: fakeTmpHome,
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:42:35 +00:00
|
|
|
providerPath, err := installer.Install()
|
2020-12-09 15:31:34 +00:00
|
|
|
mockDownloader.AssertExpectations(t)
|
|
|
|
|
|
|
|
assert.Nil(err)
|
2021-02-08 15:42:35 +00:00
|
|
|
assert.Equal(path.Join(fakeTmpHome, expectedSubFolder, config.GetBinaryName()), providerPath)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:42:35 +00:00
|
|
|
func TestProviderInstallerInstallWithoutHomeDir(t *testing.T) {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
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)
|
2021-02-08 15:42:35 +00:00
|
|
|
config := ProviderConfig{
|
|
|
|
Key: "aws",
|
|
|
|
Version: "3.19.0",
|
|
|
|
Postfix: "x5",
|
|
|
|
}
|
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
mockDownloader := mocks.ProviderDownloaderInterface{}
|
2021-02-08 15:42:35 +00:00
|
|
|
mockDownloader.On("Download", config.GetDownloadUrl(), path.Join(expectedHomeDir, expectedSubFolder)).Return(nil)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
installer := ProviderInstaller{
|
2021-02-08 15:42:35 +00:00
|
|
|
config: config,
|
2020-12-09 15:31:34 +00:00
|
|
|
downloader: &mockDownloader,
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:42:35 +00:00
|
|
|
providerPath, err := installer.Install()
|
2020-12-09 15:31:34 +00:00
|
|
|
mockDownloader.AssertExpectations(t)
|
|
|
|
|
|
|
|
assert.Nil(err)
|
2021-02-08 15:42:35 +00:00
|
|
|
assert.Equal(path.Join(expectedHomeDir, expectedSubFolder, config.GetBinaryName()), providerPath)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:42:35 +00:00
|
|
|
func TestProviderInstallerInstallAlreadyExist(t *testing.T) {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
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)
|
2020-12-09 15:31:34 +00:00
|
|
|
err := os.MkdirAll(path.Join(fakeTmpHome, expectedSubFolder), 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2021-02-08 15:42:35 +00:00
|
|
|
|
|
|
|
config := ProviderConfig{
|
|
|
|
Key: "aws",
|
|
|
|
Version: "3.19.0",
|
|
|
|
Postfix: "x5",
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Create(path.Join(fakeTmpHome, expectedSubFolder, config.GetBinaryName()))
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mockDownloader := mocks.ProviderDownloaderInterface{}
|
|
|
|
|
|
|
|
installer := ProviderInstaller{
|
|
|
|
downloader: &mockDownloader,
|
2021-02-08 15:42:35 +00:00
|
|
|
config: config,
|
2020-12-09 15:31:34 +00:00
|
|
|
homeDir: fakeTmpHome,
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:42:35 +00:00
|
|
|
providerPath, err := installer.Install()
|
2020-12-09 15:31:34 +00:00
|
|
|
mockDownloader.AssertExpectations(t)
|
|
|
|
|
|
|
|
assert.Nil(err)
|
2021-02-08 15:42:35 +00:00
|
|
|
assert.Equal(path.Join(fakeTmpHome, expectedSubFolder, config.GetBinaryName()), providerPath)
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:42:35 +00:00
|
|
|
func TestProviderInstallerInstallAlreadyExistButIsDirectory(t *testing.T) {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
|
|
|
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)
|
2021-02-08 15:42:35 +00:00
|
|
|
|
|
|
|
config := ProviderConfig{
|
|
|
|
Key: "aws",
|
|
|
|
Version: "3.19.0",
|
|
|
|
Postfix: "x5",
|
|
|
|
}
|
|
|
|
|
|
|
|
invalidDirPath := path.Join(fakeTmpHome, expectedSubFolder, config.GetBinaryName())
|
2020-12-09 15:31:34 +00:00
|
|
|
err := os.MkdirAll(invalidDirPath, 0755)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mockDownloader := mocks.ProviderDownloaderInterface{}
|
|
|
|
|
|
|
|
installer := ProviderInstaller{
|
|
|
|
downloader: &mockDownloader,
|
2021-02-08 15:42:35 +00:00
|
|
|
config: config,
|
2020-12-09 15:31:34 +00:00
|
|
|
homeDir: fakeTmpHome,
|
|
|
|
}
|
|
|
|
|
2021-02-08 15:42:35 +00:00
|
|
|
providerPath, err := installer.Install()
|
2020-12-09 15:31:34 +00:00
|
|
|
mockDownloader.AssertExpectations(t)
|
|
|
|
|
|
|
|
assert.Empty(providerPath)
|
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Equal(
|
|
|
|
fmt.Sprintf(
|
|
|
|
"found directory instead of provider binary in %s",
|
|
|
|
invalidDirPath,
|
|
|
|
),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|