How to have 2 domains name to a single Azure website

I previously wrote about how to configure a custom domain with Azure website, Azure DNS zone and Namecheap. What if you need to have two different domains that needs to be link to a single website? This is possible. First of all, you may wondering why to do so? Many scenarios are possible. For example, you can have a .com and a .net and you do not want to redirect. You may have multiple domains per language too.

2Domains1Website

The steps explained in the previous article are exactly what you need to follow, but twice. You will need to go in your domain provider (like namecheap) and setup the nameserver to a new Dns Zone. Yes that right, you need a single DNS zone per domain. From there, you can go in your website and add an additional custom external domain.

Azure Dns Zone and Namecheap

This is a confusing topic. It’s probably simpler to have your domain bought from Azure directly but most website may be bought before, thus needs to go the longer way. Azure documentation is not bad, there some pages interesting about how to configure a custom domain name with Azure App Service and how to create a DNS zone in Azure portal, but that is not very clear. I believe I am not the only one who had to stop and think since they are even providing some way to do it with the popular domain provider GoDaddy. Still, there is a lot of way to configure and if you are more a developer than a network guru, it may be confusing.

Before anything, let’s demystify some concepts. To have any website link to a domain you need 3 parts. The domain, a nameserver and your server that host your files. If you have a background of using VPS under Linux with CPanel, the domain and nameserver part is done when you setup the initial VPS, thus transparent for you. That was the confusing part for me. Because it was done for me, any other domain that I wanted was pretty straight forward : go in Namecheap, set the nameserver to the one provided by the VPS merchant, go in CPanel to set the domain name to the VPS directory, that’s it. So what are those three parts?

DomainDnsWebSite

Domain : This is a name you buy like “facebook.com” or “google.com”. This one can be configured in many ways. It can be just a redirection to an existing website, or it can be link a Nameserver .

Nameservers : fundamental part of the Domain Name System (DNS). The nameserver is the one in charge of mapping the domain to an IP of a server.

Server : This will be your Azure App Service (website).

NameCheapAzureDnsAzureAppService

The first step is to setup the domain to the nameserver. You have multiple options. You can use Namecheap to handle it or to user Azure. Namecheap have a BasicDNS, a WebHosting DNS and a Custom DNS. Normally, if you are adding a domain to an existing VPS (not from NameCheap), you select “Custom DNS” and set the nameserver to the one provided by the VPS merchant during the setup of your account. For azure, it’s the same thing. In NameCheap, go in the “Domain List”, click “Manage” next to the domain you want to modify. Under the first tab, named “Domain” change the Custom DNS name for Azure ones.

CustomDNSNameserver

The advanced DNS, should remain with the value Standard Nameserver.
PersonalDnsServer

The values you see are from Azure DNS zone. This is one way to do it. You can use a custom nameserver directly at Namecheap, but at this moment, is more expensive than using Azure.

So, we need to create inside the Azure Portal a DNS Zone. This is at this moment in preview, so expect some bugs on the UI. Click “New” and search for “DNS Zone”. You will see

DNSZone

You will see two entries with “@”. This is fine and normal. You will need to add few more. First, you need to add 3 entries (maybe less but … ) to verify that you are the owner of the nameserver. You need to create 3 CNAME type.

1- “awverify” to awverify.XXXXX.azurewebsites.net
2- “wwww” to XXXXX.azurewebsites.net
3- “awverify.www” to awverify.XXXXX.azurewebsites.net

Replace the XXXXX by your website on Azure. This is required because the next step is to go to your website and attach the domain. This step will verify the domain that you will enter by doing a call. Since you configured the domain to point to the nameserver, on Azure, it will try to do a awverify/wwww call. Since you are the owner, and have configured these new entries, the server will return that everything is okay. The forth entry that need to be setup is the XXXXX.com to the website. This is done by add a “@” entry to the website IP which is available on the website blade, under the blade that you add your domain. So, let’s go on App Service Blade, select your website, in the settings’ blade select “Custom domains and SSL”. This is located under the Routing category.

CustomDomainsSetting

This last blade will open and let you type your domain. You should type XXXX.com for example (no www. or any subdomain). If everything is configured correctly between Namecheap and Azure DNS zone, you won’t get any error message. However, allow some time. For me, it took about 45 minutes once I configured the Dns on Namecheap to change to Azure DNS. Once it’s added, you can take the IP address on that blade and setup the “A record” which is the forth “@”.

The Back Side of the DevOps Trend

At this moment, if you do not agree that DevOps is the best thing in the world – your are out, well not in the “gang”. Indeed, Facebook do it, Google do it, Amazon do it and Microsoft is also in transition to do it. Everybody do it, so it must be the best thing in the world? Well, if you remove the fact that DevOps always existed in small companies, than it is nothing new than simplifying your workforce expertise into a big bucket. The concept of DevOps is that an individual can contribute almost end-to-end of a product. If you want, this is the opposite of what Henry Ford used to be efficient. It is the opposite to divide to conquer. So, instead of having 1 analyst, 1 developer, 1 tester and 1 IT guy; you just use a single person that know everything. This is something you have to do if you run a small company because you do not have enough money to support all those expertise to be spread on different person. With DevOps, the same person setup the server, go talk with customers, do the planning, code the product and test it. The reason behind having huge corporations going in that path is mostly because it increase the deployment time of feature. The justification is true in some extend because you have less overhead of communication and also less waiting for a particular set of skills. Also, you have a team that have better overall picture of the system. So far, everything is “better” on paper. Who can argue against that every one can be easily replaced or that anyone do a better development since he or she knows how to code from a testing perspective or a deployment perspective. Last thing, this also merge the support team with the development team. So, they are no anymore a team that is doing the new stuff and one that is repairing it.

However, here are some problems. If you are “DevOping” only for a part of your organization, than it is not really more efficient. For example, if you have 3 levels of manager that must agree for every decision than you have DevOps for your “coding operation” not for the overall “development operation” of your product. On a small company, you talk directly to the boss and thing can move fast — sometime the boss is also a developer! It works. However, if you need to include in the loop your lead developer, you manager level 1,2,3,4… than you product manager which must go in meeting with other managers, you loss a lot of benefits like deploying fast new features, having innovative features developed, etc. In fact, from my experience, people are waiting and while they are waiting they are trying to understand the field of knowledge that they do not understand. When they are not waiting, they are doing stuff, but most of the time is on researching about the knowledge that they do not know. At the end, the proportion of time passed into developing the feature itself is not high at all. Also, since your development team is handling all the support than the development that was supposed to be more efficient is cut by the time of understanding every bugs, making the fix, testing again, etc. In a single week, the development time is shrunk rapidly.

DevOps has a bigger caveat if you have a big software : the code. For example, a software that is built by 10 developers or 100 developers or 500 has more different coding standards across the product and also a lot more codes for the same development period of time. This mean that just for development tasks, this require a huge investment of time to understand the current code base. This is not without saying that their is so many technologies implied now that reading the code base force you to know more than just 2-3 languages, it can go very fast above 6-7-8… At that point, we are not even talking about front-end and back-end code. DevOps merges front-end and back-end dev but also, like I said analysis tasks and skills; design tasks, tool, standard, meeting ; coding with different technologies, standards, best practices, debugging and software; testing with unit test frameworks that is different from techno to techno but also from unit, integration, functional, etc; deploying locally, on a server or on the cloud; infrastructure with cluster, load-balancing, network VPN, DNS; etc. So, after a few times, expert in some field become average in every fields.

It is impossible to have a single individual that is an expert in CSS, JavaScript, TypeScript, Angular, ASP, SQL, ORM, Rest Service, security, cloud storage, deployment, unit testing, etc. Indeed, a single individual can be an expert on multiple technologies and systems, but not all of them. This is why the model of Henry Ford was good for development of thing that does not change because every phase was mastered by a single entity. In software, everything change, so a pure segregate model does not apply, but on the other spectrum, the “know it all” model does not either. This is also even more true with the new trend of having new version out so fast. Today, code base is working with version 1 of one framework, in 1 month it will be version 2 out… multiply that by the tremendous amount of frameworks, libraries, technologies required and you are almost changing every weeks something. Keeping track of the good way to do stuff become harder and harder. Of course, you can learn on every task you must do, but still, you will know the basis without being an expert. The cherry at the top is that now, is that every thing is shipped so fast that it contains bugs which if you stumble into one that you are often said that “it is open source, fix it” –indeed.

So, I am all about having a wide range of knowledge. I never been someone that was dividing the front-end development from the back-end development. In my mind, you must know how it works from the request that ask the web page to how to get the data from the database (or other source). I am also all into having developers building unit test and even integration test. In fact, I have projects that I do end-to-end. However, from my professional experiences, if it goes beyond that point and you have a huge code base, the performance of the team is not better with a DevOps approach than having some experts in every part of the process. In fact, it is worth because we are all average and we loss the expertise. Whilst your expert programmers are doing functional tests, or try to see how to deploy on the IIS farm or need to go in meeting with managers to figure out what to do, they are not at full speed at what they are good at. Also, some developer does not have any interest to do analysis, neither to gather requirements, doing tasks management or to work with third party partners — they want to do what they know the best develop. Same thing for testers or any other expert in the team.

This trend is strong right now, and will be there for few times before migrating to something else. Management likes DevOps because they have a pool of individual that can be easily switched and allow them to have for few days a full team of developer and tomorrow a full team of testers as well as one team in one product today which can be moved into a different division later. I am not against that movement, but contrary to a lot of people, I simply do not think that this is the way to go in long term. Keeping developer having expertise without having them exhausted with all those different tasks and technologies to keep up is going to be challenging.

To conclude, I am curious to see why this mentality does not goes in the management’s zone. Because, DevOps could also be applied: we should only have 1 layer of ManOps: “Management Operations”. All the benefits would be also there. Faster decisions, less hierarchies to reach the person who can do something tangible, no middle man or distortion of information, faster delivering features or innovative ideas to be incorporate inside the product…

How many unit tests should you write?

I always found automated tests very powerful. On the long run, you accumulate a lot of values by having a fast way to see if something has changed that wasn’t originally planned. It serves as a good living documentation (if tests are simple to understand). The current trend in the industry is to rely solely on automated tests. Whence, all tests need to be well written. Writing good unit tests is something that is harder that you can think. Since the last 2 years, I have been in multiple teams and I realized that most people do not really know how to unit test properly.

One thing that need to be clear is that even if you touched a line with a test, it doesn’t mean that this one doesn’t need to be touched by multiple tests. I see often people running code coverage to know if they are done or not when testing. That is wrong.

A second thing is that each method needs different amount of test depending of the complexity of those ones. You cannot have only one successful unit test, and one failure unit test and be done with your method.

Another concept that seem to be not understood is that even if you “know” that the value in parameter will never be null, you have to test it for null if you allow it. You cannot discriminate scenario because you know what is the current use of the code.

There is also the notion of the coverage not being a good metric to know if you have a well written plan. While I agree that 100% doesn’t mean 100% free bug, I do not agree that it’s acceptable to have 50% of coverage. If your software quality bar is to have only 50% of your code tested, than it means that you are not ready to not have human to test your software. In fact, you should probably test your code to have above 100% coverage — multiple lines will get hit per multiple tests.

Assertions is also debatable. Most of the time, people are asserting a lot of variable which make the test hard to maintain. Every tests should assert a minimum set of variable. In fact, you should aim to assert a specific scenario. If you are asserting a lot, than you are probably testing more than one thing, thus not testing a unit but a batch. Some exception are clear, for example, testing to map a class to another class. However, a rule of thumb is to identify these big tests and make them smaller by dividing the actual code into several methods.

Preparing your unit tests should be clean. You shouldn’t have to write 20 lines of code in each of your test to create your scenario to test. Divide that arrange code into builder class. This way, they can be reused across several tests. A bonus is that by doing so, you can use those building method to seed your development database.

Naming your test is something important. The reason is that other developer won’t be able to find specific scenario and might duplicate test or not look at them. If you have thousand and thousand of tests, it become even more important. I like following the name with GivenXXX_WhenXXX_ThenXXX. It forces to have a structure of Given a specific case When specific configuration is provided Then you expect a specific result.

Here is a simple code. How many test should be created?

public class SimpleCode
{
    public string Value { get; set; }  

    public SimpleCode Merge(SimpleCode parameter)
    {
        var mergedObject = new SimpleCode();
        if (parameter.Value != null || this.Value != null)
        {
            mergedObject.Value = parameter.Value ?? this.Value;
        }
        return mergedObject;
    }
}

Most people might see two or three tests, but the answer is six.

[TestMethod]
    public void GivenSimpleCode_WhenMainInstanceValueNull_AndParameterValueNull_ThenValueNull()
    {
        // Arrange
        var mainInstance = new SimpleCode() {Value= null};
        var simpleCodeParameter = new SimpleCode() { Value = null };

        // Act
        var result = mainInstance.Merge(simpleCodeParameter);

        // Assert
        Assert.IsNull(result.Value);
    }

    [TestMethod]
    public void GivenSimpleCode_WhenMainInstanceValueNotNull_AndParameterValueNull_ThenMainInstanceValue()
    {
        // Arrange
        const string VALUE = "Test";
        var mainInstance = new SimpleCode() { Value = VALUE };
        var simpleCodeParameter = new SimpleCode() { Value = null };

        // Act
        var result = mainInstance.Merge(simpleCodeParameter);

        // Assert
        Assert.AreEqual(VALUE, result.Value);
    }
    
    [TestMethod]
    public void GivenSimpleCode_WhenMainInstanceValueNull_AndParameterValueNotNull_ThenParameterValue()
    {
        // Arrange
        const string VALUE = "Test";
        var mainInstance = new SimpleCode() { Value = null };
        var simpleCodeParameter = new SimpleCode() { Value = VALUE };

        // Act
        var result = mainInstance.Merge(simpleCodeParameter);

        // Assert
        Assert.AreEqual(VALUE, result.Value);
    }

    [TestMethod]
    public void GivenSimpleCode_WhenMainInstanceValueNotNull_AndParameterValueNotNull_ThenValueParameterValue()
    {
        // Arrange
        const string VALUE1 = "Test1";
        const string VALUE2 = "Test2";
        var mainInstance = new SimpleCode() { Value = VALUE1 };
        var simpleCodeParameter = new SimpleCode() { Value = VALUE2 };

        // Act
        var result = mainInstance.Merge(simpleCodeParameter);

        // Assert
        Assert.AreEqual(VALUE2, result.Value);
    }

    [TestMethod]
    [ExpectedException(typeof(NullReferenceException))]
    public void GivenSimpleCode_WhenParameterNull_ThenException()
    {
        // Arrange
        var mainInstance = new SimpleCode();
        SimpleCode simpleCodeParameter = null;

        // Act & Assert
        mainInstance.Merge(simpleCodeParameter);
    }


    [TestMethod]
    public void GivenSimpleCode_DefaultValue_ValueNull()
    {
        // Arrange & Act
        var mainInstance = new SimpleCode();

        // Assert
        Assert.IsNull(mainInstance.Value);
    }
}

Often, people do not test the default value of a class. This is something everyone should do. What happen if at some point a default value change but was required to be a specific value depending of the scenario. Without this test, you are not notified that something is breaking from the expected value. In that example, the expected value is NULL. If someone change it to an empty string, this might break other scenario that rely to compare on NULL to determine some state. Another testing scenario that I see often is about passing wrong value has parameter. In that particular example, null will break and return a null reference exception. Creating the test make you realize that you should probably handle that case and have a better code, like the following one:

public SimpleCode Merge(SimpleCode parameter)
{
    if (parameter == null)
    {
        throw new ArgumentNullException(nameof(parameter));
    }
    var mergedObject = new SimpleCode();
    if (parameter.Value != null || this.Value != null)
    {
        mergedObject.Value = parameter.Value ?? this.Value;
    }
    return mergedObject;
}

In that case, I just thrown an exception and will need to assert that particular exception. Why is that better than just having the null reference exception. For three reasons, first, it’s clear that it shows that the null parameter is a known scenario. Second, today it’s an exception, tomorrow it could be that we return a value. A change will make this particular test that expect a specific exception to become red and thus having a change in the test. The third reason is that if another exception is thrown, that I’ll get an error. I expect to only have an argument null exception, not a null reference or an overflow. Any other cases will be considered a bug, not the one handled.

How many test should your write? As many as your code required. Is that time consuming? Of course. Automated tests take about 30-50% of the development time when well done. Your code base will have has many line of code for tests and for code. That is the cost of being able to run a lot of test multiple times per day as well as not having human resource to test. It’s worth it because all these scenarios are hard to test for a human and since these tests are supposed to be fast, that you can run them often. The gain is enormous as the size of your code base increase. Your confidence in changing your code should remain high and new developer coming into the team won’t be afraid to change something that disrupt the expectation. That said, I still strongly believe that automated tests should be paired with human testing before being released into the public, but this is another story. You can download and play with the code in this Git Hub repository.

Migrating your Local SQL Server Database to Microsoft Azure

Migrating a SQL Server Database into Azure is easy but not obviously. Here is few steps that might save you some hours. These steps are migrating the schema by creating a new database at destination with all data.

First of all, you need to open Microsoft SQL Server Management Studio. Right click the database you want to migrate to Azure and select Task, and Export Data-tier Application. It’s important that you do not choose the option Deploy Database to Windows Azure Sql Database because this option doesn’t keep the identity (primary keys) of you tables.

ExportDataTierApplication
This will generate a bacpac file that needs to be imported into Azure. To do so, you need to connect with Microsoft SQL Server Management Studio to your Azure server. To connect to Azure Server Database you need to have your IP allowed by your Azure SQL Server firewall and knows your server password.

To import into Azure, you need to right click the Databases folder of Azure SQL Server. Select Import Data-tier Application.
ImportDataTierApplication

A short wizard will open and let you select the name of the destination database as well as some primitive Azure configuration. This may take a while to transfer depending of the size of your database.

Once it’s done, it’s always a good idea to verify if everything has been transferred.

SELECT OBJECT_SCHEMA_NAME(p.object_id) AS [Schema]
    , OBJECT_NAME(p.object_id) AS [Table]
    , i.name AS [Index]
    , p.rows AS [Row Count]
FROM sys.partitions p
INNER JOIN sys.indexes i ON p.object_id = i.object_id
                         AND p.index_id = i.index_id
WHERE OBJECT_SCHEMA_NAME(p.object_id) != 'sys'
ORDER BY [Row Count], [Schema], [Table], [Index]

That query will show the information about the number of rows per table. You can compare from the local database and the Azure one to see any disperency.