Skip to content

Commit

Permalink
Director client unit tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
akgalwas committed Mar 25, 2024
1 parent 61e3cc1 commit 58cfbe6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,10 @@ func TestConfigClient_SetRuntimeStatusCondition(t *testing.T) {
Run(setExpectedResponse[graphql.RuntimeExt](t, expectedSuccessfulGetRuntimeResponse)).
Once()

var expectedResult *graphql.Runtime

client.
On("Do", context.Background(), updateRuntimeReq, &GQLResponse[graphql.Runtime]{}).
Return(nil).
Run(setExpectedResponse[graphql.Runtime](t, expectedResult)).
Run(setExpectedResponse[graphql.Runtime](t, nil)).
Once()

return client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func TestCompassConnectionController(t *testing.T) {
credentialsManagerMock)
certsConnectorClientMock.AssertCalled(t, "Configuration", requestIDCtxMatcher, nilHeaders)
certsConnectorClientMock.AssertNotCalled(t, "SignCSR", requestIDCtxMatcher, mock.AnythingOfType("string"), nilHeaders)
assertRuntimeInConnectedStateAnnotation(t, graphql.RuntimeStatusConditionConnected)
})

t.Run("Compass Connection should be reinitialized if deleted", func(t *testing.T) {
Expand All @@ -232,6 +233,7 @@ func TestCompassConnectionController(t *testing.T) {
credentialsManagerMock)
certsConnectorClientMock.AssertCalled(t, "Configuration", requestIDCtxMatcher, nilHeaders)
certsConnectorClientMock.AssertNotCalled(t, "SignCSR", requestIDCtxMatcher, mock.AnythingOfType("string"), nilHeaders)
assertRuntimeInConnectedStateAnnotation(t, graphql.RuntimeStatusConditionConnected)
})

t.Run("Should not reinitialized connection if connection is in Synchronized state", func(t *testing.T) {
Expand Down Expand Up @@ -269,6 +271,7 @@ func TestCompassConnectionController(t *testing.T) {
assertCertificateRenewed(t)
assertManagementInfoSetInCR(t)
certsConnectorClientMock.AssertCalled(t, "SignCSR", requestIDCtxMatcher, mock.AnythingOfType("string"), nilHeaders)
assertRuntimeInConnectedStateAnnotation(t, graphql.RuntimeStatusConditionConnected)
})

t.Run("Compass Connection should be in MetadataUpdateFailed state if failed to set labels on Runtime", func(t *testing.T) {
Expand All @@ -287,6 +290,7 @@ func TestCompassConnectionController(t *testing.T) {
require.NoError(t, err)
require.NoError(t, waitForResourceUpdate(v1alpha1.MetadataUpdateFailed))
assertManagementInfoSetInCR(t)
assertRuntimeInConnectedStateAnnotation(t, graphql.RuntimeStatusConditionConnected)

clearMockCalls(&configurationClientMock.Mock)
// restore previous director mock configuration to not interfere with other tests
Expand All @@ -311,6 +315,7 @@ func TestCompassConnectionController(t *testing.T) {
require.NoError(t, waitForResourceUpdate(v1alpha1.ResourceApplicationFailed))
assertManagementInfoSetInCR(t)
assertSynchronizationStatusError(t)
assertRuntimeInConnectedStateAnnotation(t, graphql.RuntimeStatusConditionConnected)

// restore previous sync service mock configuration to not interfere with other tests
clearMockCalls(&synchronizationServiceMock.Mock)
Expand All @@ -333,6 +338,7 @@ func TestCompassConnectionController(t *testing.T) {
require.NoError(t, waitForResourceUpdate(v1alpha1.SynchronizationFailed))
assertManagementInfoSetInCR(t)
assertSynchronizationStatusError(t)
assertRuntimeInConnectedStateAnnotation(t, graphql.RuntimeStatusConditionConnected)

// restore previous director mock configuration to not interfere with other tests
clearMockCalls(&configurationClientMock.Mock)
Expand All @@ -347,6 +353,7 @@ func TestCompassConnectionController(t *testing.T) {
require.Equal(t, true, isConnectionInState(v1alpha1.Synchronized))
clientsProviderMock.ExpectedCalls = nil
clientsProviderMock.Calls = nil
assertCompassRuntimeConnectedStatusAnnotationRemoved(t)
clientsProviderMock.On("GetConnectorTokensClient", connectorURL).Return(tokensConnectorClientMock, nil)
clientsProviderMock.On("GetConnectorCertSecuredClient").Return(certsConnectorClientMock, nil)
clientsProviderMock.On("GetDirectorClient", runtimeConfig).Return(nil, errors.New("error"))
Expand All @@ -361,6 +368,7 @@ func TestCompassConnectionController(t *testing.T) {
require.NoError(t, waitForResourceUpdate(v1alpha1.SynchronizationFailed))
assertManagementInfoSetInCR(t)
assertSynchronizationStatusError(t)
assertRuntimeInConnectedStateAnnotation(t, "")
})

t.Run("Compass Connection should be in SynchronizationFailed state if failed to read runtime configuration", func(t *testing.T) {
Expand Down Expand Up @@ -640,6 +648,12 @@ func assertCompassConnectionState(t *testing.T, expectedState v1alpha1.Connectio
assert.Equal(t, expectedState, connectedConnection.Status.State)
}

func assertRuntimeInConnectedStateAnnotation(t *testing.T, expected graphql.RuntimeStatusCondition) {
connectedConnection, err := compassConnectionCRClient.Get(context.Background(), compassConnectionName, v1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, string(expected), connectedConnection.Annotations[RuntimeConnectedStatusAnnotation])
}

func assertConnectionStatusSet(t *testing.T) {
connectedConnection, err := compassConnectionCRClient.Get(context.Background(), compassConnectionName, v1.GetOptions{})
require.NoError(t, err)
Expand All @@ -661,6 +675,16 @@ func assertRecreateSynchronizedConnection(t *testing.T) {
assertManagementInfoSetInCR(t)
}

func assertCompassRuntimeConnectedStatusAnnotationRemoved(t *testing.T) {
connectedConnection, err := compassConnectionCRClient.Get(context.Background(), compassConnectionName, v1.GetOptions{})
require.NoError(t, err)

delete(connectedConnection.Annotations, RuntimeConnectedStatusAnnotation)

_, err = compassConnectionCRClient.Update(context.Background(), connectedConnection, v1.UpdateOptions{})
require.NoError(t, err)
}

func assertCertificateRenewed(t *testing.T) {
connectedConnection, err := compassConnectionCRClient.Get(context.Background(), compassConnectionName, v1.GetOptions{})
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
)

const (
DefaultCompassConnectionName = "compass-connection"
DefaultCompassConnectionName = "compass-connection"
RuntimeConnectedStatusAnnotation = "operator.kyma-project.io/compass-status-connected"
)

//go:generate mockery --name=CRManager
Expand Down Expand Up @@ -213,7 +214,7 @@ func (s *crSupervisor) SynchronizeWithCompass(ctx context.Context, connection *v

func (s *crSupervisor) runtimeHasConnectedStatusInCompass(compassConnection *v1alpha1.CompassConnection) bool {
annotations := compassConnection.Annotations
_, found := annotations["operator.kyma-project.io/compass-status-connected"]
_, found := annotations[RuntimeConnectedStatusAnnotation]

return found
}
Expand All @@ -225,7 +226,7 @@ func (s *crSupervisor) setRuntimeStatusInCompass(compassConnection *v1alpha1.Com
annotations = map[string]string{}
}

annotations["operator.kyma-project.io/compass-status-connected"] = string(graphql.RuntimeStatusConditionConnected)
annotations[RuntimeConnectedStatusAnnotation] = string(graphql.RuntimeStatusConditionConnected)
compassConnection.Annotations = annotations
}

Expand Down

0 comments on commit 58cfbe6

Please sign in to comment.