-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathversion_helper_test.go
101 lines (94 loc) · 3.35 KB
/
version_helper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package terraform_module_test_helper
import (
"bufio"
"fmt"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/gruntwork-io/terratest/modules/files"
"github.com/gruntwork-io/terratest/modules/terraform"
terratest "github.com/gruntwork-io/terratest/modules/testing"
"github.com/prashantv/gostub"
"github.com/stretchr/testify/require"
)
func TestGetVersionSnapshot(t *testing.T) {
s := SuccessTestVersionSnapshot("./", "example/basic")
s.load(t)
require.NotEmpty(t, s.Versions)
require.Contains(t, s.Versions, "Terraform v")
require.Contains(t, s.Versions, "registry.terraform.io/hashicorp/null")
}
func TestVersionSnapshotToString(t *testing.T) {
snapshot := TestVersionSnapshot{
Time: time.Now(),
Success: true,
Versions: "Content",
}
s := snapshot.ToString()
scanner := bufio.NewScanner(strings.NewReader(s))
require.True(t, scanner.Scan())
title := scanner.Text()
require.True(t, strings.HasPrefix(title, "## "))
require.Equal(t, snapshot.Time.Format(time.RFC822), strings.TrimPrefix(title, "## "))
require.Contains(t, s, "Success: true")
require.Contains(t, s, snapshot.Versions)
require.Contains(t, s, NoErrorMessage)
}
func TestOutputNewTestVersionSnapshot(t *testing.T) {
defer func() {
err := os.RemoveAll("TestRecord")
if err != nil {
println(err.Error())
}
}()
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) }()
content := "Content"
stub := gostub.Stub(&initE, func(terratest.TestingT, *terraform.Options) (string, error) {
return "", nil
})
defer stub.Reset()
stub.Stub(&runTerraformCommandE, func(terratest.TestingT, *terraform.Options, ...string) (string, error) {
return content, nil
})
s := SuccessTestVersionSnapshot(".", filepath.Join("example", "basic"))
err := s.Save(t)
require.Nil(t, err)
file, err := os.ReadFile(filepath.Clean(localPath))
s.Versions = content
require.Nil(t, err)
require.Equal(t, s.ToString(), string(file))
require.True(t, files.FileExists(filepath.Clean(localPath)))
require.True(t, files.FileExists(filepath.Clean(uploadPath)))
}
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()
stub.Stub(&runTerraformCommandE, func(terratest.TestingT, *terraform.Options, ...string) (string, error) {
return "not expected output", nil
})
s := SuccessTestVersionSnapshot(".", filepath.Join("example", "basic"))
s.load(t)
assert.Equal(t, expectedOutput, s.ErrorMsg)
}
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()
stub.Stub(&runTerraformCommandE, func(terratest.TestingT, *terraform.Options, ...string) (string, error) {
return expectedOutput, fmt.Errorf(expectedOutput)
})
s := SuccessTestVersionSnapshot(".", filepath.Join("example", "basic"))
s.load(t)
assert.Equal(t, expectedOutput, s.ErrorMsg)
}