Skip to content

Commit 33860f9

Browse files
authored
add scan command to e2e tests (tellerops#106)
add scan command to e2e tests
1 parent 757be60 commit 33860f9

File tree

5 files changed

+85
-13
lines changed

5 files changed

+85
-13
lines changed

e2e/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The simple and fast way is based on `yml` file configuration. All you need to do
2424
| `init_snapshot.path` | Create file in path.
2525
| `init_snapshot.file_name` | File name.
2626
| `init_snapshot.content` | File content.
27+
| `replace_stdout_content` | Replace dynamic stdout content to static. for example, replace current timestemp to static text.
2728
| `expected_snapshot` | Compare the init_snapshot folder with the expected snapshot content. If empty, this compare check will be ignored.
2829
| `expected_snapshot.path` | Create file in path.
2930
| `expected_snapshot.file_name` | File name.

e2e/e2e_test.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"path/filepath"
8+
"regexp"
89
"strings"
910

1011
"testing"
@@ -61,11 +62,20 @@ func TestE2E(t *testing.T) {
6162

6263
flagsCommand := strings.TrimPrefix(snapshot.Command, removeBinaryPlaceholder)
6364
stdout, stderr, err := testutils.ExecCmd(binaryPath, strings.Split(flagsCommand, " "), snapshotFolder)
64-
assert.Nil(t, err, stderr)
65+
if stdout == "" {
66+
assert.Nil(t, err, stderr)
67+
}
6568

6669
// In case the stdout/stderr include the dynamic folder path, we want to replace with static-content for better snapshot text compare
6770
stdout, stderr = replaceFolderName(stdout, stderr, snapshotFolder)
6871

72+
if len(snapshot.ReplaceStdOutContent) > 0 {
73+
for _, r := range snapshot.ReplaceStdOutContent {
74+
var re = regexp.MustCompile(r.Search)
75+
stdout = re.ReplaceAllString(stdout, r.Replace)
76+
}
77+
}
78+
6979
if snapshot.ExpectedStdOut != "" {
7080
assert.Equal(t, snapshot.ExpectedStdOut, stdout)
7181
}
@@ -87,15 +97,13 @@ func TestE2E(t *testing.T) {
8797
}
8898
})
8999
}
90-
91100
// loop on register suites (from *.go files)
92101
for name, suite := range register.GetSuites() {
93102
t.Run(name, func(t *testing.T) {
94103

95104
// creates temp dir for test path.
96105
tempFolder, err := os.MkdirTemp(t.TempDir(), strings.ReplaceAll(name, " ", ""))
97106
assert.Nil(t, err, "could not create temp folder")
98-
99107
defer os.RemoveAll(tempFolder)
100108

101109
// initialized test case

e2e/tests/scan.yml

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
name: scan entries
3+
command: <binary-path> scan
4+
config_file_name: .teller.yml
5+
config_content: >
6+
project: test
7+
8+
providers:
9+
filesystem:
10+
env_sync:
11+
path: {{.Folder}}/settings/test/all
12+
env:
13+
FOO:
14+
path: {{.Folder}}/settings/test/foo
15+
severity: low
16+
BAR:
17+
path: {{.Folder}}/settings/test/bar
18+
severity: medium
19+
init_snapshot:
20+
- path: settings/test
21+
file_name: foo
22+
content: shazam
23+
- path: settings/test
24+
file_name: bar
25+
content: shazam
26+
- path: settings/test
27+
file_name: file
28+
content: content
29+
- path: settings/test/all
30+
file_name: secret-a
31+
content: mailman
32+
- path: settings/test/all
33+
file_name: secret-b
34+
content: shazam
35+
expected_snapshot:
36+
replace_stdout_content:
37+
- search: "matches in [0-9].[0-9]+(.*)"
38+
replace: matches in 1.000ms
39+
expected_stdout: |
40+
[high] settings/test/all/secret-a (1,0): found match for filesystem/secret-a (ma*****)
41+
[high] settings/test/all/secret-b (1,0): found match for filesystem/secret-b (sh*****)
42+
[low] settings/test/all/secret-b (1,0): found match for filesystem/FOO (sh*****)
43+
[medium] settings/test/all/secret-b (1,0): found match for filesystem/BAR (sh*****)
44+
[high] settings/test/bar (1,0): found match for filesystem/secret-b (sh*****)
45+
[low] settings/test/bar (1,0): found match for filesystem/FOO (sh*****)
46+
[medium] settings/test/bar (1,0): found match for filesystem/BAR (sh*****)
47+
[high] settings/test/foo (1,0): found match for filesystem/secret-b (sh*****)
48+
[low] settings/test/foo (1,0): found match for filesystem/FOO (sh*****)
49+
[medium] settings/test/foo (1,0): found match for filesystem/BAR (sh*****)
50+
51+
Scanning for 4 entries: found 10 matches in 1.000ms
52+
expected_stderr:

e2e/testutils/config.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ import (
1111
)
1212

1313
type SnapshotSuite struct {
14-
Name string `yaml:"name,omitempty"`
15-
Command string `yaml:"command,omitempty"`
16-
ConfigFileName string `yaml:"config_file_name,omitempty"`
17-
Config string `yaml:"config_content,omitempty"`
18-
InitSnapshot []SnapshotData `yaml:"init_snapshot,omitempty"`
19-
ExpectedSnapshot []SnapshotData `yaml:"expected_snapshot,omitempty"`
20-
ExpectedStdOut string `yaml:"expected_stdout,omitempty"`
21-
ExpectedStdErr string `yaml:"expected_stderr,omitempty"`
14+
Name string `yaml:"name,omitempty"`
15+
Command string `yaml:"command,omitempty"`
16+
ConfigFileName string `yaml:"config_file_name,omitempty"`
17+
Config string `yaml:"config_content,omitempty"`
18+
InitSnapshot []SnapshotData `yaml:"init_snapshot,omitempty"`
19+
ExpectedSnapshot []SnapshotData `yaml:"expected_snapshot,omitempty"`
20+
ExpectedStdOut string `yaml:"expected_stdout,omitempty"`
21+
ExpectedStdErr string `yaml:"expected_stderr,omitempty"`
22+
ReplaceStdOutContent []ReplaceStdOutContent `yaml:"replace_stdout_content,omitempty"`
2223
}
2324

2425
type SnapshotData struct {
@@ -27,6 +28,11 @@ type SnapshotData struct {
2728
Content string `yaml:"content"`
2829
}
2930

31+
type ReplaceStdOutContent struct {
32+
Search string `yaml:"search"`
33+
Replace string `yaml:"replace"`
34+
}
35+
3036
// GetYmlSnapshotSuites returns list of snapshot suite from yml files
3137
func GetYmlSnapshotSuites(folder string) ([]*SnapshotSuite, error) {
3238

pkg/porcelain.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"math"
88
"os"
9+
"sort"
910
"strings"
1011
"time"
1112

@@ -129,8 +130,12 @@ func (p *Porcelain) PrintMatches(matches []core.Match) {
129130
green := color.New(color.FgGreen).SprintFunc()
130131
white := color.New(color.FgWhite).SprintFunc()
131132
red := color.New(color.FgRed).SprintFunc()
132-
//nolint
133-
for _, m := range matches {
133+
134+
sort.Slice(matches, func(i, j int) bool {
135+
return matches[i].Path < matches[j].Path
136+
})
137+
138+
for _, m := range matches { //nolint
134139
sevcolor := white
135140
if m.Entry.Severity == core.High {
136141
sevcolor = red

0 commit comments

Comments
 (0)