@@ -7,6 +7,7 @@ package dump
77import (
88 "context"
99 "encoding/json"
10+ "errors"
1011 "io/fs"
1112 "io/ioutil"
1213 "os"
@@ -15,30 +16,87 @@ import (
1516
1617 "github.com/stretchr/testify/assert"
1718 "github.com/stretchr/testify/require"
19+ "github.com/stretchr/testify/suite"
1820
21+ "github.com/elastic/elastic-package/internal/elasticsearch"
1922 estest "github.com/elastic/elastic-package/internal/elasticsearch/test"
2023 "github.com/elastic/elastic-package/internal/files"
2124)
2225
23- func TestInstalledObjectsDumpAll (t * testing.T ) {
24- client := estest .ElasticsearchClient (t , "./testdata/elasticsearch-mock-dump-apache" )
25- outputDir := t .TempDir ()
26- dumper := NewInstalledObjectsDumper (client , "apache" )
26+ func TestDumpInstalledObjects (t * testing.T ) {
27+ // Files for each suite are recorded automatically on first test run.
28+ // To add a new suite:
29+ // - Configure it here.
30+ // - Install the package in a running stack.
31+ // - Configure environment variables for this stack (eval "$(elastic-package stack shellinit)").
32+ // - Run tests.
33+ // - Check that recorded files make sense and commit them.
34+ suites := []* installedObjectsDumpSuite {
35+ & installedObjectsDumpSuite {
36+ PackageName : "apache" ,
37+ RecordDir : "./testdata/elasticsearch-7-mock-dump-apache" ,
38+ DumpDir : "./testdata/elasticsearch-7-apache-dump-all" ,
39+ },
40+ & installedObjectsDumpSuite {
41+ PackageName : "apache" ,
42+ RecordDir : "./testdata/elasticsearch-8-mock-dump-apache" ,
43+ DumpDir : "./testdata/elasticsearch-8-apache-dump-all" ,
44+ },
45+ }
46+
47+ for _ , s := range suites {
48+ suite .Run (t , s )
49+ }
50+ }
51+
52+ type installedObjectsDumpSuite struct {
53+ suite.Suite
54+
55+ // PackageName is the name of the package.
56+ PackageName string
57+
58+ // RecordDir is where responses from Elasticsearch are recorded.
59+ RecordDir string
60+
61+ // DumpDir is where the expected dumped files are stored.
62+ DumpDir string
63+ }
64+
65+ func (s * installedObjectsDumpSuite ) SetupTest () {
66+ _ , err := os .Stat (s .DumpDir )
67+ if errors .Is (err , os .ErrNotExist ) {
68+ client , err := elasticsearch .Client ()
69+ s .Require ().NoError (err )
70+
71+ dumper := NewInstalledObjectsDumper (client .API , s .PackageName )
72+ n , err := dumper .DumpAll (context .Background (), s .DumpDir )
73+ s .Require ().NoError (err )
74+ s .Require ().Greater (n , 0 )
75+ } else {
76+ s .Require ().NoError (err )
77+ }
78+ }
79+
80+ func (s * installedObjectsDumpSuite ) TestDumpAll () {
81+ client := estest .ElasticsearchClient (s .T (), s .RecordDir )
82+
83+ outputDir := s .T ().TempDir ()
84+ dumper := NewInstalledObjectsDumper (client , s .PackageName )
2785 n , err := dumper .DumpAll (context .Background (), outputDir )
28- require . NoError (t , err )
86+ s . Require (). NoError (err )
2987
30- filesExpected := countFiles (t , "./testdata/apache-dump-all" )
31- assert . Equal (t , filesExpected , n )
88+ filesExpected := countFiles (s . T (), s . DumpDir )
89+ s . Assert (). Equal (filesExpected , n )
3290
33- filesFound := countFiles (t , outputDir )
34- assert . Equal (t , filesExpected , filesFound )
91+ filesFound := countFiles (s . T () , outputDir )
92+ s . Assert (). Equal (filesExpected , filesFound )
3593
36- assertEqualDumps (t , "./testdata/apache-dump-all" , outputDir )
94+ assertEqualDumps (s . T (), s . DumpDir , outputDir )
3795}
3896
39- func TestInstalledObjectsDumpSome ( t * testing. T ) {
40- client := estest .ElasticsearchClient (t , "./testdata/elasticsearch-mock-dump-apache" )
41- dumper := NewInstalledObjectsDumper (client , "apache" )
97+ func ( s * installedObjectsDumpSuite ) TestDumpSome ( ) {
98+ client := estest .ElasticsearchClient (s . T (), s . RecordDir )
99+ dumper := NewInstalledObjectsDumper (client , s . PackageName )
42100
43101 // In a map so order of execution is randomized.
44102 dumpers := map [string ]func (ctx context.Context , outputDir string ) (int , error ){
@@ -49,19 +107,19 @@ func TestInstalledObjectsDumpSome(t *testing.T) {
49107 }
50108
51109 for dir , dumpFunction := range dumpers {
52- t .Run (dir , func (t * testing. T ) {
53- outputDir := t .TempDir ()
110+ s .Run (dir , func () {
111+ outputDir := s . T () .TempDir ()
54112 n , err := dumpFunction (context .Background (), outputDir )
55- require . NoError (t , err )
113+ s . Require (). NoError (err )
56114
57- expectedDir := subDir (t , "./testdata/apache-dump-all" , dir )
58- filesExpected := countFiles (t , expectedDir )
59- assert . Equal (t , filesExpected , n )
115+ expectedDir := subDir (s . T (), s . DumpDir , dir )
116+ filesExpected := countFiles (s . T () , expectedDir )
117+ s . Assert (). Equal (filesExpected , n )
60118
61- filesFound := countFiles (t , outputDir )
62- assert . Equal (t , filesExpected , filesFound )
119+ filesFound := countFiles (s . T () , outputDir )
120+ s . Assert (). Equal (filesExpected , filesFound )
63121
64- assertEqualDumps (t , expectedDir , outputDir )
122+ assertEqualDumps (s . T () , expectedDir , outputDir )
65123 })
66124 }
67125}
0 commit comments