How can set dynamic attribute value in .kitchen.yml - test-kitchen

I am using kitchen with ec2 driver. I would like to add Name tag to ec2 instances based on the instance name kitchen creates. If I had a 'default' suite and was using centos7.2, kitchen list would name the instance 'default-centos-72'.
I could hard code something like this:
suites:
- name: default
driver_config:
tags: { "Name": "kitchen-default-centos-72" }
But what I'd really like is something like this:
suites:
- name: default
driver_config:
tags: { "Name": <%= figure out instance name and prepend kitchen- %> }
My example suggests using ERB which seems like the way to go to me. But I can't seem to figure out what code to use to get the name of the instance. I tried using a bit of Kitchen::Config.new... but couldn't figure out something that worked. Any suggestions would be much appreciated.

Took me a while but I finally ran across an example that may have showed me the light. While looking through the InSpec options for kitchen I found you can have it output a results file with the platform and suite name that was used during the test run. The below syntax in your platforms: block nested under the driver: option should work. I haven't tested this by examining the instance during a run but hopefully I can find some time to do that soon. If it doesn't work let me know and we can tweak it until it does.
platforms:
- name: ubuntu
driver:
tags:
Name: test-kitchen-%{platform}-%{suite}
How this should work is that the .kitchen.yml file gets run through an ERB pre-processor so the %{platform} resolves to an instance variable during the loop across the platforms and suites arrays.

As far as I can tell there seems to be no straightforward way to include instance properties in the kitchen YAML. I added the following snippet to my kitchen.yml to check what is available in the kitchen YAML's ERB namespace:
<%
puts "Instance vars: #{instance_variables}"
puts "Local vars: #{local_variables}"
puts "Global vars: #{global_variables}"
puts "Methods: #{methods}"
%>
The results when running kitchen create for a specific instance were disappointing, containing nothing that looks like instance specification data:
Instance vars: []
Local vars: [:_erbout, :spec, :bin_file]
Global vars: [:$-0, :$\, :$DEBUG, :$-W, :$0, :$-d, :$-p, :$PROGRAM_NAME, :$:, :$-I, :$LOAD_PATH, :$", :$LOADED_FEATURES, :$,, :$/, :$INPUT_LINE_NUMBER, :$-l, :$-a, :$INPUT_RECORD_SEPARATOR, :$ORS, :$OUTPUT_RECORD_SEPARATOR, :$PROCESS_ID, :$NR, :$#, :$!, :$DEFAULT_INPUT, :$PID, :$PREMATCH, :$CHILD_STATUS, :$LAST_MATCH_INFO, :$LAST_READ_LINE, :$DEFAULT_OUTPUT, :$MATCH, :$fileutils_rb_have_lchown, :$POSTMATCH, :$LAST_PAREN_MATCH, :$IGNORECASE, :$ARGV, :$fileutils_rb_have_lchmod, :$stdin, :$stdout, :$stderr, :$>, :$<, :$., :$FILENAME, :$-i, :$*, :$SAFE, :$thor_runner, :$_, :$~, :$;, :$-F, :$?, :$$, :$ERROR_INFO, :$&, :$`, :$', :$+, :$=, :$KCODE, :$-K, :$ERROR_POSITION, :$FS, :$FIELD_SEPARATOR, :$OFS, :$OUTPUT_FIELD_SEPARATOR, :$RS, :$VERBOSE, :$-v, :$-w]
Methods: [:inspect, :to_s, :to_yaml, :to_json, :instance_variable_defined?, :remove_instance_variable, :instance_of?, :kind_of?, :is_a?, :tap, :methods, :instance_variable_set, :protected_methods, :instance_variables, :instance_variable_get, :private_methods, :public_methods, :method, :define_singleton_method, :public_send, :singleton_method, :public_method, :extend, :to_enum, :enum_for, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :object_id, :send, :display, :class, :nil?, :hash, :dup, :singleton_class, :clone, :then, :itself, :yield_self, :untaint, :taint, :tainted?, :untrusted?, :trust, :frozen?, :untrust, :singleton_methods, :equal?, :!, :__id__, :==, :instance_exec, :!=, :instance_eval, :__send__]
The local variable spec looked hopeful at first, but turned out to be a GemSpec object.
All things considered, you will probably have to create a convention to always specify the instance in some external way. You could use for example an environment variable of your choice, which you could then access in the template as <%= ENV['<VARNAME>'] %> (where you replace <VARNAME> with the name of your environment variable). There are probably other ways of getting the information in there, but you will still have to specify it in more places than just the Test Kitchen command.

Related

How to specify test tags correctly in dart_test.yaml?

I have a dart project that has several tests, when I try to run an isolated test I get this warning:
Warning: A tag was used that wasn't specified in dart_test.yaml.
"tagName" was used in the suite itself
how should i declare these tags correctly in dart_test.yaml?
Steps
Create a file dart_test.yaml at the root of your project
Add your tags one after another under a tags field
Add tags to your test or testWidget declaration
Run your tests with the -t flag followed by the wanted tag
Sample
Let's say I want to add the following tags: golden, atom, molecule, organism, mobile, desktop. My dart_test.yaml will look like this:
tags:
golden:
atom:
molecule:
organism:
mobile:
desktop:
And everything should be okay you can write your test:
void main() {
testWidgets(
'this is a test',
(tester) async {
// ...
},
tags: ['atom', 'mobile'],
);
}
You can run it with the following command:
$ flutter test -t mobile
source

Use RegEx comparison in Gitlab workflow rules

I'm trying to use a regex comparisons in the workflow:rules section of my gitlab-ci file, but it doesn't seem to be working. Here is a basic version:
stages:
- prep
variables:
VAR1: "no value"
APPURL: "no value"
workflow: #Goal: only run pipeline for push events set some variables based on branch/commit_ref_name
rules:
- if: "CI_PIPELINE_SOURCE == "push"
- if: "CI_COMMIT_REF_NAME =~ /^dev/
variables:
VAR1: "Dev Value"
APPURL: "https://devurl.com"
test_job:
stage: prep
image: runner.image/url
script:
- echo "$VAR1"
- echo "APPURL"
When push a change from a branch named something like "dev1-jirastory", the test job output says "no value" for both variables. So, its not catching the commit_ref_name rule for some reason.
Can someone tell me if you can use regex comparisons in the workflow:rules statements? All the stuff I've found so far refers to job rules. As I want these variables set for multiple jobs, I want to set them for the entire workflow and subsequent jobs, not use the same rules in every single job, which can grow and not be managable.
I did try accomplishing those value determinations in a root "before_script" section, but that gets overwritten if I need to have to do other actions in a before_script for any individual job, so that won't work for me either.
Lastly, if anyone can tell me if I can do any "command" statements for parsing the commit_ref_name, that would be great. I'd love to do something like:
"$CI_COMMIT_REF_NAME" | awk -F "-" '{print $1}')
to pull out the "dev1" portion of the ref name like my sample above for use in jobs as well.
Thanks in advance.
Using variables in workflow works, just a couple of small changes needed for your pipeline:
stages:
- prep
variables:
VAR1: "no value"
APPURL: "no value"
workflow: #Goal: only run pipeline for push events set some variables based on branch/commit_ref_name
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
- if: '$CI_COMMIT_REF_SLUG =~ /^dev/'
variables:
VAR1: "Dev Value"
APPURL: "https://devurl.com"
test_job:
stage: prep
script:
- echo "$VAR1"
- echo "$APPURL"
You where missing the $ before the variables in you rules clauses. And I would recommend to use $CI_COMMIT_REF_SLUG instead of CI_COMMIT_REF_NAME to be independent of the case.

Azure Pipeline dynamic parameters to template file from YAML pipeline

I am currently working with Azure Devops Build Pipelines, and am trying to call a template file to do some tasks from my build yaml.
I am facing some difficulties to pass parameters to the template file. Let assume that this is my template file (simplified) which works fine :
parameters:
iterations: []
steps:
- ${{ each i in parameters.iterations }}:
- task: PowerShell#2
displayName: "Get key values ${{i}}"
name: getKeyValues_${{i}}
inputs:
targetType: 'inline'
script: |
$item = "${{i}}"
Write-Host "item : $($item)"
$keyVal = $item -split "_"
Write-Host $keyVal
Write-Host "key: $($keyVal[0]) | value: $($keyVal[1])"
echo "##vso[task.setvariable variable=key;isOutput=true]$($keyVal[0])"
echo "##vso[task.setvariable variable=value;isOutput=true]$($keyVal[1])"
So I want my iterations parameter contain something like this :
iterations: ["1_60", "2_40"]
Inside my Yaml pipeline I have the following code(also simplified) :
Not working scenario
- task: PowerShell#2
displayName: Calculate iterations for $(copies) copies
name: calculateIterations
inputs:
targetType: 'inline'
script: |
# Do some stuf here to get the arrow below from int value = 100
$iterations = ["1_60, "2_40"]
echo "##vso[task.setvariable variable=iterations;isOutput=true]$($iterations)"
- template: container-template.yml
parameters:
iterations: $(calculateIterations.iterations)
Working scenario
- task: PowerShell#2
displayName: Calculate iterations for $(copies) copies
name: calculateIterations
inputs:
targetType: 'inline'
script: |
# Do some stuf here to get the arrow below from int value = 100
$iterations = ["1_60, "2_40"]
echo "##vso[task.setvariable variable=iterations;isOutput=true]$($iterations)"
- template: container-template.yml
parameters:
iterations: ["1_60, "2_40"]
As you can see, the problem is that I am unable to use the output variable of my script to pass it as parameter to my template.
When I run the not working scenario I have the following error :
I have found this post, but no solutions yet...
As what 4c74356b41 said, this is the dilemma at present. In another word, the Not working scenario you mentioned does not support to achieve until now.
Now, we must let the template know the clear text at compile time. Because during this compile time, we have difficulty to do 2 or more things in one step at same time, especially contain compiling variable value, passing to corresponding template dynamic arguments and etc.
Expected a sequence or mapping. Actual value
'$(calculateIterations.iterations)'
More detailed, during compile time (after you click Run but before pipeline start truely):
1) Firstly we map the value that come from YAML pipeline, to make sure the - ${{ each i in parameters.iterations }} has clear value to start.
2) After it is done, then parse exact value on name: getKeyValues_${{i}} in script order.
In your scenario, it cannot even satisfy the first step since what you passed is a variable, and we do not has parse value process here. That's why you saw error said Expected a sequence or mapping. Actual value '$(calculateIterations.iterations)'.
Another expression of this error message is: we(template) are looking forward to exact value(s) to map our dynamic parameters, but what you give is a unrecognized content $(calculateIterations.iterations). Sorry, we cannot start to run.

Exact field input mongoose for nodejs

I've recently been attempting to pull my hair out getting stuck on what I would call a trivial issue.
The way mongoose in nodejs handles the specific field inputs. I have a specific issue where mongoose is not doing the same as mongodb is doing.
The issue is the following, if I use a "-" symbol in my field name, mongoose seems to run some weird operation on it, instead of accepting it as part of a string.
I've tried running several regex commands, some / escapes, however it should literally just take the input, as I know the specific data I'm looking for.
The code causing the issue is the following:
datapoints.find({type: "charging-type", device: device._id})
.exec(function(err, objects){
if(!objects){
log("Can't find objects");
}
});
Going straight into mongo shell and typing:
db.datapoints.count({type: "charging-type", device: device._id})
taking out the type makes everything work, changing the type to for example: shuntvoltage, current, ... all work perfectly.
The problem thus occurs with the usage of this - symbol.
What would be the way to enter this inputstring with a special character as an exact string?
Any help would be much appreciated.
Edit as per request; I don't get any error, I get objects==undefined (or !objects), schema is below.
var datapointSchema = mongoose.Schema({
type: { type: String, lowercase: true},
value: { type: Number},
timestamp: { type: Number},
device: { type: ObjectId, ref: "devices"}
});
module.exports = mongoose.model('datapoints', datapointSchema)
Manually updated the field in mongodb from charging-status to chargingstatus, as no useful answer was produced.
It's a workaround and should be considered unresolved.
Final code ended up looking as such:
var cursor = calcpoints.find({device: device._id}).where('type').equals('chargingstatus').sort({timestamp: 1}).cursor();

How do I use 'include' to apply rules to a capture when writing a Sublime Text syntax definition?

I'm trying to write a syntax definition for Gradle in Sublime Text 3. Many pieces of a Gradle build file are really just Groovy and so I'm trying to take advantage of the current Groovy highlighting support by using include. Thus far this is working fairly well, by I'm stuck on how to apply it to a particular piece.
Here is the Gradle snippet I am trying to highlight:
task copyTask (group: 'Install NGA - deploy', type: Copy, dependsOn: 'whoCares') {
from 'resources'
into 'target'
include('**/*.txt')
}
And this is the syntax I'm using to match that snippet:
- name: copy.task.source.gradle
begin: '\s*(task)\s+(\w+)\s*\((.*type: Copy.*)\)\s*{'
comment: 'Copy task definition'
beginCaptures:
'1': {name: keyword.task.source.gradle}
'2': {name: entity.name.function}
'3': {name: source.groovy}
end: '}'
contentName: copy.body.source.gradle
patterns:
- include: source.groovy
Most of this appears to work as intended. (Always hard to know with RegEx.) My problem is that the third capture. I want to apply all the rules contained in 'source.groovy' to the text between the parentheses and what I have above is not getting the job done.
To clarify: the text is "captured" and tagged as source.groovy, but that's not actually quite what I want. I don't want it simply tagged as source.groovy, I want the rules from source.groovy to be used when evaluating the text. The last line of my example successfully does this to the "content" section (text in between the braces) but simply putting include does not work.
'3': {include: source.groovy} # This gets an error.
If there is a syntax to apply include directly to a capture I can't find it, and I can't figure out another technique. Maybe something that has nested begin and end tags?
If I am understanding this correctly you would like the third capture group source.groovy to match the group: 'Install NGA - deploy', type: Copy, dependsOn: 'whoCares' part of your example.
In that case you would just need to alter you expression to capture more of the string like so:
begin: '\s*(task)\s+(\w+)\s*\((.*type: Copy.*?)\)\s*{'