How can I run flapdoodle MongodStarter with --auth option - mongodb

I recently configured my mongoDB to use --auth while starting the process.
Now I need to change the accessing Javacode and the tests. For the tests I am using flapdoodle tools like MongodStarter.
There are some example codes at http://www.programcreek.com/java-api-examples/index.php?api=de.flapdoodle.embed.process.config.IRuntimeConfig
But I still need help to build the correct code to start the MongodStarter with the --auth option.
For example I could do this
MongodStarter runtime = MongodStarter.getDefaultInstance();
IMongoCmdOptions cmdOptions = new MongoCmdOptionsBuilder.MongoCmdOptions(null, null, true, false, false, false, false, true, true)
def mongodConfig = new MongodConfigBuilder.ImmutableMongodConfig(Version.V3_0_4, new Net(27017, Network.localhostIsIPv6()),new Timeout(), cmdOptions, "CT.pid", new Storage(), false, null, new HashMap())
mongodExe = runtime.prepare(mongodConfig);
mongod = mongodExe.start();
mongo = new Mongo("localhost", 27017);
But in the .MongoCmdOptions()-method there are a lot of parameters, which I don't care about and don't know suitable values. Only the 8th is the one, that I want to use. It is the --auth param. Filling the others with some values, I get exceptions.
A similar problem I have with MongodConfigBuilder.ImmutableMongodConfig(). I put some fantasy values to it, because there are a lot.
Can you give me a working example configuration I can test with? I crafted for some days now, but still didn't find a good combination of configuration types and values. I could imagine, that there is a simpler way, but I could not find an example.

This "embedded-services" library has the authentication configuration built in for flapdoodle's Embedded MongoDB. The full working example you want is in MongoEmbeddedService.java file (line 179)
It's not a bad idea to use this tool instead of writing your own. Otherwise, make sure you create yourself an admin user.

Related

Stop huge error output from testing-library

I love testing-library, have used it a lot in a React project, and I'm trying to use it in an Angular project now - but I've always struggled with the enormous error output, including the HTML text of the render. Not only is this not usually helpful (I couldn't find an element, here's the HTML where it isn't); but it gets truncated, often before the interesting line if you're running in debug mode.
I simply added it as a library alongside the standard Angular Karma+Jasmine setup.
I'm sure you could say the components I'm testing are too large if the HTML output causes my console window to spool for ages, but I have a lot of integration tests in Protractor, and they are SO SLOW :(.
I would say the best solution would be to use the configure method and pass a custom function for getElementError which does what you want.
You can read about configuration here: https://testing-library.com/docs/dom-testing-library/api-configuration
An example of this might look like:
configure({
getElementError: (message: string, container) => {
const error = new Error(message);
error.name = 'TestingLibraryElementError';
error.stack = null;
return error;
},
});
You can then put this in any single test file or use Jest's setupFiles or setupFilesAfterEnv config options to have it run globally.
I am assuming you running jest with rtl in your project.
I personally wouldn't turn it off as it's there to help us, but everyone has a way so if you have your reasons, then fair enough.
1. If you want to disable errors for a specific test, you can mock the console.error.
it('disable error example', () => {
const errorObject = console.error; //store the state of the object
console.error = jest.fn(); // mock the object
// code
//assertion (expect)
console.error = errorObject; // assign it back so you can use it in the next test
});
2. If you want to silence it for all the test, you could use the jest --silent CLI option. Check the docs
The above might even disable the DOM printing that is done by rtl, I am not sure as I haven't tried this, but if you look at the docs I linked, it says
"Prevent tests from printing messages through the console."
Now you almost certainly have everything disabled except the DOM recommendations if the above doesn't work. On that case you might look into react-testing-library's source code and find out what is used for those print statements. Is it a console.log? is it a console.warn? When you got that, just mock it out like option 1 above.
UPDATE
After some digging, I found out that all testing-library DOM printing is built on prettyDOM();
While prettyDOM() can't be disabled you can limit the number of lines to 0, and that would just give you the error message and three dots ... below the message.
Here is an example printout, I messed around with:
TestingLibraryElementError: Unable to find an element with the text: Hello ther. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
...
All you need to do is to pass in an environment variable before executing your test suite, so for example with an npm script it would look like:
DEBUG_PRINT_LIMIT=0 npm run test
Here is the doc
UPDATE 2:
As per the OP's FR on github this can also be achieved without injecting in a global variable to limit the PrettyDOM line output (in case if it's used elsewhere). The getElementError config option need to be changed:
dom-testing-library/src/config.js
// called when getBy* queries fail. (message, container) => Error
getElementError(message, container) {
const error = new Error(
[message, prettyDOM(container)].filter(Boolean).join('\n\n'),
)
error.name = 'TestingLibraryElementError'
return error
},
The callstack can also be removed
You can change how the message is built by setting the DOM testing library message building function with config. In my Angular project I added this to test.js:
configure({
getElementError: (message: string, container) => {
const error = new Error(message);
error.name = 'TestingLibraryElementError';
error.stack = null;
return error;
},
});
This was answered here: https://github.com/testing-library/dom-testing-library/issues/773 by https://github.com/wyze.

Using IncludePackageName = false in AddServiceFabricConfiguration does not change setting in Service Fabric AspNetCore config?

I'm trying to get rid of the "Config:" prefix added by AddServiceFabricConfiguration with AspNet Core to follow the same naming regardless of configuration source.
According to the documentation you should set IncludePackageName = false which I do like this:
.ConfigureAppConfiguration(builder => {
builder.AddServiceFabricConfiguration(serviceContext.CodePackageActivationContext, options => options.IncludePackageName = false);
})
But when I'm running the application the configuration is populated like this with IncludePackageName set to true anyway.
How can I make this setting work? The ServiceFabricConfigurationProvider which the helper adds is not public so I can't find a suitable workaround.
Update: This seems to have been some issue with my local environment rather than the actual code. Reboot, clean and rebuild fixed the issue.

Protractor Custom Locator: Not available in production, but working absolutely fine on localhost

I have added a custom locator in protractor, below is the code
const customLocaterFunc = function (locater: string, parentElement?: Element, rootSelector?: any) {
var using = parentElement || (rootSelector && document.querySelector(rootSelector)) || document;
return using.querySelector("[custom-locater='" + locater + "']");
}
by.addLocator('customLocater', customLocaterFunc);
And then, I have configured it inside protractor.conf.js file, in onPrepare method like this:
...
onPrepare() {
require('./path-to-above-file/');
...
}
...
When I run my tests on the localhost, using browser.get('http://localhost:4200/login'), the custom locator function works absolutely fine. But when I use browser.get('http://11.15.10.111/login'), the same code fails to locate the element.
Please note, that the test runs, the browser gets open, user input gets provided, the user gets logged-in successfully as well, but the element which is referred via this custom locator is not found.
FYI, 11.15.10.111 is the remote machine (a virtual machine) where the application is deployed. So, in short the custom locator works as expected on localhost, but fails on production.
Not an answer, but something you'll want to consider.
I remember adding this custom locator, and encounter some problems with it and realised it's just an attribute name... nothing fancy, so I thought it's actually much faster to write
let elem = $('[custom-locator="locator"]')
which is equivalent to
let elem = element(by.css('[custom-locator="locator"]'))
than
let elem = element(by.customLocator('locator'))
And I gave up on this idea. So maybe you'll want to go this way too
I was able to find a solution to this problem, I used data- prefix for the custom attribute in the HTML. Using which I can find that custom attribute on the production build as well.
This is an HTML5 principle to prepend data- for any custom attribute.
Apart from this, another mistake that I was doing, is with the selector's name. In my code, the selector name is in camelCase (loginBtn), but in the production build, it was replaced with loginbtn (all small case), that's why my custom locater was not able to find it on the production build.

MongoDB : How to find if slaveOk=true is set or not

We are using Mongo DB in our Application .
We have one primary and one secondary for this purpose .
How can i make sure that my Application is reading data from Primary Or Secondary ??
My question is how can i know if slaveOk=true is set or not ??
Thanks in advance .
Edited Part
Not sure of the Driver what i am uisng
I am connecting Mongo DB through Java as shown
ServerAddress addr = new ServerAddress(new InetSocketAddress(host, port));
servAddrList.add(addr);
}
MongoOptions options = new MongoOptions();
options.autoConnectRetry = true;
options.connectionsPerHost = Config.intParam(
"mongo.connectionsPerHost", 1200);
mongo = new Mongo("10.11.13.111", 27017);
And i am using mongo 2.4.jar
Please let m know how can i find what driver i am using ??
What are you looking for in my opinion is readPreference : http://docs.mongodb.org/manual/applications/replication/#replica-set-read-preference
In the api (http://api.mongodb.org/java/2.10.1/com/mongodb/ReadPreference.html) documentation there is an isSlaveOk() method : http://api.mongodb.org/java/2.10.1/com/mongodb/ReadPreference.html#isSlaveOk()
See this question: How to perform read operations from primary only
There is the answer for setting read preference
mongo.setReadPreference(ReadPreference.primary());
and your answer is to get read preference:
mongo.getReadPreference().isSlaveOk();
Unfortunately these features do not exist in versions after 2.4.
looks like earlier java driver just have mongo.slaveOk() method, call it and it is slaveOk. no way to examine.

Zend_Session_SaveHandler_Interface and a session_id mysterie

I'm trying to setup my own Zend_Session_SaveHandler based on this code
http://blog.digitalstruct.com/2010/10/24/zend-framework-cache-backend-libmemcached-session-cache/
This works great, except that my session_id behave mysteriously.
I'm using the Zend_Session_SaveHandler_Cache class as you can find it in the blog above (except that I parked it in my own library, so it's name now starts with My_).
In my bootstrap I have:
protected function _initSession()
{
$session = $this->getPluginResource('session');
$session->init();
Zend_Session::getSaveHandler()->setCache( $this->_manager->getCache( 'memcached' ) );
}
To get my session going based on this code in my .ini file
resources.cachemanager.memcached.frontend.name = Core
resources.cachemanager.memcached.frontend.options.automatic_serialization = On
resources.cachemanager.memcached.backend.name = Libmemcached
resources.cachemanager.memcached.backend.options.servers.one.host = localhost
resources.cachemanager.memcached.backend.options.servers.one.port = 11213
So far so good. Until somebody tries to login and Zend_Session::rememberMe() is called. In the comments of Zend_Session one can read
normally "rememberMe()" represents a security context change, so
should use new session id
This of course is very true, and a new session id is generated. The users Zend_Auth data, after a successful log in, is written into this new session. I can see this because I added some logging functionality to the original class from the blog.
And here is where things go wrong. This new id isn't passed on the Zend_Session apparently, because Zend_Session keeps on reading the old id's session data. In other words, the one without the Zend_Auth instance. Hence, the user can no longer log in.
So the question is, how to make my saveHandler work with the new id after the regeneration?
Cheers for any help.
Ok, I'm blushing here....
I was looking at the wrong place to find this error. My session saveHandler was working just fine (so I can recommend Mike Willbanks his work if you want libmemcached session management).
What did go wrong then? Well, besides switching from file to libmemcached, I also switched from setting up my session in bootstrap to setting it up in my application.ini. So, instead of putting lines like
session.cookie_domain = mydomain.com
in my application.ini (which were then used in bootstrap as options to setup my session), I now, properly, wrote
resources.session.cookie_domain = mydomain.com
And this is were things went wrong, because.... I only changed those lines for production, I forgot to change them further down the ini file. In other words, my development env. got the cookie_domain of my production env., which is wrong as I use an other domain name during devolepment. So, on every page load, my cookie was invalidaded and a new session started. Mysterie solved...