Skip to content

Commit

Permalink
Test: add tests for workqueue and config parser (#36)
Browse files Browse the repository at this point in the history
* Test: add tests for workqueue and config parser

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* reviewable

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* fix tests

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* add boilerplate

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

* add vendors to boilerplate exclusion

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>

---------

Signed-off-by: Charlie Chiang <charlie_c_0129@outlook.com>
  • Loading branch information
charlie0129 authored Mar 13, 2023
1 parent d513147 commit defe4c0
Show file tree
Hide file tree
Showing 20 changed files with 1,495 additions and 71 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/unit-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,12 @@ jobs:
run: |
go install -mod=mod github.com/onsi/ginkgo/v2/ginkgo
- name: Prepare minikube
# For whatever reason, certain unit tests relies on k8s.
# Although such reliance should be removed, in the meantime,
# we set up a local k8s cluster to run such tests.
# Use minikube simply because it comes with GitHub runner.
run: minikube start

- name: Run tests
run: make test
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ svgformat:

# Check possible issues before committing code
reviewable: generate checklicense lint
go mod tidy

# Run tests
test: envtest
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ require (
cloud.google.com/go v0.102.0
cuelang.org/go v0.5.0-beta.2.0.20230130095913-d573e0c2f041
github.com/crossplane/crossplane-runtime v0.14.1-0.20210722005935-0b469fcc77cd
github.com/google/go-cmp v0.5.9
github.com/kubevela/pkg v0.0.0-20230307020100-8537f7bfc02b
github.com/mitchellh/hashstructure/v2 v2.0.2
github.com/mitchellh/mapstructure v1.5.0
github.com/oam-dev/kubevela-core-api v1.6.0-alpha.1
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.0
Expand Down Expand Up @@ -58,7 +58,6 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,6 @@ github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0Qu
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
Expand Down
13 changes: 9 additions & 4 deletions hack/boilerplate/boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,22 @@ def file_extension(filename):
return os.path.splitext(filename)[1].split(".")[-1].lower()

skipped_dirs = [
'.git',
"pkg/util/workqueue",
".git",
".go/gocache",
".go/gomodcache",
".idea",
".vscode",
"default.etcd",
"docs",
"build",
"bin"
"bin",
"pkg/workqueue",
"vendors",
]

# list all the files contain 'DO NOT EDIT', but are not generated
skipped_ungenerated_files = [
'hack/boilerplate/boilerplate.py',
"hack/generate-go-const-from-file.sh"
]

def normalize_files(files):
Expand Down
53 changes: 53 additions & 0 deletions pkg/config/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2023 The KubeVela Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)

func TestNewFromFileOrDir(t *testing.T) {
a := assert.New(t)
// From file
cf, err := NewFromFileOrDir("testdata/golden/conf.cue")
a.NoError(err)
a.Equal(1, len(cf.Triggers))
// From dir
cd, err := NewFromFileOrDir("testdata/golden")
a.NoError(err)
a.Equal(4, len(cd.Triggers))
// Every trigger should be the same
prev := cf.Triggers[0]
for _, t := range cd.Triggers {
reflect.DeepEqual(prev, t)
}
}

func TestNewFromFileOrDirInvalid(t *testing.T) {
a := assert.New(t)

// Report error if file is invalid
_, err := NewFromFileOrDir("testdata/invalidext/conf.invalid")
a.Error(err)

// No error if files in dir is invalid, just skips
c, err := NewFromFileOrDir("testdata/invalidext")
a.NoError(err)
a.Equal(0, len(c.Triggers))
}
20 changes: 20 additions & 0 deletions pkg/config/testdata/golden/conf.cue
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
triggers: [{
source: {
type: "resource-watcher"
properties: {
clusters: [
"cn-shanghai",
]
apiVersion: "apps/v1"
kind: "Deployment"
events: [
"update",
]
}
}
filter: "context.data.status.readyReplicas == context.data.status.replicas"
action: {
type: "sae-record-event"
properties: nameSelector: fromLabel: "workflowrun.oam.dev/name"
}
}]
28 changes: 28 additions & 0 deletions pkg/config/testdata/golden/conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"triggers": [
{
"source": {
"type": "resource-watcher",
"properties": {
"clusters": [
"cn-shanghai"
],
"apiVersion": "apps/v1",
"kind": "Deployment",
"events": [
"update"
]
}
},
"filter": "context.data.status.readyReplicas == context.data.status.replicas",
"action": {
"type": "sae-record-event",
"properties": {
"nameSelector": {
"fromLabel": "workflowrun.oam.dev/name"
}
}
}
}
]
}
16 changes: 16 additions & 0 deletions pkg/config/testdata/golden/conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
triggers:
- source:
type: resource-watcher
properties:
clusters:
- "cn-shanghai"
apiVersion: apps/v1
kind: Deployment
events:
- update
filter: context.data.status.readyReplicas == context.data.status.replicas
action:
type: sae-record-event
properties:
nameSelector:
fromLabel: "workflowrun.oam.dev/name"
16 changes: 16 additions & 0 deletions pkg/config/testdata/golden/conf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
triggers:
- source:
type: resource-watcher
properties:
clusters:
- "cn-shanghai"
apiVersion: apps/v1
kind: Deployment
events:
- update
filter: context.data.status.readyReplicas == context.data.status.replicas
action:
type: sae-record-event
properties:
nameSelector:
fromLabel: "workflowrun.oam.dev/name"
Empty file.
27 changes: 27 additions & 0 deletions pkg/config/testdata/invalidschema/conf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
triggers:
- sources:
type: resource-watcher
properties:
apiVersion: core.oam.dev/v1alpha1
kind: WorkflowRun
events:
- create
matchingLabels:
trigger.oam.dev/watch: "true"
action:
type: create-event-listener
- source:
type: resource-watcher
properties:
clusters:
- "cn-shanghai"
apiVersion: apps/v1
kind: Deployment
events:
- update
filter: context.data.status.readyReplicas == context.data.status.replicas
action:
type: sae-record-event
properties:
nameSelector:
fromLabel: "workflowrun.oam.dev/name"
46 changes: 0 additions & 46 deletions pkg/util/test_utils.go

This file was deleted.

Loading

0 comments on commit defe4c0

Please sign in to comment.