github.com/verrazzano/verrazzano@v1.7.1/application-operator/controllers/metricstrait/operator_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 metricstrait
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	"github.com/golang/mock/gomock"
    11  	"github.com/stretchr/testify/assert"
    12  	vzapi "github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1"
    13  	"github.com/verrazzano/verrazzano/application-operator/mocks"
    14  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    15  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    16  )
    17  
    18  func TestOperatorReconcile(t *testing.T) {
    19  	tests := []struct {
    20  		name        string
    21  		trait       *vzapi.MetricsTrait
    22  		expectError bool
    23  		requeue     bool
    24  	}{
    25  		{
    26  			name:        "Test reconcile empty trait",
    27  			trait:       &vzapi.MetricsTrait{},
    28  			expectError: false,
    29  			requeue:     false,
    30  		},
    31  	}
    32  	var r Reconciler
    33  	mock := getNewMock(t)
    34  	mock.EXPECT().Update(gomock.Any(), gomock.Not(gomock.Nil()))
    35  	mock.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Not(gomock.Nil())).Times(2)
    36  	r.Client = mock
    37  	for _, tt := range tests {
    38  		t.Run(tt.name, func(t *testing.T) {
    39  			result, err := r.doOperatorReconcile(context.TODO(), tt.trait, vzlog.DefaultLogger())
    40  			if tt.expectError {
    41  				assert.Error(t, err, "Expected an error from the reconcile")
    42  			} else {
    43  				assert.NoError(t, err, err, "Expected no error from the reconcile")
    44  			}
    45  			assert.Equal(t, reconcile.Result{Requeue: tt.requeue}, result)
    46  		})
    47  	}
    48  }
    49  
    50  func getNewMock(t *testing.T) *mocks.MockClient {
    51  	mocker := gomock.NewController(t)
    52  	return mocks.NewMockClient(mocker)
    53  }