Skip to content

Commit

Permalink
refactor: Removing clienttest usage in favor of mocks
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Aug 8, 2021
1 parent 252888d commit dcf4a8d
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ scripts/
# Golang
bin/
release/
mocks/
coverage.*
profile.out

Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ init:
go install github.com/kisielk/errcheck@latest
go install golang.org/x/lint/golint@latest
go install golang.org/x/tools/cmd/goimports@latest
go install github.com/golang/mock/[email protected]
$(MAKE) mocks
go mod tidy

## format: Format code. e.g Prettier (js), format (golang)
Expand All @@ -68,6 +70,12 @@ style:
errcheck -ignoretests $(PACKAGES)
go vet $(PACKAGES)

## mocks: Generate mocks
.PHONY: mocks
mocks:
find . -name "mocks" -type d -exec rm -r "{}" \+
mockgen -destination pkg/mocks/mailer.go -mock_names Mailer=Mailer -package mocks github.com/ViBiOh/ketchup/pkg/notifier Mailer

## test: Shortcut to launch all the test tasks (unit, functional and integration).
.PHONY: test
test:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/ViBiOh/auth/v2 v2.10.0
github.com/ViBiOh/httputils/v4 v4.19.3
github.com/ViBiOh/mailer v1.22.1
github.com/golang/mock v1.6.0
github.com/lib/pq v1.10.2
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.29.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -228,6 +230,7 @@ github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down Expand Up @@ -270,6 +273,7 @@ golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzB
golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -322,6 +326,7 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -419,6 +424,7 @@ golang.org/x/tools v0.0.0-20200729194436-6467de6f59a7/go.mod h1:njjCfa9FT2d7l9Bc
golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
11 changes: 8 additions & 3 deletions pkg/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/ViBiOh/ketchup/pkg/service/ketchup"
"github.com/ViBiOh/ketchup/pkg/service/repository"
"github.com/ViBiOh/ketchup/pkg/service/user"
mailer "github.com/ViBiOh/mailer/pkg/client"
mailerModel "github.com/ViBiOh/mailer/pkg/model"
"github.com/prometheus/client_golang/prometheus/push"
)
Expand All @@ -26,6 +25,12 @@ var (
pageSize = uint(20)
)

// Mailer interface client
type Mailer interface {
Enabled() bool
Send(context.Context, mailerModel.MailRequest) error
}

// App of package
type App interface {
Notify(context.Context) error
Expand All @@ -41,7 +46,7 @@ type app struct {
repositoryService repository.App
ketchupService ketchup.App
userService user.App
mailerApp mailer.App
mailerApp Mailer
helmApp helm.App

clock *Clock
Expand All @@ -59,7 +64,7 @@ func Flags(fs *flag.FlagSet, prefix string) Config {
}

// New creates new App from Config
func New(config Config, repositoryService repository.App, ketchupService ketchup.App, userService user.App, mailerApp mailer.App, helmApp helm.App) App {
func New(config Config, repositoryService repository.App, ketchupService ketchup.App, userService user.App, mailerApp Mailer, helmApp helm.App) App {
return app{
loginID: uint64(*config.loginID),
pushURL: strings.TrimSpace(*config.pushURL),
Expand Down
33 changes: 23 additions & 10 deletions pkg/notifier/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
"testing"

authModel "github.com/ViBiOh/auth/v2/pkg/model"
"github.com/ViBiOh/ketchup/pkg/mocks"
"github.com/ViBiOh/ketchup/pkg/model"
"github.com/ViBiOh/ketchup/pkg/semver"
"github.com/ViBiOh/ketchup/pkg/service/ketchup/ketchuptest"
"github.com/ViBiOh/ketchup/pkg/service/repository/repositorytest"
"github.com/ViBiOh/mailer/pkg/client/clienttest"
"github.com/golang/mock/gomock"
)

var (
Expand Down Expand Up @@ -338,9 +339,7 @@ func TestSendNotification(t *testing.T) {
},
{
"mailer disabled",
app{
mailerApp: clienttest.New(false),
},
app{},
args{
ctx: context.Background(),
ketchupToNotify: map[model.User][]model.Release{
Expand All @@ -361,9 +360,7 @@ func TestSendNotification(t *testing.T) {
},
{
"mailer error",
app{
mailerApp: clienttest.New(true),
},
app{},
args{
ctx: context.TODO(),
ketchupToNotify: map[model.User][]model.Release{
Expand All @@ -384,9 +381,7 @@ func TestSendNotification(t *testing.T) {
},
{
"multiple releases",
app{
mailerApp: clienttest.New(true),
},
app{},
args{
ctx: context.Background(),
ketchupToNotify: map[model.User][]model.Release{
Expand Down Expand Up @@ -415,6 +410,24 @@ func TestSendNotification(t *testing.T) {

for _, tc := range cases {
t.Run(tc.intention, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mailerApp := mocks.NewMailer(ctrl)
tc.instance.mailerApp = mailerApp

switch tc.intention {
case "no mailer":
tc.instance.mailerApp = nil
case "mailer disabled":
mailerApp.EXPECT().Enabled().Return(false)
case "mailer error":
mailerApp.EXPECT().Enabled().Return(true)
mailerApp.EXPECT().Send(gomock.Any(), gomock.Any()).Return(errors.New("invalid context"))
case "multiple releases":
mailerApp.EXPECT().Enabled().Return(false)
}

gotErr := tc.instance.sendNotification(tc.args.ctx, tc.args.ketchupToNotify)

failed := false
Expand Down
16 changes: 9 additions & 7 deletions pkg/provider/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ import (

var (
apiURL = "https://api.github.com"

httpClient = &http.Client{
Timeout: 30 * time.Second,
CheckRedirect: func(r *http.Request, via []*http.Request) error {
logger.Info("Redirect from %s to %s", via[len(via)-1].URL.Path, r.URL.Path)
return nil
},
}
)

// Tag describes a GitHub Tag
Expand Down Expand Up @@ -70,13 +78,7 @@ func New(config Config, redisApp redis.App) App {
}

func (a app) newClient() *request.Request {
return request.New().Header("Authorization", fmt.Sprintf("token %s", a.token)).WithClient(http.Client{
Timeout: 30 * time.Second,
CheckRedirect: func(r *http.Request, via []*http.Request) error {
logger.Info("Redirect from %s to %s", via[len(via)-1].URL.Path, r.URL.Path)
return nil
},
})
return request.New().Header("Authorization", fmt.Sprintf("token %s", a.token)).WithClient(httpClient)
}

func (a app) Start(registerer prometheus.Registerer, done <-chan struct{}) {
Expand Down

0 comments on commit dcf4a8d

Please sign in to comment.