mirror of
https://github.com/1Password/onepassword-operator.git
synced 2025-10-22 07:28:06 +00:00
Update tests to use testify mock
This commit is contained in:
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
|
|||||||
kind: CustomResourceDefinition
|
kind: CustomResourceDefinition
|
||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
controller-gen.kubebuilder.io/version: v0.13.0
|
controller-gen.kubebuilder.io/version: v0.14.0
|
||||||
name: onepassworditems.onepassword.com
|
name: onepassworditems.onepassword.com
|
||||||
spec:
|
spec:
|
||||||
group: onepassword.com
|
group: onepassword.com
|
||||||
@@ -20,14 +20,19 @@ spec:
|
|||||||
description: OnePasswordItem is the Schema for the onepassworditems API
|
description: OnePasswordItem is the Schema for the onepassworditems API
|
||||||
properties:
|
properties:
|
||||||
apiVersion:
|
apiVersion:
|
||||||
description: 'APIVersion defines the versioned schema of this representation
|
description: |-
|
||||||
of an object. Servers should convert recognized schemas to the latest
|
APIVersion defines the versioned schema of this representation of an object.
|
||||||
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
|
Servers should convert recognized schemas to the latest internal value, and
|
||||||
|
may reject unrecognized values.
|
||||||
|
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
|
||||||
type: string
|
type: string
|
||||||
kind:
|
kind:
|
||||||
description: 'Kind is a string value representing the REST resource this
|
description: |-
|
||||||
object represents. Servers may infer this from the endpoint the client
|
Kind is a string value representing the REST resource this object represents.
|
||||||
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
|
Servers may infer this from the endpoint the client submits requests to.
|
||||||
|
Cannot be updated.
|
||||||
|
In CamelCase.
|
||||||
|
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
|
||||||
type: string
|
type: string
|
||||||
metadata:
|
metadata:
|
||||||
type: object
|
type: object
|
||||||
|
@@ -2,9 +2,6 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"github.com/1Password/connect-sdk-go/onepassword"
|
|
||||||
"github.com/1Password/onepassword-operator/pkg/mocks"
|
|
||||||
op "github.com/1Password/onepassword-operator/pkg/onepassword"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
@@ -17,6 +14,7 @@ import (
|
|||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
|
|
||||||
onepasswordv1 "github.com/1Password/onepassword-operator/api/v1"
|
onepasswordv1 "github.com/1Password/onepassword-operator/api/v1"
|
||||||
|
op "github.com/1Password/onepassword-operator/pkg/onepassword"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -106,17 +104,8 @@ var _ = Describe("Deployment controller", func() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mockGetItemFunc := func() {
|
mockGetItemFunc := func() {
|
||||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
// mock GetItemByID to return test item 'item1'
|
||||||
item := onepassword.Item{}
|
mockGetItemByIDFunc.Return(item1.ToModel(), nil)
|
||||||
item.Fields = []*onepassword.ItemField{}
|
|
||||||
for k, v := range item1.Data {
|
|
||||||
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
|
|
||||||
}
|
|
||||||
item.Version = item1.Version
|
|
||||||
item.Vault.ID = vaultUUID
|
|
||||||
item.ID = uuid
|
|
||||||
return &item, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BeforeEach(func() {
|
BeforeEach(func() {
|
||||||
@@ -151,17 +140,10 @@ var _ = Describe("Deployment controller", func() {
|
|||||||
|
|
||||||
It("Should update existing K8s Secret using deployment", func() {
|
It("Should update existing K8s Secret using deployment", func() {
|
||||||
By("Updating secret")
|
By("Updating secret")
|
||||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
|
||||||
item := onepassword.Item{}
|
// mock GetItemByID to return test item 'item2'
|
||||||
item.Fields = []*onepassword.ItemField{}
|
mockGetItemByIDFunc.Return(item2.ToModel(), nil)
|
||||||
for k, v := range item2.Data {
|
|
||||||
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
|
|
||||||
}
|
|
||||||
item.Version = item2.Version
|
|
||||||
item.Vault.ID = vaultUUID
|
|
||||||
item.ID = uuid
|
|
||||||
return &item, nil
|
|
||||||
}
|
|
||||||
Eventually(func() error {
|
Eventually(func() error {
|
||||||
updatedDeployment := &appsv1.Deployment{
|
updatedDeployment := &appsv1.Deployment{
|
||||||
TypeMeta: metav1.TypeMeta{
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
@@ -2,10 +2,6 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/1Password/connect-sdk-go/onepassword"
|
|
||||||
"github.com/1Password/onepassword-operator/pkg/mocks"
|
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
@@ -16,6 +12,7 @@ import (
|
|||||||
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
"sigs.k8s.io/controller-runtime/pkg/reconcile"
|
||||||
|
|
||||||
onepasswordv1 "github.com/1Password/onepassword-operator/api/v1"
|
onepasswordv1 "github.com/1Password/onepassword-operator/api/v1"
|
||||||
|
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -32,17 +29,8 @@ var _ = Describe("OnePasswordItem controller", func() {
|
|||||||
err = k8sClient.DeleteAllOf(context.Background(), &v1.Secret{}, client.InNamespace(namespace))
|
err = k8sClient.DeleteAllOf(context.Background(), &v1.Secret{}, client.InNamespace(namespace))
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
item := item1.ToModel()
|
||||||
item := onepassword.Item{}
|
mockGetItemByIDFunc.Return(item, nil)
|
||||||
item.Fields = []*onepassword.ItemField{}
|
|
||||||
for k, v := range item1.Data {
|
|
||||||
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
|
|
||||||
}
|
|
||||||
item.Version = item1.Version
|
|
||||||
item.Vault.ID = vaultUUID
|
|
||||||
item.ID = uuid
|
|
||||||
return &item, nil
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
Context("Happy path", func() {
|
Context("Happy path", func() {
|
||||||
@@ -99,17 +87,13 @@ var _ = Describe("OnePasswordItem controller", func() {
|
|||||||
"password": []byte("##newPassword##"),
|
"password": []byte("##newPassword##"),
|
||||||
"extraField": []byte("dev"),
|
"extraField": []byte("dev"),
|
||||||
}
|
}
|
||||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
|
||||||
item := onepassword.Item{}
|
item := item2.ToModel()
|
||||||
item.Fields = []*onepassword.ItemField{}
|
for k, v := range newData {
|
||||||
for k, v := range newData {
|
item.Fields = append(item.Fields, model.ItemField{Label: k, Value: v})
|
||||||
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
|
|
||||||
}
|
|
||||||
item.Version = item1.Version + 1
|
|
||||||
item.Vault.ID = vaultUUID
|
|
||||||
item.ID = uuid
|
|
||||||
return &item, nil
|
|
||||||
}
|
}
|
||||||
|
mockGetItemByIDFunc.Return(item, nil)
|
||||||
|
|
||||||
_, err := onePasswordItemReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: key})
|
_, err := onePasswordItemReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: key})
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
@@ -178,18 +162,11 @@ var _ = Describe("OnePasswordItem controller", func() {
|
|||||||
"ice-cream-type": []byte(iceCream),
|
"ice-cream-type": []byte(iceCream),
|
||||||
}
|
}
|
||||||
|
|
||||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
item := item2.ToModel()
|
||||||
item := onepassword.Item{}
|
for k, v := range testData {
|
||||||
item.Title = "!my sECReT it3m%"
|
item.Fields = append(item.Fields, model.ItemField{Label: k, Value: v})
|
||||||
item.Fields = []*onepassword.ItemField{}
|
|
||||||
for k, v := range testData {
|
|
||||||
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
|
|
||||||
}
|
|
||||||
item.Version = item1.Version + 1
|
|
||||||
item.Vault.ID = vaultUUID
|
|
||||||
item.ID = uuid
|
|
||||||
return &item, nil
|
|
||||||
}
|
}
|
||||||
|
mockGetItemByIDFunc.Return(item, nil)
|
||||||
|
|
||||||
By("Creating a new OnePasswordItem successfully")
|
By("Creating a new OnePasswordItem successfully")
|
||||||
Expect(k8sClient.Create(ctx, toCreate)).Should(Succeed())
|
Expect(k8sClient.Create(ctx, toCreate)).Should(Succeed())
|
||||||
|
@@ -26,13 +26,12 @@ package controller
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/1Password/onepassword-operator/pkg/mocks"
|
|
||||||
|
|
||||||
. "github.com/onsi/ginkgo/v2"
|
. "github.com/onsi/ginkgo/v2"
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
|
|
||||||
@@ -45,6 +44,8 @@ import (
|
|||||||
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
||||||
|
|
||||||
onepasswordcomv1 "github.com/1Password/onepassword-operator/api/v1"
|
onepasswordcomv1 "github.com/1Password/onepassword-operator/api/v1"
|
||||||
|
"github.com/1Password/onepassword-operator/pkg/mocks"
|
||||||
|
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
||||||
//+kubebuilder:scaffold:imports
|
//+kubebuilder:scaffold:imports
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -78,8 +79,11 @@ var (
|
|||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
onePasswordItemReconciler *OnePasswordItemReconciler
|
onePasswordItemReconciler *OnePasswordItemReconciler
|
||||||
deploymentReconciler *DeploymentReconciler
|
deploymentReconciler *DeploymentReconciler
|
||||||
|
mockGetItemByIDFunc *mock.Call
|
||||||
|
|
||||||
item1 = &TestItem{
|
item1 = &TestItem{
|
||||||
|
ItemID: "nwrhuano7bcwddcviubpp4mhfq",
|
||||||
|
VaultID: "hfnjvi6aymbsnfc2xeeoheizda",
|
||||||
Name: "test-item",
|
Name: "test-item",
|
||||||
Version: 123,
|
Version: 123,
|
||||||
Path: "vaults/hfnjvi6aymbsnfc2xeeoheizda/items/nwrhuano7bcwddcviubpp4mhfq",
|
Path: "vaults/hfnjvi6aymbsnfc2xeeoheizda/items/nwrhuano7bcwddcviubpp4mhfq",
|
||||||
@@ -94,6 +98,8 @@ var (
|
|||||||
}
|
}
|
||||||
|
|
||||||
item2 = &TestItem{
|
item2 = &TestItem{
|
||||||
|
ItemID: "nwrhuano7bcwddcviubpp4mhf2",
|
||||||
|
VaultID: "hfnjvi6aymbsnfc2xeeoheizd2",
|
||||||
Name: "test-item2",
|
Name: "test-item2",
|
||||||
Path: "vaults/hfnjvi6aymbsnfc2xeeoheizd2/items/nwrhuano7bcwddcviubpp4mhf2",
|
Path: "vaults/hfnjvi6aymbsnfc2xeeoheizd2/items/nwrhuano7bcwddcviubpp4mhf2",
|
||||||
Version: 456,
|
Version: 456,
|
||||||
@@ -109,6 +115,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type TestItem struct {
|
type TestItem struct {
|
||||||
|
ItemID string
|
||||||
|
VaultID string
|
||||||
Name string
|
Name string
|
||||||
Version int
|
Version int
|
||||||
Path string
|
Path string
|
||||||
@@ -116,6 +124,20 @@ type TestItem struct {
|
|||||||
SecretData map[string][]byte
|
SecretData map[string][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ti *TestItem) ToModel() *model.Item {
|
||||||
|
item := &model.Item{}
|
||||||
|
item.Version = ti.Version
|
||||||
|
item.VaultID = ti.VaultID
|
||||||
|
item.ID = ti.ItemID
|
||||||
|
|
||||||
|
item.Fields = []model.ItemField{}
|
||||||
|
for k, v := range ti.Data {
|
||||||
|
item.Fields = append(item.Fields, model.ItemField{Label: k, Value: v})
|
||||||
|
}
|
||||||
|
|
||||||
|
return item
|
||||||
|
}
|
||||||
|
|
||||||
func TestAPIs(t *testing.T) {
|
func TestAPIs(t *testing.T) {
|
||||||
RegisterFailHandler(Fail)
|
RegisterFailHandler(Fail)
|
||||||
|
|
||||||
@@ -153,12 +175,13 @@ var _ = BeforeSuite(func() {
|
|||||||
})
|
})
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
opConnectClient := &mocks.TestClient{}
|
mockOpClient := &mocks.TestClient{}
|
||||||
|
mockGetItemByIDFunc = mockOpClient.On("GetItemByID", mock.Anything, mock.Anything)
|
||||||
|
|
||||||
onePasswordItemReconciler = &OnePasswordItemReconciler{
|
onePasswordItemReconciler = &OnePasswordItemReconciler{
|
||||||
Client: k8sManager.GetClient(),
|
Client: k8sManager.GetClient(),
|
||||||
Scheme: k8sManager.GetScheme(),
|
Scheme: k8sManager.GetScheme(),
|
||||||
OpConnectClient: opConnectClient,
|
OpClient: mockOpClient,
|
||||||
}
|
}
|
||||||
err = (onePasswordItemReconciler).SetupWithManager(k8sManager)
|
err = (onePasswordItemReconciler).SetupWithManager(k8sManager)
|
||||||
Expect(err).ToNot(HaveOccurred())
|
Expect(err).ToNot(HaveOccurred())
|
||||||
@@ -167,7 +190,7 @@ var _ = BeforeSuite(func() {
|
|||||||
deploymentReconciler = &DeploymentReconciler{
|
deploymentReconciler = &DeploymentReconciler{
|
||||||
Client: k8sManager.GetClient(),
|
Client: k8sManager.GetClient(),
|
||||||
Scheme: k8sManager.GetScheme(),
|
Scheme: k8sManager.GetScheme(),
|
||||||
OpConnectClient: opConnectClient,
|
OpClient: mockOpClient,
|
||||||
OpAnnotationRegExp: r,
|
OpAnnotationRegExp: r,
|
||||||
}
|
}
|
||||||
err = (deploymentReconciler).SetupWithManager(k8sManager)
|
err = (deploymentReconciler).SetupWithManager(k8sManager)
|
||||||
|
@@ -3,11 +3,10 @@ package kubernetessecrets
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/1Password/connect-sdk-go/onepassword"
|
|
||||||
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
@@ -21,10 +20,10 @@ func TestCreateKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
|||||||
secretName := "test-secret-name"
|
secretName := "test-secret-name"
|
||||||
namespace := "test"
|
namespace := "test"
|
||||||
|
|
||||||
item := onepassword.Item{}
|
item := model.Item{}
|
||||||
item.Fields = generateFields(5)
|
item.Fields = generateFields(5)
|
||||||
item.Version = 123
|
item.Version = 123
|
||||||
item.Vault.ID = "hfnjvi6aymbsnfc2xeeoheizda"
|
item.VaultID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||||
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
||||||
|
|
||||||
kubeClient := fake.NewClientBuilder().Build()
|
kubeClient := fake.NewClientBuilder().Build()
|
||||||
@@ -49,10 +48,10 @@ func TestKubernetesSecretFromOnePasswordItemOwnerReferences(t *testing.T) {
|
|||||||
secretName := "test-secret-name"
|
secretName := "test-secret-name"
|
||||||
namespace := "test"
|
namespace := "test"
|
||||||
|
|
||||||
item := onepassword.Item{}
|
item := model.Item{}
|
||||||
item.Fields = generateFields(5)
|
item.Fields = generateFields(5)
|
||||||
item.Version = 123
|
item.Version = 123
|
||||||
item.Vault.ID = "hfnjvi6aymbsnfc2xeeoheizda"
|
item.VaultID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||||
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
||||||
|
|
||||||
kubeClient := fake.NewClientBuilder().Build()
|
kubeClient := fake.NewClientBuilder().Build()
|
||||||
@@ -94,10 +93,10 @@ func TestUpdateKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
|||||||
secretName := "test-secret-update"
|
secretName := "test-secret-update"
|
||||||
namespace := "test"
|
namespace := "test"
|
||||||
|
|
||||||
item := onepassword.Item{}
|
item := model.Item{}
|
||||||
item.Fields = generateFields(5)
|
item.Fields = generateFields(5)
|
||||||
item.Version = 123
|
item.Version = 123
|
||||||
item.Vault.ID = "hfnjvi6aymbsnfc2xeeoheizda"
|
item.VaultID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||||
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
||||||
|
|
||||||
kubeClient := fake.NewClientBuilder().Build()
|
kubeClient := fake.NewClientBuilder().Build()
|
||||||
@@ -111,10 +110,10 @@ func TestUpdateKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Updating kubernetes secret with new item
|
// Updating kubernetes secret with new item
|
||||||
newItem := onepassword.Item{}
|
newItem := model.Item{}
|
||||||
newItem.Fields = generateFields(6)
|
newItem.Fields = generateFields(6)
|
||||||
newItem.Version = 456
|
newItem.Version = 456
|
||||||
newItem.Vault.ID = "hfnjvi6aymbsnfc2xeeoheizda"
|
newItem.VaultID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||||
newItem.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
newItem.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
||||||
err = CreateKubernetesSecretFromItem(kubeClient, secretName, namespace, &newItem, restartDeploymentAnnotation, secretLabels, secretType, nil)
|
err = CreateKubernetesSecretFromItem(kubeClient, secretName, namespace, &newItem, restartDeploymentAnnotation, secretLabels, secretType, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -147,7 +146,7 @@ func TestBuildKubernetesSecretFromOnePasswordItem(t *testing.T) {
|
|||||||
annotations := map[string]string{
|
annotations := map[string]string{
|
||||||
annotationKey: annotationValue,
|
annotationKey: annotationValue,
|
||||||
}
|
}
|
||||||
item := onepassword.Item{}
|
item := model.Item{}
|
||||||
item.Fields = generateFields(5)
|
item.Fields = generateFields(5)
|
||||||
labels := map[string]string{}
|
labels := map[string]string{}
|
||||||
secretType := ""
|
secretType := ""
|
||||||
@@ -173,10 +172,10 @@ func TestBuildKubernetesSecretFixesInvalidLabels(t *testing.T) {
|
|||||||
"annotationKey": "annotationValue",
|
"annotationKey": "annotationValue",
|
||||||
}
|
}
|
||||||
labels := map[string]string{}
|
labels := map[string]string{}
|
||||||
item := onepassword.Item{}
|
item := model.Item{}
|
||||||
secretType := ""
|
secretType := ""
|
||||||
|
|
||||||
item.Fields = []*onepassword.ItemField{
|
item.Fields = []model.ItemField{
|
||||||
{
|
{
|
||||||
Label: "label w%th invalid ch!rs-",
|
Label: "label w%th invalid ch!rs-",
|
||||||
Value: "value1",
|
Value: "value1",
|
||||||
@@ -209,10 +208,10 @@ func TestCreateKubernetesTLSSecretFromOnePasswordItem(t *testing.T) {
|
|||||||
secretName := "tls-test-secret-name"
|
secretName := "tls-test-secret-name"
|
||||||
namespace := "test"
|
namespace := "test"
|
||||||
|
|
||||||
item := onepassword.Item{}
|
item := model.Item{}
|
||||||
item.Fields = generateFields(5)
|
item.Fields = generateFields(5)
|
||||||
item.Version = 123
|
item.Version = 123
|
||||||
item.Vault.ID = "hfnjvi6aymbsnfc2xeeoheizda"
|
item.VaultID = "hfnjvi6aymbsnfc2xeeoheizda"
|
||||||
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
item.ID = "h46bb3jddvay7nxopfhvlwg35q"
|
||||||
|
|
||||||
kubeClient := fake.NewClientBuilder().Build()
|
kubeClient := fake.NewClientBuilder().Build()
|
||||||
@@ -235,13 +234,13 @@ func TestCreateKubernetesTLSSecretFromOnePasswordItem(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func compareAnnotationsToItem(annotations map[string]string, item onepassword.Item, t *testing.T) {
|
func compareAnnotationsToItem(annotations map[string]string, item model.Item, t *testing.T) {
|
||||||
actualVaultId, actualItemId, err := ParseVaultIdAndItemIdFromPath(annotations[ItemPathAnnotation])
|
actualVaultId, actualItemId, err := ParseVaultIdAndItemIdFromPath(annotations[ItemPathAnnotation])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Was unable to parse Item Path")
|
t.Errorf("Was unable to parse Item Path")
|
||||||
}
|
}
|
||||||
if actualVaultId != item.Vault.ID {
|
if actualVaultId != item.VaultID {
|
||||||
t.Errorf("Expected annotation vault id to be %v but was %v", item.Vault.ID, actualVaultId)
|
t.Errorf("Expected annotation vault id to be %v but was %v", item.VaultID, actualVaultId)
|
||||||
}
|
}
|
||||||
if actualItemId != item.ID {
|
if actualItemId != item.ID {
|
||||||
t.Errorf("Expected annotation item id to be %v but was %v", item.ID, actualItemId)
|
t.Errorf("Expected annotation item id to be %v but was %v", item.ID, actualItemId)
|
||||||
@@ -255,7 +254,7 @@ func compareAnnotationsToItem(annotations map[string]string, item onepassword.It
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func compareFields(actualFields []*onepassword.ItemField, secretData map[string][]byte, t *testing.T) {
|
func compareFields(actualFields []model.ItemField, secretData map[string][]byte, t *testing.T) {
|
||||||
for i := 0; i < len(actualFields); i++ {
|
for i := 0; i < len(actualFields); i++ {
|
||||||
value, found := secretData[actualFields[i].Label]
|
value, found := secretData[actualFields[i].Label]
|
||||||
if !found {
|
if !found {
|
||||||
@@ -267,14 +266,13 @@ func compareFields(actualFields []*onepassword.ItemField, secretData map[string]
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateFields(numToGenerate int) []*onepassword.ItemField {
|
func generateFields(numToGenerate int) []model.ItemField {
|
||||||
fields := []*onepassword.ItemField{}
|
fields := []model.ItemField{}
|
||||||
for i := 0; i < numToGenerate; i++ {
|
for i := 0; i < numToGenerate; i++ {
|
||||||
field := onepassword.ItemField{
|
fields = append(fields, model.ItemField{
|
||||||
Label: "key" + fmt.Sprint(i),
|
Label: "key" + fmt.Sprint(i),
|
||||||
Value: "value" + fmt.Sprint(i),
|
Value: "value" + fmt.Sprint(i),
|
||||||
}
|
})
|
||||||
fields = append(fields, &field)
|
|
||||||
}
|
}
|
||||||
return fields
|
return fields
|
||||||
}
|
}
|
||||||
|
@@ -1,151 +1,37 @@
|
|||||||
package mocks
|
package mocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/1Password/connect-sdk-go/onepassword"
|
"github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
|
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TestClient struct {
|
type TestClient struct {
|
||||||
GetVaultsFunc func() ([]onepassword.Vault, error)
|
mock.Mock
|
||||||
GetVaultsByTitleFunc func(title string) ([]onepassword.Vault, error)
|
|
||||||
GetVaultFunc func(uuid string) (*onepassword.Vault, error)
|
|
||||||
GetVaultByUUIDFunc func(uuid string) (*onepassword.Vault, error)
|
|
||||||
GetVaultByTitleFunc func(title string) (*onepassword.Vault, error)
|
|
||||||
GetItemFunc func(itemQuery string, vaultQuery string) (*onepassword.Item, error)
|
|
||||||
GetItemByUUIDFunc func(uuid string, vaultQuery string) (*onepassword.Item, error)
|
|
||||||
GetItemByTitleFunc func(title string, vaultQuery string) (*onepassword.Item, error)
|
|
||||||
GetItemsFunc func(vaultQuery string) ([]onepassword.Item, error)
|
|
||||||
GetItemsByTitleFunc func(title string, vaultQuery string) ([]onepassword.Item, error)
|
|
||||||
CreateItemFunc func(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error)
|
|
||||||
UpdateItemFunc func(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error)
|
|
||||||
DeleteItemFunc func(item *onepassword.Item, vaultQuery string) error
|
|
||||||
DeleteItemByIDFunc func(itemUUID string, vaultQuery string) error
|
|
||||||
DeleteItemByTitleFunc func(title string, vaultQuery string) error
|
|
||||||
GetFilesFunc func(itemQuery string, vaultQuery string) ([]onepassword.File, error)
|
|
||||||
GetFileFunc func(uuid string, itemQuery string, vaultQuery string) (*onepassword.File, error)
|
|
||||||
GetFileContentFunc func(file *onepassword.File) ([]byte, error)
|
|
||||||
DownloadFileFunc func(file *onepassword.File, targetDirectory string, overwrite bool) (string, error)
|
|
||||||
LoadStructFromItemByUUIDFunc func(config interface{}, itemUUID string, vaultQuery string) error
|
|
||||||
LoadStructFromItemByTitleFunc func(config interface{}, itemTitle string, vaultQuery string) error
|
|
||||||
LoadStructFromItemFunc func(config interface{}, itemQuery string, vaultQuery string) error
|
|
||||||
LoadStructFunc func(config interface{}) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
func (tc *TestClient) GetItemByID(vaultID, itemID string) (*model.Item, error) {
|
||||||
DoGetVaultsFunc func() ([]onepassword.Vault, error)
|
args := tc.Called(vaultID, itemID)
|
||||||
DoGetVaultsByTitleFunc func(title string) ([]onepassword.Vault, error)
|
if args.Get(0) == nil {
|
||||||
DoGetVaultFunc func(uuid string) (*onepassword.Vault, error)
|
return nil, args.Error(1)
|
||||||
DoGetVaultByUUIDFunc func(uuid string) (*onepassword.Vault, error)
|
}
|
||||||
DoGetVaultByTitleFunc func(title string) (*onepassword.Vault, error)
|
return args.Get(0).(*model.Item), args.Error(1)
|
||||||
DoGetItemFunc func(itemQuery string, vaultQuery string) (*onepassword.Item, error)
|
|
||||||
DoGetItemByUUIDFunc func(uuid string, vaultQuery string) (*onepassword.Item, error)
|
|
||||||
DoGetItemByTitleFunc func(title string, vaultQuery string) (*onepassword.Item, error)
|
|
||||||
DoGetItemsFunc func(vaultQuery string) ([]onepassword.Item, error)
|
|
||||||
DoGetItemsByTitleFunc func(title string, vaultQuery string) ([]onepassword.Item, error)
|
|
||||||
DoCreateItemFunc func(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error)
|
|
||||||
DoUpdateItemFunc func(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error)
|
|
||||||
DoDeleteItemFunc func(item *onepassword.Item, vaultQuery string) error
|
|
||||||
DoDeleteItemByIDFunc func(itemUUID string, vaultQuery string) error
|
|
||||||
DoDeleteItemByTitleFunc func(title string, vaultQuery string) error
|
|
||||||
DoGetFilesFunc func(itemQuery string, vaultQuery string) ([]onepassword.File, error)
|
|
||||||
DoGetFileFunc func(uuid string, itemQuery string, vaultQuery string) (*onepassword.File, error)
|
|
||||||
DoGetFileContentFunc func(file *onepassword.File) ([]byte, error)
|
|
||||||
DoDownloadFileFunc func(file *onepassword.File, targetDirectory string, overwrite bool) (string, error)
|
|
||||||
DoLoadStructFromItemByUUIDFunc func(config interface{}, itemUUID string, vaultQuery string) error
|
|
||||||
DoLoadStructFromItemByTitleFunc func(config interface{}, itemTitle string, vaultQuery string) error
|
|
||||||
DoLoadStructFromItemFunc func(config interface{}, itemQuery string, vaultQuery string) error
|
|
||||||
DoLoadStructFunc func(config interface{}) error
|
|
||||||
)
|
|
||||||
|
|
||||||
// Do is the mock client's `Do` func
|
|
||||||
|
|
||||||
func (m *TestClient) GetVaults() ([]onepassword.Vault, error) {
|
|
||||||
return DoGetVaultsFunc()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *TestClient) GetVaultsByTitle(title string) ([]onepassword.Vault, error) {
|
func (tc *TestClient) GetItemsByTitle(vaultID, itemTitle string) ([]model.Item, error) {
|
||||||
return DoGetVaultsByTitleFunc(title)
|
args := tc.Called(vaultID, itemTitle)
|
||||||
|
return args.Get(0).([]model.Item), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *TestClient) GetVault(vaultQuery string) (*onepassword.Vault, error) {
|
func (tc *TestClient) GetFileContent(vaultID, itemID, fileID string) ([]byte, error) {
|
||||||
return DoGetVaultFunc(vaultQuery)
|
args := tc.Called(vaultID, itemID, fileID)
|
||||||
|
if args.Get(0) == nil {
|
||||||
|
return nil, args.Error(1)
|
||||||
|
}
|
||||||
|
return args.Get(0).([]byte), args.Error(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *TestClient) GetVaultByUUID(uuid string) (*onepassword.Vault, error) {
|
func (tc *TestClient) GetVaultsByTitle(title string) ([]model.Vault, error) {
|
||||||
return DoGetVaultByUUIDFunc(uuid)
|
args := tc.Called(title)
|
||||||
}
|
return args.Get(0).([]model.Vault), args.Error(1)
|
||||||
|
|
||||||
func (m *TestClient) GetVaultByTitle(title string) (*onepassword.Vault, error) {
|
|
||||||
return DoGetVaultByTitleFunc(title)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) GetItem(itemQuery string, vaultQuery string) (*onepassword.Item, error) {
|
|
||||||
return DoGetItemFunc(itemQuery, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) GetItemByUUID(uuid string, vaultQuery string) (*onepassword.Item, error) {
|
|
||||||
return DoGetItemByUUIDFunc(uuid, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) GetItemByTitle(title string, vaultQuery string) (*onepassword.Item, error) {
|
|
||||||
return DoGetItemByTitleFunc(title, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) GetItems(vaultQuery string) ([]onepassword.Item, error) {
|
|
||||||
return DoGetItemsFunc(vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) GetItemsByTitle(title string, vaultQuery string) ([]onepassword.Item, error) {
|
|
||||||
return DoGetItemsByTitleFunc(title, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) CreateItem(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error) {
|
|
||||||
return DoCreateItemFunc(item, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) UpdateItem(item *onepassword.Item, vaultQuery string) (*onepassword.Item, error) {
|
|
||||||
return DoUpdateItemFunc(item, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) DeleteItem(item *onepassword.Item, vaultQuery string) error {
|
|
||||||
return DoDeleteItemFunc(item, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) DeleteItemByID(itemUUID string, vaultQuery string) error {
|
|
||||||
return DoDeleteItemByIDFunc(itemUUID, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) DeleteItemByTitle(title string, vaultQuery string) error {
|
|
||||||
return DoDeleteItemByTitleFunc(title, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) GetFiles(itemQuery string, vaultQuery string) ([]onepassword.File, error) {
|
|
||||||
return DoGetFilesFunc(itemQuery, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) GetFile(uuid string, itemQuery string, vaultQuery string) (*onepassword.File, error) {
|
|
||||||
return DoGetFileFunc(uuid, itemQuery, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) GetFileContent(file *onepassword.File) ([]byte, error) {
|
|
||||||
return DoGetFileContentFunc(file)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) DownloadFile(file *onepassword.File, targetDirectory string, overwrite bool) (string, error) {
|
|
||||||
return DoDownloadFileFunc(file, targetDirectory, overwrite)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) LoadStructFromItemByUUID(config interface{}, itemUUID string, vaultQuery string) error {
|
|
||||||
return DoLoadStructFromItemByUUIDFunc(config, itemUUID, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) LoadStructFromItemByTitle(config interface{}, itemTitle string, vaultQuery string) error {
|
|
||||||
return DoLoadStructFromItemByTitleFunc(config, itemTitle, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) LoadStructFromItem(config interface{}, itemQuery string, vaultQuery string) error {
|
|
||||||
return DoLoadStructFromItemFunc(config, itemQuery, vaultQuery)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m *TestClient) LoadStruct(config interface{}) error {
|
|
||||||
return DoLoadStructFunc(config)
|
|
||||||
}
|
}
|
||||||
|
@@ -4,11 +4,14 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/mock"
|
||||||
|
|
||||||
"github.com/1Password/onepassword-operator/pkg/mocks"
|
"github.com/1Password/onepassword-operator/pkg/mocks"
|
||||||
|
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
|
||||||
|
|
||||||
"github.com/1Password/connect-sdk-go/onepassword"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
appsv1 "k8s.io/api/apps/v1"
|
appsv1 "k8s.io/api/apps/v1"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
errors2 "k8s.io/apimachinery/pkg/api/errors"
|
errors2 "k8s.io/apimachinery/pkg/api/errors"
|
||||||
@@ -802,19 +805,20 @@ func TestUpdateSecretHandler(t *testing.T) {
|
|||||||
// Create a fake client to mock API calls.
|
// Create a fake client to mock API calls.
|
||||||
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objs...).Build()
|
cl := fake.NewClientBuilder().WithScheme(s).WithRuntimeObjects(objs...).Build()
|
||||||
|
|
||||||
opConnectClient := &mocks.TestClient{}
|
mockOpClient := &mocks.TestClient{}
|
||||||
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
mockOpClient.On("GetItemByID", mock.Anything, mock.Anything).Return(createItem(), nil)
|
||||||
|
//mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
|
||||||
item := onepassword.Item{}
|
//
|
||||||
item.Fields = generateFields(testData.opItem["username"], testData.opItem["password"])
|
// item := onepassword.Item{}
|
||||||
item.Version = itemVersion
|
// item.Fields = generateFields(testData.opItem["username"], testData.opItem["password"])
|
||||||
item.Vault.ID = vaultUUID
|
// item.Version = itemVersion
|
||||||
item.ID = uuid
|
// item.Vault.ID = vaultUUID
|
||||||
return &item, nil
|
// item.ID = uuid
|
||||||
}
|
// return &item, nil
|
||||||
|
//}
|
||||||
h := &SecretUpdateHandler{
|
h := &SecretUpdateHandler{
|
||||||
client: cl,
|
client: cl,
|
||||||
opConnectClient: opConnectClient,
|
opClient: mockOpClient,
|
||||||
shouldAutoRestartDeploymentsGlobal: testData.globalAutoRestartEnabled,
|
shouldAutoRestartDeploymentsGlobal: testData.globalAutoRestartEnabled,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -879,16 +883,23 @@ func TestIsUpdatedSecret(t *testing.T) {
|
|||||||
assert.True(t, isUpdatedSecret(secretName, updatedSecrets))
|
assert.True(t, isUpdatedSecret(secretName, updatedSecrets))
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateFields(username, password string) []*onepassword.ItemField {
|
func createItem() *model.Item {
|
||||||
fields := []*onepassword.ItemField{
|
return &model.Item{
|
||||||
{
|
ID: itemId,
|
||||||
Label: "username",
|
VaultID: vaultId,
|
||||||
Value: username,
|
Version: itemVersion,
|
||||||
},
|
Tags: []string{"tag1", "tag2"},
|
||||||
{
|
Fields: []model.ItemField{
|
||||||
Label: "password",
|
{
|
||||||
Value: password,
|
Label: "username",
|
||||||
|
Value: username,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Label: "password",
|
||||||
|
Value: password,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
Files: []model.File{},
|
||||||
|
CreatedAt: time.Now(),
|
||||||
}
|
}
|
||||||
return fields
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user