From 368f6d0dc105f0b0a21d1514cf31a6cc22fbf6b7 Mon Sep 17 00:00:00 2001 From: grezar Date: Wed, 22 Dec 2021 21:37:15 +0900 Subject: [PATCH 1/2] return an error when the matching context is not found --- provider/to/circleci/provider.go | 2 + provider/to/circleci/provider_test.go | 55 +++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/provider/to/circleci/provider.go b/provider/to/circleci/provider.go index 7d98b10..fee3fae 100644 --- a/provider/to/circleci/provider.go +++ b/provider/to/circleci/provider.go @@ -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) diff --git a/provider/to/circleci/provider_test.go b/provider/to/circleci/provider_test.go index a37d23c..780b2ea 100644 --- a/provider/to/circleci/provider_test.go +++ b/provider/to/circleci/provider_test.go @@ -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 @@ -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 }, @@ -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 { From f306133d5d71c7278ea28ef4a533190367bdd184 Mon Sep 17 00:00:00 2001 From: grezar Date: Wed, 22 Dec 2021 21:38:13 +0900 Subject: [PATCH 2/2] add a test to check it returns an error when the matching project is not found --- provider/to/circleci/provider_test.go | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/provider/to/circleci/provider_test.go b/provider/to/circleci/provider_test.go index 780b2ea..bd50677 100644 --- a/provider/to/circleci/provider_test.go +++ b/provider/to/circleci/provider_test.go @@ -2,6 +2,7 @@ package circleci import ( "context" + "errors" "testing" "github.com/golang/mock/gomock" @@ -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{