Provisioning: count for Connection reference in Validation and Tester
This commit is contained in:
@@ -5,13 +5,15 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
provisioning "github.com/grafana/grafana/apps/provisioning/pkg/apis/provisioning/v0alpha1"
|
||||
"github.com/grafana/grafana/apps/provisioning/pkg/repository"
|
||||
"github.com/grafana/grafana/apps/provisioning/pkg/repository/git"
|
||||
"github.com/grafana/grafana/apps/provisioning/pkg/util"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
//go:generate mockery --name WebhookURLBuilder --structname MockWebhookURLBuilder --inpackage --filename webhook_builder_mock.go --with-expecter
|
||||
type WebhookURLBuilder interface {
|
||||
WebhookURL(ctx context.Context, r *provisioning.Repository) string
|
||||
}
|
||||
@@ -35,24 +37,22 @@ func (e *extra) Type() provisioning.RepositoryType {
|
||||
}
|
||||
|
||||
func (e *extra) Build(ctx context.Context, r *provisioning.Repository) (repository.Repository, error) {
|
||||
if r == nil || r.Spec.GitHub == nil {
|
||||
return nil, fmt.Errorf("github configuration is required")
|
||||
}
|
||||
logger := logging.FromContext(ctx).With("url", r.Spec.GitHub.URL, "branch", r.Spec.GitHub.Branch, "path", r.Spec.GitHub.Path)
|
||||
logger.Info("Instantiating Github repository")
|
||||
|
||||
secure := e.decrypter(r)
|
||||
cfg := r.Spec.GitHub
|
||||
if cfg == nil {
|
||||
return nil, fmt.Errorf("github configuration is required")
|
||||
}
|
||||
|
||||
token, err := secure.Token(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to decrypt token: %w", err)
|
||||
}
|
||||
|
||||
gitRepo, err := git.NewRepository(ctx, r, git.RepositoryConfig{
|
||||
URL: cfg.URL,
|
||||
Branch: cfg.Branch,
|
||||
Path: cfg.Path,
|
||||
URL: r.Spec.GitHub.URL,
|
||||
Branch: r.Spec.GitHub.Branch,
|
||||
Path: r.Spec.GitHub.Path,
|
||||
Token: token,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,410 @@
|
||||
package github_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
provisioning "github.com/grafana/grafana/apps/provisioning/pkg/apis/provisioning/v0alpha1"
|
||||
"github.com/grafana/grafana/apps/provisioning/pkg/repository"
|
||||
"github.com/grafana/grafana/apps/provisioning/pkg/repository/github"
|
||||
common "github.com/grafana/grafana/pkg/apimachinery/apis/common/v0alpha1"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
)
|
||||
|
||||
type mockSecureValues struct {
|
||||
token common.RawSecureValue
|
||||
tokenErr error
|
||||
webhookSecret common.RawSecureValue
|
||||
webhookErr error
|
||||
}
|
||||
|
||||
func (m *mockSecureValues) Token(_ context.Context) (common.RawSecureValue, error) {
|
||||
return m.token, m.tokenErr
|
||||
}
|
||||
|
||||
func (m *mockSecureValues) WebhookSecret(_ context.Context) (common.RawSecureValue, error) {
|
||||
return m.webhookSecret, m.webhookErr
|
||||
}
|
||||
|
||||
func TestExtra_Type(t *testing.T) {
|
||||
e := github.Extra(nil, nil, nil)
|
||||
assert.Equal(t, provisioning.GitHubRepositoryType, e.Type())
|
||||
}
|
||||
|
||||
func TestExtra_Build(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
repo *provisioning.Repository
|
||||
setupDecrypter func() repository.Decrypter
|
||||
setupWebhook func(t *testing.T, repo *provisioning.Repository) github.WebhookURLBuilder
|
||||
expectedError string
|
||||
validateResult func(t *testing.T, repo repository.Repository)
|
||||
}{
|
||||
{
|
||||
name: "missing github config",
|
||||
repo: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: nil,
|
||||
},
|
||||
},
|
||||
setupDecrypter: func() repository.Decrypter {
|
||||
return func(r *provisioning.Repository) repository.SecureValues {
|
||||
return &mockSecureValues{}
|
||||
}
|
||||
},
|
||||
setupWebhook: func(t *testing.T, repo *provisioning.Repository) github.WebhookURLBuilder { return nil },
|
||||
expectedError: "github configuration is required",
|
||||
},
|
||||
{
|
||||
name: "nil repository",
|
||||
repo: nil,
|
||||
setupDecrypter: func() repository.Decrypter {
|
||||
return func(r *provisioning.Repository) repository.SecureValues {
|
||||
return &mockSecureValues{}
|
||||
}
|
||||
},
|
||||
setupWebhook: func(t *testing.T, repo *provisioning.Repository) github.WebhookURLBuilder { return nil },
|
||||
expectedError: "github configuration is required",
|
||||
},
|
||||
{
|
||||
name: "error decrypting token",
|
||||
repo: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: &provisioning.GitHubRepositoryConfig{
|
||||
URL: "https://github.com/test/repo",
|
||||
Branch: "main",
|
||||
},
|
||||
},
|
||||
},
|
||||
setupDecrypter: func() repository.Decrypter {
|
||||
return func(r *provisioning.Repository) repository.SecureValues {
|
||||
return &mockSecureValues{
|
||||
tokenErr: errors.New("decryption failed"),
|
||||
}
|
||||
}
|
||||
},
|
||||
setupWebhook: func(t *testing.T, repo *provisioning.Repository) github.WebhookURLBuilder { return nil },
|
||||
expectedError: "unable to decrypt token",
|
||||
},
|
||||
{
|
||||
name: "success without webhooks",
|
||||
repo: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: &provisioning.GitHubRepositoryConfig{
|
||||
URL: "https://github.com/test/repo",
|
||||
Branch: "main",
|
||||
},
|
||||
},
|
||||
},
|
||||
setupDecrypter: func() repository.Decrypter {
|
||||
return func(r *provisioning.Repository) repository.SecureValues {
|
||||
return &mockSecureValues{
|
||||
token: common.RawSecureValue("test-token"),
|
||||
}
|
||||
}
|
||||
},
|
||||
setupWebhook: func(t *testing.T, repo *provisioning.Repository) github.WebhookURLBuilder {
|
||||
return nil
|
||||
},
|
||||
validateResult: func(t *testing.T, repo repository.Repository) {
|
||||
assert.NotNil(t, repo)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "success with webhooks",
|
||||
repo: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: &provisioning.GitHubRepositoryConfig{
|
||||
URL: "https://github.com/test/repo",
|
||||
Branch: "main",
|
||||
},
|
||||
},
|
||||
},
|
||||
setupDecrypter: func() repository.Decrypter {
|
||||
return func(r *provisioning.Repository) repository.SecureValues {
|
||||
return &mockSecureValues{
|
||||
token: common.RawSecureValue("test-token"),
|
||||
webhookSecret: common.RawSecureValue("webhook-secret"),
|
||||
}
|
||||
}
|
||||
},
|
||||
setupWebhook: func(t *testing.T, repo *provisioning.Repository) github.WebhookURLBuilder {
|
||||
mockWebhook := github.NewMockWebhookURLBuilder(t)
|
||||
mockWebhook.EXPECT().WebhookURL(mock.Anything, repo).Return("https://example.com/webhook")
|
||||
return mockWebhook
|
||||
},
|
||||
validateResult: func(t *testing.T, repo repository.Repository) {
|
||||
assert.NotNil(t, repo)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "skip webhook setup when URL is empty",
|
||||
repo: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: &provisioning.GitHubRepositoryConfig{
|
||||
URL: "https://github.com/test/repo",
|
||||
Branch: "main",
|
||||
},
|
||||
},
|
||||
},
|
||||
setupDecrypter: func() repository.Decrypter {
|
||||
return func(r *provisioning.Repository) repository.SecureValues {
|
||||
return &mockSecureValues{
|
||||
token: common.RawSecureValue("test-token"),
|
||||
}
|
||||
}
|
||||
},
|
||||
setupWebhook: func(t *testing.T, repo *provisioning.Repository) github.WebhookURLBuilder {
|
||||
mockWebhook := github.NewMockWebhookURLBuilder(t)
|
||||
mockWebhook.EXPECT().WebhookURL(mock.Anything, repo).Return("")
|
||||
return mockWebhook
|
||||
},
|
||||
validateResult: func(t *testing.T, repo repository.Repository) {
|
||||
assert.NotNil(t, repo)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "error decrypting webhook secret",
|
||||
repo: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: &provisioning.GitHubRepositoryConfig{
|
||||
URL: "https://github.com/test/repo",
|
||||
Branch: "main",
|
||||
},
|
||||
},
|
||||
},
|
||||
setupDecrypter: func() repository.Decrypter {
|
||||
return func(r *provisioning.Repository) repository.SecureValues {
|
||||
return &mockSecureValues{
|
||||
token: common.RawSecureValue("test-token"),
|
||||
webhookErr: errors.New("webhook decryption failed"),
|
||||
}
|
||||
}
|
||||
},
|
||||
setupWebhook: func(t *testing.T, repo *provisioning.Repository) github.WebhookURLBuilder {
|
||||
mockWebhook := github.NewMockWebhookURLBuilder(t)
|
||||
mockWebhook.EXPECT().WebhookURL(mock.Anything, repo).Return("https://example.com/webhook")
|
||||
return mockWebhook
|
||||
},
|
||||
expectedError: "decrypt webhookSecret",
|
||||
},
|
||||
{
|
||||
name: "success with custom path",
|
||||
repo: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: &provisioning.GitHubRepositoryConfig{
|
||||
URL: "https://github.com/test/repo",
|
||||
Branch: "main",
|
||||
Path: "custom/path",
|
||||
},
|
||||
},
|
||||
},
|
||||
setupDecrypter: func() repository.Decrypter {
|
||||
return func(r *provisioning.Repository) repository.SecureValues {
|
||||
return &mockSecureValues{
|
||||
token: common.RawSecureValue("test-token"),
|
||||
}
|
||||
}
|
||||
},
|
||||
setupWebhook: func(t *testing.T, repo *provisioning.Repository) github.WebhookURLBuilder {
|
||||
return nil
|
||||
},
|
||||
validateResult: func(t *testing.T, repo repository.Repository) {
|
||||
assert.NotNil(t, repo)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
decrypter := tt.setupDecrypter()
|
||||
webhookBuilder := tt.setupWebhook(t, tt.repo)
|
||||
factory := github.ProvideFactory()
|
||||
|
||||
e := github.Extra(decrypter, factory, webhookBuilder)
|
||||
|
||||
result, err := e.Build(ctx, tt.repo)
|
||||
|
||||
if tt.expectedError != "" {
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tt.expectedError)
|
||||
assert.Nil(t, result)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
if tt.validateResult != nil {
|
||||
tt.validateResult(t, result)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtra_Mutate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
obj runtime.Object
|
||||
expectedError bool
|
||||
validateObj func(t *testing.T, obj runtime.Object)
|
||||
}{
|
||||
{
|
||||
name: "mutates repository with github config",
|
||||
obj: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: &provisioning.GitHubRepositoryConfig{
|
||||
URL: "https://github.com/test/repo.git/",
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedError: false,
|
||||
validateObj: func(t *testing.T, obj runtime.Object) {
|
||||
repo := obj.(*provisioning.Repository)
|
||||
assert.Equal(t, "https://github.com/test/repo", repo.Spec.GitHub.URL)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "handles repository without github config",
|
||||
obj: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: nil,
|
||||
},
|
||||
},
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "handles non-repository object",
|
||||
obj: &runtime.Unknown{},
|
||||
expectedError: false,
|
||||
},
|
||||
{
|
||||
name: "trims only trailing slash",
|
||||
obj: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: &provisioning.GitHubRepositoryConfig{
|
||||
URL: "https://github.com/test/repo/",
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedError: false,
|
||||
validateObj: func(t *testing.T, obj runtime.Object) {
|
||||
repo := obj.(*provisioning.Repository)
|
||||
assert.Equal(t, "https://github.com/test/repo", repo.Spec.GitHub.URL)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "trims only .git suffix",
|
||||
obj: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: &provisioning.GitHubRepositoryConfig{
|
||||
URL: "https://github.com/test/repo.git",
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedError: false,
|
||||
validateObj: func(t *testing.T, obj runtime.Object) {
|
||||
repo := obj.(*provisioning.Repository)
|
||||
assert.Equal(t, "https://github.com/test/repo", repo.Spec.GitHub.URL)
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "no changes when URL is clean",
|
||||
obj: &provisioning.Repository{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-repo",
|
||||
Namespace: "default",
|
||||
},
|
||||
Spec: provisioning.RepositorySpec{
|
||||
Type: provisioning.GitHubRepositoryType,
|
||||
GitHub: &provisioning.GitHubRepositoryConfig{
|
||||
URL: "https://github.com/test/repo",
|
||||
},
|
||||
},
|
||||
},
|
||||
expectedError: false,
|
||||
validateObj: func(t *testing.T, obj runtime.Object) {
|
||||
repo := obj.(*provisioning.Repository)
|
||||
assert.Equal(t, "https://github.com/test/repo", repo.Spec.GitHub.URL)
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
e := github.Extra(nil, nil, nil)
|
||||
|
||||
err := e.Mutate(ctx, tt.obj)
|
||||
|
||||
if tt.expectedError {
|
||||
require.Error(t, err)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
if tt.validateObj != nil {
|
||||
tt.validateObj(t, tt.obj)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
// Code generated by mockery v2.53.4. DO NOT EDIT.
|
||||
|
||||
package github
|
||||
|
||||
import (
|
||||
context "context"
|
||||
|
||||
v0alpha1 "github.com/grafana/grafana/apps/provisioning/pkg/apis/provisioning/v0alpha1"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// MockWebhookURLBuilder is an autogenerated mock type for the WebhookURLBuilder type
|
||||
type MockWebhookURLBuilder struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
type MockWebhookURLBuilder_Expecter struct {
|
||||
mock *mock.Mock
|
||||
}
|
||||
|
||||
func (_m *MockWebhookURLBuilder) EXPECT() *MockWebhookURLBuilder_Expecter {
|
||||
return &MockWebhookURLBuilder_Expecter{mock: &_m.Mock}
|
||||
}
|
||||
|
||||
// WebhookURL provides a mock function with given fields: ctx, r
|
||||
func (_m *MockWebhookURLBuilder) WebhookURL(ctx context.Context, r *v0alpha1.Repository) string {
|
||||
ret := _m.Called(ctx, r)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for WebhookURL")
|
||||
}
|
||||
|
||||
var r0 string
|
||||
if rf, ok := ret.Get(0).(func(context.Context, *v0alpha1.Repository) string); ok {
|
||||
r0 = rf(ctx, r)
|
||||
} else {
|
||||
r0 = ret.Get(0).(string)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// MockWebhookURLBuilder_WebhookURL_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'WebhookURL'
|
||||
type MockWebhookURLBuilder_WebhookURL_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// WebhookURL is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - r *v0alpha1.Repository
|
||||
func (_e *MockWebhookURLBuilder_Expecter) WebhookURL(ctx interface{}, r interface{}) *MockWebhookURLBuilder_WebhookURL_Call {
|
||||
return &MockWebhookURLBuilder_WebhookURL_Call{Call: _e.mock.On("WebhookURL", ctx, r)}
|
||||
}
|
||||
|
||||
func (_c *MockWebhookURLBuilder_WebhookURL_Call) Run(run func(ctx context.Context, r *v0alpha1.Repository)) *MockWebhookURLBuilder_WebhookURL_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
run(args[0].(context.Context), args[1].(*v0alpha1.Repository))
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockWebhookURLBuilder_WebhookURL_Call) Return(_a0 string) *MockWebhookURLBuilder_WebhookURL_Call {
|
||||
_c.Call.Return(_a0)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockWebhookURLBuilder_WebhookURL_Call) RunAndReturn(run func(context.Context, *v0alpha1.Repository) string) *MockWebhookURLBuilder_WebhookURL_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// NewMockWebhookURLBuilder creates a new instance of MockWebhookURLBuilder. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
|
||||
// The first argument is typically a *testing.T value.
|
||||
func NewMockWebhookURLBuilder(t interface {
|
||||
mock.TestingT
|
||||
Cleanup(func())
|
||||
}) *MockWebhookURLBuilder {
|
||||
mock := &MockWebhookURLBuilder{}
|
||||
mock.Mock.Test(t)
|
||||
|
||||
t.Cleanup(func() { mock.AssertExpectations(t) })
|
||||
|
||||
return mock
|
||||
}
|
||||
Reference in New Issue
Block a user