Skip to content

Commit

Permalink
#29 Fix unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sklein94 committed May 23, 2024
1 parent a463649 commit dceadc5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 30 deletions.
9 changes: 5 additions & 4 deletions packages/doguHealth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ func (s *server) getDoguHealthResponse(ctx context.Context, doguName string) (*p

// When replicas is > 0 it indicates that the dogu is not stopped.
// The admin dogu notices that there is a Dogu which is not healthy with a deployment > 0 and assumes: Dogu is starting
shouldStart := doguDeployment.Status.UnavailableReplicas > 0
hasReplica := *doguDeployment.Spec.Replicas > 0

containerStatusResult := &pbHealth.DoguHealthCheck{
Type: checkTypeContainer,
Success: shouldStart,
Message: "Check whether a deployment contains at least one ready replica.",
Type: checkTypeContainer,
Success: hasReplica,
Message: "Check whether a deployment has at least one replica, ready or not.",
Critical: false,
}

response.Results = append(response.Results, containerStatusResult)
Expand Down
91 changes: 65 additions & 26 deletions packages/doguHealth/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func Test_server_GetByName(t *testing.T) {

request := &health.DoguHealthRequest{DoguName: "my-dogu"}
deploymentMock := newMockDeploymentClient(t)
unhealthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 0}}
var replicas = new(int32)
*replicas = 1
unhealthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 0}, Spec: v1.DeploymentSpec{Replicas: replicas}}
deploymentMock.EXPECT().Get(context.TODO(), "my-dogu", metav1.GetOptions{}).Return(unhealthyDeployment, nil)
appsMock := newMockAppsV1Client(t)
appsMock.EXPECT().Deployments("ecosystem").Return(deploymentMock)
Expand All @@ -91,7 +93,14 @@ func Test_server_GetByName(t *testing.T) {
ShortName: "my-dogu",
DisplayName: "my-dogu",
Healthy: false,
Results: []*health.DoguHealthCheck{},
Results: []*health.DoguHealthCheck{
{
Type: "container",
Success: true,
Message: "Check whether a deployment has at least one replica, ready or not.",
Critical: false,
},
},
}

// when
Expand All @@ -109,7 +118,9 @@ func Test_server_GetByName(t *testing.T) {

request := &health.DoguHealthRequest{DoguName: "my-dogu"}
deploymentMock := newMockDeploymentClient(t)
healthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}}
var replicas = new(int32)
*replicas = 1
healthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}, Spec: v1.DeploymentSpec{Replicas: replicas}}
deploymentMock.EXPECT().Get(context.TODO(), "my-dogu", metav1.GetOptions{}).Return(healthyDeployment, nil)
appsMock := newMockAppsV1Client(t)
appsMock.EXPECT().Deployments("ecosystem").Return(deploymentMock)
Expand All @@ -124,7 +135,7 @@ func Test_server_GetByName(t *testing.T) {
Results: []*health.DoguHealthCheck{{
Type: "container",
Success: true,
Message: "Check whether a deployment contains at least one ready replica.",
Message: "Check whether a deployment has at least one replica, ready or not.",
}},
}

Expand All @@ -134,6 +145,9 @@ func Test_server_GetByName(t *testing.T) {
// then
require.NoError(t, err)
assert.Equal(t, expectedResponse, actual)
appsMock.AssertExpectations(t)
clientMock.AssertExpectations(t)
deploymentMock.AssertExpectations(t)
})
}

Expand All @@ -147,7 +161,9 @@ func Test_server_GetByNames(t *testing.T) {
request := &health.DoguHealthListRequest{Dogus: []string{"will-fail", "will-succeed"}}
deploymentMock := newMockDeploymentClient(t)
deploymentMock.EXPECT().Get(context.TODO(), "will-fail", metav1.GetOptions{}).Return(nil, assert.AnError)
healthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}}
var replicas = new(int32)
*replicas = 1
healthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}, Spec: v1.DeploymentSpec{Replicas: replicas}}
deploymentMock.EXPECT().Get(context.TODO(), "will-succeed", metav1.GetOptions{}).Return(healthyDeployment, nil)
appsMock := newMockAppsV1Client(t)
appsMock.EXPECT().Deployments("ecosystem").Return(deploymentMock)
Expand All @@ -170,9 +186,10 @@ func Test_server_GetByNames(t *testing.T) {
DisplayName: "will-succeed",
Healthy: true,
Results: []*health.DoguHealthCheck{{
Type: "container",
Success: true,
Message: "Check whether a deployment contains at least one ready replica.",
Type: "container",
Success: true,
Message: "Check whether a deployment has at least one replica, ready or not.",
Critical: false,
}},
},
},
Expand Down Expand Up @@ -237,9 +254,13 @@ func Test_server_GetByNames(t *testing.T) {

request := &health.DoguHealthListRequest{Dogus: []string{"healthy", "unhealthy"}}
deploymentMock := newMockDeploymentClient(t)
healthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}}
var replicasHealthy = new(int32)
*replicasHealthy = 1
healthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}, Spec: v1.DeploymentSpec{Replicas: replicasHealthy}}
deploymentMock.EXPECT().Get(context.TODO(), "healthy", metav1.GetOptions{}).Return(healthyDeployment, nil)
unhealthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 0}}
var replicasUnHealthy = new(int32)
*replicasUnHealthy = 0
unhealthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 0}, Spec: v1.DeploymentSpec{Replicas: replicasUnHealthy}}
deploymentMock.EXPECT().Get(context.TODO(), "unhealthy", metav1.GetOptions{}).Return(unhealthyDeployment, nil)
appsMock := newMockAppsV1Client(t)
appsMock.EXPECT().Deployments("ecosystem").Return(deploymentMock)
Expand All @@ -255,17 +276,23 @@ func Test_server_GetByNames(t *testing.T) {
DisplayName: "healthy",
Healthy: true,
Results: []*health.DoguHealthCheck{{
Type: "container",
Success: true,
Message: "Check whether a deployment contains at least one ready replica.",
Type: "container",
Success: true,
Message: "Check whether a deployment has at least one replica, ready or not.",
Critical: false,
}},
},
"unhealthy": {
FullName: "unhealthy",
ShortName: "unhealthy",
DisplayName: "unhealthy",
Healthy: false,
Results: []*health.DoguHealthCheck{},
Results: []*health.DoguHealthCheck{{
Type: "container",
Success: false,
Message: "Check whether a deployment has at least one replica, ready or not.",
Critical: false,
}},
},
},
}
Expand All @@ -285,9 +312,11 @@ func Test_server_GetByNames(t *testing.T) {

request := &health.DoguHealthListRequest{Dogus: []string{"healthy1", "healthy2"}}
deploymentMock := newMockDeploymentClient(t)
healthy1Deployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}}
var replicas = new(int32)
*replicas = 1
healthy1Deployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}, Spec: v1.DeploymentSpec{Replicas: replicas}}
deploymentMock.EXPECT().Get(context.TODO(), "healthy1", metav1.GetOptions{}).Return(healthy1Deployment, nil)
healthy2Deployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}}
healthy2Deployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}, Spec: v1.DeploymentSpec{Replicas: replicas}}
deploymentMock.EXPECT().Get(context.TODO(), "healthy2", metav1.GetOptions{}).Return(healthy2Deployment, nil)
appsMock := newMockAppsV1Client(t)
appsMock.EXPECT().Deployments("ecosystem").Return(deploymentMock)
Expand All @@ -305,7 +334,7 @@ func Test_server_GetByNames(t *testing.T) {
Results: []*health.DoguHealthCheck{{
Type: "container",
Success: true,
Message: "Check whether a deployment contains at least one ready replica.",
Message: "Check whether a deployment has at least one replica, ready or not.",
}},
},
"healthy2": {
Expand All @@ -316,7 +345,7 @@ func Test_server_GetByNames(t *testing.T) {
Results: []*health.DoguHealthCheck{{
Type: "container",
Success: true,
Message: "Check whether a deployment contains at least one ready replica.",
Message: "Check whether a deployment has at least one replica, ready or not.",
}},
},
},
Expand Down Expand Up @@ -383,8 +412,12 @@ func Test_server_GetAll(t *testing.T) {
config.CurrentNamespace = "ecosystem"

deploymentMock := newMockDeploymentClient(t)
healthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}}
unhealthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 0}}
var replicasHealthy = new(int32)
*replicasHealthy = 1
healthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}, Spec: v1.DeploymentSpec{Replicas: replicasHealthy}}
var replicasUnhealthy = new(int32)
*replicasUnhealthy = 1
unhealthyDeployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 0}, Spec: v1.DeploymentSpec{Replicas: replicasUnhealthy}}
deploymentMock.EXPECT().Get(context.TODO(), "healthy", metav1.GetOptions{}).Return(healthyDeployment, nil)
deploymentMock.EXPECT().Get(context.TODO(), "unhealthy", metav1.GetOptions{}).Return(unhealthyDeployment, nil)
appsMock := newMockAppsV1Client(t)
Expand Down Expand Up @@ -412,15 +445,19 @@ func Test_server_GetAll(t *testing.T) {
Results: []*health.DoguHealthCheck{{
Type: "container",
Success: true,
Message: "Check whether a deployment contains at least one ready replica.",
Message: "Check whether a deployment has at least one replica, ready or not.",
}},
},
"unhealthy": {
FullName: "unhealthy",
ShortName: "unhealthy",
DisplayName: "unhealthy",
Healthy: false,
Results: []*health.DoguHealthCheck{},
Results: []*health.DoguHealthCheck{{
Type: "container",
Success: true,
Message: "Check whether a deployment has at least one replica, ready or not.",
}},
},
},
}
Expand All @@ -439,8 +476,10 @@ func Test_server_GetAll(t *testing.T) {
config.CurrentNamespace = "ecosystem"

deploymentMock := newMockDeploymentClient(t)
healthy1Deployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}}
healthy2Deployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}}
var replicas = new(int32)
*replicas = 1
healthy1Deployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}, Spec: v1.DeploymentSpec{Replicas: replicas}}
healthy2Deployment := &v1.Deployment{Status: v1.DeploymentStatus{ReadyReplicas: 1}, Spec: v1.DeploymentSpec{Replicas: replicas}}
deploymentMock.EXPECT().Get(context.TODO(), "healthy1", metav1.GetOptions{}).Return(healthy1Deployment, nil)
deploymentMock.EXPECT().Get(context.TODO(), "healthy2", metav1.GetOptions{}).Return(healthy2Deployment, nil)
appsMock := newMockAppsV1Client(t)
Expand Down Expand Up @@ -468,7 +507,7 @@ func Test_server_GetAll(t *testing.T) {
Results: []*health.DoguHealthCheck{{
Type: "container",
Success: true,
Message: "Check whether a deployment contains at least one ready replica.",
Message: "Check whether a deployment has at least one replica, ready or not.",
}},
},
"healthy2": {
Expand All @@ -479,7 +518,7 @@ func Test_server_GetAll(t *testing.T) {
Results: []*health.DoguHealthCheck{{
Type: "container",
Success: true,
Message: "Check whether a deployment contains at least one ready replica.",
Message: "Check whether a deployment has at least one replica, ready or not.",
}},
},
},
Expand Down

0 comments on commit dceadc5

Please sign in to comment.