<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jean Baudin on Medium]]></title>
        <description><![CDATA[Stories by Jean Baudin on Medium]]></description>
        <link>https://medium.com/@TheJBStart?source=rss-ecdc0bacde68------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*BgTjZKJpGJJErMZF.jpg</url>
            <title>Stories by Jean Baudin on Medium</title>
            <link>https://medium.com/@TheJBStart?source=rss-ecdc0bacde68------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 21 Jul 2017 22:58:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/@TheJBStart" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Google Cloud Functions, Serverless framework and environment variables.]]></title>
            <link>https://medium.com/@TheJBStart/google-cloud-functions-serverless-framework-and-environment-variables-b428218decb9?source=rss-ecdc0bacde68------2</link>
            <guid isPermaLink="false">https://medium.com/p/b428218decb9</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[serverless-framework]]></category>
            <dc:creator><![CDATA[Jean Baudin]]></dc:creator>
            <pubDate>Sun, 16 Jul 2017 11:33:21 GMT</pubDate>
            <atom:updated>2017-07-17T15:01:53.355Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Yet another article about Serverless</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/312/1*TrlkwMmKkeYFc3BJnyLbKg.png" /><figcaption>google cloud and serverless</figcaption></figure><p>Yes, I know it <em>looks</em> quite trendy at this time to talk about Serverless architecture and the framework of the same name. Instead of spending time explaining to you what they are, I invite you to read the following articles describing what serverless architecture (<a href="https://martinfowler.com/articles/serverless.html">https://martinfowler.com/articles/serverless.html</a>) and what the Serverless framework (<a href="https://serverless.com/framework/">https://serverless.com/framework</a>) are all about.</p><h4>Our use case</h4><p>Our company works with the Google Cloud platform. We are an international business built around a bunch of branded websites. Each brand operates in one or more countries.</p><p>All brands use the same <em>core </em>source code<em> </em>but with different features. All of these features can be activated/deactivated whenever we want. Some features are for all websites. Others are more specific to one website.</p><p>Technically, it means that you are building all your brands as soon as you are bringing a new feature to your core <em>bundle. </em>Even if your code is composed of multiple apps — such as what <a href="http://frint.js.org">Frint</a>, a framework that we use and maintain at Travix, proposes, whenever you merge something to your master branch you have to be sure that your core bundle will have all the necessary features available at any time after deployment.</p><p>Each build runs in a Docker image in Google Cloud Container Engine.</p><p>If you draw what I have just explained it looks more or less like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/301/1*XzRu1H8nQLuE_AkJQgEmXQ.png" /><figcaption>The overview</figcaption></figure><p>Now if you are thinking about how it’s maintained in terms of CI / CD, it does get quite tedious and expensive to perform multiple builds throughout the day. In the traditional approach, the applications would be your ancestors. When the build of one of the application happens, it triggers the build of the core website. After the core website is built, all other related builds for the sites are triggered out.</p><p>Another issue that you can have with such approach is about the speed of your websites. If you are making everything to be available at any time it means that you are loading lots of unused scripts. And that’s bad.</p><p>So how to build the right website when required without having to rebuild the whole thing?</p><h4>An alternative approach</h4><p>Such as what the title suggests we will explore how we could solve this problem using <a href="https://cloud.google.com/functions/docs/">Google Cloud Functions</a>. And because we want to be the best hipsters ever let’s bring in to it the Serverless framework for managing our functions and <a href="https://cloud.google.com/pubsub/docs/">Google Cloud Pubsub</a>.</p><p>Google Cloud Functions is the answer of Google Cloud regarding the Serverless architecture. The Serverless framework was briefly introduced at the beginning of the article. In our case, we will see a basic usage of the framework. However, the framework provides a complete set of commands such as rollback, delete and others which can be quite useful when you integrate a new service in your infrastructure.</p><p>Pubsub is a message pattern. It means Publish — Subscribe. All the messages are published to “topics”, which are in turn distributing them to the “subscribers”. <a href="https://cloud.google.com/pubsub/docs/overview">Google Cloud Pubsub</a> is one solution you can use for triggering your functions. In our case it is adapted because we need to be able to queue the requests and we cannot directly trigger the API (on-going builds).</p><p>The Pubsub API lets you decide if a message must be re-sent for a certain period of time. It is called <a href="https://cloud.google.com/pubsub/docs/reference/rpc/google.pubsub.v1#google.pubsub.v1.ModifyAckDeadlineRequest">acknowledgment deadline</a> (ackDeadline). For managing your queue when a build is ongoing you can return an error for postponing the message X minutes later. Here we will use the default value, 80 seconds.</p><p>The aim is to be able to build ‘site A’ when ‘application A’ is built instead of rebuilding everything because only ‘site A’ uses ‘application A’ at this time.</p><p>We need to be able to keep track of the build status of ‘application A’. The application is built in a CI, so when it’s complete, it triggers the ‘core website’ build.</p><p>Instead of triggering the ‘core website’ directly, we send a Pubsub message if the build was successful.</p><p>The code blocks bellow assume you’re using a *nix OS and has NodeJS 6.11+ with NPM available in your path.</p><p>We will also assume that your CI has an API available for triggering builds and this API manages the authentication using HTTP basic authentication.</p><p>First step, install the “gcloud” binary. Here is a guide on how to do it: <a href="https://cloud.google.com/sdk/downloads">https://cloud.google.com/sdk/downloads</a></p><p>Create a service account from the Google Cloud console. It must have access to the following resources: Google Cloud Functions, Google Cloud Deployment Manager, Google Cloud Storage, Stackdriver Logging and Google Cloud KMS. Here also a link for installing it: <a href="https://serverless.com/framework/docs/providers/google/guide/credentials/">https://serverless.com/framework/docs/providers/google/guide/credentials</a></p><p>Now let’s create our directory project and run the following commands in a terminal:</p><pre>$&gt; mkdir build-trigger<br>$&gt; cd build-trigger<br>$&gt; npm init<br>$&gt; npm install --save serverless<br>$&gt; $(npm bin)/serverless create --template google-nodejs --path success-handler</pre><p>Here we installed the Serverless NPM module in our project “build-trigger”. The next step is to create the “success-handler” function using the template for the Google Cloud Functions.</p><p>Let’s move into the directory and install everything:</p><pre>$&gt; cd success-handler<br>$&gt; npm install<br>$&gt; npm install --save gcloud-kms-helper</pre><p>Here we installed the dependencies for the function. We installed as well <a href="https://www.npmjs.com/package/gcloud-kms-helper">gcloud-kms-helper</a> . gcloud-kms-helper is a small NPM package I released for encrypting / decrypting secrets using Google Cloud KMS. Go to the Google Cloud Console. Select “IAM &amp; Admin &gt; Encryption keys”. Create a new keyring then create a new key in this keyring.</p><p>Now go into your favourite IDE and open the “success-handler” directory.</p><p>Edit the “serverless.yaml” as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7aa3194ad1f4d20ce5a5ff7635822df2/href">https://medium.com/media/7aa3194ad1f4d20ce5a5ff7635822df2/href</a></iframe><p>Here we are using the variables capability of the Serverless framework. Awesome !</p><p>Your “env.js” file should look like that:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63352ee121ec7eff4a3c4660c82125d5/href">https://medium.com/media/63352ee121ec7eff4a3c4660c82125d5/href</a></iframe><p>In this file, we are defining a function which returns an object. The returned object has all the environment variables you use in both your script and your configuration file.</p><p>When Serverless deploys your functions it copies the files specified in the “package” property into a zip file which is uploaded in the Google Cloud Storage Bucket. If the “include” property is not defined then everything not defined in “exclude” is uploaded.</p><p>Having an environment management solution allows you to deploy your functions in other clusters as well following your workflow.</p><p>Let’s now create a small script named “env-generator” in “success-handler/bin” which will generate for us the environment before the upload:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/279a51b9ffc5704dbe556b8ac4956c68/href">https://medium.com/media/279a51b9ffc5704dbe556b8ac4956c68/href</a></iframe><p>Now open your “package.json” file and add the following script:</p><pre>{<br>  // your package.json<br>  &quot;scripts&quot;: {<br>    &quot;postinstall&quot;: &quot;./bin/env-generator&quot;</pre><pre>  }</pre><pre>}</pre><p>Each time you will run the command “npm install” it automatically generate the “env.json” file for you.</p><p>Let’s implement our function. Open the “index.js” file and perform the following modifications:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3585ac2345df78fd41435c739821bb98/href">https://medium.com/media/3585ac2345df78fd41435c739821bb98/href</a></iframe><p>Our function expects a message with the following format:</p><pre>{  <br>  &quot;targets&quot;: [</pre><pre>    &quot;site-a&quot;<br>  ]</pre><pre>}</pre><p>If the message has the correct format then we decrypt our key and trigger our build using the CI API.</p><p>Now let’s deploy our function:</p><pre>$&gt; rm -rf node_modules &amp;&amp; npm i<br>$&gt; serverless deploy</pre><p>At first we reinstall the NPM modules to make sure that we have a clean state and our environment config and secrets for communicating with the CI API are properly generated</p><p>The framework will take care of creating and uploading the zip file to the Google Cloud servers. Also it will create the Pubsub topic if required.</p><p>The next step is to synchronise your system for managing your website settings with your Pubsub topic. This way your build reacts to both builds of your applications and to any modification of your settings.</p><p>Let’s assume that you are building your website using a bash script. Let’s create a file named “pubsub.sh”:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ce03398b6543fee61bf3467a0c8909c/href">https://medium.com/media/1ce03398b6543fee61bf3467a0c8909c/href</a></iframe><p>Include this bash script in your main build script:</p><pre>. pubsub.sh</pre><p>When your build is done and successful just call the method “publish_event”</p><pre>publish_event &quot;{ target: [ \&quot;site-a\&quot; ]}&quot;</pre><p>It will publish a message to your topic which in turn will trigger your function.</p><p>I hope this article will help you build an even more exciting infrastructure using the Serverless framework. I plan to update to the module <a href="https://www.npmjs.com/package/gcloud-kms-helper">gcloud-kms-helper</a> so that it can also handle the keyring and key’s creation automatically for you.</p><p>The entire template you can use to get started can be found here: <a href="https://github.com/jackTheRipper/gcf-template-env">https://github.com/jackTheRipper/gcf-template-env</a></p><p>Enjoy !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b428218decb9" width="1" height="1">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Write a unit test for a Frint-React component]]></title>
            <link>https://medium.com/@TheJBStart/write-a-unit-test-for-a-frint-react-component-b6846da3bcc9?source=rss-ecdc0bacde68------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6846da3bcc9</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[frintjs]]></category>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Jean Baudin]]></dc:creator>
            <pubDate>Thu, 06 Apr 2017 09:11:28 GMT</pubDate>
            <atom:updated>2017-04-06T09:23:42.756Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/846/1*AkNr3mEpVtIbvF1ngnTHoA.png" /></figure><p>In this first article of a <a href="https://medium.com/@TheJBStart/testing-a-frint-application-55f54ecf7e2c">serie of 3</a> we are going to write a test for the root component of the counter-app in the kitchensink example available in the Frint repository on Github.</p><p>The sources can be found here: <a href="https://github.com/Travix-International/frint/tree/master/examples/kitchensink">https://github.com/Travix-International/frint/tree/master/examples/kitchensink</a></p><p>I assume that everybody is already aware of what Frint is so let’s start coding.</p><p>First, clone the Frint repository. Go to the counter-app directory in examples → kitchensink. Run npm i.</p><p>The root component can be found here: <a href="https://github.com/Travix-International/frint/blob/master/examples/kitchensink/app-counter/components/Root.js">https://github.com/Travix-International/frint/blob/master/examples/kitchensink/app-counter/components/Root.js</a></p><p>In our case we will focus on what the component renders.</p><h3>What do we want to test?</h3><p>We want to check that our component is correctly rendering. For checking this let’s assert that it contains “&lt;strong&gt;Services:&lt;/strong&gt;” and it calls getAppName from the ‘Foo’ and ‘Bar’ providers.</p><h3>What do we need for the test?</h3><p>We have to mount the component to have access to its DOM, so we can verify if it contains the elements mentioned before.</p><p>To do this, we will use the framework mocha with enzyme.</p><p>Here is a basic setup for the “mocha.opts” file:</p><pre>--colors<br>--compilers js:babel-register<br>--require frint-test-utils/register<br>--require ./test/config/bootstrap.js<br>--recursive</pre><p>In the bootstrap file we define all the test utilities.</p><p>Now that we are done with the setup, let’s write the first test.</p><p>We need to mock the Frint application for mounting the root component :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0b639c11fffe502a5032c5640d6b5a3f/href">https://medium.com/media/0b639c11fffe502a5032c5640d6b5a3f/href</a></iframe><p>In our mock, the first item of the providers list is the counter-app’s Root component. Foo and Bar are spies. We will use them for verifying that they have been called. The store defines the initial state of the component.</p><h3>Writing the test</h3><p>Now that the mock is created, let’s write the code to test the expected behaviour.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c42275c7f321ec368e9a44cc63d515a/href">https://medium.com/media/9c42275c7f321ec368e9a44cc63d515a/href</a></iframe><p>Frint-React function getMountableComponent takes as argument the instance of the mock App we created. It returns a mountable React component that we can mount using Enzyme mount function.</p><p>Now that our test is ready, run the following command:</p><pre>npm test</pre><p>You should see the test passing.</p><p>The sources for the article are available here: <a href="https://github.com/Travix-International/frint/tree/master/examples/kitchensink/app-counter">https://github.com/Travix-International/frint/tree/master/examples/kitchensink/app-counter</a></p><p>Don’t hesitate to leave a comment if you have any questions regarding the article.</p><blockquote>Enjoy !</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6846da3bcc9" width="1" height="1">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Testing a Frint application]]></title>
            <link>https://medium.com/@TheJBStart/testing-a-frint-application-55f54ecf7e2c?source=rss-ecdc0bacde68------2</link>
            <guid isPermaLink="false">https://medium.com/p/55f54ecf7e2c</guid>
            <category><![CDATA[unit-testing]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[frintjs]]></category>
            <dc:creator><![CDATA[Jean Baudin]]></dc:creator>
            <pubDate>Thu, 06 Apr 2017 09:10:46 GMT</pubDate>
            <atom:updated>2017-04-06T09:12:04.776Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*SJnGwic9eeQlE2KMMxuhvw.png" /></figure><p><a href="https://frint.js.org">Frint</a> is a JavaScript framework developed by Travix International.</p><p>Better than describing what Frint is I suggest you to take a look at some articles written by <a href="https://medium.com/u/27f01376bd57">Fahad Ibnay Heylaal</a> such as the <a href="https://travix.io/introducing-frintjs-a9a8cdd29812">‘Introduction to Frint’</a>.</p><p>This article is an introduction to a “how-to” guide of 3 articles explaining how to write your unit tests for a Frint application.</p><p>Here is the list of articles:</p><ol><li><a href="https://medium.com/@TheJBStart/write-a-unit-test-for-a-frint-react-component-b6846da3bcc9">Writing a unit test for a Frint-React component</a></li><li>Writing a unit test for a Frint application (under construction)</li><li>Using the Time Travel Debugging with Frint (under construction)</li></ol><p>I hope the bunch of articles will help you.</p><p>Feel free to suggest some topics if you think that something is missing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55f54ecf7e2c" width="1" height="1">]]></content:encoded>
        </item>
    </channel>
</rss>