Skip to content

Commit

Permalink
Allow to define creationTimestamp mock (#1882)
Browse files Browse the repository at this point in the history
* Allow to define the creation timestamp in the mock
  • Loading branch information
johscheuer authored Nov 14, 2023
1 parent ff58381 commit b592587
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
7 changes: 5 additions & 2 deletions mock-kubernetes-client/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,11 @@ func (client *MockClient) generateIP() string {

// Create creates a new object
func (client *MockClient) Create(ctx context.Context, object ctrlClient.Object, options ...ctrlClient.CreateOption) error {
// Ensure the default values are set.
object.SetCreationTimestamp(metav1.Time{Time: time.Now()})
// Ensure the default values are set if not present.
if object.GetCreationTimestamp().Time.IsZero() {
object.SetCreationTimestamp(metav1.Time{Time: time.Now()})
}

object.SetGeneration(object.GetGeneration() + 1)
object.SetUID(uuid.NewUUID())

Expand Down
25 changes: 20 additions & 5 deletions mock-kubernetes-client/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,8 @@ var _ = Describe("[mock client]", func() {
var mockClient *MockClient

BeforeEach(func() {
err := scheme.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())
err = fdbv1beta2.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

Expect(scheme.AddToScheme(scheme.Scheme)).NotTo(HaveOccurred())
Expect(fdbv1beta2.AddToScheme(scheme.Scheme)).NotTo(HaveOccurred())
mockClient = NewMockClient(scheme.Scheme)
})

Expand All @@ -88,6 +85,24 @@ var _ = Describe("[mock client]", func() {
})
})

When("creating and getting an object with a custom creationTimestamp", func() {
It("should create and get the object", func() {
pod := createDummyPod()
expectedCreationTime := time.Now().Add(-15 * time.Minute)
pod.CreationTimestamp.Time = expectedCreationTime
Expect(mockClient.Create(context.TODO(), pod)).NotTo(HaveOccurred())
Expect(pod.ObjectMeta.Generation).To(Equal(int64(1)))

podCopy := &corev1.Pod{}
Expect(mockClient.Get(context.TODO(), types.NamespacedName{Namespace: "default", Name: "pod1"}, podCopy)).NotTo(HaveOccurred())
Expect(podCopy.Name).To(Equal("pod1"))
Expect(len(podCopy.Spec.Containers)).To(Equal(1))
Expect(podCopy.Spec.Containers[0].Name).To(Equal("test-container"))
Expect(podCopy.ObjectMeta.Generation).To(Equal(int64(1)))
Expect(podCopy.ObjectMeta.CreationTimestamp.Time.Minute()).To(Equal(expectedCreationTime.Minute()))
})
})

When("creating and getting a Pod", func() {
var initialPod *corev1.Pod

Expand Down

0 comments on commit b592587

Please sign in to comment.