Skip to content

Commit 6a62eb5

Browse files
xoanmmJorge Turrado FerreroMatteoManzoni
authored
feat: add unit tests (#5)
Co-authored-by: Jorge Turrado Ferrero <jorge.turrado@docplanner.com> Co-authored-by: MatteoManzoni <manzoni.matteo@mailfence.com>
1 parent f331775 commit 6a62eb5

File tree

27 files changed

+1438
-61
lines changed

27 files changed

+1438
-61
lines changed

.github/workflows/pull_request_tests.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,25 @@ jobs:
1717
- uses: golangci/golangci-lint-action@v2
1818
with:
1919
version: v1.40.1
20+
21+
tests:
22+
name: Tests
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v2
26+
27+
- uses: actions/setup-go@v2
28+
with:
29+
go-version: 1.17
30+
31+
- name: Launch git-server
32+
run: make launch-test-deps
33+
- name: Configure test known_hosts
34+
run: |
35+
chmod 600 test-git-server/private_keys/helm-repo-updater-test
36+
mkdir -p ~/.ssh/
37+
ssh-keyscan -p 2222 localhost >> ~/.ssh/known_hosts
38+
39+
- run: make build
40+
41+
- run: make test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919

2020
### Intellij ###
2121
.idea
22+
23+
# Generated binary files
24+
bin

Makefile

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
GO = $(shell which go)
2+
GOBIN_TOOL = $(shell which gobin || echo $(GOPATH)/gobin)
3+
GO_INPUT = $(CURDIR)/main.go
4+
GO_OUTPUT = $(CURDIR)/bin/$(APP_NAME)
5+
APP_NAME ?= helm-repo-updater
6+
GO_TEST_DEFAULT_ARG = -v ./internal/...
7+
8+
.PHONY: build
9+
build: clean
10+
@echo "`date +'%d.%m.%Y %H:%M:%S'` Building $(GO_INPUT)"
11+
$(GO) build \
12+
-ldflags "-s -w" \
13+
-o $(GO_OUTPUT) $(GO_INPUT)
14+
15+
.PHONY: clean
16+
clean:
17+
rm -f $(GO_OUTPUT)
18+
19+
.PHONY: launch-test-deps
20+
launch-test-deps:
21+
docker-compose -f test-git-server/docker-compose.yaml up -d
22+
23+
.PHONY: clean-test-deps
24+
clean-test-deps:
25+
docker-compose -f test-git-server/docker-compose.yaml down
26+
docker volume prune -f && docker system prune -f
27+
28+
.PHONY: test
29+
test: test-unit
30+
31+
.PHONY: test-benchmark
32+
test-benchmark: launch-test-deps
33+
$(GO) test ${GO_TEST_DEFAULT_ARG} -cpu 1,2,4,8 -benchmem -bench .
34+
35+
.PHONY: test-unit
36+
test-unit: launch-test-deps
37+
$(GO) test ${GO_TEST_DEFAULT_ARG}
38+
39+
.PHONY: test-coverage
40+
test-coverage: launch-test-deps
41+
$(GO) test ${GO_TEST_DEFAULT_ARG} -cover
42+
43+
.PHONY: lint
44+
lint: golangci-lint
45+
46+
.PHONY: golangci-lint
47+
golangci-lint: $(GOBIN_TOOL)
48+
GOOS=linux $(GOBIN_TOOL) -run github.com/golangci/golangci-lint/cmd/golangci-lint@v1.40.1 run
49+
50+
.PHONY: gofumpt
51+
gofumpt: $(GOBIN_TOOL)
52+
GOOS=linux $(GOBIN_TOOL) -run mvdan.cc/gofumpt -l -w .
53+
54+
$(GOBIN_TOOL):
55+
go get github.com/myitcv/gobin
56+
go install github.com/myitcv/gobin

cmd/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ func runImageUpdater(cfg updater.HelmUpdaterConfig) error {
103103
err := func(cfg updater.HelmUpdaterConfig) error {
104104
log.Debugf("Processing application %s in directory %s", cfg.AppName, cfg.File)
105105

106-
err := updater.UpdateApplication(cfg, syncState)
106+
_, err := updater.UpdateApplication(cfg, syncState)
107107
if err != nil {
108+
fmt.Printf("err is %v\n", err)
108109
return err
109110
}
110111

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ go 1.17
44

55
require (
66
github.com/argoproj-labs/argocd-image-updater v0.11.0
7+
github.com/google/go-cmp v0.5.6
78
github.com/mikefarah/yq/v4 v4.16.2
89
github.com/pkg/errors v0.9.1
910
github.com/spf13/cobra v1.3.0
1011
github.com/spf13/viper v1.10.0
1112
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473
1213
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
14+
gotest.tools v2.2.0+incompatible
1315
)
1416

1517
require (
@@ -54,13 +56,14 @@ require (
5456
github.com/timtadh/lexmachine v0.2.2 // indirect
5557
github.com/xanzy/ssh-agent v0.2.1 // indirect
5658
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
57-
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
59+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
5860
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d // indirect
5961
golang.org/x/text v0.3.7 // indirect
6062
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
6163
gopkg.in/ini.v1 v1.66.2 // indirect
6264
gopkg.in/warnings.v0 v0.1.2 // indirect
6365
gopkg.in/yaml.v2 v2.4.0 // indirect
66+
gotest.tools/v3 v3.0.3
6467
)
6568

6669
replace (

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,9 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b
991991
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
992992
golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
993993
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
994-
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d h1:LO7XpTYMwTqxjLcGWPijK3vRXg1aWdlNOVOHRq45d7c=
995994
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
995+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY=
996+
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
996997
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
997998
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
998999
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@@ -1378,8 +1379,10 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
13781379
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
13791380
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
13801381
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1382+
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
13811383
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
13821384
gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk=
1385+
gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0=
13831386
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
13841387
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
13851388
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

internal/app/decoder/decoder_yaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ func (dec *yamlDecoder) Init(reader io.Reader) {
2424

2525
func (dec *yamlDecoder) Decode(rootYamlNode *yaml.Node) error {
2626
return dec.decoder.Decode(rootYamlNode)
27-
}
27+
}

internal/app/git/creds.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package git
22

33
import (
44
"fmt"
5+
56
"github.com/argoproj-labs/argocd-image-updater/ext/git"
67
)
78

@@ -14,18 +15,24 @@ type Credentials struct {
1415
}
1516

1617
// NewCreds returns the credentials for the given repo url.
17-
func (g Credentials) NewCreds(repoURL string) (git.Creds, error) {
18+
func (g Credentials) NewGitCreds(repoURL string) (git.Creds, error) {
1819
if ok, _ := git.IsSSHURL(repoURL); ok {
1920
if g.SSHPrivKey != "" {
20-
2121
return git.NewSSHCreds(g.SSHPrivKey, "", true), nil
2222
}
23+
return nil, fmt.Errorf(
24+
"sshPrivKey not provided for authenticatication to repository %s",
25+
repoURL,
26+
)
2327
} else if git.IsHTTPSURL(repoURL) {
2428
if g.Username != "" && g.Password != "" {
25-
2629
return git.NewHTTPSCreds(g.Username, g.Password, "", "", true, ""), nil
2730
}
31+
return nil, fmt.Errorf(
32+
"no value provided for username and password for authentication to repository %s",
33+
repoURL,
34+
)
2835
}
2936

30-
return nil, fmt.Errorf("unknown repository type")
37+
return nil, fmt.Errorf("unknown repository type for git repository URL %s", repoURL)
3138
}

internal/app/git/creds_test.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package git
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"testing"
7+
8+
"github.com/argoproj-labs/argocd-image-updater/ext/git"
9+
app_utils "github.com/docplanner/helm-repo-updater/internal/app/utils"
10+
"github.com/google/go-cmp/cmp"
11+
"gotest.tools/v3/assert"
12+
)
13+
14+
const validGitCredentialsEmail = "test-user@docplanner.com"
15+
const validGitCredentialsUsername = "test-user"
16+
const validGitCredentialsPassword = "test-password"
17+
const validSSHPrivKeyRelativeRoute = "/test-git-server/private_keys/helm-repo-updater-test"
18+
const validGitRepoSSHURL = "git@github.com:kubernetes/kubernetes.git"
19+
const validGitRepoHTTPSURL = "https://github.com/kubernetes/kubernetes.git"
20+
const invalidGitRepoURL = "github.com/kubernetes/kubernetes.git"
21+
22+
func TestNewCredsSSHURLSShPrivKey(t *testing.T) {
23+
24+
sshPrivKeyRoute, err := app_utils.GetRouteRelativePath(2, validSSHPrivKeyRelativeRoute)
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
29+
g := Credentials{
30+
Email: validGitCredentialsEmail,
31+
SSHPrivKey: *sshPrivKeyRoute,
32+
}
33+
34+
repoURL := validGitRepoSSHURL
35+
36+
creds, err := g.NewGitCreds(repoURL)
37+
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
42+
expectedCreds := git.NewSSHCreds(*sshPrivKeyRoute, "", true)
43+
44+
assert.DeepEqual(t, creds, expectedCreds, cmp.AllowUnexported(git.SSHCreds{}))
45+
}
46+
47+
func TestNewCredsHTPPSURLUsernamePassword(t *testing.T) {
48+
49+
g := Credentials{
50+
Email: validGitCredentialsEmail,
51+
Username: validGitCredentialsUsername,
52+
Password: validGitCredentialsPassword,
53+
}
54+
55+
creds, err := g.NewGitCreds(validGitRepoHTTPSURL)
56+
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
61+
expectedCreds := git.NewHTTPSCreds(g.Username, g.Password, "", "", true, "")
62+
63+
assert.DeepEqual(t, creds, expectedCreds, cmp.AllowUnexported(git.HTTPSCreds{}))
64+
}
65+
66+
func TestNewCredsSSHURLWithoutSShPrivKey(t *testing.T) {
67+
68+
g := Credentials{
69+
Email: validGitCredentialsEmail,
70+
SSHPrivKey: "",
71+
}
72+
73+
repoURL := validGitRepoSSHURL
74+
75+
_, err := g.NewGitCreds(repoURL)
76+
77+
expectedErrorMessage := fmt.Sprintf(
78+
"sshPrivKey not provided for authenticatication to repository %s",
79+
repoURL,
80+
)
81+
82+
assert.Error(t, err, expectedErrorMessage)
83+
}
84+
85+
func TestNewCredsHTPPSURLWithoutUsernameWithPassword(t *testing.T) {
86+
87+
g := Credentials{
88+
Email: validGitCredentialsEmail,
89+
Username: "",
90+
Password: validGitCredentialsPassword,
91+
}
92+
93+
repoURL := validGitRepoHTTPSURL
94+
95+
_, err := g.NewGitCreds(repoURL)
96+
97+
expectedErrorMessage := fmt.Sprintf(
98+
"no value provided for username and password for authentication to repository %s",
99+
repoURL,
100+
)
101+
102+
assert.Error(t, err, expectedErrorMessage)
103+
}
104+
105+
func TestNewCredsHTPPSURLWitUsernameWithoutPassword(t *testing.T) {
106+
107+
g := Credentials{
108+
Email: validGitCredentialsEmail,
109+
Username: validGitCredentialsUsername,
110+
Password: "",
111+
}
112+
113+
repoURL := validGitRepoHTTPSURL
114+
115+
_, err := g.NewGitCreds(repoURL)
116+
117+
expectedErrorMessage := fmt.Sprintf(
118+
"no value provided for username and password for authentication to repository %s",
119+
repoURL,
120+
)
121+
122+
assert.Error(t, err, expectedErrorMessage)
123+
}
124+
125+
func TestNewCredsInvalidURL(t *testing.T) {
126+
127+
g := Credentials{
128+
Email: validGitCredentialsEmail,
129+
Username: validGitCredentialsUsername,
130+
Password: validGitCredentialsPassword,
131+
}
132+
133+
repoURL := invalidGitRepoURL
134+
135+
_, err := g.NewGitCreds(repoURL)
136+
137+
expectedErrorMessage := fmt.Sprintf(
138+
"unknown repository type for git repository URL %s",
139+
repoURL,
140+
)
141+
142+
assert.Error(t, err, expectedErrorMessage)
143+
}

0 commit comments

Comments
 (0)