Skip to content

Commit

Permalink
return an error when the matching context is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
grezar committed Dec 22, 2021
1 parent 74343e0 commit 368f6d0
Show file tree
Hide file tree
Showing 2 changed files with 53 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
55 changes: 51 additions & 4 deletions provider/to/circleci/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,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 +320,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 +406,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 368f6d0

Please sign in to comment.