2021-09-22 18:48:01 +00:00
|
|
|
package shared_test
|
2019-06-04 13:04:18 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-28 10:56:59 +00:00
|
|
|
"context"
|
2021-09-17 19:40:43 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2019-06-04 13:04:18 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 16:25:51 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/remote/mocks"
|
2021-09-22 18:48:01 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/shared"
|
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-09-25 23:23:17 +00:00
|
|
|
type file struct {
|
|
|
|
name string
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
dummyData := []byte("TEST")
|
|
|
|
|
2021-08-30 20:54:21 +00:00
|
|
|
testTable := []struct {
|
2021-09-17 19:40:43 +00:00
|
|
|
name string
|
|
|
|
repoConfig string
|
2021-09-25 23:23:17 +00:00
|
|
|
files []file
|
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: "",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/text.txt",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/release.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/image.png",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
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: "",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
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: "",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{{
|
|
|
|
name: ".drone.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
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: "",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{},
|
2021-09-17 19:40:43 +00:00
|
|
|
expectedFileNames: []string{},
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Default config - Additional sub-folders",
|
|
|
|
repoConfig: "",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/test.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/sub-folder/config.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
2021-09-17 19:40:43 +00:00
|
|
|
expectedFileNames: []string{
|
|
|
|
".woodpecker/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Default config - Additional none .yml files",
|
|
|
|
repoConfig: "",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/notes.txt",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/image.png",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/test.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
|
|
|
expectedFileNames: []string{
|
2021-09-17 19:40:43 +00:00
|
|
|
".woodpecker/test.yml",
|
2021-08-30 20:54:21 +00:00
|
|
|
},
|
2021-09-25 23:23:17 +00:00
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Default config - Empty Folder",
|
|
|
|
repoConfig: " ",
|
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/.keep",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker.yml",
|
|
|
|
data: nil,
|
|
|
|
}, {
|
|
|
|
name: ".drone.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
2021-08-30 20:54:21 +00:00
|
|
|
expectedFileNames: []string{
|
2021-09-25 23:23:17 +00:00
|
|
|
".drone.yml",
|
2021-09-17 19:40:43 +00:00
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - folder (ignoring default files)",
|
|
|
|
repoConfig: ".my-ci-folder/",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/test.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".drone.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".my-ci-folder/test.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
2021-09-17 19:40:43 +00:00
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - folder",
|
|
|
|
repoConfig: ".my-ci-folder/",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{{
|
|
|
|
name: ".my-ci-folder/test.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
2021-09-17 19:40:43 +00:00
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - subfolder",
|
|
|
|
repoConfig: ".my-ci-folder/my-config/",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{{
|
|
|
|
name: ".my-ci-folder/my-config/test.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
2021-09-17 19:40:43 +00:00
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/my-config/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - file",
|
|
|
|
repoConfig: ".config.yml",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{{
|
|
|
|
name: ".config.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
2021-09-17 19:40:43 +00:00
|
|
|
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",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{{
|
|
|
|
name: ".my-ci-folder/sub-folder/config.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
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",
|
2021-09-25 23:23:17 +00:00
|
|
|
files: []file{},
|
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-10-02 08:59:34 +00:00
|
|
|
repo := &model.Repo{Owner: "laszlocph", Name: "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 {
|
2021-09-28 10:56:59 +00:00
|
|
|
r.On("File", mock.Anything, mock.Anything, mock.Anything, mock.Anything, file.name).Return(file.data, nil)
|
2021-09-25 23:23:17 +00:00
|
|
|
path := filepath.Dir(file.name)
|
|
|
|
if path != "." {
|
|
|
|
dirs[path] = append(dirs[path], &remote.FileMeta{
|
|
|
|
Name: file.name,
|
|
|
|
Data: file.data,
|
|
|
|
})
|
|
|
|
}
|
2021-09-17 19:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for path, files := range dirs {
|
2021-09-28 10:56:59 +00:00
|
|
|
r.On("Dir", mock.Anything, 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
|
2021-09-28 10:56:59 +00:00
|
|
|
r.On("File", mock.Anything, 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, mock.Anything).Return(nil, fmt.Errorf("Directory not found"))
|
2021-08-30 20:54:21 +00:00
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
configFetcher := shared.NewConfigFetcher(
|
2021-08-30 20:54:21 +00:00
|
|
|
r,
|
|
|
|
&model.User{Token: "xxx"},
|
|
|
|
repo,
|
|
|
|
&model.Build{Commit: "89ab7b2d6bfb347144ac7c557e638ab402848fee"},
|
|
|
|
)
|
2021-09-28 10:56:59 +00:00
|
|
|
files, err := configFetcher.Fetch(context.Background())
|
2021-08-30 20:54:21 +00:00
|
|
|
if tt.expectedError && err == nil {
|
|
|
|
t.Fatal("expected an error")
|
|
|
|
} else if !tt.expectedError && err != nil {
|
|
|
|
t.Fatal("error fetching config:", err)
|
|
|
|
}
|
|
|
|
|
2021-09-25 23:23:17 +00:00
|
|
|
matchingFiles := make([]string, len(files))
|
|
|
|
for i := range files {
|
|
|
|
matchingFiles[i] = files[i].Name
|
2021-08-30 20:54:21 +00:00
|
|
|
}
|
2021-09-25 23:23:17 +00:00
|
|
|
assert.ElementsMatch(t, tt.expectedFileNames, matchingFiles, "expected some other pipeline files")
|
2021-08-30 20:54:21 +00:00
|
|
|
})
|
2019-06-04 13:04:18 +00:00
|
|
|
}
|
|
|
|
}
|