Skip to content

Commit

Permalink
Tamper test structure
Browse files Browse the repository at this point in the history
Co-authored-by: iknite <[email protected]>
  • Loading branch information
Jose Luis Lucas and iknite committed Dec 18, 2018
1 parent 52ed874 commit 0be89c2
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 40 deletions.
118 changes: 78 additions & 40 deletions tests/e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ import (
"time"

"github.com/bbva/qed/client"
"github.com/bbva/qed/gossip"
"github.com/bbva/qed/gossip/auditor"
"github.com/bbva/qed/gossip/member"
"github.com/bbva/qed/gossip/monitor"
"github.com/bbva/qed/gossip/publisher"
"github.com/bbva/qed/server"
Expand All @@ -34,47 +36,70 @@ var apiKey, storageType, keyFile string
var cacheSize uint64

const (
QEDUrl = "http://127.0.0.1:9000"
PubUrl = "http://127.0.0.1:9000"
QEDUrl = "http://127.0.0.1:8080"
PubUrl = "http://127.0.0.1:8888"
APIKey = "my-key"
)

func init() {
apiKey = APIKey
cacheSize = 50000
storageType = "badger"

usr, _ := user.Current()
keyFile = fmt.Sprintf("%s/.ssh/id_ed25519", usr.HomeDir)
}

// merge function is a helper function that execute all the variadic parameters
// inside a score.TestF function
func merge(list ...scope.TestF) scope.TestF {
return func(t *testing.T) {
for _, elem := range list {
elem()
elem(t)
}
}
}

func init() {
apiKey = APIKey
cacheSize = 50000
storageType = "badger"
func newAgent(id int, name string, role member.Type, p gossip.Processor, t *testing.T) {
agentConf := gossip.DefaultConfig()
agentConf.NodeName = fmt.Sprintf("%s%d", name, id)

switch role {
case member.Auditor:
agentConf.BindAddr = fmt.Sprintf("127.0.0.1:910%d", id)
case member.Monitor:
agentConf.BindAddr = fmt.Sprintf("127.0.0.1:920%d", id)
case member.Publisher:
agentConf.BindAddr = fmt.Sprintf("127.0.0.1:930%d", id)
}

usr, _ := user.Current()
keyFile = fmt.Sprintf("%s/.ssh/id_ed25519", usr.HomeDir)
agentConf.StartJoin = []string{QEDUrl}
agentConf.EnableCompression = true
agentConf.AlertsUrls = []string{PubUrl}
agentConf.Role = role

_, err := gossip.NewAgent(agentConf, []gossip.Processor{p})
if err != nil {
t.Fatalf("Failed to start the QED %s: %v", name, err)
}
}

func setupAuditor(t *testing.T) (scope.TestF, scope.TestF) {
func setupAuditor(id int, t *testing.T) (scope.TestF, scope.TestF) {
var au *auditor.Auditor
var err error

before := func(t *testing.T) {
conf := auditor.DefaultConfig()
conf.QEDUrls = []string{QEDUrl}
conf.PubUrls = []string{PubUrl}
conf.APIKey = APIKey
auditorConf := auditor.DefaultConfig()
auditorConf.QEDUrls = []string{QEDUrl}
auditorConf.PubUrls = []string{PubUrl}
auditorConf.APIKey = APIKey

go (func() {
au, err = auditor.NewAuditor(conf)
if err != nil {
t.Fatalf("Unable to create a new auditor: %v", err)
}
})()
time.Sleep(2 * time.Second)
au, err = auditor.NewAuditor(auditorConf)
if err != nil {
t.Fatalf("Unable to create a new auditor: %v", err)
}

newAgent(id, "auditor", member.Auditor, au, t)
}

after := func(t *testing.T) {
Expand All @@ -87,22 +112,21 @@ func setupAuditor(t *testing.T) (scope.TestF, scope.TestF) {
return before, after
}

func setupMonitor(t *testing.T) (scope.TestF, scope.TestF) {
func setupMonitor(id int, t *testing.T) (scope.TestF, scope.TestF) {
var mn *monitor.Monitor
var err error

before := func(t *testing.T) {
conf := monitor.DefaultConfig()
conf.QEDEndpoints = []string{QEDUrl}
conf.APIKey = APIKey
monitorConf := monitor.DefaultConfig()
monitorConf.QEDEndpoints = []string{QEDUrl}
monitorConf.APIKey = APIKey

go (func() {
mn, err = monitor.NewMonitor(conf)
if err != nil {
t.Fatalf("Unable to create a new monitor: %v", err)
}
})()
time.Sleep(2 * time.Second)
mn, err = monitor.NewMonitor(monitorConf)
if err != nil {
t.Fatalf("Unable to create a new monitor: %v", err)
}

newAgent(id, "monitor", member.Monitor, mn, t)
}

after := func(t *testing.T) {
Expand All @@ -115,21 +139,20 @@ func setupMonitor(t *testing.T) (scope.TestF, scope.TestF) {
return before, after
}

func setupPublisher(t *testing.T) (scope.TestF, scope.TestF) {
func setupPublisher(id int, t *testing.T) (scope.TestF, scope.TestF) {
var pu *publisher.Publisher
var err error

before := func(t *testing.T) {
conf := publisher.DefaultConfig()
conf.PubUrls = []string{PubUrl}

go (func() {
pu, err = publisher.NewPublisher(conf)
if err != nil {
t.Fatalf("Unable to create a new publisher: %v", err)
}
})()
time.Sleep(2 * time.Second)
pu, err = publisher.NewPublisher(conf)
if err != nil {
t.Fatalf("Unable to create a new publisher: %v", err)
}

newAgent(id, "publisher", member.Publisher, pu, t)
}

after := func(t *testing.T) {
Expand All @@ -142,6 +165,21 @@ func setupPublisher(t *testing.T) (scope.TestF, scope.TestF) {
return before, after
}

func setupStore(t *testing.T) (scope.TestF, scope.TestF) {
var s *Service
before := func(t *testing.T) {
go (func() {
s = NewService()
s.Start()
})()
}

after := func(t *testing.T) {
s.Shutdown()
}
return before, after
}

func setupServer(id int, joinAddr string, t *testing.T) (scope.TestF, scope.TestF) {
var srv *server.Server
var err error
Expand Down
50 changes: 50 additions & 0 deletions tests/e2e/tamper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A.
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 e2e

import (
"testing"

"github.com/bbva/qed/testutils/rand"
"github.com/bbva/qed/testutils/scope"
assert "github.com/stretchr/testify/require"
)

func TestTamper(t *testing.T) {
bStore, aStore := setupStore(t)
bServer, aServer := setupServer(0, "", t)
bAuditor, aAuditor := setupAuditor(0, t)
bMonitor, aMonitor := setupMonitor(0, t)
bPublisher, aPublisher := setupPublisher(0, t)

scenario, let := scope.Scope(t,
merge(bStore, bServer, bAuditor, bMonitor, bPublisher),
merge(aStore, aServer, aAuditor, aMonitor, aPublisher),
)

client := getClient(0)

event := rand.RandomString(10)

scenario("S", func() {
var err error

let("Add event", func(t *testing.T) {
_, err = client.Add(event)
assert.NoError(t, err)
})
})
}
9 changes: 9 additions & 0 deletions tests/e2e/test_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ type Service struct {
snaps *snapStore
alerts *alertStore
stats *statStore
quitCh chan bool
}

func NewService() *Service {
Expand All @@ -212,6 +213,7 @@ func NewService() *Service {
snaps: &snaps,
alerts: &alerts,
stats: &stats,
quitCh: make(chan bool),
}
}

Expand All @@ -225,6 +227,8 @@ func (s *Service) Start() {
fmt.Println("Request per second: ", c)
fmt.Println("Counters ", s.stats.count)
atomic.StoreUint64(&s.stats.count[RPS], 0)
case <-s.quitCh:
return
}
}
}()
Expand All @@ -235,3 +239,8 @@ func (s *Service) Start() {
http.HandleFunc("/alert", s.alertHandler())
log.Fatal(http.ListenAndServe("127.0.0.1:8888", nil))
}

func (s *Service) Shutdown() {
s.quitCh <- true
close(s.quitCh)
}

0 comments on commit 0be89c2

Please sign in to comment.