Skip to content

Commit

Permalink
Chore/cleanup linting (#43)
Browse files Browse the repository at this point in the history
* refactor: more robust E2E tests

Added a new test case for the COSIGN_REPOSITORY variable
and made the e2e tests more robust by properly handling timeouts,
which weren't working half the time.

Signed-off-by: Bruno Bressi <[email protected]>

* chore: added linting suggestions
- Reduced complexity of Server
- Reduce memory footprint for checking loops
- Other minor renamings & fixes

Signed-off-by: Bruno Bressi <[email protected]>

* fix: E2E tests should work now

Signed-off-by: Bruno Bressi <[email protected]>

* chore: removed dead test & renamed slog back to log

Signed-off-by: Bruno Bressi <[email protected]>

* chore: removed comment

Signed-off-by: Bruno Bressi <[email protected]>

* chore: bumped deps & added some comments

Signed-off-by: Bruno Bressi <[email protected]>

* chore: install go version for e2e tests

Signed-off-by: Bruno Bressi <[email protected]>

* chore: moved back to placeholder logs in main

Signed-off-by: Bruno Bressi <[email protected]>

* chore: added godoc

Signed-off-by: Bruno Bressi <[email protected]>

* chore: unordered list formatting fixed

Signed-off-by: Bruno Bressi <[email protected]>

* fix: failure tests fail reliably now

Signed-off-by: Bruno Bressi <[email protected]>

---------

Signed-off-by: Bruno Bressi <[email protected]>
  • Loading branch information
puffitos authored Feb 5, 2024
1 parent 7d840c9 commit 68485e0
Show file tree
Hide file tree
Showing 14 changed files with 772 additions and 1,148 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/end2end.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ jobs:
steps:
- name: Checkout Repo
uses: actions/checkout@v3
- name: Install go
uses: actions/setup-go@v3
with:
go-version: 1.21
- name: Install k3d
run: |
curl -s https://raw.githubusercontent.com/rancher/k3d/main/install.sh | bash
Expand Down
107 changes: 107 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
linters-settings:
dupl:
threshold: 100
funlen:
lines: -1 # the number of lines (code + empty lines) is not a right metric and leads to code without empty line or one-liner.
statements: 58
goconst:
min-len: 2
min-occurrences: 3
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
disabled-checks:
- dupImport # https://github.com/go-critic/go-critic/issues/845
- ifElseChain
- octalLiteral
- whyNoLint
gocyclo:
min-complexity: 15
gofmt:
rewrite-rules:
- pattern: 'interface{}'
replacement: 'any'
goimports:
local-prefixes: github.com/golangci/golangci-lint
gomnd:
# don't include the "operation" and "assign"
checks:
- argument
- case
- condition
- return
ignored-numbers:
- '0'
- '1'
- '2'
- '3'
ignored-functions:
- strings.SplitN

govet:
check-shadowing: true
settings:
printf:
funcs:
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
lll:
line-length: 140
misspell:
locale: US
nolintlint:
allow-unused: false # report any unused nolint directives
require-explanation: false # don't require an explanation for nolint directives
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
revive:
rules:
- name: unexported-return
disabled: true
- name: unused-parameter

linters:
disable-all: true
enable:
- bodyclose
- dogsled
- dupl
- errcheck
- exportloopref
- funlen
- gocheckcompilerdirectives
- gochecknoinits
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- gomnd
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- misspell
- nakedret
- noctx
- nolintlint
- revive
- staticcheck
- typecheck
- unconvert
- unparam
- unused
- whitespace

run:
timeout: 5m
skip-files:
- .*_test\.go
skip-dirs:
- test/
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
repos:
- repo: local
hooks:
- id: go-test
name: go-unit-tests
entry: make test-unit
language: system
types: [go]
- repo: https://github.com/tekwizely/pre-commit-golang
rev: v1.0.0-rc.1
hooks:
- id: go-mod-tidy-repo
- id: go-vet-repo-mod
- id: go-fumpt-repo
args: [ -l, -w ]
- id: golangci-lint-repo-mod
args: [ --config, .golangci.yaml, --, --fix ]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ e2e-keys:

e2e-images:
@echo "Checking for cosign.key..."
@test -f cosign.key || (echo "cosign.key not found. Run 'make generate-key' to generate one." && exit 1)
@test -f cosign.key || (echo "cosign.key not found. Run 'make e2e-keys' to generate the pairs needed for the tests." && exit 1)
@echo "Building test image..."
@docker build -t k3d-registry.localhost:5000/cosignwebhook:dev .
@echo "Pushing test image..."
Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,15 @@ make test-e2e
The E2E tests require a running kubernetes cluster. Currently, the namespace and webhook are deployed via helper make
targets. To run the tests the following is required:

- docker
- cosign (v2)
* docker
* cosign (v2)

To run the E2E tests, the following steps are required (in order):

- signing keys are generated (`make e2e-keys`)
- a new `cosignwebhook` image is build and signed with a temp key (`make e2e-images`)
- the image is pushed to a local registry & deployed to the test cluster (`make e2e-deploy`)
* create a k3d local cluster for the tests and a local iamge registry (`make e2e-cluster`)
* signing keys are generated (`make e2e-keys`)
* a new `cosignwebhook` image is build and signed with a temp key (`make e2e-images`)
* the image is pushed to a local registry & deployed to the test cluster (`make e2e-deploy`)

To do all of the above, simply run `make e2e-prep`. Each step should also be able to be executed individually. To clean
up the E2E setup, run `make e2e-cleanup`. This will delete everything created by the E2E preparation.
Expand All @@ -157,8 +158,8 @@ CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-extldflags "-static"' -o cosignw

## Credits

- Bruno Bressi <[email protected]>
- Frank Kloeker <[email protected]>
* Bruno Bressi <[email protected]>
* Frank Kloeker <[email protected]>

Life is for sharing. If you have an issue with the code or want to improve it, feel free to open an issue or an pull
request.
Expand Down
Loading

0 comments on commit 68485e0

Please sign in to comment.