Skip to content

Commit

Permalink
Create auditor test. Now it fails.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jose Luis Lucas authored and iknite committed Dec 18, 2018
1 parent b675ab6 commit 16be795
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 52 deletions.
39 changes: 19 additions & 20 deletions gossip/auditor/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type MembershipTask struct {
}

func (t *MembershipTask) Do() {
proof, err := t.qed.Membership(t.s.Snapshot.EventDigest, t.s.Snapshot.Version)
proof, err := t.qed.MembershipDigest(t.s.Snapshot.EventDigest, t.s.Snapshot.Version)
if err != nil {
// retry
log.Errorf("Error executing membership query: %v", err)
Expand Down Expand Up @@ -135,54 +135,53 @@ func (t *MembershipTask) sendAlert(msg string) {
log.Infof("Error getting response from alertStore saving a batch: %v", err)
}
}()

}

func (m Auditor) Process(b *protocol.BatchSnapshots) {
func (a Auditor) Process(b *protocol.BatchSnapshots) {

task := &MembershipTask{
qed: m.qed,
pubUrl: m.conf.PubUrls[0],
taskCh: m.taskCh,
qed: a.qed,
pubUrl: a.conf.PubUrls[0],
taskCh: a.taskCh,
s: b.Snapshots[0],
}

m.taskCh <- task
a.taskCh <- task
}

func (m *Auditor) runTaskDispatcher() {
m.executionTicker = time.NewTicker(m.conf.TaskExecutionInterval)
func (a *Auditor) runTaskDispatcher() {
a.executionTicker = time.NewTicker(a.conf.TaskExecutionInterval)
for {
select {
case <-m.executionTicker.C:
case <-a.executionTicker.C:
log.Debug("Dispatching tasks...")
m.dispatchTasks()
case <-m.quitCh:
a.dispatchTasks()
case <-a.quitCh:
return
}
}
}

func (m *Auditor) Shutdown() {
m.executionTicker.Stop()
m.quitCh <- true
close(m.quitCh)
close(m.taskCh)
func (a *Auditor) Shutdown() {
a.executionTicker.Stop()
a.quitCh <- true
close(a.quitCh)
close(a.taskCh)
}

func (m *Auditor) dispatchTasks() {
func (a *Auditor) dispatchTasks() {
count := 0
var task Task
defer log.Debugf("%d tasks dispatched", count)
for {
select {
case task = <-m.taskCh:
case task = <-a.taskCh:
go task.Do()
count++
default:
return
}
if count >= m.conf.MaxInFlightTasks {
if count >= a.conf.MaxInFlightTasks {
return
}
}
Expand Down
105 changes: 105 additions & 0 deletions tests/e2e/agents_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
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 (
"fmt"
"io/ioutil"
"net/http"
"strings"
"testing"
"time"

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

func getSnapshot(version uint64) (*protocol.SignedSnapshot, error) {
resp, err := http.Get(fmt.Sprintf("%s/snapshot?v=%d", StoreUrl, version))
if err != nil {
return nil, fmt.Errorf("Error getting snapshot from the store: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Error getting snapshot from the store. Status: %d", resp.StatusCode)
}

buf, _ := ioutil.ReadAll(resp.Body)
s := &protocol.SignedSnapshot{}
err = s.Decode(buf)
if err != nil {
return nil, fmt.Errorf("Error decoding signed snapshot %d codec", version)
}
return s, nil
}

func getAlert() ([]byte, error) {
resp, err := http.Get(fmt.Sprintf("%s/alert", StoreUrl))
if err != nil {
return []byte{}, fmt.Errorf("Error getting alert from alertStore: %v", err)
}
defer resp.Body.Close()
alerts, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, fmt.Errorf("Error parsing alert from alertStore: %v", err)
}
return alerts, nil
}

func TestAgents(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(aAuditor /*,aMonitor*/, aPublisher, aServer, aStore),
)

client := getClient(0)
event := rand.RandomString(10)

scenario("Add one event and check that it has been published", func() {
var snapshot *protocol.Snapshot
var ss *protocol.SignedSnapshot
var err error

let("Add event", func(t *testing.T) {
snapshot, err = client.Add(event)
assert.NoError(t, err)
})

let("Get signed snapshot from snapshot public storage", func(t *testing.T) {
time.Sleep(1 * time.Second)
ss, err = getSnapshot(0)
assert.NoError(t, err)
assert.Equal(t, snapshot, ss.Snapshot, "Snapshots must be equal")
})

let("Check Auditor do not create any alert", func(t *testing.T) {
time.Sleep(1 * time.Second)
alerts, err := getAlert()
fmt.Println(string(alerts))
assert.NoError(t, err)
assert.False(t, strings.Contains(string(alerts), "Unable to verify"), "Must not exist alerts")
})
})

}
4 changes: 2 additions & 2 deletions tests/e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
QEDUrl = "http://127.0.0.1:8080"
StoreUrl = "http://127.0.0.1:8888"
APIKey = "my-key"
QEDGossip = "127.0.0.1:9100"
QEDGossip = "127.0.0.1:9010"
)

func init() {
Expand Down Expand Up @@ -202,7 +202,7 @@ func setupServer(id int, joinAddr string, t *testing.T) (scope.TestF, scope.Test
conf.HttpAddr = fmt.Sprintf("127.0.0.1:808%d", id)
conf.RaftAddr = fmt.Sprintf("127.0.0.1:900%d", id)
conf.MgmtAddr = fmt.Sprintf("127.0.0.1:809%d", id)
conf.GossipAddr = fmt.Sprintf("127.0.0.1:910%d", id)
conf.GossipAddr = fmt.Sprintf("127.0.0.1:901%d", id)
conf.DBPath = path + "data"
conf.RaftPath = path + "raft"
conf.PrivateKeyPath = keyFile
Expand Down
36 changes: 6 additions & 30 deletions tests/e2e/tamper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
package e2e

import (
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"

Expand All @@ -29,35 +26,16 @@ import (
assert "github.com/stretchr/testify/require"
)

func getSnapshot(version uint64) (*protocol.SignedSnapshot, error) {
resp, err := http.Get(fmt.Sprintf("%s/snapshot?v=%d", StoreUrl, version))
if err != nil {
return nil, fmt.Errorf("Error getting snapshot from the store: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Error getting snapshot from the store. Status: %d", resp.StatusCode)
}

buf, _ := ioutil.ReadAll(resp.Body)
s := &protocol.SignedSnapshot{}
err = s.Decode(buf)
if err != nil {
return nil, fmt.Errorf("Error decoding signed snapshot %d codec", version)
}
return s, nil
}

func TestTamper(t *testing.T) {
bStore, aStore := setupStore(t)
bServer, aServer := setupServer(0, "", t)
// bAuditor, aAuditor := setupAuditor(0, t)
// bMonitor, aMonitor := setupMonitor(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( /*aAuditor, aMonitor,*/ aPublisher, aServer, aStore),
merge(bStore, bServer, bAuditor, bMonitor, bPublisher),
merge(aAuditor, aMonitor, aPublisher, aServer, aStore),
)

client := getClient(0)
Expand Down Expand Up @@ -110,11 +88,9 @@ func TestTamper(t *testing.T) {
})

let("Get signed snapshot from snapshot public storage", func(t *testing.T) {
time.Sleep(2 * time.Second)
time.Sleep(1 * time.Second)
ss, err := getSnapshot(0)
if err != nil {
fmt.Println("Error: ", err)
}
assert.NoError(t, err)
assert.Equal(t, snapshot, ss.Snapshot)
})
})
Expand Down

0 comments on commit 16be795

Please sign in to comment.