2023-01-12 07:21:53 +03:00
|
|
|
package terraform_module_test_helper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2023-01-31 11:59:10 +03:00
|
|
|
"fmt"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-01-12 07:21:53 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2023-01-19 10:22:18 +03:00
|
|
|
|
|
|
|
"github.com/gruntwork-io/terratest/modules/files"
|
2023-01-31 11:59:10 +03:00
|
|
|
"github.com/gruntwork-io/terratest/modules/terraform"
|
|
|
|
terratest "github.com/gruntwork-io/terratest/modules/testing"
|
2023-01-19 10:22:18 +03:00
|
|
|
"github.com/prashantv/gostub"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-12 07:21:53 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetVersionSnapshot(t *testing.T) {
|
2023-01-31 11:59:10 +03:00
|
|
|
s := SuccessTestVersionSnapshot("./", "example/basic")
|
2023-02-01 18:41:43 +03:00
|
|
|
s.load(t)
|
2023-01-31 13:05:52 +03:00
|
|
|
require.NotEmpty(t, s.Versions)
|
|
|
|
require.Contains(t, s.Versions, "Terraform v")
|
|
|
|
require.Contains(t, s.Versions, "registry.terraform.io/hashicorp/null")
|
2023-01-12 07:21:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionSnapshotToString(t *testing.T) {
|
|
|
|
snapshot := TestVersionSnapshot{
|
2023-01-31 13:05:52 +03:00
|
|
|
Time: time.Now(),
|
|
|
|
Success: true,
|
|
|
|
Versions: "Content",
|
2023-01-12 07:21:53 +03:00
|
|
|
}
|
|
|
|
s := snapshot.ToString()
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(s))
|
|
|
|
require.True(t, scanner.Scan())
|
|
|
|
title := scanner.Text()
|
|
|
|
require.True(t, strings.HasPrefix(title, "## "))
|
2023-01-12 10:12:15 +03:00
|
|
|
require.Equal(t, snapshot.Time.Format(time.RFC822), strings.TrimPrefix(title, "## "))
|
2023-01-12 07:21:53 +03:00
|
|
|
require.Contains(t, s, "Success: true")
|
2023-01-31 13:05:52 +03:00
|
|
|
require.Contains(t, s, snapshot.Versions)
|
2023-01-12 07:21:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOutputNewTestVersionSnapshot(t *testing.T) {
|
2023-02-03 02:16:57 +03:00
|
|
|
defer func() {
|
|
|
|
err := os.RemoveAll("TestRecord")
|
|
|
|
if err != nil {
|
|
|
|
println(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
2023-02-02 04:28:18 +03:00
|
|
|
localPath := filepath.Join("example", "basic", "TestRecord.md.tmp")
|
|
|
|
defer func() { _ = os.Remove(localPath) }()
|
|
|
|
uploadPath := filepath.Join("TestRecord", "basic", "TestRecord.md.tmp")
|
|
|
|
defer func() { _ = os.Remove(uploadPath) }()
|
2023-01-31 11:59:10 +03:00
|
|
|
content := "Content"
|
|
|
|
stub := gostub.Stub(&initE, func(terratest.TestingT, *terraform.Options) (string, error) {
|
|
|
|
return "", nil
|
2023-01-12 09:30:05 +03:00
|
|
|
})
|
|
|
|
defer stub.Reset()
|
2023-02-02 04:28:18 +03:00
|
|
|
stub.Stub(&runTerraformCommandE, func(terratest.TestingT, *terraform.Options, ...string) (string, error) {
|
2023-01-31 11:59:10 +03:00
|
|
|
return content, nil
|
|
|
|
})
|
|
|
|
s := SuccessTestVersionSnapshot(".", filepath.Join("example", "basic"))
|
2023-02-01 18:41:43 +03:00
|
|
|
err := s.Save(t)
|
2023-01-12 07:21:53 +03:00
|
|
|
require.Nil(t, err)
|
2023-02-02 04:28:18 +03:00
|
|
|
file, err := os.ReadFile(filepath.Clean(localPath))
|
2023-01-31 13:05:52 +03:00
|
|
|
s.Versions = content
|
2023-01-12 07:21:53 +03:00
|
|
|
require.Nil(t, err)
|
2023-01-31 11:59:10 +03:00
|
|
|
require.Equal(t, s.ToString(), string(file))
|
2023-02-02 04:28:18 +03:00
|
|
|
require.True(t, files.FileExists(filepath.Clean(localPath)))
|
|
|
|
require.True(t, files.FileExists(filepath.Clean(uploadPath)))
|
2023-01-12 07:21:53 +03:00
|
|
|
}
|
2023-01-31 11:59:10 +03:00
|
|
|
|
|
|
|
func TestVersionSnapshotRecord_initCommandErrorShouldReturnInitCommandError(t *testing.T) {
|
|
|
|
expectedOutput := "init error"
|
|
|
|
stub := gostub.Stub(&initE, func(terratest.TestingT, *terraform.Options) (string, error) {
|
|
|
|
return expectedOutput, fmt.Errorf("init error")
|
|
|
|
})
|
|
|
|
defer stub.Reset()
|
2023-02-02 04:28:18 +03:00
|
|
|
stub.Stub(&runTerraformCommandE, func(terratest.TestingT, *terraform.Options, ...string) (string, error) {
|
2023-01-31 11:59:10 +03:00
|
|
|
return "not expected output", nil
|
|
|
|
})
|
|
|
|
s := SuccessTestVersionSnapshot(".", filepath.Join("example", "basic"))
|
2023-02-01 18:41:43 +03:00
|
|
|
s.load(t)
|
2023-01-31 13:05:52 +03:00
|
|
|
assert.Equal(t, expectedOutput, s.ErrorMsg)
|
2023-01-31 11:59:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestVersionSnapshotRecord_versionCommandErrorShouldReturnVersionCommandError(t *testing.T) {
|
|
|
|
expectedOutput := "version command error"
|
|
|
|
stub := gostub.Stub(&initE, func(terratest.TestingT, *terraform.Options) (string, error) {
|
|
|
|
return "not expected output", nil
|
|
|
|
})
|
|
|
|
defer stub.Reset()
|
2023-02-02 04:28:18 +03:00
|
|
|
stub.Stub(&runTerraformCommandE, func(terratest.TestingT, *terraform.Options, ...string) (string, error) {
|
2023-01-31 11:59:10 +03:00
|
|
|
return expectedOutput, fmt.Errorf(expectedOutput)
|
|
|
|
})
|
|
|
|
s := SuccessTestVersionSnapshot(".", filepath.Join("example", "basic"))
|
2023-02-01 18:41:43 +03:00
|
|
|
s.load(t)
|
2023-01-31 13:05:52 +03:00
|
|
|
assert.Equal(t, expectedOutput, s.ErrorMsg)
|
2023-02-02 04:28:18 +03:00
|
|
|
}
|