Skip to content

Commit

Permalink
refactor: improve coverage to 95% (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdh authored Jul 19, 2022
1 parent 74bbfc6 commit 837bff1
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 24 deletions.
8 changes: 5 additions & 3 deletions core/rule/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,16 @@ func (s *Service) Upsert(ctx context.Context, rl *Rule) error {
return err
}

if prov.Type == provider.TypeCortex {
switch prov.Type {
case provider.TypeCortex:
if err = PostRuleGroupWithCortex(ctx, s.cortexClient, s.templateService, rl.Namespace, rl.GroupName, ns.URN, rulesWithinGroup); err != nil {
if err := s.repository.Rollback(ctx, err); err != nil {
return err
}
return err
}
default:
return errors.ErrInvalid.WithCausef("unknown provider type %s", prov.Type)
}

if err := s.repository.Commit(ctx); err != nil {
Expand Down Expand Up @@ -168,8 +171,7 @@ func PostRuleGroupWithCortex(ctx context.Context, client CortexClient, templateS
}

if renderedBodyForThisGroup == "" {
err := client.DeleteRuleGroup(cortex.NewContext(ctx, tenantName), nspace, groupName)
if err != nil {
if err := client.DeleteRuleGroup(cortex.NewContext(ctx, tenantName), nspace, groupName); err != nil {
if err.Error() == "requested resource not found" {
return nil
}
Expand Down
196 changes: 178 additions & 18 deletions core/rule/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestService_Upsert(t *testing.T) {
type testCase struct {
Description string
Rule *rule.Rule
Setup func(*mocks.RuleRepository, *mocks.TemplateService, *mocks.NamespaceService, *mocks.ProviderService)
Setup func(*mocks.RuleRepository, *mocks.TemplateService, *mocks.NamespaceService, *mocks.ProviderService, *mocks.CortexClient)
ErrString string
}
var (
Expand All @@ -38,7 +38,7 @@ func TestService_Upsert(t *testing.T) {
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService) {
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(nil, errors.New("some error"))
},
ErrString: "some error",
Expand All @@ -57,7 +57,7 @@ func TestService_Upsert(t *testing.T) {
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService) {
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(nil, errors.New("some error"))
},
Expand All @@ -77,15 +77,15 @@ func TestService_Upsert(t *testing.T) {
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService) {
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&namespace.Namespace{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(nil, errors.New("some error"))
},
ErrString: "some error",
},
{
Description: "should return error if list repository return error",
Description: "should return error if upsert repository return error",
Rule: &rule.Rule{
Name: "foo",
Namespace: "namespace",
Expand All @@ -98,19 +98,20 @@ func TestService_Upsert(t *testing.T) {
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService) {
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&namespace.Namespace{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{}, nil)

rr.EXPECT().WithTransaction(ctx).Return(ctx)
rr.EXPECT().Upsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*rule.Rule")).Return(errors.New("some error"))
rr.EXPECT().Rollback(ctx, errors.New("some error")).Return(nil)
rr.EXPECT().Upsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*rule.Rule")).Return(nil)
rr.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("rule.Filter")).Return(nil, errors.New("some error"))
rr.EXPECT().Rollback(ctx, mock.Anything).Return(nil)
},
ErrString: "some error",
},
{
Description: "should return error if upsert repository return error",
Description: "should return error if upsert repository return error and rollback error",
Rule: &rule.Rule{
Name: "foo",
Namespace: "namespace",
Expand All @@ -123,18 +124,68 @@ func TestService_Upsert(t *testing.T) {
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService) {
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&namespace.Namespace{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{}, nil)

rr.EXPECT().WithTransaction(ctx).Return(ctx)
rr.EXPECT().Upsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*rule.Rule")).Return(nil)
rr.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("rule.Filter")).Return(nil, errors.New("some error"))
rr.EXPECT().Rollback(ctx, mock.Anything).Return(errors.New("rollback error"))
},
ErrString: "rollback error",
},
{
Description: "should return error if list repository return error",
Rule: &rule.Rule{
Name: "foo",
Namespace: "namespace",
Variables: []rule.RuleVariable{
{
Name: "var1",
Type: "type",
Value: "value",
Description: "description",
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&namespace.Namespace{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{}, nil)

rr.EXPECT().WithTransaction(ctx).Return(ctx)
rr.EXPECT().Upsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*rule.Rule")).Return(errors.New("some error"))
rr.EXPECT().Rollback(ctx, errors.New("some error")).Return(nil)
},
ErrString: "some error",
},
{
Description: "should return error if list repository return error and rollback error",
Rule: &rule.Rule{
Name: "foo",
Namespace: "namespace",
Variables: []rule.RuleVariable{
{
Name: "var1",
Type: "type",
Value: "value",
Description: "description",
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&namespace.Namespace{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{}, nil)

rr.EXPECT().WithTransaction(ctx).Return(ctx)
rr.EXPECT().Upsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*rule.Rule")).Return(errors.New("some error"))
rr.EXPECT().Rollback(ctx, errors.New("some error")).Return(errors.New("rollback error"))
},
ErrString: "rollback error",
},
{
Description: "should return nil error if upsert repository success",
Rule: &rule.Rule{
Expand All @@ -149,7 +200,7 @@ func TestService_Upsert(t *testing.T) {
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService) {
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{
Variables: []template.Variable{
{
Expand All @@ -161,7 +212,9 @@ func TestService_Upsert(t *testing.T) {
},
}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&namespace.Namespace{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{
Type: provider.TypeCortex,
}, nil)

rr.EXPECT().WithTransaction(ctx).Return(ctx)
rr.EXPECT().Upsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*rule.Rule")).Return(nil)
Expand All @@ -177,17 +230,116 @@ func TestService_Upsert(t *testing.T) {
},
},
}}, nil)
cc.EXPECT().DeleteRuleGroup(mock.Anything, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(nil)
rr.EXPECT().Commit(ctx).Return(nil)
},
},
{
Description: "should return nil error if upsert repository success but post rule failed and rollback success",
Rule: &rule.Rule{
Name: "foo",
Namespace: "namespace",
Variables: []rule.RuleVariable{
{
Name: "var1",
Type: "type",
Value: "value",
Description: "description",
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{
Variables: []template.Variable{
{
Name: "var1",
Type: "type",
Default: "value",
Description: "description",
},
},
}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&namespace.Namespace{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{
Type: provider.TypeCortex,
}, nil)

rr.EXPECT().WithTransaction(ctx).Return(ctx)
rr.EXPECT().Upsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*rule.Rule")).Return(nil)
rr.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("rule.Filter")).Return([]rule.Rule{{
Name: "foo",
Namespace: "namespace",
Variables: []rule.RuleVariable{
{
Name: "var1",
Type: "type",
Value: "value",
Description: "description",
},
},
}}, nil)
cc.EXPECT().DeleteRuleGroup(mock.Anything, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(errors.New("some error"))
rr.EXPECT().Rollback(ctx, mock.Anything).Return(nil)
},
ErrString: "error calling cortex: some error",
},
{
Description: "should return nil error if upsert repository success but post rule failed and rollback failed",
Rule: &rule.Rule{
Name: "foo",
Namespace: "namespace",
Variables: []rule.RuleVariable{
{
Name: "var1",
Type: "type",
Value: "value",
Description: "description",
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{
Variables: []template.Variable{
{
Name: "var1",
Type: "type",
Default: "value",
Description: "description",
},
},
}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&namespace.Namespace{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{
Type: provider.TypeCortex,
}, nil)

rr.EXPECT().WithTransaction(ctx).Return(ctx)
rr.EXPECT().Upsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*rule.Rule")).Return(nil)
rr.EXPECT().List(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("rule.Filter")).Return([]rule.Rule{{
Name: "foo",
Namespace: "namespace",
Variables: []rule.RuleVariable{
{
Name: "var1",
Type: "type",
Value: "value",
Description: "description",
},
},
}}, nil)
cc.EXPECT().DeleteRuleGroup(mock.Anything, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(errors.New("some error"))
rr.EXPECT().Rollback(ctx, mock.Anything).Return(errors.New("rollback error"))
},
ErrString: "rollback error",
},
{
Description: "should return nil error if upsert repository success and rule has no variables",
Rule: &rule.Rule{
Name: "foo",
Namespace: "namespace",
Variables: []rule.RuleVariable{},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService) {
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{
Variables: []template.Variable{
{
Expand All @@ -199,7 +351,9 @@ func TestService_Upsert(t *testing.T) {
},
}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&namespace.Namespace{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{
Type: provider.TypeCortex,
}, nil)

rr.EXPECT().WithTransaction(ctx).Return(ctx)
rr.EXPECT().Upsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*rule.Rule")).Return(nil)
Expand All @@ -215,6 +369,7 @@ func TestService_Upsert(t *testing.T) {
},
},
}}, nil)
cc.EXPECT().DeleteRuleGroup(mock.Anything, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(nil)
rr.EXPECT().Commit(ctx).Return(nil)
},
},
Expand All @@ -232,7 +387,7 @@ func TestService_Upsert(t *testing.T) {
},
},
},
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService) {
Setup: func(rr *mocks.RuleRepository, ts *mocks.TemplateService, ns *mocks.NamespaceService, ps *mocks.ProviderService, cc *mocks.CortexClient) {
ts.EXPECT().GetByName(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("string")).Return(&template.Template{
Variables: []template.Variable{
{
Expand All @@ -244,7 +399,9 @@ func TestService_Upsert(t *testing.T) {
},
}, nil)
ns.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&namespace.Namespace{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{}, nil)
ps.EXPECT().Get(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("uint64")).Return(&provider.Provider{
Type: provider.TypeCortex,
}, nil)

rr.EXPECT().WithTransaction(ctx).Return(ctx)
rr.EXPECT().Upsert(mock.AnythingOfType("*context.emptyCtx"), mock.AnythingOfType("*rule.Rule")).Return(nil)
Expand All @@ -260,6 +417,7 @@ func TestService_Upsert(t *testing.T) {
},
},
}}, nil)
cc.EXPECT().DeleteRuleGroup(mock.Anything, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(nil)
rr.EXPECT().Commit(ctx).Return(errors.New("some commit error"))
},
ErrString: "some commit error",
Expand All @@ -274,10 +432,11 @@ func TestService_Upsert(t *testing.T) {
templateServiceMock = new(mocks.TemplateService)
namespaceServiceMock = new(mocks.NamespaceService)
providerServiceMock = new(mocks.ProviderService)
cortexClientMock = new(mocks.CortexClient)
)
svc := rule.NewService(repositoryMock, templateServiceMock, namespaceServiceMock, providerServiceMock, nil)
svc := rule.NewService(repositoryMock, templateServiceMock, namespaceServiceMock, providerServiceMock, cortexClientMock)

tc.Setup(repositoryMock, templateServiceMock, namespaceServiceMock, providerServiceMock)
tc.Setup(repositoryMock, templateServiceMock, namespaceServiceMock, providerServiceMock, cortexClientMock)

err := svc.Upsert(ctx, tc.Rule)
if tc.ErrString != "" {
Expand All @@ -290,6 +449,7 @@ func TestService_Upsert(t *testing.T) {
templateServiceMock.AssertExpectations(t)
namespaceServiceMock.AssertExpectations(t)
providerServiceMock.AssertExpectations(t)
cortexClientMock.AssertExpectations(t)
})
}
}
Expand Down
Loading

0 comments on commit 837bff1

Please sign in to comment.