Skip to content

Commit

Permalink
✨ Add gen-test command for chatops
Browse files Browse the repository at this point in the history
Signed-off-by: Rintaro Okamura <rintaro.okamura@gmail.com>
  • Loading branch information
rinx authored and actions-user committed May 19, 2020
1 parent df143c9 commit 083f62d
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 48 deletions.
6 changes: 6 additions & 0 deletions .github/chatops_permissions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,41 @@ roles:
- rebase
- approve
- add-contributors
- gen-test
- label
- format
author:
policies:
- rebase
- approve
- add-contributors
- gen-test
- label
- format
sig-lead:
policies:
- rebase
- approve
- gen-test
- label
- format
sig-sre:
policies:
- rebase
- add-contributors
- gen-test
- label
- format
sig-set:
policies:
- rebase
- gen-test
- label
- format
sig-core:
policies:
- rebase
- gen-test
- label
- format
contributor:
Expand Down
108 changes: 108 additions & 0 deletions .github/workflows/chatops.yml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ jobs:
git add CONTRIBUTORS README.md
git commit --signoff -m ":robot: Automatically add contributor"
git pull --rebase
git remote set-url origin "https://${GITHUB_USER}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git push origin ${HEAD_BRANCH}
Expand All @@ -237,11 +239,117 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.DISPATCH_TOKEN }}
API_URL: ${{ github.event.issue.comments_url }}
gentest:
name: Add tests
needs:
- rebase
runs-on: ubuntu-latest
steps:
- name: check PR Comments
id: check_comments_gen_test
uses: machine-learning-apps/actions-chatops@master
with:
TRIGGER_PHRASE: "/gen-test"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: install yq
if: steps.check_comments_gen_test.outputs.BOOL_TRIGGERED == 'true'
run: |
sudo curl -Lo /usr/local/bin/yq https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/yq_linux_amd64
sudo chmod a+x /usr/local/bin/yq
- name: check permissions
if: steps.check_comments_gen_test.outputs.BOOL_TRIGGERED == 'true'
id: check_permissions
run: |
curl -o chatops_permissions.yaml https://raw.githubusercontent.com/vdaas/vald/master/.github/chatops_permissions.yaml
roles=`yq r chatops_permissions.yaml "users.${USERNAME}.role.*"`
if [ "$roles" = "" ]; then
roles=`yq r chatops_permissions.yaml "default-roles.*"`
fi
for role in `echo $roles`
do
if yq r chatops_permissions.yaml "roles.${role}.policies.*" | grep "^${REQUIRED_POLICY}$" > /dev/null 2>&1 ; then
echo "[OK] add-contributor requested by ${USERNAME}"
echo "::set-output name=EXECUTABLE::true"
break
fi
done
env:
USERNAME: ${{ steps.check_comments_gen_test.outputs.COMMENTER_USERNAME }}
REQUIRED_POLICY: gen-test
- name: check executable
if: steps.check_comments_gen_test.outputs.BOOL_TRIGGERED == 'true' && steps.check_permissions.outputs.EXECUTABLE == 'true'
run: |
curl --include --verbose --fail \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-H "Authorization: token ${GITHUB_TOKEN}" \
--request POST \
--data "{\"body\": \"**[GEN TEST]** Generating missing test files triggered by ${USERNAME}.\"}" \
$API_URL
env:
GITHUB_TOKEN: ${{ secrets.DISPATCH_TOKEN }}
USERNAME: ${{ steps.check_comments_gen_test.outputs.COMMENTER_USERNAME }}
PR_AUTHOR: ${{ github.event.issue.user.login }}
API_URL: ${{ github.event.issue.comments_url }}
- uses: actions/checkout@v1
if: steps.check_comments_gen_test.outputs.BOOL_TRIGGERED == 'true' && steps.check_permissions.outputs.EXECUTABLE == 'true'
with:
fetch-depth: 10
- name: Generate tests and push
if: steps.check_comments_gen_test.outputs.BOOL_TRIGGERED == 'true' && steps.check_permissions.outputs.EXECUTABLE == 'true'
run: |
curl -s ${PR_INFO_URL} > /tmp/pr_info.json
PR_RESP=`cat /tmp/pr_info.json`
HEAD_BRANCH=$(cat /tmp/pr_info.json | jq -r .head.ref)
if [[ -z "$HEAD_BRANCH" ]]; then
echo "Cannot get head branch information for PR #${PR_NUM}!"
echo "API response: $PR_RESP"
exit 1
fi
echo "Head branch for PR #${PR_NUM} is ${HEAD_BRANCH}"
git checkout ${HEAD_BRANCH}
make gotests/install
make -j 4 gotests/gen
git config --global user.name "vdaas-ci"
git config --global user.email "ci@vdaas.org"
git add cmd hack internal pkg
git commit --signoff -m ":robot: Add automatically generated tests"
git pull --rebase
git remote set-url origin "https://${GITHUB_USER}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git push origin ${HEAD_BRANCH}
env:
GITHUB_USER: ${{ secrets.DISPATCH_USER }}
GITHUB_TOKEN: ${{ secrets.DISPATCH_TOKEN }}
PR_INFO_URL: ${{ github.event.issue.pull_request.url }}
PR_AUTHOR: ${{ github.event.issue.user.login }}
PR_NUM: ${{ github.event.issue.number }}
- name: failure comment
if: failure()
run: |
curl --include --verbose --fail \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-H "Authorization: token ${GITHUB_TOKEN}" \
--request POST \
--data "{\"body\": \"**[GEN TEST]** Failed to generate tests.\"}" \
$API_URL
env:
GITHUB_TOKEN: ${{ secrets.DISPATCH_TOKEN }}
API_URL: ${{ github.event.issue.comments_url }}
format:
name: Add license and run goimports
needs:
- rebase
- add-contributors
- gentest
runs-on: ubuntu-latest
steps:
- name: check PR Comments
Expand Down
53 changes: 41 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,46 @@ PROTO_PATHS = \
$(GOPATH)/src/github.com/danielvladco/go-proto-gql \
$(GOPATH)/src/github.com/envoyproxy/protoc-gen-validate

GO_SOURCES = $(shell find \
./cmd \
./hack \
./internal \
./pkg \
-not -path './cmd/cli/*' \
-not -path './internal/core/ngt/*' \
-not -path './hack/benchmark/internal/client/ngtd/*' \
-not -path './hack/benchmark/internal/starter/agent/*' \
-not -path './hack/benchmark/internal/starter/external/*' \
-not -path './hack/benchmark/internal/starter/gateway/*' \
-not -path './hack/license/*' \
-not -path './hack/swagger/*' \
-not -path './hack/tools/*' \
-type f \
-name '*.go' \
-not -regex '.*options?\.go' \
-not -name '*_test.go' \
-not -name 'doc.go')
GO_OPTION_SOURCES = $(shell find \
./cmd \
./hack \
./internal \
./pkg \
-not -path './cmd/cli/*' \
-not -path './internal/core/ngt/*' \
-not -path './hack/benchmark/internal/client/ngtd/*' \
-not -path './hack/benchmark/internal/starter/agent/*' \
-not -path './hack/benchmark/internal/starter/external/*' \
-not -path './hack/benchmark/internal/starter/gateway/*' \
-not -path './hack/license/*' \
-not -path './hack/swagger/*' \
-not -path './hack/tools/*' \
-type f \
-regex '.*options?\.go' \
-not -name '*_test.go' \
-not -name 'doc.go')
GO_TEST_SOURCES = $(GO_SOURCES:%.go=%_test.go)
GO_OPTION_TEST_SOURCES = $(GO_OPTION_SOURCES:%.go=%_test.go)

COMMA := ,
SHELL = bash

Expand Down Expand Up @@ -268,18 +308,6 @@ tensorflow/install: /usr/local/lib/libtensorflow.so
rm -f libtensorflow-cpu-linux-x86_64-$(TENSORFLOW_C_VERSION).tar.gz
ldconfig

.PHONY: gentest
## gentest
gentest:
$(call gen-test)


.PHONY: fixtest
## fixtest
fixtest:
$(call fix-test)


.PHONY: test
## run tests
test:
Expand Down Expand Up @@ -318,3 +346,4 @@ include Makefile.d/k8s.mk
include Makefile.d/kind.mk
include Makefile.d/client.mk
include Makefile.d/ml.mk
include Makefile.d/test.mk
36 changes: 0 additions & 36 deletions Makefile.d/functions.mk
Original file line number Diff line number Diff line change
Expand Up @@ -90,39 +90,3 @@ define telepresence
## https://github.com/telepresenceio/telepresence/commit/bb7473fbf19ed4f61796a5e32747e23de6ab03da
## --deployment-type "$(SWAP_DEPLOYMENT_TYPE)"
endef

define gen-test
find . -type d | \
grep "./cmd\|./hack\|./internal\|./pkg" | \
grep -v "./cmd/cli\| \
./internal/core/ngt\| \
./hack/benchmark/internal/client/ngtd\| \
./hack/benchmark/internal/starter/agent\| \
./hack/benchmark/internal/starter/external\| \
./hack/benchmark/internal/starter/gateway\| \
./hack/license\| \
./hack/swagger\| \
./hack/tools"| \
while read dir; do \
files=`find $${dir} -type f -maxdepth 1 -name "*.go" -not -name '*_test.go' -not -name 'doc.go'`; \
for file in $${files}; do \
path='./assets/test/templates/common'; \
if [[ $${file} =~ .*options?\.go ]] ; then \
path='./assets/test/templates/option'; \
fi; \
echo start generate test code: $${file}; \
gotests -w -template_dir $${path} -all $${file}; \
done; \
done
rm $(ROOTDIR)/internal/core/ngt/*test.go
endef

define fix-test
find $(ROOTDIR)/internal/k8s/* -name '*_test.go' | xargs sed -i -E "s%k8s.io/apimachinery/pkg/api/errors%github.com/vdaas/vald/internal/errors%g"
find $(ROOTDIR)/* -name '*_test.go' | xargs sed -i -E "s%cockroachdb/errors%vdaas/vald/internal/errors%g"
find $(ROOTDIR)/* -name '*_test.go' | xargs sed -i -E "s%pkg/errors%vdaas/vald/internal/errors%g"
find $(ROOTDIR)/* -name '*_test.go' | xargs sed -i -E "s%go-errors/errors%vdaas/vald/internal/errors%g"
find $(ROOTDIR)/internal/errors -name '*_test.go' | xargs sed -i -E "s%\"github.com/vdaas/vald/internal/errors\"%%g"
find $(ROOTDIR)/internal/errors -name '*_test.go' | xargs sed -i -E "s/errors\.//g"
go mod tidy
endef
53 changes: 53 additions & 0 deletions Makefile.d/test.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#
# Copyright (C) 2019-2020 Vdaas.org Vald team ( kpango, rinx, kmrmt )
#
# 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
#
# https://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.
#

.PHONY: gotests/install
gotests/install:
go get -u github.com/cweill/gotests/...

.PHONY: gotests/gen
## generate missing go test files
gotests/gen: \
$(GO_TEST_SOURCES) \
$(GO_OPTION_TEST_SOURCES) \
gotests/patch

.PHONY: gotests/patch
## apply patches to generated go test files
gotests/patch: \
$(GO_TEST_SOURCES) \
$(GO_OPTION_TEST_SOURCES)
@$(call green, "apply patches to go test files...")
find $(ROOTDIR)/internal/k8s/* -name '*_test.go' | xargs sed -i -E "s%k8s.io/apimachinery/pkg/api/errors%github.com/vdaas/vald/internal/errors%g"
find $(ROOTDIR)/* -name '*_test.go' | xargs sed -i -E "s%cockroachdb/errors%vdaas/vald/internal/errors%g"
find $(ROOTDIR)/* -name '*_test.go' | xargs sed -i -E "s%pkg/errors%vdaas/vald/internal/errors%g"
find $(ROOTDIR)/* -name '*_test.go' | xargs sed -i -E "s%go-errors/errors%vdaas/vald/internal/errors%g"
find $(ROOTDIR)/internal/errors -name '*_test.go' | xargs sed -i -E "s%\"github.com/vdaas/vald/internal/errors\"%%g"
find $(ROOTDIR)/internal/errors -name '*_test.go' | xargs sed -i -E "s/errors\.//g"

# force to rebuild all GO_TEST_SOURCES targets
.PHONY: $(GO_TEST_SOURCES)
$(GO_TEST_SOURCES): \
./assets/test/templates/common
@$(call green, $(patsubst %,"generating go test file: %",$@))
gotests -w -template_dir ./assets/test/templates/common -all $(patsubst %_test.go,%.go,$@)

# force to rebuild all GO_OPTION_TEST_SOURCES targets
.PHONY: $(GO_OPTION_TEST_SOURCES)
$(GO_OPTION_TEST_SOURCES): \
./assets/test/templates/option
@$(call green, $(patsubst %,"generating go test file: %",$@))
gotests -w -template_dir ./assets/test/templates/option -all $(patsubst %_test.go,%.go,$@)

0 comments on commit 083f62d

Please sign in to comment.