github.com/verrazzano/verrazzano@v1.7.0/application-operator/apis/oam/v1alpha1/loggingtrait_methods_test.go (about)

     1  // Copyright (c) 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package v1alpha1
     5  
     6  import (
     7  	"testing"
     8  
     9  	oamrt "github.com/crossplane/crossplane-runtime/apis/common/v1"
    10  	asserts "github.com/stretchr/testify/assert"
    11  	corev1 "k8s.io/api/core/v1"
    12  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    13  )
    14  
    15  // TestGetCondition tests the GetCondition method
    16  func TestLoggingTraitGetCondition(t *testing.T) {
    17  	assert := asserts.New(t)
    18  	var trait LoggingTrait
    19  	var cond oamrt.Condition
    20  
    21  	trait = LoggingTrait{
    22  		Status: LoggingTraitStatus{
    23  			ConditionedStatus: oamrt.ConditionedStatus{Conditions: []oamrt.Condition{{
    24  				Type:               oamrt.TypeSynced,
    25  				Status:             corev1.ConditionTrue,
    26  				LastTransitionTime: metav1.Now(),
    27  				Reason:             "test-reason",
    28  				Message:            "test-message"}}}}}
    29  
    30  	// GIVEN a trait with a synced condition
    31  	// WHEN the syned condition is retrieved
    32  	// THEN verify that the correct condition was retrieved
    33  	cond = trait.GetCondition(oamrt.TypeSynced)
    34  	assert.Equal(oamrt.TypeSynced, cond.Type)
    35  	assert.Equal(corev1.ConditionTrue, cond.Status)
    36  	assert.Equal(oamrt.ConditionReason("test-reason"), cond.Reason)
    37  	assert.Equal("test-message", cond.Message)
    38  
    39  	// GIVEN a trait with a synced condition
    40  	// WHEN the ready condition is retrieved
    41  	// THEN verify that an unknown status condition is returned
    42  	cond = trait.GetCondition(oamrt.TypeReady)
    43  	assert.Equal(oamrt.TypeReady, cond.Type)
    44  	assert.Equal(corev1.ConditionUnknown, cond.Status)
    45  }
    46  
    47  // TestSetCondition tests the SetConditions method.
    48  func TestLoggingTraitSetCondition(t *testing.T) {
    49  	assert := asserts.New(t)
    50  	var trait LoggingTrait
    51  	var cond = oamrt.Condition{
    52  		Type:               oamrt.TypeSynced,
    53  		Status:             corev1.ConditionTrue,
    54  		LastTransitionTime: metav1.Now(),
    55  		Reason:             "test-reason",
    56  		Message:            "test-message"}
    57  
    58  	// GIVEN an trait with no conditions
    59  	// WHEN a condition is set
    60  	// THEN verify that the fields are correctly populated
    61  	trait = LoggingTrait{}
    62  	trait.SetConditions(cond)
    63  	assert.Len(trait.Status.Conditions, 1)
    64  	assert.Equal(cond, trait.Status.Conditions[0])
    65  }
    66  
    67  // TestGetWorkloadReference tests the GetWorkloadReference method.
    68  func TestLoggingTraitGetWorkloadReference(t *testing.T) {
    69  	assert := asserts.New(t)
    70  	var trait LoggingTrait
    71  	var expected oamrt.TypedReference
    72  	var actual oamrt.TypedReference
    73  
    74  	// GIVEN a trait with a workload reference
    75  	// WHEN the workload reference is retrieved
    76  	// THEN verify the correct workload reference information is returned
    77  	expected = oamrt.TypedReference{
    78  		APIVersion: "test-api/ver",
    79  		Kind:       "test-kind",
    80  		Name:       "test-name",
    81  		UID:        "test-uid"}
    82  	trait = LoggingTrait{}
    83  	trait.Spec.WorkloadReference = expected
    84  	actual = trait.GetWorkloadReference()
    85  	assert.Equal(expected, actual)
    86  }
    87  
    88  // TestSetWorkloadReference test the SetWorkloadReference method.
    89  func TestLoggingTraitSetWorkloadReference(t *testing.T) {
    90  	assert := asserts.New(t)
    91  	var trait LoggingTrait
    92  	var expected oamrt.TypedReference
    93  	var actual oamrt.TypedReference
    94  
    95  	// GIVEN a trait with a workload reference
    96  	// WHEN the workload reference is retrieved
    97  	// THEN verify the correct workload reference information is returned
    98  	expected = oamrt.TypedReference{
    99  		APIVersion: "test-api/ver",
   100  		Kind:       "test-kind",
   101  		Name:       "test-name",
   102  		UID:        "test-uid"}
   103  	trait = LoggingTrait{}
   104  	trait.SetWorkloadReference(expected)
   105  	actual = trait.Spec.WorkloadReference
   106  	assert.Equal(expected, actual)
   107  }