|
| 1 | +package terraformrun |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + configv1alpha1 "github.com/padok-team/burrito/api/v1alpha1" |
| 9 | + "github.com/padok-team/burrito/internal/controllers/terraformpullrequest/comment" |
| 10 | + "github.com/padok-team/burrito/internal/controllers/terraformpullrequest/status" |
| 11 | + repositorytypes "github.com/padok-team/burrito/internal/repository/types" |
| 12 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 13 | +) |
| 14 | + |
| 15 | +type fakeAPIProvider struct { |
| 16 | + setStatusCalls []status.CommitStatus |
| 17 | + setStatusErr error |
| 18 | + getProviderErr error |
| 19 | +} |
| 20 | + |
| 21 | +func (p *fakeAPIProvider) GetChanges(repository *configv1alpha1.TerraformRepository, pullRequest *configv1alpha1.TerraformPullRequest) ([]string, error) { |
| 22 | + return nil, nil |
| 23 | +} |
| 24 | + |
| 25 | +func (p *fakeAPIProvider) Comment(repository *configv1alpha1.TerraformRepository, pullRequest *configv1alpha1.TerraformPullRequest, c comment.Comment) error { |
| 26 | + return nil |
| 27 | +} |
| 28 | + |
| 29 | +func (p *fakeAPIProvider) ListPullRequests(repository *configv1alpha1.TerraformRepository) ([]configv1alpha1.TerraformPullRequest, error) { |
| 30 | + return nil, nil |
| 31 | +} |
| 32 | + |
| 33 | +func (p *fakeAPIProvider) SetStatus(repository *configv1alpha1.TerraformRepository, pullRequest *configv1alpha1.TerraformPullRequest, s status.CommitStatus) error { |
| 34 | + p.setStatusCalls = append(p.setStatusCalls, s) |
| 35 | + return p.setStatusErr |
| 36 | +} |
| 37 | + |
| 38 | +func (p *fakeAPIProvider) GetMergeCommit(repository *configv1alpha1.TerraformRepository, pullRequest *configv1alpha1.TerraformPullRequest) (string, error) { |
| 39 | + return "", nil |
| 40 | +} |
| 41 | + |
| 42 | +func testRepository() *configv1alpha1.TerraformRepository { |
| 43 | + return &configv1alpha1.TerraformRepository{ |
| 44 | + ObjectMeta: metav1.ObjectMeta{Name: "repo", Namespace: "default"}, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func testMainLayer() *configv1alpha1.TerraformLayer { |
| 49 | + return &configv1alpha1.TerraformLayer{ |
| 50 | + ObjectMeta: metav1.ObjectMeta{Name: "pwet", Namespace: "default"}, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func testPullRequestLayer() *configv1alpha1.TerraformLayer { |
| 55 | + return &configv1alpha1.TerraformLayer{ |
| 56 | + ObjectMeta: metav1.ObjectMeta{ |
| 57 | + Name: "pwet-pr-abcdef", |
| 58 | + Namespace: "default", |
| 59 | + OwnerReferences: []metav1.OwnerReference{ |
| 60 | + {Kind: "TerraformPullRequest", Name: "repo-1"}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func testRun(action string, revision string) *configv1alpha1.TerraformRun { |
| 67 | + return &configv1alpha1.TerraformRun{ |
| 68 | + ObjectMeta: metav1.ObjectMeta{Name: "pwet-" + action + "-abcde", Namespace: "default"}, |
| 69 | + Spec: configv1alpha1.TerraformRunSpec{ |
| 70 | + Action: action, |
| 71 | + Layer: configv1alpha1.TerraformRunLayer{Revision: revision}, |
| 72 | + }, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestIsPullRequestLayer(t *testing.T) { |
| 77 | + if isPullRequestLayer(testMainLayer()) { |
| 78 | + t.Fatalf("expected a plain main-branch layer to not be considered a pull request layer") |
| 79 | + } |
| 80 | + if !isPullRequestLayer(testPullRequestLayer()) { |
| 81 | + t.Fatalf("expected a layer owned by a TerraformPullRequest to be considered a pull request layer") |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestSetCommitStatusForDirectPushSkipsPullRequestLayers(t *testing.T) { |
| 86 | + provider := &fakeAPIProvider{} |
| 87 | + r := &Reconciler{ |
| 88 | + APIProviderFactory: func(repository *configv1alpha1.TerraformRepository) (repositorytypes.APIProvider, error) { |
| 89 | + return provider, nil |
| 90 | + }, |
| 91 | + } |
| 92 | + r.setCommitStatusForDirectPush(context.Background(), testRun("plan", "sha123"), testPullRequestLayer(), testRepository(), status.StateSuccess) |
| 93 | + if len(provider.setStatusCalls) != 0 { |
| 94 | + t.Fatalf("expected no commit status to be set for a pull request layer, got %d calls", len(provider.setStatusCalls)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestSetCommitStatusForDirectPushSkipsWhenRevisionIsEmpty(t *testing.T) { |
| 99 | + provider := &fakeAPIProvider{} |
| 100 | + r := &Reconciler{ |
| 101 | + APIProviderFactory: func(repository *configv1alpha1.TerraformRepository) (repositorytypes.APIProvider, error) { |
| 102 | + return provider, nil |
| 103 | + }, |
| 104 | + } |
| 105 | + r.setCommitStatusForDirectPush(context.Background(), testRun("plan", ""), testMainLayer(), testRepository(), status.StateSuccess) |
| 106 | + if len(provider.setStatusCalls) != 0 { |
| 107 | + t.Fatalf("expected no commit status to be set when the run has no revision, got %d calls", len(provider.setStatusCalls)) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestSetCommitStatusForDirectPushPostsPlanStatus(t *testing.T) { |
| 112 | + provider := &fakeAPIProvider{} |
| 113 | + r := &Reconciler{ |
| 114 | + APIProviderFactory: func(repository *configv1alpha1.TerraformRepository) (repositorytypes.APIProvider, error) { |
| 115 | + return provider, nil |
| 116 | + }, |
| 117 | + } |
| 118 | + layer := testMainLayer() |
| 119 | + r.setCommitStatusForDirectPush(context.Background(), testRun("plan", "sha123"), layer, testRepository(), status.StateSuccess) |
| 120 | + |
| 121 | + if len(provider.setStatusCalls) != 1 { |
| 122 | + t.Fatalf("expected exactly one commit status to be set, got %d", len(provider.setStatusCalls)) |
| 123 | + } |
| 124 | + got := provider.setStatusCalls[0] |
| 125 | + if got.Phase != status.PhasePlan { |
| 126 | + t.Errorf("expected phase %q, got %q", status.PhasePlan, got.Phase) |
| 127 | + } |
| 128 | + if got.State != status.StateSuccess { |
| 129 | + t.Errorf("expected state %q, got %q", status.StateSuccess, got.State) |
| 130 | + } |
| 131 | + if got.Commit != "sha123" { |
| 132 | + t.Errorf("expected commit %q, got %q", "sha123", got.Commit) |
| 133 | + } |
| 134 | + wantContext := "burrito/plan/pwet" |
| 135 | + if got.Context != wantContext { |
| 136 | + t.Errorf("expected context %q, got %q", wantContext, got.Context) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func TestSetCommitStatusForDirectPushPostsApplyFailureStatus(t *testing.T) { |
| 141 | + provider := &fakeAPIProvider{} |
| 142 | + r := &Reconciler{ |
| 143 | + APIProviderFactory: func(repository *configv1alpha1.TerraformRepository) (repositorytypes.APIProvider, error) { |
| 144 | + return provider, nil |
| 145 | + }, |
| 146 | + } |
| 147 | + r.setCommitStatusForDirectPush(context.Background(), testRun("apply", "sha456"), testMainLayer(), testRepository(), status.StateFailure) |
| 148 | + |
| 149 | + if len(provider.setStatusCalls) != 1 { |
| 150 | + t.Fatalf("expected exactly one commit status to be set, got %d", len(provider.setStatusCalls)) |
| 151 | + } |
| 152 | + got := provider.setStatusCalls[0] |
| 153 | + if got.Phase != status.PhaseApply { |
| 154 | + t.Errorf("expected phase %q, got %q", status.PhaseApply, got.Phase) |
| 155 | + } |
| 156 | + if got.State != status.StateFailure { |
| 157 | + t.Errorf("expected state %q, got %q", status.StateFailure, got.State) |
| 158 | + } |
| 159 | + if got.Description != "Burrito apply failed" { |
| 160 | + t.Errorf("unexpected description: %q", got.Description) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +func TestSetCommitStatusForDirectPushDoesNotPanicOnProviderError(t *testing.T) { |
| 165 | + r := &Reconciler{ |
| 166 | + APIProviderFactory: func(repository *configv1alpha1.TerraformRepository) (repositorytypes.APIProvider, error) { |
| 167 | + return nil, errors.New("no provider configured") |
| 168 | + }, |
| 169 | + } |
| 170 | + r.setCommitStatusForDirectPush(context.Background(), testRun("plan", "sha123"), testMainLayer(), testRepository(), status.StateSuccess) |
| 171 | +} |
0 commit comments