Skip to content

Commit

Permalink
Updating metav1.Object -> client.Object, and using type switch to get…
Browse files Browse the repository at this point in the history
… observedGeneration.
  • Loading branch information
naemono committed Feb 22, 2022
1 parent b96cd85 commit 2bc4fd9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions test/e2e/test/agent/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ func (b Builder) DeletionTestSteps(k *test.K8sClient) test.StepList {
func (b Builder) MutationTestSteps(k *test.K8sClient) test.StepList {
var agentGenerationBeforeMutation, agentObservedGenerationBeforeMutation int64
return test.StepList{
generation.RetrieveAgentGenerationsStep(b.Agent, k, &agentGenerationBeforeMutation, &agentObservedGenerationBeforeMutation),
generation.RetrieveAgentGenerationsStep(&b.Agent, k, &agentGenerationBeforeMutation, &agentObservedGenerationBeforeMutation),
}.WithSteps(b.UpgradeTestSteps(k)).
WithSteps(b.CheckK8sTestSteps(k)).
WithSteps(b.CheckStackTestSteps(k)).
WithStep(generation.CompareObjectGenerationsStep(b.Agent, k, &agentGenerationBeforeMutation, &agentObservedGenerationBeforeMutation))
WithStep(generation.CompareObjectGenerationsStep(&b.Agent, k, &agentGenerationBeforeMutation, &agentObservedGenerationBeforeMutation))
}

func (b Builder) MutationReversalTestContext() test.ReversalTestContext {
Expand Down
24 changes: 15 additions & 9 deletions test/e2e/test/generation/generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,36 @@ import (
"errors"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"

agentv1alpha1 "github.com/elastic/cloud-on-k8s/pkg/apis/agent/v1alpha1"
"github.com/elastic/cloud-on-k8s/test/e2e/test"
)

func getGeneration(obj metav1.Object, k *test.K8sClient) (int64, error) {
if err := k.Client.Get(context.Background(), types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, &obj); err != nil {
func getGeneration(obj client.Object, k *test.K8sClient) (int64, error) {
if err := k.Client.Get(context.Background(), types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, obj); err != nil {
return 0, err
}

return obj.GetObjectMeta().GetGeneration(), nil
return obj.GetGeneration(), nil
}

func getObservedGeneration(obj metav1.Object, k *test.K8sClient) (int64, error) {
if err := k.Client.Get(context.Background(), types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, &obj); err != nil {
func getObservedGeneration(obj client.Object, k *test.K8sClient) (int64, error) {
if err := k.Client.Get(context.Background(), types.NamespacedName{Namespace: obj.GetNamespace(), Name: obj.GetName()}, obj); err != nil {
return 0, err
}

return obj.Status.ObservedGeneration, nil
switch v := obj.(type) {
case *agentv1alpha1.Agent:
return v.Status.ObservedGeneration, nil
default:
return 0, fmt.Errorf("unsupported type while retrieving status.observedGeneration: %T", v)
}
}

// RetrieveAgentGenerationsStep stores the current metadata.Generation, and status.ObservedGeneration into the given fields.
func RetrieveAgentGenerationsStep(obj metav1.Object, k *test.K8sClient, generation, observedGeneration *int64) test.Step {
func RetrieveAgentGenerationsStep(obj client.Object, k *test.K8sClient, generation, observedGeneration *int64) test.Step {
return test.Step{
Name: "Retrieve Objects metadata.Generation, and status.ObservedGeneration for comparison purpose",
Test: test.Eventually(func() error {
Expand All @@ -53,7 +59,7 @@ func RetrieveAgentGenerationsStep(obj metav1.Object, k *test.K8sClient, generati

// CompareObjectGenerationsStep compares the current object's metadata.generation, and status.observedGeneration
// and fails if they don't match expectations.
func CompareObjectGenerationsStep(obj metav1.Object, k *test.K8sClient, previousObjectGeneration, previousObjectObservedGeneration *int64) test.Step {
func CompareObjectGenerationsStep(obj client.Object, k *test.K8sClient, previousObjectGeneration, previousObjectObservedGeneration *int64) test.Step {
return test.Step{
Name: "Cluster metadata.generation, and status.observedGeneration should have been incremented from previous state, and should be equal",
Test: test.Eventually(func() error {
Expand Down

0 comments on commit 2bc4fd9

Please sign in to comment.