Skip to content

Commit

Permalink
add additional unit tests for auto merge
Browse files Browse the repository at this point in the history
Signed-off-by: davidwin93 <[email protected]>
  • Loading branch information
davidwin93 committed Oct 19, 2023
1 parent aed8c9e commit 7b842ef
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions pkg/vcs/gitlab/mr_status_updater_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package gitlab

import (
"context"
"errors"
"testing"

"github.com/hashicorp/go-tfe"
gogitlab "github.com/xanzy/go-gitlab"
"github.com/zapier/tfbuddy/pkg/mocks"
"github.com/zapier/tfbuddy/pkg/runstream"
"github.com/zapier/tfbuddy/pkg/vcs"
"go.uber.org/mock/gomock"
)

func TestAutoMergeNoChangesApply(t *testing.T) {

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
testSuite := mocks.CreateTestSuite(mockCtrl, mocks.TestOverrides{}, t)

testSuite.MockGitClient.EXPECT().MergeMR(gomock.Any(), gomock.Any(), gomock.Any())
testSuite.MockGitClient.EXPECT().GetPipelinesForCommit(gomock.Any(), gomock.Any(), gomock.Any()).Return([]vcs.ProjectPipeline{&GitlabPipeline{&gogitlab.PipelineInfo{ID: 1}}}, nil).AnyTimes()
testSuite.MockGitClient.EXPECT().SetCommitStatus(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("could not commit status")).AnyTimes()
testSuite.InitTestSuite()
r := &RunStatusUpdater{
tfc: testSuite.MockApiClient,
client: testSuite.MockGitClient,
rs: testSuite.MockStreamClient,
}
r.updateCommitStatusForRun(context.Background(), &tfe.Run{
Status: tfe.RunPlannedAndFinished,
HasChanges: false,
}, &runstream.TFRunMetadata{
Action: "apply",
AutoMerge: true,
})
}
func TestAutoMergeTargetedNoChangesApply(t *testing.T) {

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
testSuite := mocks.CreateTestSuite(mockCtrl, mocks.TestOverrides{}, t)

testSuite.MockGitClient.EXPECT().MergeMR(gomock.Any(), gomock.Any(), gomock.Any()).MaxTimes(0)

testSuite.MockGitClient.EXPECT().GetPipelinesForCommit(gomock.Any(), gomock.Any(), gomock.Any()).Return([]vcs.ProjectPipeline{&GitlabPipeline{&gogitlab.PipelineInfo{ID: 1}}}, nil).AnyTimes()
testSuite.MockGitClient.EXPECT().SetCommitStatus(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("could not commit status")).AnyTimes()
testSuite.InitTestSuite()
r := &RunStatusUpdater{
tfc: testSuite.MockApiClient,
client: testSuite.MockGitClient,
rs: testSuite.MockStreamClient,
}
r.updateCommitStatusForRun(context.Background(), &tfe.Run{
Status: tfe.RunPlannedAndFinished,
HasChanges: false,
TargetAddrs: []string{"module.foo"},
}, &runstream.TFRunMetadata{
Action: "apply",
AutoMerge: true,
})
}

func TestAutoMergeApply(t *testing.T) {

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
testSuite := mocks.CreateTestSuite(mockCtrl, mocks.TestOverrides{}, t)

testSuite.MockGitClient.EXPECT().MergeMR(gomock.Any(), gomock.Any(), gomock.Any())
testSuite.MockGitClient.EXPECT().GetPipelinesForCommit(gomock.Any(), gomock.Any(), gomock.Any()).Return([]vcs.ProjectPipeline{&GitlabPipeline{&gogitlab.PipelineInfo{ID: 1}}}, nil).AnyTimes()
testSuite.MockGitClient.EXPECT().SetCommitStatus(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("could not commit status")).AnyTimes()
testSuite.InitTestSuite()
r := &RunStatusUpdater{
tfc: testSuite.MockApiClient,
client: testSuite.MockGitClient,
rs: testSuite.MockStreamClient,
}
r.updateCommitStatusForRun(context.Background(), &tfe.Run{
Status: tfe.RunApplied,
HasChanges: true,
}, &runstream.TFRunMetadata{
Action: "apply",
AutoMerge: true,
})
}

func TestAutoMergeTargetedApply(t *testing.T) {

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
testSuite := mocks.CreateTestSuite(mockCtrl, mocks.TestOverrides{}, t)

testSuite.MockGitClient.EXPECT().MergeMR(gomock.Any(), gomock.Any(), gomock.Any()).MaxTimes(0)

testSuite.MockGitClient.EXPECT().GetPipelinesForCommit(gomock.Any(), gomock.Any(), gomock.Any()).Return([]vcs.ProjectPipeline{&GitlabPipeline{&gogitlab.PipelineInfo{ID: 1}}}, nil).AnyTimes()
testSuite.MockGitClient.EXPECT().SetCommitStatus(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("could not commit status")).AnyTimes()
testSuite.InitTestSuite()
r := &RunStatusUpdater{
tfc: testSuite.MockApiClient,
client: testSuite.MockGitClient,
rs: testSuite.MockStreamClient,
}
r.updateCommitStatusForRun(context.Background(), &tfe.Run{
Status: tfe.RunApplied,
HasChanges: true,
TargetAddrs: []string{"module.foo"},
}, &runstream.TFRunMetadata{
Action: "apply",
AutoMerge: true,
})
}

0 comments on commit 7b842ef

Please sign in to comment.