我有一些日志需要分析以檢查日志是否沒有例外并且可以說是不正確的形式。
我為它生成了一個 CSV 檔案:
"timestamp","source","message
"2021-10-18T09:12:29.000Z","Storage","Storage apache: [18/Oct/2021:09:12:29 0800] 10.102.141.82 - GET /deviceManager/rest/
"2021-10-18T09:12:29.000Z","Storage","Storage apache: [18/Oct/2021:09:12:29 0800] 10.102.141.82 - GET /deviceManager/rest/
"2021-10-18T09:12:29.000Z","Storage","Storage apache: [18/Oct/2021:09:12:29 0800] 10.102.141.82 - GET /deviceManager/rest/
"2021-10-18T09:12:29.000Z","Storage","Storage apache: [18/Oct/2021:09:12:29 0800] 10.102.141.82 - GET /deviceManager/rest/
我使用 CSV gem 來決議/讀取這個檔案,并使用 RSpec 測驗來期望一些值/文本/時間格式等。我已經撰寫了下面的代碼。例如,它需要從 8 到 12 的行,我希望在這些行中有一個名為“Huawei”fe 的文本。
RSpec.describe "Log parsing" do
it 'returns the source' do
table = CSV.read("Messages_result.csv")
puts arr = table.values_at(8..12)
arr.each do |rows|
expect(rows).to include('Huawei')
end
end
end
我遇到的問題是它總是執行第一行的期望,但我想決議/迭代整個 CSV 檔案,并且還應該為每一行顯示結果。我的期望訊息當然會改變,但我只想先檢查像華為這樣的基本文本。有人可以請說明我做錯了什么,因為理論上每個做都應該遍歷完整的行并為每個做一個期望嗎?
uj5u.com熱心網友回復:
你可能想做這樣的事情?
RSpec.describe "Log parsing" do
it 'returns the source' do
CSV.foreach("Messages_result.csv", :headers => true) do |row|
expect(row.to_h.values).to include("Storage")
end
end
end
更新
我想決議/迭代整個 csv 檔案,并且還應該為每一行顯示一個結果。
你想使用 Rspec 作為記錄器嗎?這是不可能的,請參閱添加配置選項以在失敗時繼續。
也就是說,通過稍微修改代碼,您可以讓 Rspec 檢查整個檔案并顯示所有失敗的行expect:
RSpec.describe "Log parsing" do
CSV.foreach("Messages_result.csv", :headers => true) do |row|
it 'returns the source' do
expect(row.to_h.values).to include("Huawei")
end
end
end
輸出:
FF
Failures:
1) Log parsing returns the source
Failure/Error: expect(row.to_h.values).to include("Huawei")
expected ["2021-10-18T09:10:29.000Z", "Storage", "Storage apache: [18/Oct/2021:09:10:29 0800] 10.102.141.82 - GET /deviceManager/rest/"] to include "Huawei"
# ./Messages_result.rb:10:in `block (3 levels) in <main>'
2) Log parsing returns the source
Failure/Error: expect(row.to_h.values).to include("Huawei")
expected ["2021-10-18T09:11:24.000Z", "Storage", "Storage apache: [18/Oct/2021:09:11:24 0800] 10.102.141.82 -...../license/feature HTTP/1.1 python-requests/2.21.0 - - application/json - / gzip, deflate 200 49 0"] to include "Huawei"
# ./Messages_result.rb:10:in `block (3 levels) in <main>'
...
如果您確實需要為 CSV 的每一行輸出一條訊息,那么您除了自己列印之外別無選擇。例如:
# get the output stream that Rspec is currently using
ostream = RSpec.configure { |c| c.output_stream }
# define a few colorization helpers
if ostream.tty?
def red str; "\e[31m#{str}\e[0m"; end
def green str; "\e[32m#{str}\e[0m"; end
else
def red str; str; end
def green str; str; end
end
RSpec.describe "Log parsing" do
it 'returns the source' do
ostream.puts
ostream.puts " -) Log parsing returns the source - details"
expected = "Huawei"
success = true
CSV.foreach("Messages_result.csv", :headers => true) do |row|
values = row.to_h.values
detail = "expected #{values} to include #{expected.inspect}"
ostream.print ' ' * 5
if values.include?(expected)
ostream.puts green("PASSED: #{detail}")
else
ostream.puts red("FAILED: #{detail}")
success = false
end
end
ostream.puts
ostream.flush
expect(success).to be(true)
end
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/336764.html
上一篇:有沒有辦法專門回圈多次
