Skip to content

Commit

Permalink
Merge pull request #55 from grezar/grezar-patch-1
Browse files Browse the repository at this point in the history
make sure that circleci provider returns an error when the matching resource is not found
  • Loading branch information
Riki Makita authored Dec 22, 2021
2 parents 32c5091 + f306133 commit ab0c6b0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
2 changes: 2 additions & 0 deletions provider/to/circleci/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ func (s *Spec) UpdateContexts(ctx context.Context, dryRun bool, api *circleci.Cl
var contextID string
if v, ok := contextList[c.Name]; ok {
contextID = v.ID
} else {
return fmt.Errorf("circleci context not found: %s", c.Name)
}

variableValue, err := secrets.ExecuteTemplate(ctx, v.Value)
Expand Down
83 changes: 79 additions & 4 deletions provider/to/circleci/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package circleci

import (
"context"
"errors"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -183,6 +184,33 @@ func TestSpec_UpdateProjectVariables(t *testing.T) {
},
},
},
{
name: "It returns an error when the matching project is not found",
fields: fields{
Owner: "org1",
ProjectVariables: []*ProjectVariable{
{
Project: "prj1",
Variables: []*Variable{
{
Name: "SECRET1",
Value: "111",
},
},
},
},
Projects: func(t *testing.T, ctrl *gomock.Controller) *mock.MockProjects {
t.Helper()

ctx := context.Background()
project := "prj1"
mock := mock.NewMockProjects(ctrl)
mock.EXPECT().ListVariables(ctx, project).Return(nil, errors.New("project is not found"))
return mock
},
},
wantErr: true,
},
{
name: "Met conditions for updating project variables but doesn't do destructive changes in dry-run mode",
fields: fields{
Expand Down Expand Up @@ -275,12 +303,17 @@ func TestSpec_UpdateiContexts(t *testing.T) {
OwnerSlug: circleci.String("org1"),
PageToken: circleci.String(""),
}).Return(&circleci.ContextList{
Items: []*circleci.Context{},
Items: []*circleci.Context{
{
ID: "ctx-1",
Name: "ctx1",
},
},
}, nil)
mock.EXPECT().AddOrUpdateVariable(ctx, "", "SECRET1", circleci.ContextAddOrUpdateVariableOptions{
mock.EXPECT().AddOrUpdateVariable(ctx, "ctx-1", "SECRET1", circleci.ContextAddOrUpdateVariableOptions{
Value: circleci.String("111"),
})
mock.EXPECT().AddOrUpdateVariable(ctx, "", "SECRET2", circleci.ContextAddOrUpdateVariableOptions{
mock.EXPECT().AddOrUpdateVariable(ctx, "ctx-1", "SECRET2", circleci.ContextAddOrUpdateVariableOptions{
Value: circleci.String("222"),
})
return mock
Expand Down Expand Up @@ -315,7 +348,12 @@ func TestSpec_UpdateiContexts(t *testing.T) {
OwnerSlug: circleci.String("org1"),
PageToken: circleci.String(""),
}).Return(&circleci.ContextList{
Items: []*circleci.Context{},
Items: []*circleci.Context{
{
ID: "ctx-1",
Name: "ctx1",
},
},
}, nil)
return mock
},
Expand Down Expand Up @@ -396,6 +434,43 @@ func TestSpec_UpdateiContexts(t *testing.T) {
dryRun: true,
},
},
{
name: "It returns an error when the matching context is not found",
fields: fields{
Owner: "org1",
ContextVariables: []*Context{
{
Name: "ctx-not-found",
Variables: []*Variable{
{
Name: "SECRET1",
Value: "111",
},
},
},
},
Contexts: func(t *testing.T, ctrl *gomock.Controller) *mock.MockContexts {
t.Helper()

ctx := context.Background()
mock := mock.NewMockContexts(ctrl)
mock.EXPECT().List(ctx, circleci.ContextListOptions{
OwnerSlug: circleci.String("org1"),
PageToken: circleci.String(""),
}).Return(&circleci.ContextList{
Items: []*circleci.Context{
{
ID: "ctx-1",
Name: "ctx1",
},
},
}, nil)
return mock
},
dryRun: true,
},
wantErr: true,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit ab0c6b0

Please sign in to comment.