Decision in Inspec profiles - postgresql

I'm running a postgres inspec profile and would like to run certain tests only if the node is a master node. Here is my profile
sql = postgres_session('postgres', password, 'localhost')
result = describe sql.query('SELECT pg_is_in_recovery()') do
its('output') { should eq 'f' }
end
if result == 'f'
describe sql.query('SELECT state from pg_stat_replication') do
its('output') { should match 'streaming' }
end
end
But this doesn't work as the variable result doesn't store value 'f'.
My question is how do i store a value in a variable and use that for the next test in inspec ?
How do we print variable values in inspec (debug statements)

speaking only from my memory, you should assign the sql query into a variable, pass that variable to the describe block so you could use the matcher (but it feels that you do not need it in your case), and then place a condition on that variable. it should be something like:
sql = postgres_session('postgres', password, 'localhost')
result = sql.query('SELECT pg_is_in_recovery()')
if result.output == 'f'
describe sql.query('SELECT state from pg_stat_replication') do
its('output') { should match 'streaming' }
end
end

Related

Perl `defined`function complexity

I have a perl script, which is accepting a list of arguments(mandatory and non-mandatory). Based on these args flow of script is determined. Where i am confused is, if the arg(which is in the form of switch) is used multiple times in the script to determine flow(most of the time used in if loop), then which one is better
if(defined arg){}
OR
my $switch = defined arg ? 1:0; if($switch){}
defined already returns true or false. Using the result of defined to select a true value for true case and a false value for the false case is just extra work. This does all the work you need and no extra work:
if( defined $foo ) { ... }
Sometimes I want to print that true or false value, so I'll use the double bang (negate, and negate again) to normalize the value:
!! defined $foo

Preventing Function Overriding in Lua Table

In my program when two functions with the same name are defined for the same table, I want my program to give an error. What's happening is that it's simply just calling the last function and executing it.
Here's a sample code
Class{'Cat'}
function Cat:meow( )
print("Meow!")
end
function Cat:meow()
print("Mmm")
end
kitty = Cat:create()
kitty:meow()
The result of the execution is only: "Mmm"
Instead I want something like an error message to be given.
Unfortunately, __newindex does not intercept assignments to fields which already exist. So the only way to do this is to keep Cat empty and store all its contents in a proxy table.
I don't know the nature of your OOP library, so you'll have to incorporate this example on your own:
local Cat_mt = {}
-- Hide the proxy table in closures.
do
local proxy = {}
function Cat_mt:__index(key)
return proxy[key]
end
function Cat_mt:__newindex(key, value)
if proxy[key] ~= nil then
error("Don't change that!")
end
proxy[key] = value
end
end
Cat = setmetatable({}, Cat_mt)

MATLAB assign variable name

I have a variable called 'int' with alot of data in it. I would like to find a way to programically rename this variable with a user input. So I can query the user indentifcation information about the data, say the response is 'AA1', I want either rename the variable 'int' to 'AA1' or make 'AA1' a variable that is identical to int.
A problem using the input command arises because it allows the user to assign a value to an already created varialbe, instead of actually creating a variable name. Would using the eval function, or a variation of it, help me achieve this? Or is there an easier way?
Thanks!
Just for the record, int is a rather poor variable name choice.
That aside, you can do what you want as follows
say foo is the variable that holds a string that the user input. You can do the following:
% eliminate leading/trailing whitespace
foo = strtrim(foo);
a = regexp('[a-zA-Z][a-zA-Z0-9_]*',foo));
if numel(a) == 0
fprintf('Sorry, %s is not a valid variable name in matlab\n', foo);
elseif a ~= 1
fprintf('Sorry, %s is not a valid variable name in matlab\n', foo);
elseif 2 == exist(foo,'var')
fprintf('Sorry, %s already in use as a variable name.');
else
eval([foo,' = int']);
end
Assuming int (and now foo) is a structure with field named bar, you can read bar as follows:
barVal = eval([foo,'.bar']);
This is all somewhat clunky.
An alternative approach, that is far less clunky is to use an associative array, and let the user store various values of int in the array. The Matlab approach for associative arrays is Maps. That would be my preferred approach to this problem. Here is an example using the same variables as above.
nameValueMap = containers.Map;
nameValueMap(foo) = int;
The above creates the association between the name stored in foo with the data in the variable int.
To get at the data, you just do the following:
intValue = nameValueMap(foo);

convert string to logical expression

I use configuration file to load logical expression which will later be replace with 0 or 1 depend on the success or failure
configuration
expression=(server.ip.1&&server.ip.2)||(server.ip.3&&server.ip.4)
Later server.ip.1 will replace with 1 or 0 depend on server availability. Later part of the logical expression evaluation has below piece of code.
my $reg_expression= configurator->get_reg_expression() ;
...
$reg_expression =~ s/$values[0]/$ip_status/g;
if ($reg_expression){
$logger->debug("no failover");
}else{
$logger->debug("falling to failover mode");
}
Issue is if condition always true what ever values assign to the expression. Issue seems to be it is taken as string so it always end up in true. Any other way that I can do the same or how can transfer above variable back to state where I can successfully use inside if condition.
$reg_expression = (0&&0)||(1&&0); # diffe
$reg_expression = '(0&&0)||(1&&0)';
You can use eval to evaluate the expression:
my $reg_expression = '(0&&0)||(1&&0)';
print eval $reg_expression, "\n";

What is it called when an assignment contains conditionals that fall through to be assigned as the value of the variable?

I know that this is simple terminology, but I can't get it via google searching... what is it called when the value of a variable being assigned passes through?
An example in php:
<?php
if($bob = 5){ echo 'The assignment came through as a truthy value!, bob now equals '.$bob.'!'; }
if($bob = false){ echo 'The assignment occurred again, but the value of the assignment is the value "false", so this if block will not be executed!. Bob now equals '.$bob.'!'; }
echo ' Finally, bob is a: '.(string) $bob;
?>
An example in javascript:
bob = bob || {};
In your first example, you're using assignment as an expression; that is, the assignment statement returns the value assigned (this behavior is a common source of bugs; often people accidentally use = instead of == in their condition).
The second example is using the short-circuiting behavior of the || operator.