-
Notifications
You must be signed in to change notification settings - Fork 10
/
pipeline_test.go
191 lines (152 loc) · 5.29 KB
/
pipeline_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package srcfingerprint
import (
"context"
"path/filepath"
"srcfingerprint/cloner"
"srcfingerprint/provider"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
git "gopkg.in/src-d/go-git.v4"
)
type PipelineTestSuite struct {
suite.Suite
}
type ProviderMock struct {
mock.Mock
}
func (mock *ProviderMock) Gather(user string) ([]provider.GitRepository, error) {
args := mock.Called(user)
return args.Get(0).([]provider.GitRepository), args.Error(1)
}
func (mock *ProviderMock) CloneRepository(ctx context.Context, cloner cloner.Cloner, repository provider.GitRepository) (string, error) {
args := mock.Called(cloner, repository)
return args.String(0), args.Error(1)
}
type gitRepositoryMock struct{ name string }
func (m gitRepositoryMock) GetName() string { return m.name }
func (m gitRepositoryMock) GetSSHUrl() string { return "" }
func (m gitRepositoryMock) GetHTTPUrl() string { return "" }
func (m gitRepositoryMock) GetCreatedAt() time.Time { return time.Unix(0, 0) }
func (m gitRepositoryMock) GetStorageSize() int64 { return 0 }
func (m gitRepositoryMock) GetPrivate() bool { return true }
func createGitRepository(name string) provider.GitRepository {
return gitRepositoryMock{name: name}
}
func openTestGitRepository(t *testing.T) *git.Repository {
repopath, _ := filepath.Abs(filepath.Join("testdata", "gitrepo", "git"))
repository, err := git.PlainOpen(repopath)
if err != nil {
t.Fatalf("could not open test git repository: %v", err)
}
return repository
}
func (suite *PipelineTestSuite) TestGather() {
outputChan := make(chan provider.GitRepository)
wg := &sync.WaitGroup{}
providerMock := &ProviderMock{}
pipeline := Pipeline{
Provider: providerMock,
}
providerMock.On("Gather", "user").Return([]provider.GitRepository{createGitRepository("1")}, nil)
wg.Add(1)
go pipeline.gather(wg, nil, "user", outputChan, 0)
repositories := make([]provider.GitRepository, 0, 2)
for output := range outputChan {
repositories = append(repositories, output)
}
wg.Wait()
providerMock.AssertExpectations(suite.T())
assert.Equal(suite.T(), []provider.GitRepository{gitRepositoryMock{name: "1"}}, repositories)
}
func (suite *PipelineTestSuite) TestGatherWithLimit() {
outputChan := make(chan provider.GitRepository)
wg := &sync.WaitGroup{}
providerMock := &ProviderMock{}
pipeline := Pipeline{
Provider: providerMock,
}
providerMock.On("Gather", "user").Return(
[]provider.GitRepository{createGitRepository("1"), createGitRepository("2")},
nil,
)
wg.Add(1)
go pipeline.gather(wg, nil, "user", outputChan, 1)
repositories := make([]provider.GitRepository, 0, 2)
for output := range outputChan {
repositories = append(repositories, output)
}
wg.Wait()
providerMock.AssertExpectations(suite.T())
assert.Equal(suite.T(), []provider.GitRepository{gitRepositoryMock{name: "1"}}, repositories)
}
func (suite *PipelineTestSuite) TestExtractGitRepository() {
suite.T().Skip("Skip until repository is stable")
eventChan := make(chan PipelineEvent)
provider := &ProviderMock{}
repository := createGitRepository("repoName")
pipeline := Pipeline{Provider: provider}
gitRepository := openTestGitRepository(suite.T())
commitIter, _ := gitRepository.CommitObjects()
// firstCommit, _ := commitIter.Next()
commitIter.Close()
provider.On("CloneRepository", nil, repository).Return(gitRepository, nil)
go func() {
defer close(eventChan)
pipeline.ExtractRepository(context.Background(), repository, "", eventChan)
}()
events := make([]PipelineEvent, 0)
for event := range eventChan {
events = append(events, event)
}
expectedEvents := []PipelineEvent{
// ResultPipelineEvent{
// Repository: repository,
// Commit: firstCommit,
// Author: firstCommit.Author,
// Committer: firstCommit.Committer,
// },
RepositoryPipelineEvent{true, true, "repoName"},
}
provider.AssertExpectations(suite.T())
assert.Equal(suite.T(), expectedEvents, events)
}
func (suite *PipelineTestSuite) TestExtractRepositories() {
suite.T().Skip("Skip until repository is stable") // Skip for now
eventChan := make(chan PipelineEvent)
providerMock := &ProviderMock{}
repository := createGitRepository("repoName")
pipeline := Pipeline{Provider: providerMock}
gitRepository := openTestGitRepository(suite.T())
commitIter, _ := gitRepository.CommitObjects()
// firstCommit, _ := commitIter.Next()
commitIter.Close()
providerMock.On("Gather", "user").Return([]provider.GitRepository{repository}, nil)
providerMock.On("CloneRepository", nil, repository).Return(gitRepository, nil)
go func() {
defer close(eventChan)
pipeline.ExtractRepositories("user", "", eventChan, 0, 0)
}()
events := make([]PipelineEvent, 0)
for event := range eventChan {
events = append(events, event)
}
expectedEvents := []PipelineEvent{
RepositoryListPipelineEvent{Repositories: []provider.GitRepository{repository}},
// ResultPipelineEvent{
// Repository: repository,
// Commit: firstCommit,
// Author: firstCommit.Author,
// Committer: firstCommit.Committer,
// },
RepositoryPipelineEvent{true, true, "repoName"},
}
providerMock.AssertExpectations(suite.T())
assert.Equal(suite.T(), expectedEvents, events)
}
func TestPipeline(t *testing.T) {
suite.Run(t, new(PipelineTestSuite))
}