Skip to content

Commit

Permalink
Fix agents tests 3rd scenario race condition.
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 0e77f3d commit b50864f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 94 deletions.
27 changes: 12 additions & 15 deletions gossip/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (m *Monitor) dispatchTasks() {
for {
select {
case task = <-m.taskCh:
go m.executeTask(task)
go m.executeTask(*task)
count++
default:
return
Expand All @@ -130,22 +130,19 @@ func (m *Monitor) dispatchTasks() {
}

func (m *Monitor) sendAlert(msg string) {

go func() {
resp, err := http.Post(fmt.Sprintf("%s/alert", m.conf.PubUrls), "application/json",
bytes.NewBufferString(msg))
if err != nil {
log.Infof("Error saving batch in alertStore: %v", err)
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Infof("Error getting response from alertStore saving a batch: %v", err)
}
}()
resp, err := http.Post(fmt.Sprintf("%s/alert", m.conf.PubUrls[0]), "application/json",
bytes.NewBufferString(msg))
if err != nil {
log.Infof("Error saving batch in alertStore: %v", err)
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Infof("Error getting response from alertStore saving a batch: %v", err)
}
}

func (m *Monitor) executeTask(task *QueryTask) {
func (m *Monitor) executeTask(task QueryTask) {
log.Debug("Executing task: %+v", task)
resp, err := m.client.Incremental(task.Start, task.End)
if err != nil {
Expand Down
143 changes: 70 additions & 73 deletions tests/e2e/agents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,77 +79,74 @@ func TestAgents(t *testing.T) {
client := getClient(0)
event := rand.RandomString(10)

// scenario("Add one event and check that it has been published without alerts", 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)
// time.Sleep(2 * time.Second)
// })
//
// 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()
// assert.NoError(t, err)
// assert.False(t, strings.Contains(string(alerts), "Unable to verify snapshot"), "Must not exist alerts")
// })
//
// let("Check Monitor do not create any alert", func(t *testing.T) {
// time.Sleep(1 * time.Second)
// alerts, err := getAlert()
// assert.NoError(t, err)
// assert.False(t, strings.Contains(string(alerts), "Unable to verify incremental"), "Must not exist alerts")
// })
//
// })
//
// scenario("Add 1st event. Tamper it. Check auditor alerts correctly", func() {
// var err error
//
// let("Add 1st event", func(t *testing.T) {
// _, err = client.Add(event)
// assert.NoError(t, err)
// time.Sleep(2 * time.Second)
// })
//
// let("Tamper 1st event", func(t *testing.T) {
// cmd := exec.Command("curl",
// "-sS",
// "-XDELETE",
// "-H", fmt.Sprintf("Api-Key:%s", APIKey),
// "-H", "Content-type: application/json",
// QEDTamperURL,
// "-d", fmt.Sprintf(`{"Digest": "%X"}`, hashing.NewSha256Hasher().Do(hashing.Digest(event))),
// )
//
// _, err := cmd.CombinedOutput()
// assert.NoError(t, err, "Subprocess must not exit with status 1")
// })
//
// let("Check Auditor alerts", func(t *testing.T) {
// time.Sleep(1 * time.Second)
// alerts, err := getAlert()
// assert.NoError(t, err)
// assert.True(t, strings.Contains(string(alerts), "Unable to verify snapshot"), "Must exist auditor alerts")
// })
//
// let("Check Monitor do not create any alert", func(t *testing.T) {
// time.Sleep(1 * time.Second)
// alerts, err := getAlert()
// assert.NoError(t, err)
// assert.False(t, strings.Contains(string(alerts), "Unable to verify incremental"), "Must not exist monitor alert")
// })
// })
scenario("Add one event and check that it has been published without alerts", 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)
time.Sleep(2 * time.Second)
})

let("Get signed snapshot from snapshot public storage", func(t *testing.T) {
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) {
alerts, err := getAlert()
assert.NoError(t, err)
assert.False(t, strings.Contains(string(alerts), "Unable to verify snapshot"), "Must not exist alerts")
})

let("Check Monitor do not create any alert", func(t *testing.T) {
alerts, err := getAlert()
assert.NoError(t, err)
assert.False(t, strings.Contains(string(alerts), "Unable to verify incremental"), "Must not exist alerts")
})

})

scenario("Add 1st event. Tamper it. Check auditor alerts correctly", func() {
var err error

let("Add 1st event", func(t *testing.T) {
_, err = client.Add(event)
assert.NoError(t, err)
time.Sleep(2 * time.Second)
})

let("Tamper 1st event", func(t *testing.T) {
cmd := exec.Command("curl",
"-sS",
"-XDELETE",
"-H", fmt.Sprintf("Api-Key:%s", APIKey),
"-H", "Content-type: application/json",
QEDTamperURL,
"-d", fmt.Sprintf(`{"Digest": "%X"}`, hashing.NewSha256Hasher().Do(hashing.Digest(event))),
)

_, err := cmd.CombinedOutput()
assert.NoError(t, err, "Subprocess must not exit with status 1")
})

let("Check Auditor alerts", func(t *testing.T) {
time.Sleep(1 * time.Second)
alerts, err := getAlert()
assert.NoError(t, err)
assert.True(t, strings.Contains(string(alerts), "Unable to verify snapshot"), "Must exist auditor alerts")
})

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

scenario("Add 1st event. Tamper it. Add 2nd event. Check monitor alerts correctly", func() {
hasher := hashing.NewSha256Hasher()
Expand Down Expand Up @@ -186,13 +183,13 @@ func TestAgents(t *testing.T) {
time.Sleep(2 * time.Second)
})

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

let("Check Monitor alert", func(t *testing.T) {
let("Check Monitor alerts", func(t *testing.T) {
alerts, err := getAlert()
assert.NoError(t, err)
assert.True(t, strings.Contains(string(alerts), "Unable to verify incremental"), "Must exist monitor alert")
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func merge(list ...scope.TestF) scope.TestF {
return func(t *testing.T) {
for _, elem := range list {
elem(t)
time.Sleep(2 * time.Second)
// time.Sleep(2 * time.Second)
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions tests/e2e/test_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,13 +239,8 @@ func (s *Service) Start() {
log.Debugf("Counters ", s.stats.count)
atomic.StoreUint64(&s.stats.count[RPS], 0)
case <-s.quitCh:
<<<<<<< HEAD
log.Debugf("\nShutting down the server...")
httpServer.Shutdown(context.Background())
=======
fmt.Println("Shutting down test service...")
_ = httpServer.Shutdown(context.Background())
>>>>>>> Fix race condition in e2e agent tests
return
}
}
Expand Down

0 comments on commit b50864f

Please sign in to comment.