Skip to content

Commit

Permalink
require Path in OneOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Mendoza committed Apr 25, 2024
1 parent 48a0f9f commit 9ce1d98
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 165 deletions.
74 changes: 37 additions & 37 deletions cloud/scope/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ func TestValidateMachineScopeParams(t *testing.T) {
func TestMachineScopeAddFinalizer(t *testing.T) {
t.Parallel()

NewSuite(t, mock.MockK8sClient{}).Run(Paths(
NewSuite(t, mock.MockK8sClient{}).Run(
Call("scheme 1", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
return s
})
}),
Either(
Call("scheme 2", func(ctx context.Context, mck Mock) {
OneOf(
Path(Call("scheme 2", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
return s
})
}),
Result("has finalizer", func(ctx context.Context, mck Mock) {
})),
Path(Result("has finalizer", func(ctx context.Context, mck Mock) {
mScope, err := NewMachineScope(ctx, "token", MachineScopeParams{
Client: mck.K8sClient,
Cluster: &clusterv1.Cluster{},
Expand All @@ -144,9 +144,9 @@ func TestMachineScopeAddFinalizer(t *testing.T) {
assert.NoError(t, mScope.AddFinalizer(ctx))
require.Len(t, mScope.LinodeMachine.Finalizers, 1)
assert.Equal(t, mScope.LinodeMachine.Finalizers[0], infrav1alpha1.GroupVersion.String())
}),
})),
),
Either(
OneOf(
Path(
Call("able to patch", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Patch(ctx, gomock.Any(), gomock.Any()).Return(nil)
Expand Down Expand Up @@ -183,20 +183,20 @@ func TestMachineScopeAddFinalizer(t *testing.T) {
}),
),
),
))
)
}

func TestNewMachineScope(t *testing.T) {
t.Parallel()

NewSuite(t, mock.MockK8sClient{}).Run(Paths(
Either(
Result("invalid params", func(ctx context.Context, mck Mock) {
NewSuite(t, mock.MockK8sClient{}).Run(
OneOf(
Path(Result("invalid params", func(ctx context.Context, mck Mock) {
mScope, err := NewMachineScope(ctx, "token", MachineScopeParams{})
require.ErrorContains(t, err, "is required")
assert.Nil(t, mScope)
}),
Result("no token", func(ctx context.Context, mck Mock) {
})),
Path(Result("no token", func(ctx context.Context, mck Mock) {
mScope, err := NewMachineScope(ctx, "", MachineScopeParams{
Client: mck.K8sClient,
Cluster: &clusterv1.Cluster{},
Expand All @@ -206,7 +206,7 @@ func TestNewMachineScope(t *testing.T) {
})
require.ErrorContains(t, err, "failed to create linode client")
assert.Nil(t, mScope)
}),
})),
Path(
Call("no secret", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(ctx, gomock.Any(), gomock.Any()).Return(apierrors.NewNotFound(schema.GroupResource{}, "example"))
Expand All @@ -231,14 +231,14 @@ func TestNewMachineScope(t *testing.T) {
}),
),
),
Either(
Call("valid scheme", func(ctx context.Context, mck Mock) {
OneOf(
Path(Call("valid scheme", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Scheme().DoAndReturn(func() *runtime.Scheme {
s := runtime.NewScheme()
infrav1alpha1.AddToScheme(s)
return s
})
}),
})),
Path(
Call("invalid scheme", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Scheme().Return(runtime.NewScheme())
Expand All @@ -256,8 +256,8 @@ func TestNewMachineScope(t *testing.T) {
}),
),
),
Either(
Call("credentials in secret", func(ctx context.Context, mck Mock) {
OneOf(
Path(Call("credentials in secret", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(ctx, gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, key client.ObjectKey, obj *corev1.Secret, opts ...client.GetOption) error {
*obj = corev1.Secret{
Expand All @@ -267,8 +267,8 @@ func TestNewMachineScope(t *testing.T) {
}
return nil
})
}),
Result("default credentials", func(ctx context.Context, mck Mock) {
})),
Path(Result("default credentials", func(ctx context.Context, mck Mock) {
mScope, err := NewMachineScope(ctx, "token", MachineScopeParams{
Client: mck.K8sClient,
Cluster: &clusterv1.Cluster{},
Expand All @@ -278,10 +278,10 @@ func TestNewMachineScope(t *testing.T) {
})
require.NoError(t, err)
assert.NotNil(t, mScope)
}),
})),
),
Either(
Result("credentials from LinodeMachine credentialsRef", func(ctx context.Context, mck Mock) {
OneOf(
Path(Result("credentials from LinodeMachine credentialsRef", func(ctx context.Context, mck Mock) {
mScope, err := NewMachineScope(ctx, "", MachineScopeParams{
Client: mck.K8sClient,
Cluster: &clusterv1.Cluster{},
Expand All @@ -298,8 +298,8 @@ func TestNewMachineScope(t *testing.T) {
})
require.NoError(t, err)
assert.NotNil(t, mScope)
}),
Result("credentials from LinodeCluster credentialsRef", func(ctx context.Context, mck Mock) {
})),
Path(Result("credentials from LinodeCluster credentialsRef", func(ctx context.Context, mck Mock) {
mScope, err := NewMachineScope(ctx, "token", MachineScopeParams{
Client: mck.K8sClient,
Cluster: &clusterv1.Cluster{},
Expand All @@ -316,15 +316,15 @@ func TestNewMachineScope(t *testing.T) {
})
require.NoError(t, err)
assert.NotNil(t, mScope)
}),
})),
),
))
)
}

func TestMachineScopeGetBootstrapData(t *testing.T) {
t.Parallel()

NewSuite(t, mock.MockK8sClient{}).Run(Paths(
NewSuite(t, mock.MockK8sClient{}).Run(
Call("able to get secret", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(ctx, gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, key client.ObjectKey, obj *corev1.Secret, opts ...client.GetOption) error {
Expand All @@ -350,19 +350,19 @@ func TestMachineScopeGetBootstrapData(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, data, []byte("test-data"))
}),
Either(
Call("unable to get secret", func(ctx context.Context, mck Mock) {
OneOf(
Path(Call("unable to get secret", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(ctx, gomock.Any(), gomock.Any()).
Return(apierrors.NewNotFound(schema.GroupResource{}, "test-data"))
}),
Call("secret is missing data", func(ctx context.Context, mck Mock) {
})),
Path(Call("secret is missing data", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(ctx, gomock.Any(), gomock.Any()).
DoAndReturn(func(ctx context.Context, key client.ObjectKey, obj *corev1.Secret, opts ...client.GetOption) error {
*obj = corev1.Secret{}
return nil
})
}),
Result("secret ref missing", func(ctx context.Context, mck Mock) {
})),
Path(Result("secret ref missing", func(ctx context.Context, mck Mock) {
mScope := MachineScope{
Client: mck.K8sClient,
Machine: &clusterv1.Machine{},
Expand All @@ -372,7 +372,7 @@ func TestMachineScopeGetBootstrapData(t *testing.T) {
data, err := mScope.GetBootstrapData(ctx)
require.ErrorContains(t, err, "bootstrap data secret is nil")
assert.Empty(t, data)
}),
})),
),
Result("error", func(ctx context.Context, mck Mock) {
mScope := MachineScope{
Expand All @@ -391,5 +391,5 @@ func TestMachineScopeGetBootstrapData(t *testing.T) {
require.Error(t, err)
assert.Empty(t, data)
}),
))
)
}
50 changes: 25 additions & 25 deletions controller/linodeobjectstoragebucket_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ var _ = Describe("lifecycle", Ordered, Label("bucket", "lifecycle"), func() {
bScope.PatchHelper = patchHelper
})

suite.Run(Paths(
Either(
Call("bucket is created", func(ctx context.Context, mck Mock) {
suite.Run(
OneOf(
Path(Call("bucket is created", func(ctx context.Context, mck Mock) {
getBucket := mck.ObjectStorageClient.EXPECT().GetObjectStorageBucket(gomock.Any(), obj.Spec.Cluster, gomock.Any()).Return(nil, nil)
mck.ObjectStorageClient.EXPECT().CreateObjectStorageBucket(gomock.Any(), gomock.Any()).
After(getBucket).
Expand All @@ -115,7 +115,7 @@ var _ = Describe("lifecycle", Ordered, Label("bucket", "lifecycle"), func() {
Created: util.Pointer(time.Now()),
Hostname: "hostname",
}, nil)
}),
})),
Path(
Call("bucket is not created", func(ctx context.Context, mck Mock) {
getBucket := mck.ObjectStorageClient.EXPECT().GetObjectStorageBucket(gomock.Any(), obj.Spec.Cluster, gomock.Any()).Return(nil, nil)
Expand All @@ -128,8 +128,8 @@ var _ = Describe("lifecycle", Ordered, Label("bucket", "lifecycle"), func() {
}),
),
),
Either(
Call("keys are created", func(ctx context.Context, mck Mock) {
OneOf(
Path(Call("keys are created", func(ctx context.Context, mck Mock) {
for idx := range 2 {
mck.ObjectStorageClient.EXPECT().CreateObjectStorageKey(gomock.Any(), gomock.Any()).
Return(&linodego.ObjectStorageKey{
Expand All @@ -138,7 +138,7 @@ var _ = Describe("lifecycle", Ordered, Label("bucket", "lifecycle"), func() {
SecretKey: fmt.Sprintf("secret-key-%d", idx),
}, nil)
}
}),
})),
Path(
Call("keys are not created", func(ctx context.Context, mck Mock) {
mck.ObjectStorageClient.EXPECT().CreateObjectStorageKey(gomock.Any(), gomock.Any()).Return(nil, errors.New("create key error"))
Expand Down Expand Up @@ -193,16 +193,16 @@ var _ = Describe("lifecycle", Ordered, Label("bucket", "lifecycle"), func() {
Expect(logOutput).To(ContainSubstring("Reconciling apply"))
Expect(logOutput).To(ContainSubstring("Secret lifecycle-bucket-details was applied with new access keys"))
}),
Either(
Call("bucket is retrieved on update", func(ctx context.Context, mck Mock) {
OneOf(
Path(Call("bucket is retrieved on update", func(ctx context.Context, mck Mock) {
mck.ObjectStorageClient.EXPECT().GetObjectStorageBucket(gomock.Any(), obj.Spec.Cluster, gomock.Any()).
Return(&linodego.ObjectStorageBucket{
Label: obj.Name,
Cluster: obj.Spec.Cluster,
Created: util.Pointer(time.Now()),
Hostname: "hostname",
}, nil)
}),
})),
Path(
Call("bucket is not retrieved on update", func(ctx context.Context, mck Mock) {
mck.ObjectStorageClient.EXPECT().GetObjectStorageBucket(gomock.Any(), obj.Spec.Cluster, gomock.Any()).Return(nil, errors.New("get bucket error"))
Expand All @@ -220,7 +220,7 @@ var _ = Describe("lifecycle", Ordered, Label("bucket", "lifecycle"), func() {
obj.Spec.KeyGeneration = ptr.To(1)
Expect(k8sClient.Update(ctx, &obj)).To(Succeed())
}),
Either(
OneOf(
// nb: Order matters for paths of the same length. The leftmost path is evaluated first.
// If we evaluate the happy path first, the bucket resource is mutated so the error path won't occur.
Path(
Expand Down Expand Up @@ -260,14 +260,14 @@ var _ = Describe("lifecycle", Ordered, Label("bucket", "lifecycle"), func() {
Expect(logOutput).To(ContainSubstring("Secret lifecycle-bucket-details was applied with new access keys"))
}),
),
Once("secret is deleted", func(ctx context.Context, _ Mock) {
Path(Once("secret is deleted", func(ctx context.Context, _ Mock) {
var secret corev1.Secret
secretKey := client.ObjectKey{Namespace: "default", Name: *obj.Status.KeySecretName}
Expect(k8sClient.Get(ctx, secretKey, &secret)).To(Succeed())
Expect(k8sClient.Delete(ctx, &secret)).To(Succeed())
}),
})),
),
Either(
OneOf(
Path(
Call("keys are not retrieved", func(ctx context.Context, mck Mock) {
mck.ObjectStorageClient.EXPECT().GetObjectStorageKey(gomock.Any(), gomock.Any()).Times(2).Return(nil, errors.New("get key error"))
Expand Down Expand Up @@ -326,7 +326,7 @@ var _ = Describe("lifecycle", Ordered, Label("bucket", "lifecycle"), func() {
Expect(k8sClient.Delete(ctx, &obj)).To(Succeed())
Expect(k8sClient.Get(ctx, objectKey, &obj)).To(Succeed())
}),
Either(
OneOf(
Path(
Call("keys are not revoked", func(ctx context.Context, mck Mock) {
mck.ObjectStorageClient.EXPECT().DeleteObjectStorageKey(gomock.Any(), gomock.Any()).Times(2).Return(errors.New("revoke error"))
Expand Down Expand Up @@ -355,7 +355,7 @@ var _ = Describe("lifecycle", Ordered, Label("bucket", "lifecycle"), func() {
}),
),
),
))
)
})

var _ = Describe("errors", Label("bucket", "errors"), func() {
Expand Down Expand Up @@ -386,11 +386,11 @@ var _ = Describe("errors", Label("bucket", "errors"), func() {
}
})

suite.Run(Paths(
Either(
Call("resource can be fetched", func(ctx context.Context, mck Mock) {
suite.Run(
OneOf(
Path(Call("resource can be fetched", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
}),
})),
Path(
Call("resource is not found", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(apierrors.NewNotFound(schema.GroupResource{}, "mock"))
Expand Down Expand Up @@ -444,7 +444,7 @@ var _ = Describe("errors", Label("bucket", "errors"), func() {
Call("get bucket", func(ctx context.Context, mck Mock) {
mck.ObjectStorageClient.EXPECT().GetObjectStorageBucket(gomock.Any(), gomock.Any(), gomock.Any()).Return(&linodego.ObjectStorageBucket{Created: ptr.To(time.Now())}, nil)
}),
Either(
OneOf(
Path(
Call("failed check for deleted secret", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(errors.New("api error"))
Expand All @@ -463,16 +463,16 @@ var _ = Describe("errors", Label("bucket", "errors"), func() {
Expect(mck.Logs()).To(ContainSubstring("Failed to ensure access key secret exists"))
}),
),
Call("secret deleted", func(ctx context.Context, mck Mock) {
Path(Call("secret deleted", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(apierrors.NewNotFound(schema.GroupResource{Resource: "Secret"}, "mock-bucket-details"))
}),
})),
),
Call("get keys", func(ctx context.Context, mck Mock) {
for idx := range 2 {
mck.ObjectStorageClient.EXPECT().GetObjectStorageKey(gomock.Any(), idx).Return(&linodego.ObjectStorageKey{ID: idx}, nil)
}
}),
Either(
OneOf(
Path(
Call("secret resource creation fails", func(ctx context.Context, mck Mock) {
mck.K8sClient.EXPECT().Scheme().Return(scheme.Scheme).AnyTimes()
Expand Down Expand Up @@ -527,5 +527,5 @@ var _ = Describe("errors", Label("bucket", "errors"), func() {
Expect(err.Error()).To(ContainSubstring("failed to remove finalizer from bucket"))
Expect(mck.Events()).To(ContainSubstring("failed to remove finalizer from bucket"))
}),
))
)
})
Loading

0 comments on commit 9ce1d98

Please sign in to comment.