...

Source file src/sigs.k8s.io/cli-utils/pkg/object/mutation/testutil/object.go

Documentation: sigs.k8s.io/cli-utils/pkg/object/mutation/testutil

     1  // Copyright 2020 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package testutil
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    11  	"sigs.k8s.io/cli-utils/pkg/object/mutation"
    12  	"sigs.k8s.io/cli-utils/pkg/testutil"
    13  )
    14  
    15  // AddApplyTimeMutation returns a testutil.Mutator which adds the passed objects
    16  // as an apply-time-mutation annotation to the object which is mutated. Multiple
    17  // objects passed in means multiple substitutions in the annotation yaml list.
    18  func AddApplyTimeMutation(t *testing.T, mutation *mutation.ApplyTimeMutation) testutil.Mutator {
    19  	return applyTimeMutationMutator{
    20  		t:        t,
    21  		mutation: mutation,
    22  	}
    23  }
    24  
    25  // applyTimeMutationMutator encapsulates fields for adding apply-time-mutation
    26  // annotation to a test object. Implements the Mutator interface.
    27  type applyTimeMutationMutator struct {
    28  	t        *testing.T
    29  	mutation *mutation.ApplyTimeMutation
    30  }
    31  
    32  // Mutate for applyTimeMutationMutator sets the apply-time-mutation annotation
    33  // on the passed object.
    34  func (a applyTimeMutationMutator) Mutate(u *unstructured.Unstructured) {
    35  	err := mutation.WriteAnnotation(u, *a.mutation)
    36  	if !assert.NoError(a.t, err) {
    37  		a.t.FailNow()
    38  	}
    39  }
    40  

View as plain text