Categories
.NET Continous Integration Docker MS SQL Server Programming Project Management

Automate Testing: Gitlab, Dotnet

I always want to test my .NET based application on my personal Gitlab based repo. But since, my old project is too much a mess to do the effort for. So, when we got to rewrite the whole project, I was happy to get my experience on Unit Testing with automation of CI. I still don’t do much of CD as my development environment is still limited to development only.

So, I read about it and using Gitlab’s YML configuration, I was able to run the xUnit based Unit Test easily. It was simple too, call a MS/dotnet docker as base image, then dotnet build and later dotnet test .

But the problem arise when I have to connect to database and then I cannot get SQLCMD command to work, though I get the MS SQL Server based service to started, but until that command runs which is not present in ms dotnet docker, I cannot reload my database easily. For that I have to write code to bootstrap and create database. So, I try to search for it, and after lot of fail attempt and surprisingly no example of how to use database with YML configuration other than basic example for Mysql or Microsoft’s own docker file example. I found no info on this.

So, I decide to create a new docker image, I want to do it my way. So, I use a Ubuntu Docker image, install dotnet 2.2 on that image with mssql-cmd tools. I didn’t install MS SQL Server as I can use that as service. So using this docker image, I get my Unit testing working. This help me automate my project workflow and help me get best out of me, without need to worry about hidden errors.

My this docker image is available for anyone need it at Docker hub:

https://hub.docker.com/r/archergod/dotnetcoredb

Categories
.NET C#.NET Programming

Extensions Method in .NET

Recently I use extension method in C# project. I knew those for years, but really never get a chance to use them where they are most effect. I have couple of extension method in my project, such as ToInteger(), for a string to convert a String to Integer, which I used more frequently in my project. But that doesn’t seems a good use of technology to me.

But I now found a perfect use for extension method. i.e. I was upgrade my code from old ASP.NET project to MVC and for this I am making a class library of all objects. Now, this scenario need me to make a conversion from old enum to new enum  which are now part of namespace of my class library, rather than sitting around in App_Code folder. Here I make a extension method that convert a OLD enum to new name space base. This ease lot of my works.

Method goes like:


public static NewType GetSaleType(this OldType s)
{
	NewType r = NewType.Regular;
	switch (s)
	{
		case OldType.BK: r = NewType.BK; break;
		case OldType.IL: r = NewType.IL; break;
		case OldType.MM: r = NewType.MM; break;
		case OldType.PC: r = NewType.PC;break;
		case OldType.Regular: r = NewType.Regular; break;
		case OldType.Trigger:r = NewType.Trigger; break;
	}
	return r;
}
Categories
Continous Integration MySQL PHP Programming Project Management

Experience with BitBucket CI

I have been using bitbucket for quite sometime, and really like their service. Recently, I come to know that they have Beta of CI through pipeline, that can run any docker from Docker hub. I thought to give it a try though I have my own build setup on my local machine and really don’t need it. But why not test something new.

So, my project is regular website, which is coded in Custom PHP (no framework) and using MySQL database. I wrote some Unit Test using PHPUnit test, so all I need is to run those test when I commit to Bitbucket. It seems pretty straight forward as Bitbucket configuration example shows the configuration file for PHPUnit test. I pick it up, change path and run. My test failed, as they never get the MySQL extension for PHP. What? PHPunit test docker doesn’t have database extension installed? So, how we are suppose to test it?

Well, there are few ways, one is you use multiple docker (multi-docker environment) which I recently studied is available and I didn’t yet test it. And other is you create your own docker and use it. Install whatever you need to make it happen. Also, I realize that installing MySQL will not make any good unless I install database on it before test and change it’s certain values for testing. So, I create a new docker that has everything I need for my project, all PHP extension and database server. (You can use it from https://hub.docker.com/r/archergod/phpall/). Then I update configuration file to do following:

  1. Run MySQL service (yes by default docker do not initiate run.d etc, not sure why) and hence no service auto start.
  2. Then run `mysql` to import a Script (SQL) file to create and install database, and users needed. I put everything in that one SQL file and run it as root MySQL user. It is only a docker doing testing.
  3. then Navigate to Directory in respect to my Git folder as my default bitbucket leave your prompt at git folder.
  4. Run your test.

It does a great work, though every time you push to git, it create a CI request and download docker image, set it up based on BitBucket’s script and then run your script. So it take 2 minute extra to set the machine. If you are in habbit to push every 2 hr of work, then you end up running your test every 2 hr on every push.

But basically it is all good and is free too, which I like most. Though I have GitLab at my local machine and it works exceptionally great, but BitBucket’s CI is good and different. And in some cases is better than Gitlab’s CI.

Categories
Blog: My thoughts Operating System PHP Server Configuration

Linux Upgrade from 12.04 to 14.04: Save the day

So, this fine sunday, I think I wake with “lets spoil the day” mood. I start by checking my server to see how it is doing. A regular log view and updates I perform on my VPS. all was good, until I realise an update is available for my billing software. I decide to purchase an update for it. Before buying I forgot to check system requirement. Well it is just an update, and we install it every here and there for other things. So, I have updated copy of my billing software, which I follow the steps of upgrade and upload to my live server. All went well until I reach for software to suggest install PHP 5.6 instead of version I had. Well no problem how hard it is to update PHP in ubuntu 12.04, just matter of running apt-get.

Soon, I realise PHP 5.6 was never release for OS, and I need to use PPA. Found an old tutorial teaching me how to use PPA as it is my first time for it, the blog explain everything good, found couple more article but they are nearly the same. So, I went on and install the PPA and update the PHP 5.6. I run a command line to see what version of PHP is there and it correctly shows 5.6. So far so good, but suddenly “If anything goes wrong will go wrong” statement hit me. It only update CLI PHP not apache version. Damn, what is wrong with it.

PPA on Ubuntu 12.04 LTS need Apache 2.4 and Apache 2.4 never makes to Ubuntu 12.04. So, again there is PPA for apache but that doesn’t work for me well. I mean it does update few things, but cannot make PHP’s Lib for Apache to get installed. At this very moment, I have two options. Either I rollback all updates to my Backup Copy, or I move ahead to upgrade OS to 14.04 LTS. I choose later one.

Simply run “do-release-upgrade” and follow the course of stream. but it stuck as VestaCP’s APT repo didn’t work with upgrade. Not sure why, I just remove them from APT repo’ List and all goes well. All updated nothing is broken but Apache. It is broken because my previous attempt of PPA apache upgrade leave it broken and in conflicting state.

I remove and try reinstall apache without PPA, but that cause more conflict. After hit and trial with PPA and non PPA apache, I end up getting it upgrade and everything running.

There is no technical details shared in this post, as this post is not for Step on how to do it, but what not to do it. And that is only one thing, never miss reading about what upgrades you are going to do, never miss system requirement before paying someone. It only took me 4 hrs to get it all going, and frankly it turns out to be Positive for me, as it get my pending upgrade of OS done. But, I would have save my day if I read the simple requirement before buying the upgrade for billing software. I would have avoided unnecessary upgrade path, and reduce the down time to say 1 hr at most instead of 4 hrs. Good thing my blog is not a business, but never try that at client server.

Categories
Bookmarked Javascript

Dropzone.js: A great plugin

I recently try Dropzone JavaScript library to ease the upload process. It is really a lightweight and good library. I have a existing PHP form that take multiple images, and transfer/upgrading it to dropzone was as easy as include it’s library.

I have little to mention, but a link to site is what I want to include in this post. http://www.dropzonejs.com a must library if you need multiple image upload by user. I use it for a scientific application, where client need to bulk upload about 1000s of image file for database, and it speed up their process. There is nothing much to add for tutorial as their basic tutorial is good enough to start even for newbie.

Categories
.NET ASP.NET

ASP.NET MVC 5: Session not working

Just encounter an issue with ASP.NET MVC 5, the sessions are not working. i.e. when I put a value in controller like Session[“mysession”], it do not shows the same value in Views or in fact anywhere else. Search online, some create custom classes to manage them, and some give different patch, finally got the easiest solution. Just add “Session_Start” function call in Global.asax file and we are good to go.

Categories
C#.NET Concept/algorithm Operating System

Refreshing USB ports using C#

Finished a project in which I need to refresh the USB ports by just before we start some particular benchmarking application my client has. Seems pretty easy job, if you know to gain access of system resource. But, it become fun job, when I found that knowledge of system is not even necessary for this job. Thanks to Microsoft’s `Devcon` utility, that already take care of it. So, in all what I have to do is just pass the correct parameter to it. The parameter is pretty easy, just pass the Hardware ID of USB port to refresh.

Yeah, that’s pretty easy. But …

Yes, but how we get the hardware id of USB port? Do we still need access to system resource for that? Well, it seems so. So we are back to square one. Well not really. I found a solution on web that points to `System.Management` namespace in .NET. What it does is it give you the list of all resources that are otherwise available through Computer management or such system resources. Here is main piece of code from article

public static List GetUSBDevices()
{
	List devices = new List();

	ManagementObjectCollection collection;
	//Win32_PnPEntity
	//Win32_USBControllerDevice
	using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
		collection = searcher.Get();

	foreach (var device in collection)
	{
		devices.Add(new USBDeviceInfo(
		(string)device.GetPropertyValue("DeviceID"),
		(string)device.GetPropertyValue("PNPDeviceID"),
		(string)device.GetPropertyValue("Description"),
		device.ToString()
		));
	}

	collection.Dispose();
	return devices;
}

This provide the list of all USB port in system, and then you can pass the “DeviceID” to devcon and we are done.

For further clarification: