2021-08-20 14:32:52 +00:00
|
|
|
package server_test
|
2019-06-04 13:04:18 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-17 19:40:43 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2019-06-04 13:04:18 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-08-30 20:54:21 +00:00
|
|
|
"github.com/stretchr/testify/mock"
|
2021-05-25 12:08:27 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/model"
|
2021-08-30 20:54:21 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/remote"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/remote/mocks"
|
2021-08-20 14:32:52 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2019-06-04 13:04:18 +00:00
|
|
|
)
|
|
|
|
|
2021-08-30 20:54:21 +00:00
|
|
|
func TestFetch(t *testing.T) {
|
2019-10-06 18:30:06 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-08-30 20:54:21 +00:00
|
|
|
testTable := []struct {
|
2021-09-17 19:40:43 +00:00
|
|
|
name string
|
|
|
|
repoConfig string
|
|
|
|
files []string
|
2021-08-30 20:54:21 +00:00
|
|
|
expectedFileNames []string
|
|
|
|
expectedError bool
|
|
|
|
}{
|
|
|
|
{
|
2021-09-17 19:40:43 +00:00
|
|
|
name: "Default config - .woodpecker/",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []string{
|
|
|
|
".woodpecker/text.txt",
|
|
|
|
".woodpecker/release.yml",
|
|
|
|
".woodpecker/image.png",
|
2021-08-30 20:54:21 +00:00
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
2021-09-17 19:40:43 +00:00
|
|
|
".woodpecker/release.yml",
|
2021-08-30 20:54:21 +00:00
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2021-09-17 19:40:43 +00:00
|
|
|
name: "Default config - .woodpecker.yml",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []string{
|
|
|
|
".woodpecker.yml",
|
2021-08-30 20:54:21 +00:00
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
2021-09-17 19:40:43 +00:00
|
|
|
".woodpecker.yml",
|
2021-08-30 20:54:21 +00:00
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2021-09-17 19:40:43 +00:00
|
|
|
name: "Default config - .drone.yml",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []string{
|
|
|
|
".drone.yml",
|
2021-08-30 20:54:21 +00:00
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".drone.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2021-09-17 19:40:43 +00: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 20:54:21 +00:00
|
|
|
},
|
|
|
|
expectedFileNames: []string{
|
2021-09-17 19:40:43 +00: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 20:54:21 +00:00
|
|
|
".drone.yml",
|
2021-09-17 19:40:43 +00: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 20:54:21 +00:00
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2021-09-17 19:40:43 +00: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 20:54:21 +00:00
|
|
|
},
|
2021-09-17 19:40:43 +00: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 20:54:21 +00:00
|
|
|
expectedFileNames: []string{},
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range testTable {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-09-17 19:40:43 +00:00
|
|
|
repo := &model.Repo{Owner: "laszlocph", Name: "drone-multipipeline", Config: tt.repoConfig}
|
2021-08-30 20:54:21 +00:00
|
|
|
|
|
|
|
r := new(mocks.Remote)
|
2021-09-17 19:40:43 +00: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 20:54:21 +00:00
|
|
|
}
|
2021-09-17 19:40:43 +00: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 20:54:21 +00:00
|
|
|
|
|
|
|
configFetcher := server.NewConfigFetcher(
|
|
|
|
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-17 19:40:43 +00:00
|
|
|
receivedFileNames := []string{}
|
|
|
|
for _, file := range files {
|
|
|
|
receivedFileNames = append(receivedFileNames, file.Name)
|
|
|
|
}
|
|
|
|
t.Fatal("expected some other pipeline files", tt.expectedFileNames, receivedFileNames)
|
2021-08-30 20:54:21 +00:00
|
|
|
}
|
|
|
|
})
|
2019-06-04 13:04:18 +00:00
|
|
|
}
|
|
|
|
}
|