5ecfc79e14
* Provisioning: Add Connection resource * adding some more integration tests * updating openapi snapshot, linting * generating FE code, fixing issue in unit tests * addressing comments * addressing comments * adding more integration tests * fixing rebase issues * removing linting exception * addressing comments: improving validation and tests * adding Connection URL at mutation time, updating tests accordingly * linting
414 lines
14 KiB
Go
414 lines
14 KiB
Go
package provisioning
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/grafana/grafana/pkg/util/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
)
|
|
|
|
func TestIntegrationProvisioning_ConnectionCRUDL(t *testing.T) {
|
|
testutil.SkipIntegrationTestInShortMode(t)
|
|
|
|
helper := runGrafana(t)
|
|
createOptions := metav1.CreateOptions{FieldValidation: "Strict"}
|
|
ctx := context.Background()
|
|
|
|
t.Run("should perform CRUDL requests on connection", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "github",
|
|
"github": map[string]any{
|
|
"appID": "123456",
|
|
"installationID": "454545",
|
|
},
|
|
},
|
|
"secure": map[string]any{
|
|
"privateKey": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
// CREATE
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.NoError(t, err, "failed to create resource")
|
|
|
|
// READ
|
|
output, err := helper.Connections.Resource.Get(ctx, "connection", metav1.GetOptions{})
|
|
require.NoError(t, err, "failed to read back resource")
|
|
assert.Equal(t, "connection", output.GetName(), "name should be equal")
|
|
assert.Equal(t, "default", output.GetNamespace(), "namespace should be equal")
|
|
spec := output.Object["spec"].(map[string]any)
|
|
assert.Equal(t, "github", spec["type"], "type should be equal")
|
|
assert.Equal(t, "https://github.com/settings/installations/454545", spec["url"], "url should be equal")
|
|
require.Contains(t, spec, "github")
|
|
githubInfo := spec["github"].(map[string]any)
|
|
assert.Equal(t, "123456", githubInfo["appID"], "appID should be equal")
|
|
assert.Equal(t, "454545", githubInfo["installationID"], "installationID should be equal")
|
|
require.Contains(t, output.Object, "secure", "object should contain secure")
|
|
assert.Contains(t, output.Object["secure"], "privateKey", "secure should contain PrivateKey")
|
|
|
|
// LIST
|
|
list, err := helper.Connections.Resource.List(ctx, metav1.ListOptions{})
|
|
require.NoError(t, err, "failed to list resource")
|
|
assert.Equal(t, 1, len(list.Items), "should have one connection")
|
|
assert.Equal(t, "connection", list.Items[0].GetName(), "name should be equal")
|
|
|
|
// UPDATE
|
|
updatedConnection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "github",
|
|
"github": map[string]any{
|
|
"appID": "456789",
|
|
"installationID": "454545",
|
|
},
|
|
},
|
|
"secure": map[string]any{
|
|
"privateKey": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
res, err := helper.Connections.Resource.Update(ctx, updatedConnection, metav1.UpdateOptions{})
|
|
require.NoError(t, err, "failed to update resource")
|
|
spec = res.Object["spec"].(map[string]any)
|
|
require.Contains(t, spec, "github")
|
|
githubInfo = spec["github"].(map[string]any)
|
|
assert.Equal(t, "456789", githubInfo["appID"], "appID should be updated")
|
|
|
|
// DELETE
|
|
require.NoError(t, helper.Connections.Resource.Delete(ctx, "connection", metav1.DeleteOptions{}), "failed to delete resource")
|
|
list, err = helper.Connections.Resource.List(ctx, metav1.ListOptions{})
|
|
require.NoError(t, err, "failed to list resources")
|
|
assert.Equal(t, 0, len(list.Items), "should have no connections")
|
|
})
|
|
|
|
t.Run("viewer can't create or get connection", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "github",
|
|
"github": map[string]any{
|
|
"appID": "123456",
|
|
"installationID": "454545",
|
|
},
|
|
},
|
|
"secure": map[string]any{
|
|
"privateKey": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
|
|
result := helper.ViewerREST.Post().
|
|
Namespace("default").
|
|
Resource("connections").
|
|
Body(connection).
|
|
Do(t.Context())
|
|
|
|
require.NotNil(t, result.Error())
|
|
err := &k8serrors.StatusError{}
|
|
require.True(t, errors.As(result.Error(), &err))
|
|
assert.Equal(t, metav1.StatusReasonForbidden, err.Status().Reason)
|
|
assert.Contains(t, err.Status().Message, "User \"viewer\" cannot create resource \"connections\"")
|
|
assert.Contains(t, err.Status().Message, "admin role is required")
|
|
|
|
result = helper.ViewerREST.Get().
|
|
Namespace("default").
|
|
Resource("connections").
|
|
Name("connection").
|
|
Do(t.Context())
|
|
require.NotNil(t, result.Error())
|
|
err = &k8serrors.StatusError{}
|
|
require.True(t, errors.As(result.Error(), &err))
|
|
assert.Equal(t, metav1.StatusReasonForbidden, err.Status().Reason)
|
|
assert.Contains(t, err.Status().Message, "User \"viewer\" cannot get resource \"connections\"")
|
|
assert.Contains(t, err.Status().Message, "admin role is required")
|
|
})
|
|
}
|
|
|
|
func TestIntegrationProvisioning_ConnectionValidation(t *testing.T) {
|
|
helper := runGrafana(t)
|
|
createOptions := metav1.CreateOptions{FieldValidation: "Strict"}
|
|
ctx := context.Background()
|
|
|
|
t.Run("should fail when type is empty", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "",
|
|
},
|
|
"secure": map[string]any{
|
|
"privateKey": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "type must be specified")
|
|
})
|
|
|
|
t.Run("should fail when type is invalid", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "some-invalid-type",
|
|
},
|
|
"secure": map[string]any{
|
|
"privateKey": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "spec.type: Unsupported value: \"some-invalid-type\"")
|
|
})
|
|
|
|
t.Run("should fail when type is github but 'github' field is not there", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "github",
|
|
},
|
|
"secure": map[string]any{
|
|
"privateKey": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "github info must be specified for GitHub connection")
|
|
})
|
|
|
|
t.Run("should fail when type is github but private key is not there", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "github",
|
|
"github": map[string]any{
|
|
"appID": "123456",
|
|
"installationID": "454545",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "privateKey must be specified for GitHub connection")
|
|
})
|
|
|
|
t.Run("should fail when type is github but a client Secret is specified", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "github",
|
|
"github": map[string]any{
|
|
"appID": "123456",
|
|
"installationID": "454545",
|
|
},
|
|
},
|
|
"secure": map[string]any{
|
|
"privateKey": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
"clientSecret": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "clientSecret is forbidden in GitHub connection")
|
|
})
|
|
|
|
t.Run("should fail when type is bitbucket but 'bitbucket' field is not there", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "bitbucket",
|
|
},
|
|
"secure": map[string]any{
|
|
"clientSecret": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "bitbucket info must be specified in Bitbucket connection")
|
|
})
|
|
|
|
t.Run("should fail when type is bitbucket but client secret is not there", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "bitbucket",
|
|
"bitbucket": map[string]any{
|
|
"clientID": "123456",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "clientSecret must be specified for Bitbucket connection")
|
|
})
|
|
|
|
t.Run("should fail when type is bitbucket but a private key is specified", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "bitbucket",
|
|
"bitbucket": map[string]any{
|
|
"clientID": "123456",
|
|
},
|
|
},
|
|
"secure": map[string]any{
|
|
"privateKey": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
"clientSecret": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "privateKey is forbidden in Bitbucket connection")
|
|
})
|
|
|
|
t.Run("should fail when type is gitlab but 'gitlab' field is not there", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "gitlab",
|
|
},
|
|
"secure": map[string]any{
|
|
"clientSecret": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "gitlab info must be specified in Gitlab connection")
|
|
})
|
|
|
|
t.Run("should fail when type is gitlab but client secret is not there", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "gitlab",
|
|
"gitlab": map[string]any{
|
|
"clientID": "123456",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "clientSecret must be specified for Gitlab connection")
|
|
})
|
|
|
|
t.Run("should fail when type is gitlab but a private key is specified", func(t *testing.T) {
|
|
connection := &unstructured.Unstructured{Object: map[string]any{
|
|
"apiVersion": "provisioning.grafana.app/v0alpha1",
|
|
"kind": "Connection",
|
|
"metadata": map[string]any{
|
|
"name": "connection",
|
|
"namespace": "default",
|
|
},
|
|
"spec": map[string]any{
|
|
"type": "gitlab",
|
|
"gitlab": map[string]any{
|
|
"clientID": "123456",
|
|
},
|
|
},
|
|
"secure": map[string]any{
|
|
"privateKey": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
"clientSecret": map[string]any{
|
|
"create": "someSecret",
|
|
},
|
|
},
|
|
}}
|
|
_, err := helper.Connections.Resource.Create(ctx, connection, createOptions)
|
|
require.Error(t, err, "failed to create resource")
|
|
assert.Contains(t, err.Error(), "privateKey is forbidden in Gitlab connection")
|
|
})
|
|
}
|