Skip to content

Commit

Permalink
Adding tests and SonarCloud integration
Browse files Browse the repository at this point in the history
  • Loading branch information
cardil committed Oct 4, 2019
1 parent 1aab19a commit 9b854f6
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 36 deletions.
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,5 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Binaries
bin/

# Custom
serverless-installer-answers.yaml
# Build
build
24 changes: 14 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
language: go
sudo: false
dist: trusty
go:
- 1.13.x
matrix:
fast_finish: true
include:
- env: GOOS="linux"
- env: GOOS="windows"
- env: GOOS="darwin"
- 1.13.x
script:
- make clean binary
- sonar-scanner
branches:
only:
- master
- develop
- /^v\d/
- master
- develop
- "/^v\\d/"
notifications:
email: onchange
addons:
sonarcloud:
organization: "wavesoftware"
token:
secure: "V+dI4AgutA5XbYXgXnf1pbgG49dZE34Yz5FlM5OjMNKCagMhM3C/t0bYJrM02GUw5oI29c3APgh0TbVLKQTUL/WOSMkRli2IikUaX6gJNI1ClL/AsikaCwvIWEqDUqoKMS27T1Un4ipXbeeEkjrDVbew8C1zBYEJvDQY463GiAy/kwQ/llL40wBttmy1Wflags12sjKBfxVPmmyO8BbOeu/zYca9g48EOD0xcQVGVRvZqoOP3SwXETaU0+7F2pKd3qZbjnbpH+aJL6hI+UIAQB3iv2vWTHscMczilRmPRO7vueBC1SB41X42fG1LqQJ0vkxgMSVKftatGykrmJcrNWubK2bpczRF4sf9RhdugkE3pPR5KnstHl0VVSII4IFsQnkgPTl+2PFdPW8qxdiW4p9YI1S3iYydO3POqBcksbOj+QTbkSAxy5z6DnOoVUMZnEUAIVnyP/hfzSMTr8Q8YgFdZuuZcUMzo04Fr1ZJhboOFCo6ZMx+WcOlRd56u1KVohwhQ/gu7TalbKUMfoLwsRwNaCRiXXAmum94NLHIDK50PQwnA3DJc4JznZIVqY4SllSdWjA0fCLgSGD6rU7p9XC8/7DLyrzc1hEsKgf7UYi5bxwUPZAmH6M33XM0EaKeaZaeCtD5y5ITpg2Q0H1qeJUx/0kbWT69J2H6BYPjLUA="

25 changes: 15 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
PROJECT_DIR = "./"
PROJECT_DIR = $(shell readlink -f .)
CLI_DIR = "$(PROJECT_DIR)/cmd/installer"
BIN = "$(PROJECT_DIR)/bin/serverless-installer"
BUILD_DIR = "$(PROJECT_DIR)/build"
BIN = "$(BUILD_DIR)/serverless-installer"

GO ?= go
GOLINT ?= $(GO)lint
Expand All @@ -9,7 +10,7 @@ RICHGO ?= rich$(GO)
RESET = \033[0m
make_std_color = \033[3$1m # defined for 1 through 7
make_color = \033[38;5;$1m # defined for 1 through 255
BLUE = $(strip $(call make_color,44))
BLUE = $(strip $(call make_color,38))
PINK = $(strip $(call make_color,210))
RED = $(strip $(call make_color,206))
GREEN = $(strip $(call make_color,120))
Expand All @@ -24,27 +25,31 @@ builddeps:
@$(GO) get github.com/kyoh86/richgo
@$(GO) get golang.org/x/lint/golint

.PHONY: builddir
builddir:
@mkdir -p build

.PHONY: clean
clean: builddeps
@echo " $(GRAY)🛁 Cleaning$(RESET)"
@rm -fv $(BIN)
@rm -frv $(BUILD_DIR)

.PHONY: check
check: builddeps
@echo " $(PINK)🛂 Checking$(RESET)"
@echo " $(PINK)👮 Checking$(RESET)"
$(GOLINT) -set_exit_status ./...

.PHONY: test
test: check
test: builddir check
@echo " $(GREEN)✔️ Testing$(RESET)"
$(RICHGO) test -cover ./...
$(RICHGO) test -covermode=count -coverprofile=build/coverage.out ./...

.PHONY: binary
binary: test
binary: builddir test
@echo " $(BLUE)🔨 Building$(RESET)"
$(RICHGO) build -o $(BIN) $(CLI_DIR)

.PHONY: run
run: binary
run: builddir binary
@echo " $(RED)🏃 Running$(RESET)"
$(BIN) $(args)
cd $(BUILD_DIR) && $(BIN) $(args)
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ require (
github.com/Bowery/prompt v0.0.0-20190916142128-fa8279994f75 // indirect
github.com/google/uuid v1.1.1
github.com/kr/pretty v0.1.0 // indirect
github.com/kyoh86/richgo v0.3.3 // indirect
github.com/labstack/gommon v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mkideal/cli v0.0.3
github.com/mkideal/pkg v0.0.0-20170503154153-3e188c9e7ecc // indirect
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.4.0
golang.org/x/lint v0.0.0-20190930215403-16217165b5de // indirect
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v2 v2.2.4
Expand Down
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ github.com/Netflix/go-expect v0.0.0-20180615182759-c93bf25de8e8/go.mod h1:oX5x61
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deadcheat/goblet v1.3.1/go.mod h1:IrMNyAwyrVgB30HsND2WgleTUM4wHTS9m40yNY6NJQg=
github.com/deadcheat/gonch v0.0.0-20180528124129-c2ff7a019863/go.mod h1:/5mH3gAuXUxGN3maOBAxBfB8RXvP9tBIX5fx2x1k0V0=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ=
Expand All @@ -22,11 +25,16 @@ github.com/kr/pty v1.1.4 h1:5Myjjh3JY/NaAi4IsUbHADytDyl1VE1Y9PXDlL+P/VQ=
github.com/kr/pty v1.1.4/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kyoh86/richgo v0.3.3 h1:fW7ygWNMg1INcX3Yoha9v1C5nYlvd/IodJUf0Batz6Q=
github.com/kyoh86/richgo v0.3.3/go.mod h1:S65jllVRxBm59fqIXfCa3cPxQYRT9u9v45EPQVeuoH0=
github.com/kyoh86/xdg v0.0.0-20171007020617-d28e4c5d7b81 h1:C2Yb5TxdHWrtWXdf+j8xWW4QQu8lXuxtj1UOK816dpA=
github.com/kyoh86/xdg v0.0.0-20171007020617-d28e4c5d7b81/go.mod h1:Z5mDqe0fxyxn3W2yTxsBAOQqIrXADQIh02wrTnaRM38=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.0-20170925054904-a5cdd64afdee/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
Expand All @@ -36,22 +44,33 @@ github.com/mkideal/cli v0.0.3 h1:Y1OXyfTVI9eQ9RTiXq12h7q88y22Q9ZU4VI09ifz6lE=
github.com/mkideal/cli v0.0.3/go.mod h1:HLuSls75T7LFlTgByGeuLwcvdUmmx/aUQxnnEKxoZzY=
github.com/mkideal/pkg v0.0.0-20170503154153-3e188c9e7ecc h1:eyN9UWVX+CeeCQZPudCUAPc84xQYTjEu9MWNa2HuJrs=
github.com/mkideal/pkg v0.0.0-20170503154153-3e188c9e7ecc/go.mod h1:DECgB56amjU/mmmsKuooNPQ1856HASOMC3D4ntSVU70=
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c h1:nXxl5PrvVm2L/wCy8dQu6DMTwH4oIuGN8GJDAlqDdVE=
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/wacul/ptr v0.0.0-20170209030335-91632201dfc8 h1:js9Z9dWq50Ttu4D1YRqgS1gkSgRfpKp+rMza4io7qNw=
github.com/wacul/ptr v0.0.0-20170209030335-91632201dfc8/go.mod h1:BD0gjsZrCwtoR+yWDB9v2hQ8STlq9tT84qKfa+3txOc=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 h1:8dUaAV7K4uHsF56JQWkprecIQKdPHtR9jCHF5nB8uzc=
golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20180404174746-b3c676e531a6/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20170927054621-314a259e304f/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -62,10 +81,13 @@ golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed h1:5TJcLJn2a55mJjzYk0yOoqN8X1OdvBDUnaZaKKyQtkY=
golang.org/x/sys v0.0.0-20191002091554-b397fe3ad8ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd h1:/e+gpKk9r3dJobndpTytxS2gOy6m5uvpg+ISQoEcusQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
Expand Down
15 changes: 15 additions & 0 deletions internal/domain/contract/answers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package contract

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestStruct(t *testing.T) {
// when
answers := LocationAwareAnswers{}

// then
assert.NotNil(t, answers)
}
5 changes: 2 additions & 3 deletions internal/domain/logic/answers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"

"github.com/wavesoftware/serverless-installer/internal"
"github.com/wavesoftware/serverless-installer/internal/domain/gateway"
"github.com/wavesoftware/serverless-installer/internal/domain/model"
)
Expand All @@ -18,9 +19,7 @@ func (service answersService) Save(answers model.Answers, path string) error {
WithField("answers", answers).
Debug("Saved called")
contents, err := yaml.Marshal(answers)
if err != nil {
return err
}
internal.CheckNil("20191004:135106", err)
log.
WithField("yaml", string(contents)).
Trace("Yaml")
Expand Down
64 changes: 64 additions & 0 deletions internal/domain/logic/answers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package logic

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/wavesoftware/serverless-installer/internal/domain/model"
)

var files []file

func TestSave(t *testing.T) {
// given
files = nil
service := answersService{
fileWriter: stubWriter{},
}
answers := model.Answers{}
answers.Jeager.Install.Enabled = false
path := "/tmp/answers.yaml"
expectedYaml := strings.ReplaceAll(`jeager:
install:
enabled: false
elasticsearch:
install:
enabled: false
kiali:
install:
enabled: false
istio:
install:
enabled: false
serverless:
install:
enabled: false
`, "\t", " ")

// when
err := service.Save(answers, path)

// then
assert.Nil(t, err)
assert.Len(t, files, 1)
first := files[0]
assert.Equal(t, path, first.path)
assert.Equal(t, expectedYaml, string(first.contents))
}

type file struct {
contents []byte
path string
}

type stubWriter struct{}

func (w stubWriter) Write(contents []byte, path string) error {
files = append(files, file{
contents: contents,
path: path,
})
return nil
}
15 changes: 15 additions & 0 deletions internal/domain/logic/factories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package logic

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreateAnswersSaver(t *testing.T) {
// when
saver := CreateAnswersSaver()

// then
assert.NotNil(t, saver)
}
15 changes: 15 additions & 0 deletions internal/domain/model/answers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestStruct(t *testing.T) {
// when
answers := Answers{}

// then
assert.NotNil(t, answers)
}
15 changes: 15 additions & 0 deletions internal/persistence/factories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package persistence

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreateFileWriter(t *testing.T) {
// when
writer := CreateFileWriter()

// then
assert.NotNil(t, writer)
}
26 changes: 18 additions & 8 deletions internal/persistence/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"

"github.com/wavesoftware/serverless-installer/internal/domain/contract"
)

func TestFilesystemWriter(t *testing.T) {
// given
tmp, err := ioutil.TempDir("", "test-filesystem-writer")
checkNoError(err)
assert.Nil(t, err)
defer os.RemoveAll(tmp)
writer := filesystemWriter{}
path := filepath.Join(tmp, uuid.New().String(), contract.AnswersFilename)
Expand All @@ -24,13 +25,22 @@ func TestFilesystemWriter(t *testing.T) {
err = writer.Write(contents, path)

// then
if err != nil {
t.Error(err)
}
assert.Nil(t, err)
}

func checkNoError(err error) {
if err != nil {
panic(err)
}
func TestFilesystemWriterOnInvalidPath(t *testing.T) {
// given
writer := filesystemWriter{}
path := filepath.Join(
// invalid character in path 0x0
string([]byte{0x0}),
uuid.New().String(), contract.AnswersFilename,
)
contents := []byte("This is test")

// when
err := writer.Write(contents, path)

// then
assert.NotNil(t, err)
}
Loading

0 comments on commit 9b854f6

Please sign in to comment.