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

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