Skip to content

Commit f88ea66

Browse files
Update tests to use testify mock
1 parent 1498c22 commit f88ea66

File tree

7 files changed

+141
-259
lines changed

7 files changed

+141
-259
lines changed

config/crd/bases/onepassword.com_onepassworditems.yaml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.13.0
6+
controller-gen.kubebuilder.io/version: v0.14.0
77
name: onepassworditems.onepassword.com
88
spec:
99
group: onepassword.com
@@ -20,14 +20,19 @@ spec:
2020
description: OnePasswordItem is the Schema for the onepassworditems API
2121
properties:
2222
apiVersion:
23-
description: 'APIVersion defines the versioned schema of this representation
24-
of an object. Servers should convert recognized schemas to the latest
25-
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
23+
description: |-
24+
APIVersion defines the versioned schema of this representation of an object.
25+
Servers should convert recognized schemas to the latest internal value, and
26+
may reject unrecognized values.
27+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
2628
type: string
2729
kind:
28-
description: 'Kind is a string value representing the REST resource this
29-
object represents. Servers may infer this from the endpoint the client
30-
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
30+
description: |-
31+
Kind is a string value representing the REST resource this object represents.
32+
Servers may infer this from the endpoint the client submits requests to.
33+
Cannot be updated.
34+
In CamelCase.
35+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
3136
type: string
3237
metadata:
3338
type: object

internal/controller/deployment_controller_test.go

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ package controller
22

33
import (
44
"context"
5-
"github.com/1Password/connect-sdk-go/onepassword"
6-
"github.com/1Password/onepassword-operator/pkg/mocks"
7-
op "github.com/1Password/onepassword-operator/pkg/onepassword"
85
"time"
96

107
. "github.com/onsi/ginkgo/v2"
@@ -17,6 +14,7 @@ import (
1714
"sigs.k8s.io/controller-runtime/pkg/client"
1815

1916
onepasswordv1 "github.com/1Password/onepassword-operator/api/v1"
17+
op "github.com/1Password/onepassword-operator/pkg/onepassword"
2018
)
2119

2220
const (
@@ -106,17 +104,8 @@ var _ = Describe("Deployment controller", func() {
106104
}
107105

108106
mockGetItemFunc := func() {
109-
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
110-
item := onepassword.Item{}
111-
item.Fields = []*onepassword.ItemField{}
112-
for k, v := range item1.Data {
113-
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
114-
}
115-
item.Version = item1.Version
116-
item.Vault.ID = vaultUUID
117-
item.ID = uuid
118-
return &item, nil
119-
}
107+
// mock GetItemByID to return test item 'item1'
108+
mockGetItemByIDFunc.Return(item1.ToModel(), nil)
120109
}
121110

122111
BeforeEach(func() {
@@ -151,17 +140,10 @@ var _ = Describe("Deployment controller", func() {
151140

152141
It("Should update existing K8s Secret using deployment", func() {
153142
By("Updating secret")
154-
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
155-
item := onepassword.Item{}
156-
item.Fields = []*onepassword.ItemField{}
157-
for k, v := range item2.Data {
158-
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
159-
}
160-
item.Version = item2.Version
161-
item.Vault.ID = vaultUUID
162-
item.ID = uuid
163-
return &item, nil
164-
}
143+
144+
// mock GetItemByID to return test item 'item2'
145+
mockGetItemByIDFunc.Return(item2.ToModel(), nil)
146+
165147
Eventually(func() error {
166148
updatedDeployment := &appsv1.Deployment{
167149
TypeMeta: metav1.TypeMeta{

internal/controller/onepassworditem_controller_test.go

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ package controller
22

33
import (
44
"context"
5-
6-
"github.com/1Password/connect-sdk-go/onepassword"
7-
"github.com/1Password/onepassword-operator/pkg/mocks"
8-
95
. "github.com/onsi/ginkgo/v2"
106
. "github.com/onsi/gomega"
117

@@ -16,6 +12,7 @@ import (
1612
"sigs.k8s.io/controller-runtime/pkg/reconcile"
1713

1814
onepasswordv1 "github.com/1Password/onepassword-operator/api/v1"
15+
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
1916
)
2017

2118
const (
@@ -32,17 +29,8 @@ var _ = Describe("OnePasswordItem controller", func() {
3229
err = k8sClient.DeleteAllOf(context.Background(), &v1.Secret{}, client.InNamespace(namespace))
3330
Expect(err).ToNot(HaveOccurred())
3431

35-
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
36-
item := onepassword.Item{}
37-
item.Fields = []*onepassword.ItemField{}
38-
for k, v := range item1.Data {
39-
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
40-
}
41-
item.Version = item1.Version
42-
item.Vault.ID = vaultUUID
43-
item.ID = uuid
44-
return &item, nil
45-
}
32+
item := item1.ToModel()
33+
mockGetItemByIDFunc.Return(item, nil)
4634
})
4735

4836
Context("Happy path", func() {
@@ -99,17 +87,13 @@ var _ = Describe("OnePasswordItem controller", func() {
9987
"password": []byte("##newPassword##"),
10088
"extraField": []byte("dev"),
10189
}
102-
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
103-
item := onepassword.Item{}
104-
item.Fields = []*onepassword.ItemField{}
105-
for k, v := range newData {
106-
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
107-
}
108-
item.Version = item1.Version + 1
109-
item.Vault.ID = vaultUUID
110-
item.ID = uuid
111-
return &item, nil
90+
91+
item := item2.ToModel()
92+
for k, v := range newData {
93+
item.Fields = append(item.Fields, model.ItemField{Label: k, Value: v})
11294
}
95+
mockGetItemByIDFunc.Return(item, nil)
96+
11397
_, err := onePasswordItemReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: key})
11498
Expect(err).ToNot(HaveOccurred())
11599

@@ -178,18 +162,11 @@ var _ = Describe("OnePasswordItem controller", func() {
178162
"ice-cream-type": []byte(iceCream),
179163
}
180164

181-
mocks.DoGetItemFunc = func(uuid string, vaultUUID string) (*onepassword.Item, error) {
182-
item := onepassword.Item{}
183-
item.Title = "!my sECReT it3m%"
184-
item.Fields = []*onepassword.ItemField{}
185-
for k, v := range testData {
186-
item.Fields = append(item.Fields, &onepassword.ItemField{Label: k, Value: v})
187-
}
188-
item.Version = item1.Version + 1
189-
item.Vault.ID = vaultUUID
190-
item.ID = uuid
191-
return &item, nil
165+
item := item2.ToModel()
166+
for k, v := range testData {
167+
item.Fields = append(item.Fields, model.ItemField{Label: k, Value: v})
192168
}
169+
mockGetItemByIDFunc.Return(item, nil)
193170

194171
By("Creating a new OnePasswordItem successfully")
195172
Expect(k8sClient.Create(ctx, toCreate)).Should(Succeed())

internal/controller/suite_test.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@ package controller
2626

2727
import (
2828
"context"
29+
"github.com/stretchr/testify/mock"
2930
"path/filepath"
3031
"regexp"
3132
"testing"
3233
"time"
3334

34-
"github.com/1Password/onepassword-operator/pkg/mocks"
35-
3635
. "github.com/onsi/ginkgo/v2"
3736
. "github.com/onsi/gomega"
3837

@@ -45,6 +44,8 @@ import (
4544
"sigs.k8s.io/controller-runtime/pkg/log/zap"
4645

4746
onepasswordcomv1 "github.com/1Password/onepassword-operator/api/v1"
47+
"github.com/1Password/onepassword-operator/pkg/mocks"
48+
"github.com/1Password/onepassword-operator/pkg/onepassword/model"
4849
//+kubebuilder:scaffold:imports
4950
)
5051

@@ -78,8 +79,11 @@ var (
7879
cancel context.CancelFunc
7980
onePasswordItemReconciler *OnePasswordItemReconciler
8081
deploymentReconciler *DeploymentReconciler
82+
mockGetItemByIDFunc *mock.Call
8183

8284
item1 = &TestItem{
85+
ItemID: "nwrhuano7bcwddcviubpp4mhfq",
86+
VaultID: "hfnjvi6aymbsnfc2xeeoheizda",
8387
Name: "test-item",
8488
Version: 123,
8589
Path: "vaults/hfnjvi6aymbsnfc2xeeoheizda/items/nwrhuano7bcwddcviubpp4mhfq",
@@ -94,6 +98,8 @@ var (
9498
}
9599

96100
item2 = &TestItem{
101+
ItemID: "nwrhuano7bcwddcviubpp4mhf2",
102+
VaultID: "hfnjvi6aymbsnfc2xeeoheizd2",
97103
Name: "test-item2",
98104
Path: "vaults/hfnjvi6aymbsnfc2xeeoheizd2/items/nwrhuano7bcwddcviubpp4mhf2",
99105
Version: 456,
@@ -109,13 +115,29 @@ var (
109115
)
110116

111117
type TestItem struct {
118+
ItemID string
119+
VaultID string
112120
Name string
113121
Version int
114122
Path string
115123
Data map[string]string
116124
SecretData map[string][]byte
117125
}
118126

127+
func (ti *TestItem) ToModel() *model.Item {
128+
item := &model.Item{}
129+
item.Version = ti.Version
130+
item.VaultID = ti.VaultID
131+
item.ID = ti.ItemID
132+
133+
item.Fields = []model.ItemField{}
134+
for k, v := range ti.Data {
135+
item.Fields = append(item.Fields, model.ItemField{Label: k, Value: v})
136+
}
137+
138+
return item
139+
}
140+
119141
func TestAPIs(t *testing.T) {
120142
RegisterFailHandler(Fail)
121143

@@ -153,12 +175,13 @@ var _ = BeforeSuite(func() {
153175
})
154176
Expect(err).ToNot(HaveOccurred())
155177

156-
opConnectClient := &mocks.TestClient{}
178+
mockOpClient := &mocks.TestClient{}
179+
mockGetItemByIDFunc = mockOpClient.On("GetItemByID", mock.Anything, mock.Anything)
157180

158181
onePasswordItemReconciler = &OnePasswordItemReconciler{
159-
Client: k8sManager.GetClient(),
160-
Scheme: k8sManager.GetScheme(),
161-
OpConnectClient: opConnectClient,
182+
Client: k8sManager.GetClient(),
183+
Scheme: k8sManager.GetScheme(),
184+
OpClient: mockOpClient,
162185
}
163186
err = (onePasswordItemReconciler).SetupWithManager(k8sManager)
164187
Expect(err).ToNot(HaveOccurred())
@@ -167,7 +190,7 @@ var _ = BeforeSuite(func() {
167190
deploymentReconciler = &DeploymentReconciler{
168191
Client: k8sManager.GetClient(),
169192
Scheme: k8sManager.GetScheme(),
170-
OpConnectClient: opConnectClient,
193+
OpClient: mockOpClient,
171194
OpAnnotationRegExp: r,
172195
}
173196
err = (deploymentReconciler).SetupWithManager(k8sManager)

0 commit comments

Comments
 (0)