Skip to content

Commit

Permalink
Unit and Integration Tests (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunussandikci authored Feb 1, 2024
1 parent 5e02d5e commit de51f97
Show file tree
Hide file tree
Showing 25 changed files with 3,453 additions and 68 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ permissions:

env:
GO_VERSION: "1.21"
GOLANGCI_LINT_VERSION: "v1.54.2"
GOLANGCI_LINT_VERSION: "v1.55.2"

jobs:
lint:
name: Lint
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Download Dependencies
Expand All @@ -25,4 +25,4 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
version: ${{ env.GOLANGCI_LINT_VERSION }}
args: --timeout=10m
args: --timeout=10m
8 changes: 4 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
types: [published]

env:
IMAGE: "nccloud/mayfly"
IMAGE: ${{ github.repository_owner }}/${{ github.repository }}
REGISTRY: "ghcr.io"
GO_VERSION: "1.21"
TAG: ${{ github.event.release.tag_name }}
Expand All @@ -16,15 +16,15 @@ jobs:
runs-on: ubuntu-22.04
steps:
- name: Login
uses: docker/login-action@v2
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Build Image
Expand Down
17 changes: 13 additions & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
name: Test

on:
pull_request:
push:
Expand All @@ -16,16 +15,26 @@ jobs:
runs-on: ubuntu-22.04
steps:
- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Validate
run: |
./devops.sh generate
git diff --exit-code
- name: Test Preparation
run: |
./devops.sh prepare_envtest
- name: Coverage Report
uses: gwatts/go-coverage-action@v1
id: coverage
with:
coverage-threshold: 0
coverage-threshold: 80
cover-pkg: ./...
ignore-pattern: |
.*cmd.*
.*mocks.*
.*apis.*
.*generated.*
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
.idea
.DS_Store
*.out
bin
.vscode
volumes
*.txt
.bin
.envtest/*
21 changes: 7 additions & 14 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,17 @@ linters:
- gochecknoinits # Disallows to use init functions
- ireturn # Disallows to return Interfaces
- gci # Disable gci import ordering checker since its buggy
- forcetypeassert # Disallows to use type assertions without checking
- depguard # Disallows to use non-listed packages
- tagalign # Disallows to use non-aligned tags
- gochecknoglobals # We like global variables
- tagalign # Buggy
- lll
- funlen
linters-settings:
funlen:
lines: 90
issues:
exclude-rules:
- path: .*/metrics.go
linters:
- gochecknoglobals
cyclop:
max-complexity: 12
run:
go: '1.21'
tests: false
skip-dirs:
- cmd/benchmark
skip-files:
- cmd/.*.go
- mocks/.*.go
- common/config.go
- .*/groupversion_info.go
- .*/zz_generated.deepcopy.go
14 changes: 14 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
packages:
sigs.k8s.io/controller-runtime/pkg/client:
interfaces:
Client:
SubResourceClient:
sigs.k8s.io/controller-runtime/pkg/cache:
interfaces:
Cache:
sigs.k8s.io/controller-runtime/pkg/manager:
interfaces:
Manager:
github.com/NCCloud/mayfly/pkg/common:
interfaces:
Scheduler:
9 changes: 5 additions & 4 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main
import (
"fmt"

"github.com/NCCloud/mayfly/pkg/controllers/expiration"
"github.com/NCCloud/mayfly/pkg/controllers/scheduledresource"

"github.com/NCCloud/mayfly/pkg/apis/v1alpha1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"

"github.com/NCCloud/mayfly/pkg/common"
"github.com/NCCloud/mayfly/pkg/controllers"

"sigs.k8s.io/controller-runtime/pkg/metrics/server"

"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -54,13 +55,13 @@ func main() {
scheduler := common.NewScheduler(config)

for _, resource := range config.Resources {
if expirationControllerErr := controllers.NewExpirationController(config, client, resource, scheduler).
if expirationControllerErr := expiration.NewController(config, client, resource, scheduler).
SetupWithManager(manager); expirationControllerErr != nil {
panic(expirationControllerErr)
}
}

if scheduledResourceControllerErr := controllers.NewScheduledResourceController(config, client, scheduler).
if scheduledResourceControllerErr := scheduledresource.NewController(config, client, scheduler).
SetupWithManager(manager); scheduledResourceControllerErr != nil {
panic(scheduledResourceControllerErr)
}
Expand Down
19 changes: 12 additions & 7 deletions deploy/crds/cloud.namecheap.com_scheduledresources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.13.0
controller-gen.kubebuilder.io/version: v0.14.0
name: scheduledresources.cloud.namecheap.com
spec:
group: cloud.namecheap.com
Expand All @@ -29,14 +29,19 @@ spec:
openAPIV3Schema:
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
description: |-
APIVersion defines the versioned schema of this representation of an object.
Servers should convert recognized schemas to the latest internal value, and
may reject unrecognized values.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
description: |-
Kind is a string value representing the REST resource this object represents.
Servers may infer this from the endpoint the client submits requests to.
Cannot be updated.
In CamelCase.
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
type: string
metadata:
type: object
Expand Down
11 changes: 9 additions & 2 deletions devops.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

export CONTROLLER_GEN_VERSION="v0.13.0"
export GOLANGCI_LINT_VERSION="v1.54.2"
export CONTROLLER_GEN_VERSION="v0.14.0"
export GOLANGCI_LINT_VERSION="v1.55.2"
export MOCKERY_GEN_VERSION="v2.36.0"
export GOFUMPT_VERSION="v0.5.0"
export TESTENV_VERSION="1.25.x!"
Expand Down Expand Up @@ -39,12 +39,19 @@ generate() {
sed '/Compiled/d' pkg/apis/v1alpha1/zz_generated.deepcopy.go > pkg/apis/v1alpha1/zz_generated.deepcopy.gotmp
mv pkg/apis/v1alpha1/zz_generated.deepcopy.gotmp pkg/apis/v1alpha1/zz_generated.deepcopy.go
crd-ref-docs --source-path=./pkg/apis --config .apidoc.yaml --renderer markdown --output-path=./docs/api.md
mockery
}

install() {
kubectl apply -f deploy/crds
}

prepare_envtest() {
mkdir -p .envtest/crds
mkdir -p .envtest/bins
cp -rf "$(setup-envtest use $TESTENV_VERSION -p path)"/* .envtest/bins/
}

test() {
go test -v -coverpkg=./... ./...
}
Expand Down
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ require (
github.com/elliotchance/pie/v2 v2.8.0
github.com/go-co-op/gocron/v2 v2.2.1
github.com/go-echarts/go-echarts/v2 v2.3.3
github.com/go-logr/logr v1.4.1
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c
github.com/prometheus/client_golang v1.18.0
github.com/stretchr/testify v1.8.4
k8s.io/api v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/client-go v0.29.1
Expand All @@ -18,12 +20,12 @@ require (

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/brianvoe/gofakeit/v6 v6.28.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
Expand All @@ -45,11 +47,13 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20231219180239-dc181d75b848 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de h1:FxWPpzIjnTlhP
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de/go.mod h1:DCaWoUhZrYW9p1lxo/cm8EmUOOzAPSEZNGF2DK1dJgw=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/brianvoe/gofakeit/v6 v6.28.0 h1:Xib46XXuQfmlLS2EXRuJpqcw8St6qSZz75OUo0tgAW4=
github.com/brianvoe/gofakeit/v6 v6.28.0/go.mod h1:Xj58BMSnFqcn/fAQeSK+/PLtC5kSb7FJIq4JyGa8vEs=
github.com/caarlos0/env/v10 v10.0.0 h1:yIHUBZGsyqCnpTkbjk8asUlx6RFhhEs+h7TOBdgdzXA=
github.com/caarlos0/env/v10 v10.0.0/go.mod h1:ZfulV76NvVPw3tm591U4SwL3Xx9ldzBP9aGxzeN7G18=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
Expand Down Expand Up @@ -115,6 +117,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down
Loading

0 comments on commit de51f97

Please sign in to comment.