2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
package forge_test
|
2019-06-04 13:04:18 +00:00
|
|
|
|
|
|
|
import (
|
2021-09-28 10:56:59 +00:00
|
|
|
"context"
|
2022-06-01 18:06:27 +00:00
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/rand"
|
2022-02-28 09:56:23 +00:00
|
|
|
"encoding/json"
|
2021-09-17 19:40:43 +00:00
|
|
|
"fmt"
|
2022-08-29 23:14:07 +00:00
|
|
|
"io"
|
2022-02-28 09:56:23 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-09-17 19:40:43 +00:00
|
|
|
"path/filepath"
|
2019-06-04 13:04:18 +00:00
|
|
|
"testing"
|
2023-02-01 17:53:19 +00:00
|
|
|
"time"
|
2019-06-04 13:04:18 +00:00
|
|
|
|
2022-06-01 18:06:27 +00:00
|
|
|
"github.com/go-ap/httpsig"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/forge"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/forge/mocks"
|
2022-11-06 11:44:04 +00:00
|
|
|
forge_types "github.com/woodpecker-ci/woodpecker/server/forge/types"
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2022-06-01 18:06:27 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/plugins/config"
|
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,
|
|
|
|
},
|
2022-11-03 18:12:40 +00:00
|
|
|
{
|
|
|
|
name: "Default config with .yaml - .woodpecker/",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/text.txt",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/release.yaml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/image.png",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".woodpecker/release.yaml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Default config with .yaml, .yml mix - .woodpecker/",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/text.txt",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/release.yaml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/other.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/image.png",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".woodpecker/release.yaml",
|
|
|
|
".woodpecker/other.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
2023-04-29 08:12:36 +00:00
|
|
|
name: "Default config check .woodpecker.yaml before .woodpecker.yml",
|
2022-11-03 18:12:40 +00:00
|
|
|
repoConfig: "",
|
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker.yaml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
|
|
|
expectedFileNames: []string{
|
2023-04-29 08:12:36 +00:00
|
|
|
".woodpecker.yaml",
|
2022-11-03 18:12:40 +00:00
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
2021-08-30 20:54:21 +00:00
|
|
|
{
|
2022-02-28 09:56:23 +00:00
|
|
|
name: "Override via API with custom config",
|
2021-09-17 19:40:43 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
2022-02-28 09:56:23 +00:00
|
|
|
name: "Use old config on 204 response",
|
2021-09-17 19:40:43 +00:00
|
|
|
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
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
f := new(mocks.Forge)
|
2022-11-06 11:44:04 +00:00
|
|
|
dirs := map[string][]*forge_types.FileMeta{}
|
2021-09-17 19:40:43 +00:00
|
|
|
for _, file := range tt.files {
|
2022-11-04 23:35:06 +00:00
|
|
|
f.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 != "." {
|
2022-11-06 11:44:04 +00:00
|
|
|
dirs[path] = append(dirs[path], &forge_types.FileMeta{
|
2021-09-25 23:23:17 +00:00
|
|
|
Name: file.name,
|
|
|
|
Data: file.data,
|
|
|
|
})
|
|
|
|
}
|
2021-09-17 19:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for path, files := range dirs {
|
2022-11-04 23:35:06 +00:00
|
|
|
f.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
|
2022-11-04 23:35:06 +00:00
|
|
|
f.On("File", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("File not found"))
|
|
|
|
f.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
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
configFetcher := forge.NewConfigFetcher(
|
2022-11-04 23:35:06 +00:00
|
|
|
f,
|
2023-02-01 17:53:19 +00:00
|
|
|
time.Second*3,
|
2022-06-01 18:06:27 +00:00
|
|
|
config.NewHTTP("", ""),
|
2022-02-28 09:56:23 +00:00
|
|
|
&model.User{Token: "xxx"},
|
|
|
|
repo,
|
2022-10-18 01:24:12 +00:00
|
|
|
&model.Pipeline{Commit: "89ab7b2d6bfb347144ac7c557e638ab402848fee"},
|
2022-02-28 09:56:23 +00:00
|
|
|
)
|
|
|
|
files, err := configFetcher.Fetch(context.Background())
|
|
|
|
if tt.expectedError && err == nil {
|
|
|
|
t.Fatal("expected an error")
|
|
|
|
} else if !tt.expectedError && err != nil {
|
|
|
|
t.Fatal("error fetching config:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
matchingFiles := make([]string, len(files))
|
|
|
|
for i := range files {
|
|
|
|
matchingFiles[i] = files[i].Name
|
|
|
|
}
|
|
|
|
assert.ElementsMatch(t, tt.expectedFileNames, matchingFiles, "expected some other pipeline files")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFetchFromConfigService(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
type file struct {
|
|
|
|
name string
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
dummyData := []byte("TEST")
|
|
|
|
|
|
|
|
testTable := []struct {
|
|
|
|
name string
|
|
|
|
repoConfig string
|
|
|
|
files []file
|
|
|
|
expectedFileNames []string
|
|
|
|
expectedError bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "External Fetch empty repo",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []file{},
|
|
|
|
expectedFileNames: []string{"override1", "override2", "override3"},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Default config - Additional sub-folders",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/test.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/sub-folder/config.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
|
|
|
expectedFileNames: []string{"override1", "override2", "override3"},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Fetch empty",
|
|
|
|
repoConfig: " ",
|
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/.keep",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker.yml",
|
|
|
|
data: nil,
|
|
|
|
}, {
|
|
|
|
name: ".drone.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
|
|
|
expectedFileNames: []string{},
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Use old config",
|
|
|
|
repoConfig: ".my-ci-folder/",
|
|
|
|
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,
|
|
|
|
}},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-06-01 18:06:27 +00:00
|
|
|
pubEd25519Key, privEd25519Key, err := ed25519.GenerateKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("can't generate ed25519 key pair")
|
|
|
|
}
|
2022-02-28 09:56:23 +00:00
|
|
|
|
|
|
|
fixtureHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// check signature
|
2022-06-01 18:06:27 +00:00
|
|
|
pubKeyID := "woodpecker-ci-plugins"
|
|
|
|
|
|
|
|
keystore := httpsig.NewMemoryKeyStore()
|
|
|
|
keystore.SetKey(pubKeyID, pubEd25519Key)
|
|
|
|
|
|
|
|
verifier := httpsig.NewVerifier(keystore)
|
|
|
|
verifier.SetRequiredHeaders([]string{"(request-target)", "date"})
|
|
|
|
|
|
|
|
keyID, err := verifier.Verify(r)
|
2022-02-28 09:56:23 +00:00
|
|
|
if err != nil {
|
2022-06-01 18:06:27 +00:00
|
|
|
http.Error(w, "Invalid signature", http.StatusBadRequest)
|
2022-02-28 09:56:23 +00:00
|
|
|
return
|
|
|
|
}
|
2022-06-01 18:06:27 +00:00
|
|
|
|
|
|
|
if keyID != pubKeyID {
|
|
|
|
http.Error(w, "Used wrong key", http.StatusBadRequest)
|
2022-02-28 09:56:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type incoming struct {
|
2022-10-18 01:24:12 +00:00
|
|
|
Repo *model.Repo `json:"repo"`
|
2022-10-22 13:54:43 +00:00
|
|
|
Build *model.Pipeline `json:"pipeline"`
|
2022-10-18 01:24:12 +00:00
|
|
|
Configuration []*config `json:"config"`
|
2022-02-28 09:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var req incoming
|
2022-08-29 23:14:07 +00:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2022-02-28 09:56:23 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "can't read body", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(body, &req)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Failed to parse JSON"+err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Repo.Name == "Fetch empty" {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Repo.Name == "Use old config" {
|
|
|
|
w.WriteHeader(204)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprint(w, `{
|
|
|
|
"configs": [
|
|
|
|
{
|
|
|
|
"name": "override1",
|
|
|
|
"data": "some new pipelineconfig \n pipe, pipe, pipe"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "override2",
|
|
|
|
"data": "some new pipelineconfig \n pipe, pipe, pipe"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "override3",
|
|
|
|
"data": "some new pipelineconfig \n pipe, pipe, pipe"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(fixtureHandler))
|
|
|
|
defer ts.Close()
|
2022-06-01 18:06:27 +00:00
|
|
|
configAPI := config.NewHTTP(ts.URL, privEd25519Key)
|
2022-02-28 09:56:23 +00:00
|
|
|
|
|
|
|
for _, tt := range testTable {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
repo := &model.Repo{Owner: "laszlocph", Name: tt.name, Config: tt.repoConfig} // Using test name as repo name to provide different responses in mock server
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
f := new(mocks.Forge)
|
2022-11-06 11:44:04 +00:00
|
|
|
dirs := map[string][]*forge_types.FileMeta{}
|
2022-02-28 09:56:23 +00:00
|
|
|
for _, file := range tt.files {
|
2022-11-04 23:35:06 +00:00
|
|
|
f.On("File", mock.Anything, mock.Anything, mock.Anything, mock.Anything, file.name).Return(file.data, nil)
|
2022-02-28 09:56:23 +00:00
|
|
|
path := filepath.Dir(file.name)
|
|
|
|
if path != "." {
|
2022-11-06 11:44:04 +00:00
|
|
|
dirs[path] = append(dirs[path], &forge_types.FileMeta{
|
2022-02-28 09:56:23 +00:00
|
|
|
Name: file.name,
|
|
|
|
Data: file.data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for path, files := range dirs {
|
2022-11-04 23:35:06 +00:00
|
|
|
f.On("Dir", mock.Anything, mock.Anything, mock.Anything, mock.Anything, path).Return(files, nil)
|
2022-02-28 09:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the previous mocks do not match return not found errors
|
2022-11-04 23:35:06 +00:00
|
|
|
f.On("File", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("File not found"))
|
|
|
|
f.On("Dir", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, fmt.Errorf("Directory not found"))
|
2022-02-28 09:56:23 +00:00
|
|
|
|
2022-11-06 11:44:04 +00:00
|
|
|
configFetcher := forge.NewConfigFetcher(
|
2022-11-04 23:35:06 +00:00
|
|
|
f,
|
2023-02-01 17:53:19 +00:00
|
|
|
time.Second*3,
|
2022-02-28 09:56:23 +00:00
|
|
|
configAPI,
|
2021-08-30 20:54:21 +00:00
|
|
|
&model.User{Token: "xxx"},
|
|
|
|
repo,
|
2022-10-18 01:24:12 +00:00
|
|
|
&model.Pipeline{Commit: "89ab7b2d6bfb347144ac7c557e638ab402848fee"},
|
2021-08-30 20:54:21 +00:00
|
|
|
)
|
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
|
|
|
}
|
|
|
|
}
|