I am new to Chef Inspec, I want to test Reboot Required for (Windows Only) using Inspec code. Below is my code:
# Reboot Required Yes for windows
control 'PowerShellTest' do
impact 0.1
title 'Check powershell code inside inspec'
desc 'TestCase for Powershell'
if os.family == 'windows'
describe command(shutdown /r) do
its('action') { should be 'yes' }
its('stderr') { should eq '' }
its('exit_status') { should eq 0 }
end
end
Related
Here is the LDAP command that I want to run, which works perfectly:
/opt/openldap/setup —cli —no-prompt —rootUserDN "cn=Directory Manager" — rootUserPasswpord secretpass —hostname localhost —ldapPort 389 —adminConnectorPort 4444 —baseDN "dc=example,dc=com" —addBaseEntry —doNotStart —acceptLicense'
Here is the content of my chef attribute file (server.rb)
default["openldap"]["server"]["ROOTDN"] = "cn=Directory Manager"
default["openldap"]["server"]["ROOTPASS"] = "secret pass"
default["openldap"]["server"]["LDAPPORT"] = "389"
default["openldap"]["server"]["ADMINPORT"] = "4444"
default["openldap"]["server"]["BASEDN"] = "dc=example,dc=com"
default["openldap"]["server"]["DJDEST"] = "/opt/openldap"
Here is the content of my chef recipe file (default.rb)
execute 'Ldap: Installing Openldap Instance' do
command node["openldap"]["server"]["DJDEST"]'/setup --cli --no-prompt--rootUserDN "["openldap"]["server"]["ROOTDN"] " --rootUserPassword ["openldap"] ["server"]["ROOTPASS"]--hostname ["openldap"]["server"]["ODJFQDN"] --ldapPort ["openldap"]["server"]["LDAPPORT"] --adminConnectorPort ["openldap"]["server"]["ADMINPORT"] --baseDN "["openldap"]["server"]["BASEDN"]" --addBaseEntry --doNotStart --acceptLicense'
action :run
end
I want to run the execute resource using variables instead of manually hardcoding the ldap script in the code block. Can anyone please advice me on what i am doing wrong?
Assuming that I understand your question right, you want to generate a string from few variables and strings. Then your question is similar to this one.
You could either combine the content of a variable a = "foo" and the string "bar" using:
a = "foo"
b = a + "bar"
Or you can use string interpolation (probably more common):
a = "foo"
b = "#{a}bar"
The result in both cases is "foobar".
Translated to your chef context, the resulting execute resource would be:
execute 'Ldap: Installing Openldap Instance' do
command "#{node['openldap']['server']['DJDEST']}/setup --cli --no-prompt--rootUserDN "#{node['openldap']['server']['ROOTDN']}" --rootUserPassword #{node['openldap']['server']['ROOTPASS']} --hostname #{node['openldap']['server']['ODJFQDN']} --ldapPort #{node['openldap']['server']['LDAPPORT']} --adminConnectorPort #{node['openldap']['server']['ADMINPORT']} --baseDN "#{node['openldap']['server']['BASEDN']}" --addBaseEntry --doNotStart --acceptLicense"
action :run
end
If it makes it easier for you, you can also extract this into a variable:
installLdapCommand = "#{node['openldap']['server']['DJDEST']}/setup --cli --no-prompt--rootUserDN #{node['openldap']['server']['ROOTDN']} --rootUserPassword #{node['openldap']['server']['ROOTPASS']} --hostname #{node['openldap']['server']['ODJFQDN']} --ldapPort #{node['openldap']['server']['LDAPPORT']} --adminConnectorPort #{node['openldap']['server']['ADMINPORT']} --baseDN "#{node['openldap']['server']['BASEDN']}" --addBaseEntry --doNotStart --acceptLicense"
execute 'Ldap: Installing Openldap Instance' do
command installLdapCommand
action :run
end
Instead of
'["openldap"]["server"]["ROOTDN"] --option'
try using ruby's string interpolation
"#{node['openldap']['server"]['ROOTDN']} --option"
Like so:
execute 'Ldap: Installing Openldap Instance' do
command "#{node['openldap']['server']['DJDEST']}/setup --cli --no-prompt--rootUserDN #{node['openldap']['server']['ROOTDN']} --rootUserPassword #{node['openldap']['server']['ROOTPASS']} --hostname #{node['openldap']['server']['ODJFQDN']} --ldapPort #{node['openldap']['server']['LDAPPORT']} --adminConnectorPort #{node['openldap']['server']['ADMINPORT']} --baseDN #{node['openldap']['server']['BASEDN']} --addBaseEntry --doNotStart --acceptLicense"
action :run
end
I am using encrypted data bags within Chef and I want to add a condition within my Chef recipe as follows:
If (test kitchen) then
encryptkey = data_bag_item("tokens", "encryptkey")
If ( not test kitchen ) then
secret = Chef::EncryptedDataBagItem.load_secret("/etc/chef/encrypted_data_bag_secret")
encryptkey = Chef::EncryptedDataBagItem.load("tokens", "encryptkey", secret)
I have added data_bags_path and encrypted_data_bag_secret_key_path within kitchen.yml as follows:
provisioner:
name: chef_zero
chef_omnibus_url: omni-url/chef/install.sh
roles_path: 'test/integration/default/roles'
data_bags_path: "test/integration/default/data_bags"
encrypted_data_bag_secret_key_path: "test/integration/default/encrypted_data_bag_secret"
Use the attributes in your kitchen.yaml.
suites:
- name: default
data_bags_path: 'databags'
run_list:
- recipe[x::y]
attributes: {'kitchen' : 'true' }
Inside your recipe put if condition using the value of node['chef-mode'].
if node['kitchen'] == 'true'
#something
else
#else
end
Just use data_bag_item("tokens", "encryptkey") for both. It will take care of decryption for you automatically.
I'm trying to pass attributes to my recipe at run-time of chef client.
following is my default.rb attribute file:
if node['os'] == 'windows'
if node.attribute?('temp')
default['param']['name'] = node['temp']['tempname']
else node.attribute?('base')
default['param']['name'] = node['base']['nodename']
end
end
I first ran the chef-client with only the base attribute defined in the node.json file. My node.json looked like :
{
"base": {
"nodename": "John"
}
}
Chef-client run was successfull and the attribute was set accordingly. i.e.
default['param']['name'] = "John"
Than I ran chef-client with both base and temp attribute defined in the node.json.
And here is my node.json file :
{
"base": {
"nodename": "John"
},
"temp": {
"tempname": "Mike"
}
}
Chef-client again ran successfully and the attribute was set accordingly.
default['param']['name'] = "Mike"
The problem is when I run the chef-client again and pass only base attribute from the node.json file(see below for the file), the code doesn't seem to enter the else loop. It just runs the `if' loop with old value for temp attribute(Mike).
{
"base": {
"nodename": "John-2"
}
}
Any ideas to where I'm going wrong.
The second time you ran chef-client, it saved the node[:temp][:nodename] attribute to the node object on the Chef server.
When you ran chef-client again, it loaded the node's attributes back from the server before reaching the attributes/default.rb file, so node[:temp][:nodename] was set when it reached the conditions.
I'm not sure what you're trying to achieve, but it would probably be best to explicitly tell which attribute you want to use through another attribute (and maybe assume a default for it), such as:
default[:name_attribute] = 'temp'
then,
if node['os'] == 'windows'
if node['name_attribute'] == 'temp'
default['param']['name'] = node['temp']['tempname']
else
default['param']['name'] = node['base']['nodename']
end
end
I am using gradle to download the selenium chrome driver from maven
webtestsCompile 'org.seleniumhq.selenium:selenium-chrome-driver:2.32.0'
I am trying to use this directly and I see that I get this error :
Caused by:
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list
I have looked up a couple of questions from stack-overflow and other places it requires me to set the value of the property webdriver.chrome.driver to the location where I have downloaded with some-thing like this :
System.setProperty("webdriver.chrome.driver", "path to chrome-driver");
I am wondering what is the best way to go about this ?
EDIT 1:
I verified the java.class.path a snippet of this path looks like this :
/home/bhavya/.gradle/caches/artifacts-26/filestore/org.seleniumhq.selenium/selenium-chrome-driver/2.32.0/jar/14a4e8e32a4129c682c67381f5d7bf11f2327e1/selenium-chrome-driver-2.32.0.jar
This looks like the selenium-chrome-driver is present in the java.class.path.
Edit 2 :
I would want the chrome driver to work irrespective of the operating system that I am using, currently I am on a ubuntu box but a lot of this will get tested on a windows box. When I hard coded the value of the webdriver.chrome.driver to the value in EDIT 1, I am facing the following issue :
java.lang.IllegalStateException: The driver is not executable: /home/bhavya/.gradle/caches/artifacts-26/filestore/org.seleniumhq.selenium/selenium-chrome-driver/2.32.0/jar/14a4e8e32a4129c682c67381f5d7bf11f2327e1/selenium-chrome-driver-2.32.0.jar
Edit 3 :
Task within which I am running the test suite --
task webs(type: Test, dependsOn: updateNodeModules) {
testClassesDir = sourceSets.webtests.output.classesDir
classpath = sourceSets.webtests.runtimeClasspath
def javaHomeBin = new File(System.getProperty("java.home"), "bin");
def javaExec = new File(javaHomeBin, "java").getAbsolutePath();
systemProperties['jar.path'] = jar.archivePath
if(project.hasProperty('url')){
println" url passed as variable is $url"
systemProperties["selenium.webdriver.url"] = "$url"
}
systemProperties["selenium.webbrowser.type"] = "firefox"
if(project.hasProperty('browser')){
println "the browser passed is $browser"
systemProperties["selenium.webbrowser.type"] = "$browser"
}
include '**/UserEditControllerWebTest.class'
doFirst {
println " iterator is $it"
def chrome=configurations.testRuntime.find {
it.name.contains("selenium-chrome-driver")
}.path
println " chrome driver path is $chrome"
systemProperties["webdriver.chrome.driver"]= "$chrome"
}
}
Assuming that you need to set this system property for your tests, you can do something like:
test.doFirst {
systemProperty "webdriver.chrome.driver",
classpath.find {
it.name.contains("selenium-chrome-driver")
}.path
}
I tried this, but it doesn't seem to work
subtest 'catalyst scripts that should be executable' => sub {
plan({ skip_all => 'skip failing executable tests on windows' }) if $^O eq 'MSWin32';
my $should_exec = [ #{ $dzpcs->scripts } ];
foreach ( #{ $should_exec } ) {
ok ( -x $_ , "$_" . ' is executable' );
}
};
Here's what I got in my cpants report.
plan() doesn't understand HASH(0x286f4cc) at t/02-MintingProfileCatalyst.t line 46.
# Child (catalyst scripts that should be executable) exited without calling finalize()
# Failed test 'catalyst scripts that should be executable'
# at C:/strawberry/perl/lib/Test/Builder.pm line 252.
# Tests were run but no plan was declared and done_testing() was not seen.
So I guess it's not a hash, not really sure what it is then... what's the cleanest way to make this work? (p.s. I can't test win32, I only have my Linux box)
plan takes two parameters, not a hashref:
plan( skip_all => 'skip failing executable tests on windows' ) if $^O eq 'MSWin32';
Not everything uses Moose. ;-)
Note: for testing purposes, you could change eq to ne, so it will skip the tests on your Linux box. Just remember to change it back afterwards.