2021-09-22 20:48:01 +02:00
|
|
|
package shared_test
|
2019-06-04 15:04:18 +02:00
|
|
|
|
|
|
|
import (
|
2021-09-17 21:40:43 +02:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2019-06-04 15:04:18 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-08-30 22:54:21 +02:00
|
|
|
"github.com/stretchr/testify/mock"
|
2021-05-25 14:08:27 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/model"
|
2021-09-23 18:25:51 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/mocks"
|
2021-09-22 20:48:01 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/shared"
|
2019-06-04 15:04:18 +02:00
|
|
|
)
|
|
|
|
|
2021-08-30 22:54:21 +02:00
|
|
|
func TestFetch(t *testing.T) {
|
2019-10-06 20:30:06 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-08-30 22:54:21 +02:00
|
|
|
testTable := []struct {
|
2021-09-17 21:40:43 +02:00
|
|
|
name string
|
|
|
|
repoConfig string
|
|
|
|
files []string
|
2021-08-30 22:54:21 +02:00
|
|
|
expectedFileNames []string
|
|
|
|
expectedError bool
|
|
|
|
}{
|
|
|
|
{
|
2021-09-17 21:40:43 +02:00
|
|
|
name: "Default config - .woodpecker/",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []string{
|
|
|
|
".woodpecker/text.txt",
|
|
|
|
".woodpecker/release.yml",
|
|
|
|
".woodpecker/image.png",
|
2021-08-30 22:54:21 +02:00
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
2021-09-17 21:40:43 +02:00
|
|
|
".woodpecker/release.yml",
|
2021-08-30 22:54:21 +02:00
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2021-09-17 21:40:43 +02:00
|
|
|
name: "Default config - .woodpecker.yml",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []string{
|
|
|
|
".woodpecker.yml",
|
2021-08-30 22:54:21 +02:00
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
2021-09-17 21:40:43 +02:00
|
|
|
".woodpecker.yml",
|
2021-08-30 22:54:21 +02:00
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2021-09-17 21:40:43 +02:00
|
|
|
name: "Default config - .drone.yml",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []string{
|
|
|
|
".drone.yml",
|
2021-08-30 22:54:21 +02:00
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".drone.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2021-09-17 21:40:43 +02:00
|
|
|
name: "Default config - Empty repo",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []string{},
|
|
|
|
expectedFileNames: []string{},
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Default config - Additional sub-folders",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []string{
|
|
|
|
".woodpecker/test.yml",
|
|
|
|
".woodpecker/sub-folder/config.yml",
|
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".woodpecker/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Default config - Additional none .yml files",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []string{
|
|
|
|
".woodpecker/notes.txt",
|
|
|
|
".woodpecker/image.png",
|
|
|
|
".woodpecker/test.yml",
|
2021-08-30 22:54:21 +02:00
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
2021-09-17 21:40:43 +02:00
|
|
|
".woodpecker/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - folder (ignoring default files)",
|
|
|
|
repoConfig: ".my-ci-folder/",
|
|
|
|
files: []string{
|
|
|
|
".woodpecker/test.yml",
|
|
|
|
".woodpecker.yml",
|
2021-08-30 22:54:21 +02:00
|
|
|
".drone.yml",
|
2021-09-17 21:40:43 +02:00
|
|
|
".my-ci-folder/test.yml",
|
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - folder",
|
|
|
|
repoConfig: ".my-ci-folder/",
|
|
|
|
files: []string{
|
|
|
|
".my-ci-folder/test.yml",
|
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - subfolder",
|
|
|
|
repoConfig: ".my-ci-folder/my-config/",
|
|
|
|
files: []string{
|
|
|
|
".my-ci-folder/my-config/test.yml",
|
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/my-config/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - file",
|
|
|
|
repoConfig: ".config.yml",
|
|
|
|
files: []string{
|
|
|
|
".config.yml",
|
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".config.yml",
|
2021-08-30 22:54:21 +02:00
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2021-09-17 21:40:43 +02:00
|
|
|
name: "Special config - file inside subfolder",
|
|
|
|
repoConfig: ".my-ci-folder/sub-folder/config.yml",
|
|
|
|
files: []string{
|
|
|
|
".my-ci-folder/sub-folder/config.yml",
|
2021-08-30 22:54:21 +02:00
|
|
|
},
|
2021-09-17 21:40:43 +02:00
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/sub-folder/config.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - empty repo",
|
|
|
|
repoConfig: ".config.yml",
|
|
|
|
files: []string{},
|
2021-08-30 22:54:21 +02:00
|
|
|
expectedFileNames: []string{},
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testTable {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-09-17 21:40:43 +02:00
|
|
|
repo := &model.Repo{Owner: "laszlocph", Name: "drone-multipipeline", Config: tt.repoConfig}
|
2021-08-30 22:54:21 +02:00
|
|
|
|
|
|
|
r := new(mocks.Remote)
|
2021-09-17 21:40:43 +02:00
|
|
|
dirs := map[string][]*remote.FileMeta{}
|
|
|
|
for _, file := range tt.files {
|
|
|
|
r.On("File", mock.Anything, mock.Anything, mock.Anything, file).Return([]byte{}, nil)
|
|
|
|
path := filepath.Dir(file)
|
|
|
|
dirs[path] = append(dirs[path], &remote.FileMeta{
|
|
|
|
Name: file,
|
|
|
|
Data: []byte{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for path, files := range dirs {
|
|
|
|
r.On("Dir", mock.Anything, mock.Anything, mock.Anything, path).Return(files, nil)
|
2021-08-30 22:54:21 +02:00
|
|
|
}
|
2021-09-17 21:40:43 +02:00
|
|
|
|
|
|
|
// if the previous mocks do not match return not found errors
|
|
|
|
r.On("File", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("File not found"))
|
|
|
|
r.On("Dir", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("Directory not found"))
|
2021-08-30 22:54:21 +02:00
|
|
|
|
2021-09-22 20:48:01 +02:00
|
|
|
configFetcher := shared.NewConfigFetcher(
|
2021-08-30 22:54:21 +02:00
|
|
|
r,
|
|
|
|
&model.User{Token: "xxx"},
|
|
|
|
repo,
|
|
|
|
&model.Build{Commit: "89ab7b2d6bfb347144ac7c557e638ab402848fee"},
|
|
|
|
)
|
|
|
|
files, err := configFetcher.Fetch()
|
|
|
|
if tt.expectedError && err == nil {
|
|
|
|
t.Fatal("expected an error")
|
|
|
|
} else if !tt.expectedError && err != nil {
|
|
|
|
t.Fatal("error fetching config:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
matchingFiles := 0
|
|
|
|
for _, expectedFileName := range tt.expectedFileNames {
|
|
|
|
for _, file := range files {
|
|
|
|
if file.Name == expectedFileName {
|
|
|
|
matchingFiles += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matchingFiles != len(tt.expectedFileNames) {
|
2021-09-24 16:29:26 +02:00
|
|
|
var receivedFileNames []string
|
2021-09-17 21:40:43 +02:00
|
|
|
for _, file := range files {
|
|
|
|
receivedFileNames = append(receivedFileNames, file.Name)
|
|
|
|
}
|
|
|
|
t.Fatal("expected some other pipeline files", tt.expectedFileNames, receivedFileNames)
|
2021-08-30 22:54:21 +02:00
|
|
|
}
|
|
|
|
})
|
2019-06-04 15:04:18 +02:00
|
|
|
}
|
|
|
|
}
|