Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add e2e #4

Merged
merged 2 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Run E2E Test Suites

on:
push:
branches:
- main
- release/**
pull_request:
branches:
- main
- release/**
permissions: read-all
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true
jobs:
changes:
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
src: ${{ steps.filter.outputs.src }}
steps:
- name: Checkout
uses: actions/checkout@v3
- uses: dorny/paths-filter@v2
id: filter
with:
token: ${{ secrets.GITHUB_TOKEN }}
filters: |
src:
- '*.go'
- '**/*.go'
- 'go.mod'
- 'go.sum'
- 'Makefile'
- '.github/**'
- 'internal/**'
- 'svc/**'
- 'core/**'
run-test:
needs: changes
if: |
(needs.changes.outputs.src == 'true')
runs-on: ubuntu-latest
timeout-minutes: 20
services:
etcd:
image: bitnami/etcd:3.4.0
ports:
- 2379:2379
- 2380:2380
env:
ALLOW_NONE_AUTHENTICATION: yes
ETCD_ADVERTISE_CLIENT_URLS: http://0.0.0.0:2379
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Setup Go Environment
uses: actions/setup-go@v3
with:
go-version: '1.20.3'

- name: Install ginkgo
run: |
go install github.com/onsi/ginkgo/v2/ginkgo@latest
sudo cp ~/go/bin/ginkgo /usr/local/bin

- name: Run E2E Test Suites
working-directory: ./
run: |
export PATH=$PWD/bin:$PATH
make build
make e2e
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.vscode/

.DS_Store
go.sum

bin/
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#/bin/bash
PWD ?= $(shell pwd)
export PATH := $(PWD)/bin:$(PATH)

default: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
.PHONY: help

build: build-main build-notify ## Build main and notify
.PHONY: build

build-main:
@go build -o bin/main main.go
.PHONY: build-main

build-notify:
@go build -o bin/notify notify/main.go
.PHONY: build-notify

e2e: ## Run e2e test
@cd test/e2e && ginkgo -r
.PHONY: e2e

.PHONY: fmt
fmt: ## Format all go codes
./scripts/goimports-reviser.sh
2 changes: 1 addition & 1 deletion core/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (e *EtcdMgr) Close() {
_ = e.watcher.Close()
}

//CreateWatchChan 创建 watch 通道
// CreateWatchChan 创建 watch 通道
func (e *EtcdMgr) CreateWatchChan(ctx context.Context, key string) (clientv3.WatchChan, error) {
// constant.ChannelConfigDir
getResp, err := e.kv.Get(ctx, key, clientv3.WithPrefix())
Expand Down
26 changes: 25 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
module broadcast

go 1.16
go 1.20

require (
github.com/onsi/ginkgo/v2 v2.11.0
github.com/onsi/gomega v1.27.10
go.etcd.io/etcd/api/v3 v3.5.0
go.etcd.io/etcd/client/v3 v3.5.0
google.golang.org/grpc v1.38.0
)

require (
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
go.etcd.io/etcd/client/pkg/v3 v3.5.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.17.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.9.3 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
290 changes: 290 additions & 0 deletions go.sum

Large diffs are not rendered by default.

Empty file modified scripts/goimports-reviser.sh
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion svc/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type RegionBroad struct {
stopC chan struct{}
}

//trimPrefix 过滤出对应变动的 regionId
// trimPrefix 过滤出对应变动的 regionId
func (r *RegionBroad) trimPrefix(key string) RegionId {
return RegionId(strings.TrimPrefix(key, r.prefix))
}
Expand Down
75 changes: 75 additions & 0 deletions test/e2e/broadcast/broadcast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* MIT License
*
* Copyright (c) 2021 ashing
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package broadcast

import (
"bytes"
"os/exec"
"syscall"
"time"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
)

var _ = ginkgo.Describe("CLI", func() {
ginkgo.Context("test add agent and then notify", func() {
ginkgo.It("should return logs", func() {
// start main
var out bytes.Buffer
cmd := exec.Command("main")
cmd.Stdout = &out
cmd.Stderr = &out
err := cmd.Start()
gomega.Expect(err).Should(gomega.BeNil())
pid := cmd.Process.Pid
gomega.Expect(pid).Should(gomega.BeNumerically(">", 0))
go func() {
time.Sleep(3 * time.Second)
err = cmd.Process.Signal(syscall.SIGINT)
gomega.Expect(err).Should(gomega.BeNil())
err = cmd.Wait()
gomega.Expect(err).Should(gomega.BeNil())
}()
// start notify
var notifyOut bytes.Buffer
notifyCmd := exec.Command("notify")
notifyCmd.Stdout = &notifyOut
notifyCmd.Stderr = &notifyOut
err = notifyCmd.Run()
gomega.Expect(err).Should(gomega.BeNil())
// check log
time.Sleep(5 * time.Second)
gomega.Expect(out.String()).Should(gomega.ContainSubstring("add agent agent_1 region_123456"))
gomega.Expect(out.String()).Should(gomega.ContainSubstring("add agent agent_2 region_123456"))
gomega.Expect(out.String()).Should(gomega.ContainSubstring("key: /config/region_123456 value: value change"))
gomega.Expect(out.String()).Should(gomega.ContainSubstring("agents count 2"))
gomega.Expect(out.String()).Should(gomega.ContainSubstring("agent_1 接收到新的配置,value change"))
gomega.Expect(out.String()).Should(gomega.ContainSubstring("agent_2 接收到新的配置,value change"))

gomega.Expect(notifyOut.String()).Should(gomega.ContainSubstring("value will change"))
})
})
})
39 changes: 39 additions & 0 deletions test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* MIT License
*
* Copyright (c) 2021 ashing
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package e2e

import (
"testing"

"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"

_ "broadcast/test/e2e/broadcast"
)

func TestBroadCast(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t, "BroadCast CLI test suites")
}