Puppet 2D Iteration - multidimensional-array

I've got a two dimensional set of configuration variables:
$environments = [
{
'name' => 'foo',
'port' => '1234',
},
{
'name' => 'bar',
'port' => '4321',
},
]
Is it possible to iterate over the arrays and use the variables from the inner arrays. E.g. I want to create an user account for every name.
# How to get each name?
user { $environment:
ensure => 'present'
}

Puppet 4 provides built-in functions for iterating over aggregate values, and new, Ruby-like syntax to go with them. These are also available in recent enough versions of Puppet 3 when the future parser is enabled. If you are using such a Puppet, you could approach the problem like this:
each($environments) |$e| {
foo { $e['name']: port => $e['port'] }
}

I never really worked with iterations in puppet.
But to create multiple resources from a hash (note hash not array) you can use the create_resources() function.
The documentation has a good example.
Your hash can not contain parameters that the resource dose not understand tho. In your example port would not work with the user resource as it dose not understand that parameter.
Hope this helps a bit anyway.

Related

Symfony API-Platform serializer maxDepth does not work

The Symfony API-Platform app I am working with has many entities with relations with self and they are recursively requesting the relations and can then exceed the memory and crash.
I did find this question here on SO, but there's no conclusive solution.
Attempting to limit the depth of the recurrence, I did the follow the documentation, as follows:
/config/packages/framework
framework:
serializer:
default_context:
enable_max_depth: true
I am not sure if the above is being actually applied, as it seems to accept anything under default_context. But it does show correctly when I run php bin/console debug:config framework.
The documentation above states that enable_max_depth needs to be set to true, but it is unclear on where/how to change that.
/src/Entity/SectorHierarchy
use Symfony\Component\Serializer\Annotation\MaxDepth;
#[
ORM\ManyToOne(targetEntity: self::class),
Groups(['sectorHierarchy:post', 'sectorHierarchy:get', 'sectorHierarchy:patch']),
MaxDepth(1)
]
private ?self $parent = null;
The problem was where to set the enable_max_depth to true.
So the code above I placed on /config/packages/framework is not applicable.
Instead the following should be added to the normalization_context:
'get' => [
'normalization_context' => [
'groups' => ['sectorHierarchy:get'],
'enable_max_depth' => true,
]
]

Cypress - How can I run test files in order

When I press the "run all specs" button or use the run command that runs all files in Cypress it runs all test files alphabetically, so I don't want that.
I want to sort all of them with my own rules.
Let's say I have 3 steps in a chat app test.
Can connect the chat app
Can connect the chat
Can the user send a message
I want to test every step without being tied to each other.
What I mean, Testing one of their own function.
What I do is as follows
chat_app_connect.spec.js
describe('Server Connecting Test', () => {
it('Visit Server page', () => {
cy.visit('https://chat.page..');
});
it('Check welcome messages', () => {
cy.contains('Live Support');
cy.contains('Hello, Stranger');
});
it('Check URL and status of circle', () => {
// URL
cy.url()
.should('include', '/hello');
// Status Circle
cy.get('circle')
.should('have.class', 'positive');
});
});
chat_connect.spec.js
import './chat_app_connect.spec.js';
describe('Chat Connecting Test', () => {
it('Type customer name', () => {
cy.get('input')
.clear()
.type('E2E Test');
});
it('Click to the submit button', () => {
cy.get('.submit-button')
.click();
});
it('Check URL and status of circle', () => {
// URL
cy.url()
.should('equal', 'https://client.dev.octopus.chat/');
// Status Circle
cy.get('circle', { timeout: 5000 })
.should('have.class', 'positive');
});
});
chatting.spec.js
import './chat_connect.spec.js';
describe('Chatting Tests', () => {
it('Type a test message then press Enter and check the message if it sent', () => {
// Type
cy.get('#chat-message')
.clear()
.type('Hey I\'m a test message{enter}');
// Check the message
cy.get('.message-list')
.should('contain', 'Hey I\'m a test message');
});
});
as you see every test is tied to each other, and that is mean when I tried to test just catting functionality its call every test and the whole tests will be tested.
I don't know if it is the right way or not.
what should I do in this case or can it be an acceptable way
I have a particular case where I launch multiple instances of an app, rather than using fixtures or test data, I simply integrate user feedback as Cypress tests from login on forwards.
In any case, I used the specPattern config in cypress.json to set the spec file run order:
{
"baseUrl": "http://localhost:5000",
"specPattern": [
"login/*.js",
"leads/new-lead.spec.js",
"leads/leads-list.spec.js",
"leads/lead-detail.spec.js",
"leads/lead-modify.spec.js",
//...
]
}
No file numbering needed :D
The easiest solution is most likely to add a prefix to all your test files, such as:
01-chat_app_connect.spec.js
02-chat_connect.spec.js
etc.
Cypress is going to take those files in alphabetical order, which you can "trick" into your wanted behavior by using a number as a prefix.
Jean Lescure's answer was a lifesaver. We needed to run tests based on priority without having a bunch of duplicated tests or symlinks. The following worked for us in our default cypress config file:
"integrationFolder":"cypress/integration",
"testFiles": [
"high_priority_specs/**/*.js",
"medium_priority_specs/**/*.js",
"low_priority_specs/**/*.js"
]
To change the level of priority we used 3 configs files that were loaded using the cypress --configFile argument. To run the higher priority tests (smoke tests only) we used the following:
"integrationFolder":"cypress/integration",
"testFiles": [
"high_priority_specs/**/*.js"
]
Cypress does not intentionally let you do this, and for good reasons:
It's generally indicative of poor test design. Tests should not depend on the state of one another. Any test should be able to be run successfully in isolation from the rest of the test suite.
You'll never be able to take advantage of cypress' built in ability to run tests in parallel since you can't guarantee one spec will be ran after another
Here is a relevant discussion about this that gets into more detail: https://github.com/cypress-io/cypress/issues/390
However, if you decide to do this anyway, you can do it by prefixing the name of the specs with a number:
01-some-spec.js
02-alphabetically-first-spec.js
03-some-other-spec.js
In addition to #Brendan answer, if you have a nested folder structure, this approach will work as well.
01-folder-name
|
- 01-some-spec.js

Drupal 8, http request to server and append to the site

I have a Drupal 8 site and I need to make a http request to another server (for content) and append it into the page like footer. I can't do this after DOM is loaded because of SEO issues.
I'm familiar with WordPress and so easy to do it with WP. However, I'm confused about how to do this with .twig, Drupal 8. Any suggestions would be great. Thanks.
If you want the content to be part of the DOM when it is sent to the browser this is not something you want to do in Twig, you should have the content loaded earlier in the process.
You can create a module that defines custom block and place that block in the correct region of your theme.
The block plugin class requires you to write a build() method that returns a render array for your block. Within build() you can do whatever you need to acquire the content, including making an HTTP Request using Symfony's Guzzle client:
public function build() {
$url = 'https://www.example.com/remote/service';
$client = \Drupal::httpClient();
$request = $client->createRequest('GET', $url);
// Do whatever's needed to extract the data you need from the request...
$build = ['my_remote_block' => [
'#theme' => 'my_custom_theme_function',
'#attributes' => [
//An array of variables to pass to the theme
],
'#cache' => [
//Some appropriate cache settings
],
],
];
If you are getting HTML back from your request you could skip the custom theme function and return an array with '#type' => 'markup' and then a field for the markup. The rest of this example assumes you get data back and want to render it yourself.
In your module's .module file you can define the custom theme function (so you can use a twig file of your own design).
function my_module_theme($existing, $type, $theme, $path) {
return [
'my_custom_theme_function' => [
'variables'=> [
// defaults for variables used in this block.
],
],
];
}
Then finally you can create a twig file named my-custom-theme-function.html.twig to render the output.
Often these kinds of setups are quite slow (since the browser's request then triggers another HTTP request + processing time) so you should consider either caching the block as much as possible or using a technique like BigPipe (which is probably not an option for you based on your question but seemed worth pointing out).

Laravel 4 PHPUnit Sqlite Database, delete items before every test

I have a Laravel 4 application with sqlite db configured for testing.
I am working in a workbench package
I have a problem testing my models in a PHPUnit Test, because i defined some unique properties on my model. I run Artisan::call('migrate', array('--bench' => 'vendor/webshop')); from my Basic Test Class from which i extend other Tests.
I think this runs the database migrations, but in my opinion it does not delete the models in the database.
Because if i do
public function setUp() {
parent::setUp();
$this->order = Order::create(array(
"uniquekey" = "123"
));
}
I get an error saying, can not insert model because of violation of unique key rule.
How should i clean the database before every test?
You should define environment for testing purposes.
Actually Laravel does have one for you - notice testing folder inside your app/config.
Create app/config/testing/database.php (or modify if exists) and place this:
return array(
'default' => 'sqlite',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => ':memory:', // this will do the trick ;)
'prefix' => '',
),
),
);
When you run tests, Laravel sets it's environment to 'testing' automaticaly - no need to play with this.
SQLite in-memory DB is memory stored withing RAM, thus it's fast - and yes, tests start with empty database as well.

Can you create a secondary index on a repeated field in Riak?

Consider a JSON object such as:
{
name: 'ben',
employer: 'The Sherwin-Williams Company',
emails: ['ben#gmail.com', 'ben#sherwin-williams.com']
}
In MongoDB you can index the emails field such that you can find any object with 'ben#gmail.com' as an email. Is this possible in Riak? I couldn't tell from reading the docs.
You certainly can, but you have to manually enter the index entries.
Here's an example in Ruby:
require 'riak'
client = Riak::Client.new(:protocol => "pbc", :host => "127.0.0.1", :pb_port => 10047, :http_port => 10048)
ben = client['people'].get_or_new('ben')
ben.data = { :name => "ben",
:employer => "The Sherwin-Williams Company",
:emails => ['ben#gmail.com', 'ben#sherwin-williams.com'] }
ben.indexes['email_bin'] << "ben#gmail.com"
ben.indexes['email_bin'] << "ben#sherwin-williams.com"
ben.store
Now you can look it up via the ruby library, or through your web browser at http://127.0.0.1:10018/buckets/people/index/email_bin/ben#gmail.com
On my system this returns:
{"keys":["ben"]}
I know the Java and the Ruby Riak libraries support adding/editing index entries, I will have to check on the others and get back to you though.

Resources