Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pkg/appWorkflow/AppWorkflowService.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,9 @@ func (impl AppWorkflowServiceImpl) FindAllAppWorkflowMapping(workflowIds []int)
impl.Logger.Errorw("error occurred while filtering out delete request pipelines", "cdPipelineIds", cdPipelineIds, "err", err)
return nil, err
}
for _, workflowMapping := range workflowMappingDtos {
if workflowMapping.Type == "CD_PIPELINE" && cdPipelineIdMap[workflowMapping.ComponentId] {
workflowMapping.DeploymentAppDeleteRequest = true
for i := range workflowMappingDtos {
if workflowMappingDtos[i].Type == bean4.CD_PIPELINE_TYPE && cdPipelineIdMap[workflowMappingDtos[i].ComponentId] {
workflowMappingDtos[i].DeploymentAppDeleteRequest = true
}
}
}
Expand Down
112 changes: 112 additions & 0 deletions pkg/appWorkflow/AppWorkflowService_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package appWorkflow

import (
"testing"

"github.com/devtron-labs/devtron/internal/sql/repository/appWorkflow"
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
bean4 "github.com/devtron-labs/devtron/pkg/appWorkflow/bean"
"go.uber.org/zap"
)

// Only the two methods FindAllAppWorkflowMapping calls are implemented; the
// embedded interfaces satisfy the rest and panic if anything else is reached.
type stubAppWorkflowRepository struct {
appWorkflow.AppWorkflowRepository
mappings []*appWorkflow.AppWorkflowMapping
}

func (s stubAppWorkflowRepository) FindByWorkflowIds(_ []int) ([]*appWorkflow.AppWorkflowMapping, error) {
return s.mappings, nil
}

type stubPipelineRepository struct {
pipelineConfig.PipelineRepository
deleteRequested map[int]bool
}

func (s stubPipelineRepository) FilterDeploymentDeleteRequestedPipelineIds(_ []int) (map[int]bool, error) {
return s.deleteRequested, nil
}

func TestFindAllAppWorkflowMappingDeleteRequest(t *testing.T) {

Check failure on line 48 in pkg/appWorkflow/AppWorkflowService_test.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 19 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=devtron-labs_devtron&issues=AaFPJCfOXwLrZRhmraRL&open=AaFPJCfOXwLrZRhmraRL&pullRequest=7063
const workflowId = 1

mappings := []*appWorkflow.AppWorkflowMapping{
{Id: 1, ComponentId: 100, AppWorkflowId: workflowId, Type: bean4.CI_PIPELINE_TYPE},
{Id: 2, ComponentId: 200, AppWorkflowId: workflowId, Type: bean4.CD_PIPELINE_TYPE, ParentId: 100, ParentType: bean4.CI_PIPELINE_TYPE},
{Id: 3, ComponentId: 201, AppWorkflowId: workflowId, Type: bean4.CD_PIPELINE_TYPE, ParentId: 200, ParentType: bean4.CD_PIPELINE_TYPE},
}

tests := []struct {
name string
deleteRequested map[int]bool
want map[int]bool // component id -> expected DeploymentAppDeleteRequest
}{
{
name: "no pipeline has a delete request",
deleteRequested: map[int]bool{},
want: map[int]bool{100: false, 200: false, 201: false},
},
{
name: "one cd pipeline has a delete request",
deleteRequested: map[int]bool{200: true},
want: map[int]bool{100: false, 200: true, 201: false},
},
{
name: "both cd pipelines have a delete request",
deleteRequested: map[int]bool{200: true, 201: true},
want: map[int]bool{100: false, 200: true, 201: true},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
impl := AppWorkflowServiceImpl{
Logger: zap.NewNop().Sugar(),
appWorkflowRepository: stubAppWorkflowRepository{mappings: mappings},
pipelineRepository: stubPipelineRepository{deleteRequested: tt.deleteRequested},
}

result, err := impl.FindAllAppWorkflowMapping([]int{workflowId})
if err != nil {
t.Fatalf("FindAllAppWorkflowMapping returned error: %v", err)
}

got := map[int]bool{}
for _, dto := range result[workflowId] {
got[dto.ComponentId] = dto.DeploymentAppDeleteRequest
}
for componentId, want := range tt.want {
if got[componentId] != want {
t.Errorf("component %d: DeploymentAppDeleteRequest = %v, want %v",
componentId, got[componentId], want)
}
}

// IsLast is set on the local before the append in the same loop, so
// it arriving correctly shows the DTOs really were populated.
for _, dto := range result[workflowId] {
if dto.ComponentId == 201 && !dto.IsLast {
t.Errorf("component 201: IsLast = false, want true")
}
}
})
}
}