@@ -10,6 +10,7 @@ import (
10
10
"github.com/dedis/d-voting/contracts/evoting/types"
11
11
"github.com/dedis/d-voting/internal/testing/fake"
12
12
"github.com/dedis/d-voting/services/dkg"
13
+ "github.com/prometheus/client_golang/prometheus/testutil"
13
14
"github.com/stretchr/testify/require"
14
15
"go.dedis.ch/dela/core/access"
15
16
"go.dedis.ch/dela/core/execution"
@@ -109,6 +110,8 @@ func TestExecute(t *testing.T) {
109
110
}
110
111
111
112
func TestCommand_CreateElection (t * testing.T ) {
113
+ initMetrics ()
114
+
112
115
fakeActor := fakeDkgActor {
113
116
publicKey : suite .Point (),
114
117
err : nil ,
@@ -167,13 +170,16 @@ func TestCommand_CreateElection(t *testing.T) {
167
170
require .True (t , ok )
168
171
169
172
require .Equal (t , types .Initial , election .Status )
173
+ require .Equal (t , float64 (types .Initial ), testutil .ToFloat64 (PromElectionStatus ))
170
174
}
171
175
172
176
func TestCommand_OpenElection (t * testing.T ) {
173
177
// TODO
174
178
}
175
179
176
180
func TestCommand_CastVote (t * testing.T ) {
181
+ initMetrics ()
182
+
177
183
castVote := types.CastVote {
178
184
ElectionID : fakeElectionID ,
179
185
UserID : "dummyUserId" ,
@@ -298,11 +304,13 @@ func TestCommand_CastVote(t *testing.T) {
298
304
require .Len (t , election .Suffragia .Ciphervotes , 1 )
299
305
require .True (t , castVote .Ballot .Equal (election .Suffragia .Ciphervotes [0 ]))
300
306
301
- require .Equal (t , castVote .UserID ,
302
- election .Suffragia .UserIDs [ 0 ] )
307
+ require .Equal (t , castVote .UserID , election . Suffragia . UserIDs [ 0 ])
308
+ require . Equal ( t , float64 ( len ( election .Suffragia .Ciphervotes )), testutil . ToFloat64 ( PromElectionBallots ) )
303
309
}
304
310
305
311
func TestCommand_CloseElection (t * testing.T ) {
312
+ initMetrics ()
313
+
306
314
closeElection := types.CloseElection {
307
315
ElectionID : fakeElectionID ,
308
316
UserID : "dummyUserId" ,
@@ -349,6 +357,7 @@ func TestCommand_CloseElection(t *testing.T) {
349
357
err = cmd .closeElection (snap , makeStep (t , ElectionArg , string (data )))
350
358
require .EqualError (t , err , fmt .Sprintf ("the election is not open, " +
351
359
"current status: %d" , types .Initial ))
360
+ require .Equal (t , 0 , testutil .CollectAndCount (PromElectionStatus ))
352
361
353
362
dummyElection .Status = types .Open
354
363
@@ -372,6 +381,7 @@ func TestCommand_CloseElection(t *testing.T) {
372
381
373
382
err = cmd .closeElection (snap , makeStep (t , ElectionArg , string (data )))
374
383
require .NoError (t , err )
384
+ require .Equal (t , float64 (types .Closed ), testutil .ToFloat64 (PromElectionStatus ))
375
385
376
386
res , err := snap .Get (dummyElectionIDBuff )
377
387
require .NoError (t , err )
@@ -383,6 +393,7 @@ func TestCommand_CloseElection(t *testing.T) {
383
393
require .True (t , ok )
384
394
385
395
require .Equal (t , types .Closed , election .Status )
396
+ require .Equal (t , float64 (types .Closed ), testutil .ToFloat64 (PromElectionStatus ))
386
397
}
387
398
388
399
func TestCommand_ShuffleBallotsCannotShuffleTwice (t * testing.T ) {
@@ -429,6 +440,8 @@ func TestCommand_ShuffleBallotsCannotShuffleTwice(t *testing.T) {
429
440
}
430
441
431
442
func TestCommand_ShuffleBallotsValidScenarios (t * testing.T ) {
443
+ initMetrics ()
444
+
432
445
k := 3
433
446
434
447
// Simple Shuffle from round 0 :
@@ -454,6 +467,7 @@ func TestCommand_ShuffleBallotsValidScenarios(t *testing.T) {
454
467
455
468
err = cmd .shuffleBallots (snap , step )
456
469
require .NoError (t , err )
470
+ require .Equal (t , float64 (1 ), testutil .ToFloat64 (PromElectionShufflingInstances ))
457
471
458
472
// Valid Shuffle is over :
459
473
shuffleBallots .Round = k
@@ -483,6 +497,7 @@ func TestCommand_ShuffleBallotsValidScenarios(t *testing.T) {
483
497
484
498
err = cmd .shuffleBallots (snap , makeStep (t , ElectionArg , string (data )))
485
499
require .NoError (t , err )
500
+ require .Equal (t , float64 (k + 1 ), testutil .ToFloat64 (PromElectionShufflingInstances ))
486
501
487
502
// Check the shuffle is over:
488
503
electionTxIDBuff , err := hex .DecodeString (election .ElectionID )
@@ -497,7 +512,8 @@ func TestCommand_ShuffleBallotsValidScenarios(t *testing.T) {
497
512
election , ok := message .(types.Election )
498
513
require .True (t , ok )
499
514
500
- require .Equal (t , election .Status , types .ShuffledBallots )
515
+ require .Equal (t , types .ShuffledBallots , election .Status )
516
+ require .Equal (t , float64 (types .ShuffledBallots ), testutil .ToFloat64 (PromElectionStatus ))
501
517
}
502
518
503
519
func TestCommand_ShuffleBallotsFormatErrors (t * testing.T ) {
@@ -867,6 +883,7 @@ func TestCommand_RegisterPubShares(t *testing.T) {
867
883
868
884
err = cmd .registerPubshares (snap , makeStep (t , ElectionArg , string (data )))
869
885
require .NoError (t , err )
886
+ require .Equal (t , float64 (1 ), testutil .ToFloat64 (PromElectionPubShares ))
870
887
871
888
// With the public key already used:
872
889
election .PubsharesUnits .PubKeys = append (election .PubsharesUnits .PubKeys ,
@@ -907,6 +924,7 @@ func TestCommand_RegisterPubShares(t *testing.T) {
907
924
908
925
err = cmd .registerPubshares (snap , makeStep (t , ElectionArg , string (data )))
909
926
require .NoError (t , err )
927
+ require .Equal (t , float64 (1 ), testutil .ToFloat64 (PromElectionPubShares ))
910
928
911
929
res , err := snap .Get (dummyElectionIDBuff )
912
930
require .NoError (t , err )
@@ -1019,7 +1037,7 @@ func TestCommand_DecryptBallots(t *testing.T) {
1019
1037
1020
1038
require .Equal (t , types.Ballot {}, election .DecryptedBallots [0 ])
1021
1039
require .Equal (t , types .ResultAvailable , election .Status )
1022
-
1040
+ require . Equal ( t , float64 ( types . ResultAvailable ), testutil . ToFloat64 ( PromElectionStatus ))
1023
1041
}
1024
1042
1025
1043
func TestCommand_CancelElection (t * testing.T ) {
@@ -1079,7 +1097,7 @@ func TestCommand_CancelElection(t *testing.T) {
1079
1097
require .True (t , ok )
1080
1098
1081
1099
require .Equal (t , types .Canceled , election .Status )
1082
-
1100
+ require . Equal ( t , float64 ( types . Canceled ), testutil . ToFloat64 ( PromElectionStatus ))
1083
1101
}
1084
1102
1085
1103
func TestRegisterContract (t * testing.T ) {
@@ -1089,6 +1107,13 @@ func TestRegisterContract(t *testing.T) {
1089
1107
// -----------------------------------------------------------------------------
1090
1108
// Utility functions
1091
1109
1110
+ func initMetrics () {
1111
+ PromElectionStatus .Reset ()
1112
+ PromElectionBallots .Reset ()
1113
+ PromElectionShufflingInstances .Reset ()
1114
+ PromElectionPubShares .Reset ()
1115
+ }
1116
+
1092
1117
func initElectionAndContract () (types.Election , Contract ) {
1093
1118
fakeDkg := fakeDKG {
1094
1119
actor : fakeDkgActor {},
0 commit comments