From e8bcc298c0e207ff5233405673ddb1059a9b12a3 Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Wed, 25 Oct 2023 22:41:03 +0900 Subject: [PATCH 1/2] Add tests to model/deployment_chain Signed-off-by: Kenta Kozuka --- pkg/model/deployment_chain_test.go | 236 +++++++++++++++++++++++++++++ 1 file changed, 236 insertions(+) diff --git a/pkg/model/deployment_chain_test.go b/pkg/model/deployment_chain_test.go index 80ed67a23a..f9e7c6abd7 100644 --- a/pkg/model/deployment_chain_test.go +++ b/pkg/model/deployment_chain_test.go @@ -110,3 +110,239 @@ func TestDeploymentChainDesireStatus(t *testing.T) { }) } } +func TestListAllInChainApplicationDeploymentsMap(t *testing.T) { + dc := &DeploymentChain{ + Blocks: []*ChainBlock{ + { + Nodes: []*ChainNode{ + { + ApplicationRef: &ChainApplicationRef{ApplicationId: "app1"}, + DeploymentRef: &ChainDeploymentRef{DeploymentId: "dep1"}, + }, + { + ApplicationRef: &ChainApplicationRef{ApplicationId: "app2"}, + DeploymentRef: nil, + }, + }, + }, + { + Nodes: []*ChainNode{ + { + ApplicationRef: &ChainApplicationRef{ApplicationId: "app2"}, + DeploymentRef: &ChainDeploymentRef{DeploymentId: "dep2"}, + }, + { + ApplicationRef: &ChainApplicationRef{ApplicationId: "app3"}, + DeploymentRef: &ChainDeploymentRef{DeploymentId: "dep3"}, + }, + }, + }, + }, + } + + want := map[string]*ChainDeploymentRef{ + "app1": {DeploymentId: "dep1"}, + "app2": {DeploymentId: "dep2"}, + "app3": {DeploymentId: "dep3"}, + } + + got := dc.ListAllInChainApplicationDeploymentsMap() + + assert.Equal(t, want, got) +} +func TestListAllInChainApplications(t *testing.T) { + dc := &DeploymentChain{ + Blocks: []*ChainBlock{ + { + Nodes: []*ChainNode{ + { + ApplicationRef: &ChainApplicationRef{ApplicationId: "app1"}, + DeploymentRef: &ChainDeploymentRef{DeploymentId: "dep1"}, + }, + { + ApplicationRef: &ChainApplicationRef{ApplicationId: "app2"}, + DeploymentRef: nil, + }, + }, + }, + { + Nodes: []*ChainNode{ + { + ApplicationRef: &ChainApplicationRef{ApplicationId: "app2"}, + DeploymentRef: &ChainDeploymentRef{DeploymentId: "dep2"}, + }, + { + ApplicationRef: &ChainApplicationRef{ApplicationId: "app3"}, + DeploymentRef: &ChainDeploymentRef{DeploymentId: "dep3"}, + }, + }, + }, + }, + } + + want := []*ChainApplicationRef{ + {ApplicationId: "app1"}, + {ApplicationId: "app2"}, + {ApplicationId: "app2"}, + {ApplicationId: "app3"}, + } + + got := dc.ListAllInChainApplications() + + assert.Equal(t, want, got) +} + +func TestIsCompleted(t *testing.T) { + tests := []struct { + name string + status ChainBlockStatus + want bool + }{ + { + name: "returns true for DEPLOYMENT_BLOCK_SUCCESS", + status: ChainBlockStatus_DEPLOYMENT_BLOCK_SUCCESS, + want: true, + }, + { + name: "returns true for DEPLOYMENT_BLOCK_FAILURE", + status: ChainBlockStatus_DEPLOYMENT_BLOCK_FAILURE, + want: true, + }, + { + name: "returns true for DEPLOYMENT_BLOCK_CANCELLED", + status: ChainBlockStatus_DEPLOYMENT_BLOCK_CANCELLED, + want: true, + }, + { + name: "returns false for DEPLOYMENT_BLOCK_PENDING", + status: ChainBlockStatus_DEPLOYMENT_BLOCK_PENDING, + want: false, + }, + { + name: "returns false for DEPLOYMENT_BLOCK_RUNNING", + status: ChainBlockStatus_DEPLOYMENT_BLOCK_RUNNING, + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + b := &ChainBlock{Status: tt.status} + got := b.IsCompleted() + assert.Equal(t, tt.want, got) + }) + } +} +func TestDesiredStatus(t *testing.T) { + tests := []struct { + name string + block *ChainBlock + wantDesired ChainBlockStatus + wantIsComplete bool + }{ + { + name: "returns DEPLOYMENT_BLOCK_SUCCESS for all successful deployments", + block: &ChainBlock{ + Nodes: []*ChainNode{ + {DeploymentRef: &ChainDeploymentRef{Status: DeploymentStatus_DEPLOYMENT_SUCCESS}}, + {DeploymentRef: &ChainDeploymentRef{Status: DeploymentStatus_DEPLOYMENT_SUCCESS}}, + }, + }, + wantDesired: ChainBlockStatus_DEPLOYMENT_BLOCK_SUCCESS, + }, + { + name: "returns DEPLOYMENT_BLOCK_FAILURE for at least one failed deployment", + block: &ChainBlock{ + Nodes: []*ChainNode{ + {DeploymentRef: &ChainDeploymentRef{Status: DeploymentStatus_DEPLOYMENT_SUCCESS}}, + {DeploymentRef: &ChainDeploymentRef{Status: DeploymentStatus_DEPLOYMENT_FAILURE}}, + }, + }, + wantDesired: ChainBlockStatus_DEPLOYMENT_BLOCK_FAILURE, + }, + { + name: "returns DEPLOYMENT_BLOCK_CANCELLED for at least one cancelled deployment", + block: &ChainBlock{ + Nodes: []*ChainNode{ + {DeploymentRef: &ChainDeploymentRef{Status: DeploymentStatus_DEPLOYMENT_SUCCESS}}, + {DeploymentRef: &ChainDeploymentRef{Status: DeploymentStatus_DEPLOYMENT_CANCELLED}}, + }, + }, + wantDesired: ChainBlockStatus_DEPLOYMENT_BLOCK_CANCELLED, + }, + { + name: "returns DEPLOYMENT_BLOCK_RUNNING for at least one running deployment", + block: &ChainBlock{ + Nodes: []*ChainNode{ + {DeploymentRef: &ChainDeploymentRef{Status: DeploymentStatus_DEPLOYMENT_SUCCESS}}, + {DeploymentRef: &ChainDeploymentRef{Status: DeploymentStatus_DEPLOYMENT_RUNNING}}, + }, + }, + wantDesired: ChainBlockStatus_DEPLOYMENT_BLOCK_RUNNING, + }, + { + name: "returns original status if no deployments", + block: &ChainBlock{ + Status: ChainBlockStatus_DEPLOYMENT_BLOCK_PENDING, + }, + wantDesired: ChainBlockStatus_DEPLOYMENT_BLOCK_SUCCESS, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotDesired := tt.block.DesiredStatus() + assert.Equal(t, tt.wantDesired, gotDesired) + }) + } +} + +func TestGetNodeByDeploymentID(t *testing.T) { + block := &ChainBlock{ + Nodes: []*ChainNode{ + { + DeploymentRef: &ChainDeploymentRef{DeploymentId: "dep1"}, + }, + { + DeploymentRef: &ChainDeploymentRef{DeploymentId: "dep2"}, + }, + { + DeploymentRef: nil, + }, + }, + } + + tests := []struct { + name string + deploymentID string + wantNode *ChainNode + wantErr bool + }{ + { + name: "returns node with matching deployment ID", + deploymentID: "dep1", + wantNode: block.Nodes[0], + wantErr: false, + }, + { + name: "returns error for non-existent deployment ID", + deploymentID: "dep3", + wantNode: nil, + wantErr: true, + }, + { + name: "returns error for nil deployment ref", + deploymentID: "", + wantNode: nil, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotNode, err := block.GetNodeByDeploymentID(tt.deploymentID) + assert.Equal(t, tt.wantErr, err != nil) + assert.Equal(t, tt.wantNode, gotNode) + }) + } +} From daf20c741d921d3bed3df8fdd9aa3351c7b37c52 Mon Sep 17 00:00:00 2001 From: Kenta Kozuka Date: Wed, 1 Nov 2023 12:25:24 +0900 Subject: [PATCH 2/2] Add Parallel() Signed-off-by: Kenta Kozuka --- pkg/model/deployment_chain_test.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/model/deployment_chain_test.go b/pkg/model/deployment_chain_test.go index f9e7c6abd7..150ded5c28 100644 --- a/pkg/model/deployment_chain_test.go +++ b/pkg/model/deployment_chain_test.go @@ -21,6 +21,7 @@ import ( ) func TestDeploymentChainDesireStatus(t *testing.T) { + t.Parallel() testcases := []struct { name string deploymentChain DeploymentChain @@ -104,13 +105,16 @@ func TestDeploymentChainDesireStatus(t *testing.T) { } for _, tc := range testcases { + tc := tc t.Run(tc.name, func(t *testing.T) { + t.Parallel() desireStatus := tc.deploymentChain.DesiredStatus() assert.Equal(t, tc.expectedDesireStatus, desireStatus) }) } } func TestListAllInChainApplicationDeploymentsMap(t *testing.T) { + t.Parallel() dc := &DeploymentChain{ Blocks: []*ChainBlock{ { @@ -151,6 +155,7 @@ func TestListAllInChainApplicationDeploymentsMap(t *testing.T) { assert.Equal(t, want, got) } func TestListAllInChainApplications(t *testing.T) { + t.Parallel() dc := &DeploymentChain{ Blocks: []*ChainBlock{ { @@ -193,6 +198,7 @@ func TestListAllInChainApplications(t *testing.T) { } func TestIsCompleted(t *testing.T) { + t.Parallel() tests := []struct { name string status ChainBlockStatus @@ -226,7 +232,9 @@ func TestIsCompleted(t *testing.T) { } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { + t.Parallel() b := &ChainBlock{Status: tt.status} got := b.IsCompleted() assert.Equal(t, tt.want, got) @@ -234,6 +242,7 @@ func TestIsCompleted(t *testing.T) { } } func TestDesiredStatus(t *testing.T) { + t.Parallel() tests := []struct { name string block *ChainBlock @@ -290,7 +299,9 @@ func TestDesiredStatus(t *testing.T) { } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { + t.Parallel() gotDesired := tt.block.DesiredStatus() assert.Equal(t, tt.wantDesired, gotDesired) }) @@ -298,6 +309,7 @@ func TestDesiredStatus(t *testing.T) { } func TestGetNodeByDeploymentID(t *testing.T) { + t.Parallel() block := &ChainBlock{ Nodes: []*ChainNode{ { @@ -339,7 +351,9 @@ func TestGetNodeByDeploymentID(t *testing.T) { } for _, tt := range tests { + tt := tt t.Run(tt.name, func(t *testing.T) { + t.Parallel() gotNode, err := block.GetNodeByDeploymentID(tt.deploymentID) assert.Equal(t, tt.wantErr, err != nil) assert.Equal(t, tt.wantNode, gotNode)