2022-10-18 03:24:12 +02: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.
|
|
|
|
|
2021-09-22 20:48:01 +02:00
|
|
|
package shared_test
|
2019-06-04 15:04:18 +02:00
|
|
|
|
|
|
|
import (
|
2021-09-28 12:56:59 +02:00
|
|
|
"context"
|
2022-06-01 20:06:27 +02:00
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/rand"
|
2022-02-28 11:56:23 +02:00
|
|
|
"encoding/json"
|
2021-09-17 21:40:43 +02:00
|
|
|
"fmt"
|
2022-08-30 01:14:07 +02:00
|
|
|
"io"
|
2022-02-28 11:56:23 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-09-17 21:40:43 +02:00
|
|
|
"path/filepath"
|
2019-06-04 15:04:18 +02:00
|
|
|
"testing"
|
|
|
|
|
2022-06-01 20:06:27 +02:00
|
|
|
"github.com/go-ap/httpsig"
|
2021-10-12 09:25:13 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2022-06-01 20:06:27 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/plugins/config"
|
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
|
|
|
)
|
|
|
|
|
2022-06-16 15:35:58 +02:00
|
|
|
// TODO(974) move to new package
|
|
|
|
|
2021-08-30 22:54:21 +02:00
|
|
|
func TestFetch(t *testing.T) {
|
2019-10-06 20:30:06 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2021-09-26 01:23:17 +02:00
|
|
|
type file struct {
|
|
|
|
name string
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
dummyData := []byte("TEST")
|
|
|
|
|
2021-08-30 22:54:21 +02:00
|
|
|
testTable := []struct {
|
2021-09-17 21:40:43 +02:00
|
|
|
name string
|
|
|
|
repoConfig string
|
2021-09-26 01:23:17 +02:00
|
|
|
files []file
|
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: "",
|
2021-09-26 01:23:17 +02:00
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/text.txt",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/release.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/image.png",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
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,
|
|
|
|
},
|
2022-11-03 20:12:40 +02: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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Default config check .woodpecker.yml before .woodpecker.yaml",
|
|
|
|
repoConfig: "",
|
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker.yaml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
|
|
|
expectedFileNames: []string{
|
|
|
|
".woodpecker.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
2021-08-30 22:54:21 +02:00
|
|
|
{
|
2022-02-28 11:56:23 +02:00
|
|
|
name: "Override via API with custom config",
|
2021-09-17 21:40:43 +02:00
|
|
|
repoConfig: "",
|
2021-09-26 01:23:17 +02:00
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
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,
|
|
|
|
},
|
|
|
|
{
|
2022-02-28 11:56:23 +02:00
|
|
|
name: "Use old config on 204 response",
|
2021-09-17 21:40:43 +02:00
|
|
|
repoConfig: "",
|
2021-09-26 01:23:17 +02:00
|
|
|
files: []file{{
|
|
|
|
name: ".drone.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
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: "",
|
2021-09-26 01:23:17 +02:00
|
|
|
files: []file{},
|
2021-09-17 21:40:43 +02:00
|
|
|
expectedFileNames: []string{},
|
|
|
|
expectedError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Default config - Additional sub-folders",
|
|
|
|
repoConfig: "",
|
2021-09-26 01:23:17 +02:00
|
|
|
files: []file{{
|
|
|
|
name: ".woodpecker/test.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}, {
|
|
|
|
name: ".woodpecker/sub-folder/config.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
2021-09-17 21:40:43 +02:00
|
|
|
expectedFileNames: []string{
|
|
|
|
".woodpecker/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Default config - Additional none .yml files",
|
|
|
|
repoConfig: "",
|
2021-09-26 01:23:17 +02: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 21:40:43 +02:00
|
|
|
".woodpecker/test.yml",
|
2021-08-30 22:54:21 +02:00
|
|
|
},
|
2021-09-26 01:23:17 +02: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 22:54:21 +02:00
|
|
|
expectedFileNames: []string{
|
2021-09-26 01:23:17 +02:00
|
|
|
".drone.yml",
|
2021-09-17 21:40:43 +02:00
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - folder (ignoring default files)",
|
|
|
|
repoConfig: ".my-ci-folder/",
|
2021-09-26 01:23:17 +02: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 21:40:43 +02:00
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - folder",
|
|
|
|
repoConfig: ".my-ci-folder/",
|
2021-09-26 01:23:17 +02:00
|
|
|
files: []file{{
|
|
|
|
name: ".my-ci-folder/test.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
2021-09-17 21:40:43 +02:00
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - subfolder",
|
|
|
|
repoConfig: ".my-ci-folder/my-config/",
|
2021-09-26 01:23:17 +02:00
|
|
|
files: []file{{
|
|
|
|
name: ".my-ci-folder/my-config/test.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
2021-09-17 21:40:43 +02:00
|
|
|
expectedFileNames: []string{
|
|
|
|
".my-ci-folder/my-config/test.yml",
|
|
|
|
},
|
|
|
|
expectedError: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Special config - file",
|
|
|
|
repoConfig: ".config.yml",
|
2021-09-26 01:23:17 +02:00
|
|
|
files: []file{{
|
|
|
|
name: ".config.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
2021-09-17 21:40:43 +02:00
|
|
|
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",
|
2021-09-26 01:23:17 +02:00
|
|
|
files: []file{{
|
|
|
|
name: ".my-ci-folder/sub-folder/config.yml",
|
|
|
|
data: dummyData,
|
|
|
|
}},
|
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",
|
2021-09-26 01:23:17 +02:00
|
|
|
files: []file{},
|
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-10-02 10:59:34 +02:00
|
|
|
repo := &model.Repo{Owner: "laszlocph", Name: "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 {
|
2021-09-28 12:56:59 +02:00
|
|
|
r.On("File", mock.Anything, mock.Anything, mock.Anything, mock.Anything, file.name).Return(file.data, nil)
|
2021-09-26 01:23:17 +02:00
|
|
|
path := filepath.Dir(file.name)
|
|
|
|
if path != "." {
|
|
|
|
dirs[path] = append(dirs[path], &remote.FileMeta{
|
|
|
|
Name: file.name,
|
|
|
|
Data: file.data,
|
|
|
|
})
|
|
|
|
}
|
2021-09-17 21:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for path, files := range dirs {
|
2021-09-28 12:56:59 +02:00
|
|
|
r.On("Dir", mock.Anything, 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
|
2021-09-28 12:56:59 +02: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 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,
|
2022-06-01 20:06:27 +02:00
|
|
|
config.NewHTTP("", ""),
|
2022-02-28 11:56:23 +02:00
|
|
|
&model.User{Token: "xxx"},
|
|
|
|
repo,
|
2022-10-18 03:24:12 +02:00
|
|
|
&model.Pipeline{Commit: "89ab7b2d6bfb347144ac7c557e638ab402848fee"},
|
2022-02-28 11:56:23 +02: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 20:06:27 +02:00
|
|
|
pubEd25519Key, privEd25519Key, err := ed25519.GenerateKey(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("can't generate ed25519 key pair")
|
|
|
|
}
|
2022-02-28 11:56:23 +02:00
|
|
|
|
|
|
|
fixtureHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// check signature
|
2022-06-01 20:06:27 +02: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 11:56:23 +02:00
|
|
|
if err != nil {
|
2022-06-01 20:06:27 +02:00
|
|
|
http.Error(w, "Invalid signature", http.StatusBadRequest)
|
2022-02-28 11:56:23 +02:00
|
|
|
return
|
|
|
|
}
|
2022-06-01 20:06:27 +02:00
|
|
|
|
|
|
|
if keyID != pubKeyID {
|
|
|
|
http.Error(w, "Used wrong key", http.StatusBadRequest)
|
2022-02-28 11:56:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type incoming struct {
|
2022-10-18 03:24:12 +02:00
|
|
|
Repo *model.Repo `json:"repo"`
|
2022-10-22 15:54:43 +02:00
|
|
|
Build *model.Pipeline `json:"pipeline"`
|
2022-10-18 03:24:12 +02:00
|
|
|
Configuration []*config `json:"config"`
|
2022-02-28 11:56:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var req incoming
|
2022-08-30 01:14:07 +02:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2022-02-28 11:56:23 +02: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 20:06:27 +02:00
|
|
|
configAPI := config.NewHTTP(ts.URL, privEd25519Key)
|
2022-02-28 11:56:23 +02: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
|
|
|
|
|
|
|
|
r := new(mocks.Remote)
|
|
|
|
dirs := map[string][]*remote.FileMeta{}
|
|
|
|
for _, file := range tt.files {
|
|
|
|
r.On("File", mock.Anything, mock.Anything, mock.Anything, mock.Anything, file.name).Return(file.data, nil)
|
|
|
|
path := filepath.Dir(file.name)
|
|
|
|
if path != "." {
|
|
|
|
dirs[path] = append(dirs[path], &remote.FileMeta{
|
|
|
|
Name: file.name,
|
|
|
|
Data: file.data,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for path, files := range dirs {
|
|
|
|
r.On("Dir", mock.Anything, mock.Anything, mock.Anything, mock.Anything, path).Return(files, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the previous mocks do not match return not found errors
|
|
|
|
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"))
|
|
|
|
|
|
|
|
configFetcher := shared.NewConfigFetcher(
|
|
|
|
r,
|
|
|
|
configAPI,
|
2021-08-30 22:54:21 +02:00
|
|
|
&model.User{Token: "xxx"},
|
|
|
|
repo,
|
2022-10-18 03:24:12 +02:00
|
|
|
&model.Pipeline{Commit: "89ab7b2d6bfb347144ac7c557e638ab402848fee"},
|
2021-08-30 22:54:21 +02:00
|
|
|
)
|
2021-09-28 12:56:59 +02:00
|
|
|
files, err := configFetcher.Fetch(context.Background())
|
2021-08-30 22:54:21 +02: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-26 01:23:17 +02:00
|
|
|
matchingFiles := make([]string, len(files))
|
|
|
|
for i := range files {
|
|
|
|
matchingFiles[i] = files[i].Name
|
2021-08-30 22:54:21 +02:00
|
|
|
}
|
2021-09-26 01:23:17 +02:00
|
|
|
assert.ElementsMatch(t, tt.expectedFileNames, matchingFiles, "expected some other pipeline files")
|
2021-08-30 22:54:21 +02:00
|
|
|
})
|
2019-06-04 15:04:18 +02:00
|
|
|
}
|
|
|
|
}
|