GROOVY: Finding a string from console output if a regexp string found - jenkins

I have the following console output from a jenkins build:
23:21:16 [ ERROR ] - Problem occured while installing the chart AbCdEfG , aborting!
if the line appears, i want to be able to get only AbCdEfG and put it in error message, or in a varaiable.
i tried something like:
if (manager.logContains('.*\${error_string}')) {
error("Build failed because of ${AbCdEfG}")
regexp string that seems to be working, but stuck
[\n\r]*Problem occured while installing the chart:*([^\n\r]*)

Have you tested example 4 from plugin documentation? That example is using Java, not Groovy.
Anyway, the method getLogMatcher returns a Matcher, not a String.
Calling logContains will evaluate the log twice.
Using Groovy, (not tested inside Jenkins, only locally) you can adapt this snippet:
​ def matcher = ("23:21:16 [ ERROR ] - Problem occured while installing the chart AbCdEfG , aborting!" =~ /Problem occured while installing the chart\s?([^\n\r]*)\s?, aborting!/)
/* One item from array is the group, first item from this group is the full match and the second is group match */
if (matcher.hasGroup()) {
def msg = matcher[0][1]
println("Build failed because of ${msg}")
}

Related

Windows, Ec2ServerCreate - Cannot set JSON attributes when running knife from Rails application

I am able to launch new instance at AWS from Ruby on Rails application (Chef::Knife::Ec2ServerCreate.new()). Works fine till I try to set JSON attributes. When I set them from command line and invoke knife.bat, it works.
Looking at ec2_server_create shows that command line option --json-attributes is mapped to symbol :json_attributes. I tried to set it using following code:
Chef::Config[:knife][:json_attributes] = "{'NodeName' : 'Node001'}"
and I get error:TypeError (no implicit conversion of Symbol into Integer):
As soon as I comment this single line, instance is created, registered with chef server and recipe is running.
Any suggestion how to set json attributes of first chef-client run?
PS
Sample code shows constant value for attribute, but actual value would be dynamically created.
Error message and code line where error occurs:
/chef/knife/core/bootstrap_context.rb:188:in `[]=': no implicit conversion of Symbol into Integer (TypeError)
Looking into source you can find:
def first_boot
(#config[:first_boot_attributes] || {}).tap do |attributes|
if #config[:policy_name] && #config[:policy_group]
attributes[:policy_name] = #config[:policy_name]
attributes[:policy_group] = #config[:policy_group]
else
attributes[:run_list] = #run_list #THIS LINE CAUSES EXCEPTION
end
attributes.merge!(:tags => #config[:tags]) if #config[:tags] && !#config[:tags].empty?
end
end
I set run list.
The issue is that json_attributes needs to be a hash, not a string. It isn't the #run_list that is failing, it is "{'NodeName' : 'Node001'}"[:run_list] which when you see in on place is probably a bit clearer.

Nokogiri syntax errors translation

I have custom validation for html_document column in database:
def html_format
bad_doc = Nokogiri::HTML(html_document) { |config| config.strict }
bad_doc.errors.each do |e|
errors.add(:html_document, [e.message,e.line].join(' at line: '))
end if bad_doc.errors.present?
end
I would like to make translation of returned errors for several languages. Error which is returned from Nokogiri looks like this:
Unexpected end tag : p
I have figured out by browsing Nokogiri documentation that I can check what is the number of returned error:
[1] pry(#<Model>)> errors.first.code
=> 76
I have an idea to do translation by given code numbers.
The question is, where can I find full table of errors codes and messages?
you need to have a look at the libxml2 source code.
this file holds all error-codes: https://github.com/tenderlove/libxml2/blob/ecb5d5afdc8acceba608524f6e98c361fd2ce0e9/include/libxml/xmlerror.h#L174

How to show mbunit/gallio TestLog or Console output in Jenkins?

I'm using Gallio/MbUnit framework for my web testing, and the tests are kicked off from Jenkins. I've installed the Gallio/MbUnit plugin and it's publishing the xml report. I'm trying to find a way to display test log or console messages in the "Test Result" section so the team can easily read failures instead of digging into the "Console Output" for any failed test run.
When I run these Gallio/MbUnit tests from my local machine using Icarus everything is pretty nicely formatted, but not so much with Jenkins. I'd like to keep using it and improve how we display the errors. Suggestions?
For a failed test:
Failed
...MainMenuTests.AcctClaimsItems
Failing for the past 4 builds (Since Failed#128 )
Took 47 sec.
add description
Error Message
Expected value to be false.Actual Value : d:\Jenkins\jobs\...\workspace\WebTesting\Base\Helpers.cs:line 90d:\Jenkins\jobs\...\workspace\WebTesting\TigerEye\Tests\MainMenuTests.cs:line 329true
Stacktrace
at WebTesting.Base.Helpers.Click(IWebDriver driver, IWebElement element) in
From the raw console output:
Start time: 4:21 PM
Initializing the runtime and loading plugins.
Verifying test files.
Initializing the test runner.
Running the tests.
[failed] Test WebTesting/MainMenuTests/AcctClaimsItems
Expected value to be false.
Found System.Web Exception after click to url
.../Accounting/FETReport.aspx
Actual Value : true
at WebTesting.Base.Click(IWebDriver driver, IWebElement element) in d:\Jenkins\jobs\...\workspace\WebTesting\Base\StaticHelpers.cs:line 90 at WebTesting...\Tests.MainMenuTests.AcctClaimsItems() in d:\Jenkins\jobs\...\workspace\WebTesting\TigerEye\Tests\MainMenuTests.cs:line 329
Code:
if (driver.PageSource.Contains("System.Web - Exception"))
{
TestLog.Write("Found exception on page {0}", driver.Url);
TestLog.Write(driver.PageSource.ToString());
Console.Write("Found exception on page {0}", driver.Url);
Console.Write(driver.PageSource.ToString());
Assert.IsFalse(driver.PageSource.Contains("System.Web - Exception"), "Found System.Web Exception after click to url {0}",driver.Url);
}
There is a general console parsing plugin you can use to add a post build step https://wiki.jenkins-ci.org/display/JENKINS/Log+Parser+Plugin

Read the content of a file and display with line numbers on the view

I am trying to write code to read a Ruby script entered by the user and stored it in a temp file, then pass that temp file to jruby -c temp_file to do syntax validation.
If any errors are found, I have to show the errors, along with the script entered by the user with line numbers.
How would I do this?
If there is a syntax error you see the failing line number:
blah.rb:2: syntax error, unexpected '.'
So just split your script on newline (\n) and thats your error line (1 index based of course).
If I understand correctly what you are trying to achieve, you can use jruby-parser (install gem install jruby-parser) with JRuby to find line numbers where errors occur:
require 'jruby-parser'
# Read script file into a String
script = File.open('hello.rb', 'r').read
begin
JRubyParser.parse(script)
rescue Exception => e
# Display line number
puts e.position.start_line
# Display error message
puts e.message
end

Double closure fails in GSP

In a GSP file I write something like this:
${tgs.singleGameSheets.find{it.matchnumber==1}.awayPlayer.fullname()}
But I receive the following error:
org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed:
C__STS_Projekte_TischtennisManager_grails_app_views_league__showGameSheet_gsp:
49:expecting '}', found ')' # line 49, column 134.
heets.find{it.matchnumber==1 })
The problem seems to be the double closure as I've found a bug report here.
Unfortunately the solution from the bugreport with the %= and % at the beginning and the end of the tag is not working for me.
Are there any other workarounds or solutions for this double closure problem?
I'm using Grails 1.3.7.
You may have to split this up in to two lines.
Try assigning the find results to a separate var first
<% def r = tgs.singleGameSheets.find{it.matchnumber==1} %>
${r*.awayPlayer.fullname()}
I would recommend firstly to do this sort of data processing in the controller and hand data that is as well prepared as possible down to the view.
If you are unable to do that, I would recommend trying to use parenthesis:
${tgs.singleGameSheets.find{it.matchnumber==1}.awayPlayer.fullname()}
becomes
${(tgs.singleGameSheets.find{it.matchnumber==1}.awayPlayer.fullname())}
That has worked for me on past occasions where I had to do ${(someCollection.findAll { someClause })}

Resources