]> source.dussan.org Git - rspamd.git/commitdiff
[Test] Fix neural plugin tests 3988/head
authorPragadeesh Chandiran <pchandiran@mimecast.com>
Mon, 22 Nov 2021 23:19:12 +0000 (18:19 -0500)
committerPragadeesh Chandiran <pchandiran@mimecast.com>
Mon, 22 Nov 2021 23:42:24 +0000 (18:42 -0500)
lualib/plugins/neural.lua
test/functional/cases/330_neural/001_autotrain.robot
test/functional/cases/330_neural/002_manualtrain.robot
test/functional/lua/neural.lua

index f677119fe24b5ad54813a3fb89c850574155a4de..3ce91b5e2a3eb5d6a73ef7ddeb709ddd49d8fe3d 100644 (file)
@@ -620,7 +620,7 @@ local function spawn_train(params)
             params.rule.prefix, params.set.name)
       end
 
-      local roc_thresholds
+      local roc_thresholds = {}
       if params.rule.roc_enabled then
         local spam_threshold = get_roc_thresholds(train_ann,
                                                   inputs,
@@ -633,10 +633,10 @@ local function spawn_train(params)
                                                   params.rule.roc_misclassification_cost,
                                                   1 - params.rule.roc_misclassification_cost)
         roc_thresholds = {spam_threshold, ham_threshold}
-      end
 
-      rspamd_logger.messagex("ROC thresholds: (spam_threshold: %s, ham_threshold: %s)",
-                              roc_thresholds[1], roc_thresholds[2])
+        rspamd_logger.messagex("ROC thresholds: (spam_threshold: %s, ham_threshold: %s)",
+                                roc_thresholds[1], roc_thresholds[2])
+      end
 
       if not seen_nan then
         -- Convert to strings as ucl cannot rspamd_text properly
index 4ca29f19606c328d87812df8ef4dd621a599f392..793fc7561cd8ff6d483ea1b3e2f65efbda894a11 100644 (file)
@@ -16,23 +16,23 @@ ${RSPAMD_URL_TLD}  ${RSPAMD_TESTDIR}/../lua/unit/test_tld.dat
 *** Test Cases ***
 Train
   Sleep  2s  Wait for redis mess
-  FOR    ${INDEX}    IN RANGE    1    11
-    Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL${INDEX}"]}
+  FOR    ${INDEX}    IN RANGE    4    14
+    Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL1", "SPAM_SYMBOL2", "SPAM_SYMBOL3", "SPAM_SYMBOL${INDEX}"]}
     Expect Symbol  SPAM_SYMBOL${INDEX}
-    Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL${INDEX}"]}
+    Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL1", "HAM_SYMBOL2", "HAM_SYMBOL3", "HAM_SYMBOL${INDEX}"]}
     Expect Symbol  HAM_SYMBOL${INDEX}
   END
 
 Check Neural HAM
-  Sleep  2s  Wait for neural to be loaded
-  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL1","HAM_SYMBOL2","HAM_SYMBOL5"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
+  Sleep  5s  Wait for neural to be loaded
+  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL1","HAM_SYMBOL2","HAM_SYMBOL3","HAM_SYMBOL8"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
   Expect Symbol  NEURAL_HAM_SHORT
   Do Not Expect Symbol  NEURAL_SPAM_SHORT
   #Expect Symbol  NEURAL_HAM_SHORT_PCA
   #Do Not Expect Symbol  NEURAL_SPAM_SHORT_PCA
 
 Check Neural SPAM
-  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL2","SPAM_SYMBOL4","SPAM_SYMBOL6","SPAM_SYMBOL8"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
+  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL1","SPAM_SYMBOL2","SPAM_SYMBOL3","SPAM_SYMBOL8"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
   Expect Symbol  NEURAL_SPAM_SHORT
   Do Not Expect Symbol  NEURAL_HAM_SHORT
   #Expect Symbol  NEURAL_SPAM_SHORT_PCA
@@ -40,24 +40,24 @@ Check Neural SPAM
 
 
 Train INVERSE
-  FOR    ${INDEX}    IN RANGE    1    11
-    Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL${INDEX}"]; SPAM_SYMBOL${INDEX} = -5;}
+  FOR    ${INDEX}    IN RANGE    4    14
+    Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL1", "SPAM_SYMBOL2", "SPAM_SYMBOL3","SPAM_SYMBOL${INDEX}"]; SPAM_SYMBOL1 = -5; SPAM_SYMBOL2 = -5; SPAM_SYMBOL3 = -5; SPAM_SYMBOL${INDEX} = -5;}
     Expect Symbol  SPAM_SYMBOL${INDEX}
-    Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL${INDEX}"]; HAM_SYMBOL${INDEX} = 5;}
+    Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL1", "HAM_SYMBOL2", "HAM_SYMBOL3","HAM_SYMBOL${INDEX}"]; HAM_SYMBOL1 = 5; HAM_SYMBOL2 = 5; HAM_SYMBOL3 = 5; HAM_SYMBOL${INDEX} = 5;}
     Expect Symbol  HAM_SYMBOL${INDEX}
   END
 
 Check Neural HAM INVERSE
-  Sleep  2s  Wait for neural to be loaded
-  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL1","HAM_SYMBOL2","HAM_SYMBOL5"];groups_enabled=["neural"]}
+  Sleep  5s  Wait for neural to be loaded
+  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL1","HAM_SYMBOL2","HAM_SYMBOL3","HAM_SYMBOL5",];groups_enabled=["neural"]}
   Expect Symbol  NEURAL_SPAM_SHORT
   #Expect Symbol  NEURAL_SPAM_SHORT_PCA
   Do Not Expect Symbol  NEURAL_HAM_SHORT
   #Do Not Expect Symbol  NEURAL_HAM_SHORT_PCA
 
 Check Neural SPAM INVERSE
-  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL2","SPAM_SYMBOL4","SPAM_SYMBOL6","SPAM_SYMBOL8"];groups_enabled=["neural"]}
+  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL1","SPAM_SYMBOL2","SPAM_SYMBOL3","SPAM_SYMBOL5"];groups_enabled=["neural"]}
   Expect Symbol  NEURAL_HAM_SHORT
   #Expect Symbol  NEURAL_HAM_SHORT_PCA
   Do Not Expect Symbol  NEURAL_SPAM_SHORT
-  #Do Not Expect Symbol  NEURAL_SPAM_SHORT_PCA
+  #Do Not Expect Symbol  NEURAL_SPAM_SHORT_PCA
\ No newline at end of file
index ecee7f631417a415e8db6ea803417c2740be74d6..bbc6c2e17e3a82d228c13a19a98ec748b3f293e5 100644 (file)
@@ -15,50 +15,58 @@ ${RSPAMD_URL_TLD}  ${RSPAMD_TESTDIR}/../lua/unit/test_tld.dat
 
 *** Test Cases ***
 Collect training vectors & train manually
-  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL","SAVE_NN_ROW"]}
-  Expect Symbol  SPAM_SYMBOL
-  # Save neural inputs for later
-  ${SPAM_ROW} =  Get File  ${SCAN_RESULT}[symbols][SAVE_NN_ROW][options][0]
-  Remove File  ${SCAN_RESULT}[symbols][SAVE_NN_ROW][options][0]
-  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL","SAVE_NN_ROW"]}
-  Expect Symbol  HAM_SYMBOL
-  # Save neural inputs for later
-  ${HAM_ROW} =  Get File  ${SCAN_RESULT}[symbols][SAVE_NN_ROW][options][0]
-  Remove File  ${SCAN_RESULT}[symbols][SAVE_NN_ROW][options][0]
-  ${HAM_ROW} =  Run  ${RSPAMADM} lua -a ${HAM_ROW} ${RSPAMD_TESTDIR}/util/nn_unpack.lua
-  ${HAM_ROW} =  Evaluate  json.loads("${HAM_ROW}")
-  ${SPAM_ROW} =  Run  ${RSPAMADM} lua -a ${SPAM_ROW} ${RSPAMD_TESTDIR}/util/nn_unpack.lua
-  ${SPAM_ROW} =  Evaluate  json.loads("${SPAM_ROW}")
-  ${HAM_VEC} =  Evaluate  [${HAM_ROW}] * 10
-  ${SPAM_VEC} =  Evaluate  [${SPAM_ROW}] * 10
+  @{HAM_VEC}=    Create List
+  @{SPAM_VEC}=    Create List
+
+  FOR    ${INDEX}    IN RANGE    4    14
+    Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL1", "SPAM_SYMBOL2", "SPAM_SYMBOL3", "SPAM_SYMBOL${INDEX}", "SAVE_NN_ROW"]}
+    Expect Symbol  SPAM_SYMBOL${INDEX}
+    # Save neural inputs for later
+    ${SPAM_ROW} =  Get File  ${SCAN_RESULT}[symbols][SAVE_NN_ROW][options][0]
+    Remove File  ${SCAN_RESULT}[symbols][SAVE_NN_ROW][options][0]
+    Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL1", "HAM_SYMBOL2", "HAM_SYMBOL3", "HAM_SYMBOL${INDEX}", "SAVE_NN_ROW"]}
+    Expect Symbol  HAM_SYMBOL${INDEX}
+    # Save neural inputs for later
+    ${HAM_ROW} =  Get File  ${SCAN_RESULT}[symbols][SAVE_NN_ROW][options][0]
+    Remove File  ${SCAN_RESULT}[symbols][SAVE_NN_ROW][options][0]
+    ${HAM_ROW} =  Run  ${RSPAMADM} lua -a ${HAM_ROW} ${RSPAMD_TESTDIR}/util/nn_unpack.lua
+    ${HAM_ROW} =  Evaluate  json.loads("${HAM_ROW}")
+    ${SPAM_ROW} =  Run  ${RSPAMADM} lua -a ${SPAM_ROW} ${RSPAMD_TESTDIR}/util/nn_unpack.lua
+    ${SPAM_ROW} =  Evaluate  json.loads("${SPAM_ROW}")
+    Append To List    ${HAM_VEC}    ${HAM_ROW}
+    Append To List    ${SPAM_VEC}   ${SPAM_ROW}
+  END
+
   ${json1} =  Evaluate  json.dumps({"spam_vec": ${SPAM_VEC}, "ham_vec": ${HAM_VEC}, "rule": "SHORT"})
+  HTTP  POST  ${RSPAMD_LOCAL_ADDR}  ${RSPAMD_PORT_CONTROLLER}  /plugins/neural/learn  ${json1}
+
   # Save variables for use in inverse training
   Set Suite Variable  ${HAM_VEC}
   Set Suite Variable  ${SPAM_VEC}
-  HTTP  POST  ${RSPAMD_LOCAL_ADDR}  ${RSPAMD_PORT_CONTROLLER}  /plugins/neural/learn  ${json1}
-  Sleep  2s  Wait for neural to be loaded
+
+  Sleep  5s  Wait for neural to be loaded
 
 Check Neural HAM
-  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
+  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL1","HAM_SYMBOL2","HAM_SYMBOL3","HAM_SYMBOL8"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
   Do Not Expect Symbol  NEURAL_SPAM_SHORT
   Expect Symbol  NEURAL_HAM_SHORT
 
 Check Neural SPAM
-  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
+  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL1","SPAM_SYMBOL2","SPAM_SYMBOL3","SPAM_SYMBOL8"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
   Do Not Expect Symbol  NEURAL_HAM_SHORT
   Expect Symbol  NEURAL_SPAM_SHORT
 
 Train inverse
   ${json2} =  Evaluate  json.dumps({"spam_vec": ${HAM_VEC}, "ham_vec": ${SPAM_VEC}, "rule": "SHORT"})
   HTTP  POST  ${RSPAMD_LOCAL_ADDR}  ${RSPAMD_PORT_CONTROLLER}  /plugins/neural/learn  ${json2}
-  Sleep  2s  Wait for neural to be loaded
+  Sleep  5s  Wait for neural to be loaded
 
 Check Neural HAM - inverse
-  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
+  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["HAM_SYMBOL1","HAM_SYMBOL2","HAM_SYMBOL3","HAM_SYMBOL8"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
   Do Not Expect Symbol  NEURAL_HAM_SHORT
   Expect Symbol  NEURAL_SPAM_SHORT
 
 Check Neural SPAM - inverse
-  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
+  Scan File  ${MESSAGE}  Settings={symbols_enabled = ["SPAM_SYMBOL1","SPAM_SYMBOL2","SPAM_SYMBOL3","SPAM_SYMBOL8"];groups_enabled=["neural"];symbols_disabled = ["NEURAL_LEARN"]}
   Do Not Expect Symbol  NEURAL_SPAM_SHORT
-  Expect Symbol  NEURAL_HAM_SHORT
+  Expect Symbol  NEURAL_HAM_SHORT
\ No newline at end of file
index 32419c8c86a39c0ba7417c545bbeed063188d41c..fb599f3b0bf9e482de69ae64bbb78cde5f437453 100644 (file)
@@ -1,6 +1,6 @@
 local logger = require "rspamd_logger"
 
-for i = 1,10 do
+for i = 1,14 do
   rspamd_config:register_symbol({
     name = 'SPAM_SYMBOL'..tostring(i),
     score = 5.0,