Skip to content

Commit 4562caf

Browse files
committed
Getting tests organized.
1 parent 65677bb commit 4562caf

File tree

9 files changed

+151
-108
lines changed

9 files changed

+151
-108
lines changed

transfers/dispatcher_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ import (
2727
"github.com/stretchr/testify/assert"
2828
)
2929

30-
func TestStartAndStopDispatcher(t *testing.T) {
31-
assert := assert.New(t)
30+
// We attach the tests to this type, which runs them one by one.
31+
// NOTE: All tests are bookended by calls to the setup and breakdown functions in transfer_test.go
32+
type DispatcherTests struct{ Test *testing.T }
33+
34+
func TestDispatcher(t *testing.T) {
35+
tester := DispatcherTests{Test: t}
36+
tester.TestStartAndStop()
37+
}
38+
39+
func (t *DispatcherTests) TestStartAndStop() {
40+
assert := assert.New(t.Test)
3241
err := dispatcher.Start()
3342
assert.Nil(err)
3443
err = dispatcher.Stop()

transfers/manifestor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ import (
3434
"github.com/kbase/dts/endpoints"
3535
)
3636

37-
//-------
38-
// Mover
39-
//-------
37+
//------------
38+
// Manifestor
39+
//------------
4040

4141
// The manifestor generates a JSON manifest for each successful transfer and sends it to the
4242
// transfer's destination. The manifest contains a Frictionless DataPackage containing all

transfers/manifestor_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ import (
2727
"github.com/stretchr/testify/assert"
2828
)
2929

30-
func TestStartAndStopManifestor(t *testing.T) {
31-
assert := assert.New(t)
30+
// We attach the tests to this type, which runs them one by one.
31+
// NOTE: All tests are bookended by calls to the setup and breakdown functions in transfer_test.go
32+
type ManifestorTests struct{ Test *testing.T }
33+
34+
func TestManifestor(t *testing.T) {
35+
tester := ManifestorTests{Test: t}
36+
tester.TestStartAndStop()
37+
}
38+
39+
func (t *ManifestorTests) TestStartAndStop() {
40+
assert := assert.New(t.Test)
3241
err := manifestor.Start()
3342
assert.Nil(err)
3443
err = manifestor.Stop()

transfers/mover.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ func (channels *moverChannels) close() {
6262
// starts the mover
6363
func (m *moverState) Start() error {
6464
m.Channels = moverChannels{
65-
RequestMove: make(chan uuid.UUID, 32),
65+
RequestMove: make(chan uuid.UUID, 32),
6666
RequestCancellation: make(chan uuid.UUID, 32),
67-
Error: make(chan error, 32),
68-
Stop: make(chan struct{}),
67+
Error: make(chan error, 32),
68+
Stop: make(chan struct{}),
6969
}
7070
m.Endpoints = make(map[string]endpoints.Endpoint)
7171
go m.process()

transfers/mover_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ import (
2727
"github.com/stretchr/testify/assert"
2828
)
2929

30-
func TestStartAndStopMover(t *testing.T) {
31-
assert := assert.New(t)
30+
// We attach the tests to this type, which runs them one by one.
31+
// NOTE: All tests are bookended by calls to the setup and breakdown functions in transfer_test.go
32+
func TestMover(t *testing.T) {
33+
tester := MoverTests{Test: t}
34+
tester.TestStartAndStop()
35+
}
36+
37+
type MoverTests struct{ Test *testing.T }
38+
39+
func (t *MoverTests) TestStartAndStop() {
40+
assert := assert.New(t.Test)
3241
err := mover.Start()
3342
assert.Nil(err)
3443
err = mover.Stop()

transfers/stager.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ func (channels *stagerChannels) close() {
6161
// starts the stager
6262
func (s *stagerState) Start() error {
6363
s.Channels = stagerChannels{
64-
RequestStaging: make(chan uuid.UUID, 32),
64+
RequestStaging: make(chan uuid.UUID, 32),
6565
RequestCancellation: make(chan uuid.UUID, 32),
66-
Error: make(chan error, 32),
67-
Stop: make(chan struct{}),
66+
Error: make(chan error, 32),
67+
Stop: make(chan struct{}),
6868
}
6969
go s.process()
7070
return nil

transfers/stager_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@ import (
2727
"github.com/stretchr/testify/assert"
2828
)
2929

30-
func TestStartAndStopStager(t *testing.T) {
31-
assert := assert.New(t)
30+
// We attach the tests to this type, which runs them one by one.
31+
// NOTE: All tests are bookended by calls to the setup and breakdown functions in transfer_test.go
32+
type StagerTests struct{ Test *testing.T }
33+
34+
func TestStager(t *testing.T) {
35+
tester := StagerTests{Test: t}
36+
tester.TestStartAndStop()
37+
}
38+
39+
func (t *StagerTests) TestStartAndStop() {
40+
assert := assert.New(t.Test)
3241
err := stager.Start()
3342
assert.Nil(err)
3443
err = stager.Stop()

transfers/store_test.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,31 @@ import (
2828
"github.com/stretchr/testify/assert"
2929
)
3030

31-
func TestStartAndStopStore(t *testing.T) {
32-
assert := assert.New(t)
31+
// We attach the tests to this type, which runs them one by one.
32+
// NOTE: All tests are bookended by calls to the setup and breakdown functions in transfer_test.go
33+
type StoreTests struct{ Test *testing.T }
34+
35+
func TestStore(t *testing.T) {
36+
tester := StoreTests{Test: t}
37+
tester.TestStartAndStop()
38+
// tester.TestNewTransfer()
39+
}
40+
41+
func (t *StoreTests) TestStartAndStop() {
42+
assert := assert.New(t.Test)
3343
err := store.Start()
3444
assert.Nil(err)
3545
err = store.Stop()
3646
assert.Nil(err)
3747
}
3848

39-
func TestStoreNewTransfer(t *testing.T) {
40-
assert := assert.New(t)
49+
func (t *StoreTests) TestNewTransfer() {
50+
assert := assert.New(t.Test)
4151

4252
err := store.Start()
4353
assert.Nil(err)
4454

45-
spec := Specification{
46-
}
55+
spec := Specification{}
4756
transferId, numFiles, err := store.NewTransfer(spec)
4857
assert.Nil(err)
4958
assert.NotEqual(uuid.UUID{}, transferId)

transfers/transfers_test.go

Lines changed: 83 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -39,93 +39,16 @@ import (
3939
"github.com/kbase/dts/dtstest"
4040
)
4141

42-
// runs all tests serially
43-
func TestRunner(t *testing.T) {
44-
tester := SerialTests{Test: t}
45-
tester.TestStartAndStopTransfers()
46-
//tester.TestStopAndRestartTransfers()
47-
}
48-
49-
// This runs setup, runs all tests, and does breakdown.
50-
func TestMain(m *testing.M) {
51-
var status int
52-
setup()
53-
status = m.Run()
54-
breakdown()
55-
os.Exit(status)
56-
}
57-
58-
// this function gets called at the begіnning of a test session
59-
func setup() {
60-
dtstest.EnableDebugLogging()
61-
62-
log.Print("Creating testing directory...\n")
63-
var err error
64-
TESTING_DIR, err = os.MkdirTemp(os.TempDir(), "data-transfer-service-tests-")
65-
if err != nil {
66-
log.Panicf("Couldn't create testing directory: %s", err)
67-
}
68-
os.Chdir(TESTING_DIR)
69-
70-
// read in the config file with SOURCE_ROOT and DESTINATION_ROOT replaced
71-
myConfig := strings.ReplaceAll(transfersConfig, "TESTING_DIR", TESTING_DIR)
72-
err = config.Init([]byte(myConfig))
73-
if err != nil {
74-
log.Panicf("Couldn't initialize configuration: %s", err)
75-
}
76-
77-
// create test resources
78-
testDescriptors := map[string]map[string]any{
79-
"file1": {
80-
"id": "file1",
81-
"name": "file1.dat",
82-
"path": "dir1/file1.dat",
83-
"format": "text",
84-
"bytes": 1024,
85-
"hash": "d91f97974d06563cab48d4d43a17e08a",
86-
"endpoint": "source-endpoint",
87-
},
88-
"file2": {
89-
"id": "file2",
90-
"name": "file2.dat",
91-
"path": "dir2/file2.dat",
92-
"format": "text",
93-
"bytes": 2048,
94-
"hash": "d91f9e974d0e563cab48d4d43a17e08a",
95-
"endpoint": "source-endpoint",
96-
},
97-
"file3": {
98-
"id": "file3",
99-
"name": "file3.dat",
100-
"path": "dir3/file3.dat",
101-
"format": "text",
102-
"bytes": 4096,
103-
"hash": "e91f9e974d0e563cab48d4d43a17e08e",
104-
"endpoint": "source-endpoint",
105-
},
106-
}
107-
108-
// register test databases/endpoints referred to in config file
109-
dtstest.RegisterTestFixturesFromConfig(endpointOptions, testDescriptors)
110-
111-
// Create the data and manifest directories
112-
os.Mkdir(config.Service.DataDirectory, 0755)
113-
os.Mkdir(config.Service.ManifestDirectory, 0755)
114-
}
42+
// We attach the tests to this type, which runs them one by one.
43+
type TransferTests struct{ Test *testing.T }
11544

116-
// this function gets called after all tests have been run
117-
func breakdown() {
118-
if TESTING_DIR != "" {
119-
log.Printf("Deleting testing directory %s...\n", TESTING_DIR)
120-
os.RemoveAll(TESTING_DIR)
121-
}
45+
func TestTransfers(t *testing.T) {
46+
tester := TransferTests{Test: t}
47+
tester.TestStartAndStop()
48+
//tester.TestStopAndRestartTransfers()
12249
}
12350

124-
// To run the tests serially, we attach them to a SerialTests type and
125-
// have them run by a a single test runner.
126-
type SerialTests struct{ Test *testing.T }
127-
128-
func (t *SerialTests) TestStartAndStopTransfers() {
51+
func (t *TransferTests) TestStartAndStop() {
12952
assert := assert.New(t.Test)
13053

13154
assert.False(Running())
@@ -137,7 +60,7 @@ func (t *SerialTests) TestStartAndStopTransfers() {
13760
assert.False(Running())
13861
}
13962

140-
func (t *SerialTests) TestStopAndRestartTransfers() {
63+
func (t *TransferTests) TestStopAndRestart() {
14164
/*
14265
assert := assert.New(t.Test)
14366
@@ -175,6 +98,81 @@ func (t *SerialTests) TestStopAndRestartTransfers() {
17598
*/
17699
}
177100

101+
// This runs setup, runs all tests, and does breakdown.
102+
func TestMain(m *testing.M) {
103+
var status int
104+
setup()
105+
status = m.Run()
106+
breakdown()
107+
os.Exit(status)
108+
}
109+
110+
// this function gets called at the begіnning of a test session
111+
func setup() {
112+
dtstest.EnableDebugLogging()
113+
114+
log.Print("Creating testing directory...\n")
115+
var err error
116+
TESTING_DIR, err = os.MkdirTemp(os.TempDir(), "data-transfer-service-tests-")
117+
if err != nil {
118+
log.Panicf("Couldn't create testing directory: %s", err)
119+
}
120+
os.Chdir(TESTING_DIR)
121+
122+
// read in the config file with SOURCE_ROOT and DESTINATION_ROOT replaced
123+
myConfig := strings.ReplaceAll(transfersConfig, "TESTING_DIR", TESTING_DIR)
124+
err = config.Init([]byte(myConfig))
125+
if err != nil {
126+
log.Panicf("Couldn't initialize configuration: %s", err)
127+
}
128+
129+
// create test resources
130+
testDescriptors := map[string]map[string]any{
131+
"file1": {
132+
"id": "file1",
133+
"name": "file1.dat",
134+
"path": "dir1/file1.dat",
135+
"format": "text",
136+
"bytes": 1024,
137+
"hash": "d91f97974d06563cab48d4d43a17e08a",
138+
"endpoint": "source-endpoint",
139+
},
140+
"file2": {
141+
"id": "file2",
142+
"name": "file2.dat",
143+
"path": "dir2/file2.dat",
144+
"format": "text",
145+
"bytes": 2048,
146+
"hash": "d91f9e974d0e563cab48d4d43a17e08a",
147+
"endpoint": "source-endpoint",
148+
},
149+
"file3": {
150+
"id": "file3",
151+
"name": "file3.dat",
152+
"path": "dir3/file3.dat",
153+
"format": "text",
154+
"bytes": 4096,
155+
"hash": "e91f9e974d0e563cab48d4d43a17e08e",
156+
"endpoint": "source-endpoint",
157+
},
158+
}
159+
160+
// register test databases/endpoints referred to in config file
161+
dtstest.RegisterTestFixturesFromConfig(endpointOptions, testDescriptors)
162+
163+
// Create the data and manifest directories
164+
os.Mkdir(config.Service.DataDirectory, 0755)
165+
os.Mkdir(config.Service.ManifestDirectory, 0755)
166+
}
167+
168+
// this function gets called after all tests have been run
169+
func breakdown() {
170+
if TESTING_DIR != "" {
171+
log.Printf("Deleting testing directory %s...\n", TESTING_DIR)
172+
os.RemoveAll(TESTING_DIR)
173+
}
174+
}
175+
178176
// temporary testing directory
179177
var TESTING_DIR string
180178

0 commit comments

Comments
 (0)