-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rollouts: add unit test and fake client for simple reconcileRollouts case #3894
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
Copyright 2022. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package controllers | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
gitopsv1alpha1 "github.com/GoogleContainerTools/kpt/rollouts/api/v1alpha1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
var _ client.Client = &fakeRemoteSyncClient{} | ||
|
||
type fakeRemoteSyncClient struct { | ||
client.Client | ||
|
||
remotesyncs map[types.NamespacedName]gitopsv1alpha1.RemoteSync | ||
actions []string | ||
} | ||
|
||
func newFakeRemoteSyncClient() *fakeRemoteSyncClient { | ||
return &fakeRemoteSyncClient{ | ||
remotesyncs: make(map[types.NamespacedName]gitopsv1alpha1.RemoteSync), | ||
} | ||
} | ||
|
||
func (fc *fakeRemoteSyncClient) Create(ctx context.Context, obj client.Object, _ ...client.CreateOption) error { | ||
fc.actions = append(fc.actions, fmt.Sprintf("creating object named %q", obj.GetName())) | ||
|
||
namespacedName := types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()} | ||
if err := fc.Get(ctx, namespacedName, obj); err == nil { | ||
return fmt.Errorf("object %q already exists", obj.GetName()) | ||
} | ||
fc.remotesyncs[namespacedName] = *obj.(*gitopsv1alpha1.RemoteSync) | ||
|
||
return nil | ||
} | ||
|
||
func (fc *fakeRemoteSyncClient) Delete(_ context.Context, obj client.Object, _ ...client.DeleteOption) error { | ||
fc.actions = append(fc.actions, fmt.Sprintf("deleting object named %q", obj.GetName())) | ||
|
||
namespacedName := types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()} | ||
delete(fc.remotesyncs, namespacedName) | ||
|
||
return nil | ||
} | ||
|
||
func (fc *fakeRemoteSyncClient) Update(_ context.Context, obj client.Object, _ ...client.UpdateOption) error { | ||
fc.actions = append(fc.actions, fmt.Sprintf("updating object named %q", obj.GetName())) | ||
|
||
namespacedName := types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()} | ||
fc.remotesyncs[namespacedName] = *obj.(*gitopsv1alpha1.RemoteSync) | ||
|
||
return nil | ||
} | ||
|
||
func (fc *fakeRemoteSyncClient) List(_ context.Context, obj client.ObjectList, _ ...client.ListOption) error { | ||
fc.actions = append(fc.actions, fmt.Sprintf("listing objects")) | ||
|
||
var remoteSyncList []gitopsv1alpha1.RemoteSync | ||
for _, rs := range fc.remotesyncs { | ||
remoteSyncList = append(remoteSyncList, rs) | ||
} | ||
*obj.(*gitopsv1alpha1.RemoteSyncList) = gitopsv1alpha1.RemoteSyncList{Items: remoteSyncList} | ||
|
||
return nil | ||
} | ||
|
||
func (fc *fakeRemoteSyncClient) Get(_ context.Context, namespacedName types.NamespacedName, obj client.Object, _ ...client.GetOption) error { | ||
fc.actions = append(fc.actions, fmt.Sprintf("getting object named %q", namespacedName.Name)) | ||
|
||
rs, found := fc.remotesyncs[namespacedName] | ||
if found { | ||
*obj.(*gitopsv1alpha1.RemoteSync) = rs | ||
return nil | ||
} | ||
|
||
return &errors.StatusError{ErrStatus: metav1.Status{Reason: metav1.StatusReasonNotFound}} | ||
} | ||
|
||
func (fc *fakeRemoteSyncClient) setSyncStatus(namespacedName types.NamespacedName, syncStatus string) error { | ||
rs, found := fc.remotesyncs[namespacedName] | ||
if found { | ||
rs.Status.SyncStatus = syncStatus | ||
fc.remotesyncs[namespacedName] = rs | ||
return nil | ||
} | ||
|
||
return &errors.StatusError{ErrStatus: metav1.Status{Reason: metav1.StatusReasonNotFound}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we using this style of assertions in other parts of
kpt
project ? otherwise I have found omega matchers to be very good for this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a number of places in kpt repo that we are using the
testify/assert
andtestify/require
packages to do similar assertions in unit tests.require
is a subtly different flavor ofassert
, except thatrequire
exits the test immediately, whileassert
continues running the rest of the test even when it encounters an error.That said, I am happy to convert to omega matchers if you prefer!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't see usage of
require
before. But the failure behavior ofrequire
does make more sense.I am fine using
require
here, so let's ship it.