DevOps and Linux practice pt. 3

Continuing from the DevOps and Linux practice pt.2, this time I’m going to use Cron to run the tests automatically and update the web site based on the results.

First I had to start a new Java project in the repository, after reading a little bit I saw that running tests from console was easier if I used Maven. So I first installed Maven using sudo apt install maven and then started a Maven project with VSCode.  To set up maven correctly and run my first tests with it I followed this tutorial.

The test results will be exported to a file so that the web page could read it and update the info in it. For this part I had a lot of issues trying to read a file and pass the text to the HTML, I could not run a reader script from the HTML because ExpressJS is not made for client applications. A simple solution for this is to write the output as if it were a HTML file and then just host that new HTML. Before rewriting the new file I formatted the output with regular expressions firsts in bash.

All this work took me a lot of time to do, especially the part where I had to show the testing results in the page somehow. I tried a lot of methods I found on Internet and I ended doing a mixture of them to do something I was happy with. At the end of this week I’m going to write more about this and show the final results.

 

Reflection about Blogging and Me

The Bloggin and Me blog post by Ana is an insightful and fun read. She writes about a lot of topics that not many people want to talk about like personal fears and life problems, but she does it in a very relaxed style with some jokes. I like to read about others people’s life because sometimes I find that I’m not very different to others and I can relate a lot of their thoughts and experiences to mine.

I can imagine how she felt when she stopped having fun for working and earning money. I’ve always been very curious about technology, science and engineering since I was a kid, I liked to be creative, build stuff, experiment with new knowledge and have fun doing anything non-school related. I liked to challenge myself and discover or learn new things along the way. However, I stopped feeling like that since my mid years in college. Now, I rarely feel like I want to do something new and interesting, I don’t feel that fresh inspiration that pushes me to try something new or stupid or just fun. I know is not that I don’t have time, I have a lot of free time, I just don’t feel like that anymore. I spent most of my free time playing video games or watching series, tired, maybe thinking about the next assignment, the next exam, the grades or that I should sleep more. Now I see those fun activities as a waste of time and unproductive, but then I proceed to do something even less productive or worst, less fulfilling. Perhaps I’m burned out by school, I’m forced to think that I should spend my free time doing productive stuff to be more competent in my area, but that thinking makes me get tired quicker, more stressed and with the constant and unfulfilled desire to rest. I feel like when I was younger when I do challenging homework that pushes me to be creative, but each time is harder to find the right spot of time to start the work and put a decent amount of thinking to it. I mostly do homework to finish it, not to learn from it, hardly I sit and take my time, I don’t have fun anymore, and that is sad.

It is very inspiring how she quickly started to make conscience about her own thoughts and feelings to start working over them. She realized that a lot of people around her were toxic, and that most of her fears were a common occurrence among the developer community. In her words “Your fears are part of who you are and use them to your benefit”. This line made me think about a conference where Jürgen Klaric talked about fear, and how fear forges our experience as human being. Fear is good because it saves us from apparent danger, it’s an instinct that is hard to avoid and very natural to follow. But fear is what stop us from doing more and better things with our lifes. He mentioned that eliminating our fears is impossible, fear is always going to be there to warn us, we cannot avoid it nor eliminate it, but we can learn to take advantage of it by understanding it.

At the end she mentions the personal benefits of having a space for you in your blog, more that doing it for someone else, is doing it for you. I think that having a space just for me and my silly thoughts, record my new learning or posting any content would very nice to have, but I just don’t find the motivation to do it. I hope that somewhere in the near future I can get healed from this weird mental illness and finally enjoy life as I used to, without worrying about grades, without worrying about how much I earn, without worrying about how “productive” I am and without worrying about what everyone else thinks of me. I think that maybe giving me the space to write a blog about anything could work as meditation and a relaxation activity. Maybe I could try it in the future when I’m done with the semester. I’m of course doing what I can to enjoy every second of my life and my beautiful student life, but is still hard sometimes to get out of the comfort zone and have enough energy to do something for myself.

"gears" flickr photo by el_rogos https://flickr.com/photos/elrogos/7169326979 shared under a Creative Commons (BY-NC-ND) license

Development Operations

Development Operations, or DevOps, is a work philosophy that is based on the thinking that “software isn’t done until it’s successfully delivered to a user and meets their expectations around availability, performance, and pace of change”(Mueller, 2010). A lot of people see it as a job title where the person with the job has the responsibility of building the infrastructure for development and make sure the software product is delivered in the right way. As the What is DevOps? blog post explains, it’s just more than that. DevOps role is perhaps misunderstood as “the guy who tell us to use docker and chef”; as I said in the beginning, it’s an entire work philosophy that picks the main ideas from Agile development and transforms them into a set of practices, tools and principles to deliver a product continuously.

In past years DevOps was not even a thing because no industry needed that, but that changed with the software demand explosion. Now software is not a product that is delivered once and that’s it, we consume software as a continuous service that needs maintenance, adjustments and changes on the run.  Developers now need to add new features to a software that has already launched and ten thousand people are using at the same time. The goal of DevOps is to solve this problem with automation to make it easier and reduce error. This overall means that DevOps is about Infrastructure Automation, Continuous Delivery and Site Reliability Engineering as Mueller suggests.

Doing a little bit of research I found a REALLY good video about DevOps that focuses a little more on explaining what is the DevOps mindset and why it’s important to understand it. Andrew Clay Shafer is a DevOps evangelist that visited Guadalajara a week ago to talk about his passion, I didn’t have the chance to go to the conference but this video might be the equivalent:

“DevOps is optimizing human performance and experience operating software…with software… and humans” (Shafer, 2018)

One interesting idea that I picked from the video is that organizations and humans need to learn to improve and become more productive, and to learn we all must be able to make mistakes and change our behavior. DevOps is a culture of continuous improvement, where different persons with different backgrounds and knowledge gather together to reach the same goal, that is to get better.

Chapter 4 activity

For this post we are going to show how we solved the exercises for chapter 4 using TDD. We need to add new operations like multiplying and division for the Calc.java file. The test file only has a test for the add operation as it is the only function we have in the class.

testcalc1

First of all, we need to write simple tests that describe what our new operations do. Of course, these tests should fail as we haven’t coded the functions yet.

testcalc2

Now we can start writing the functionality now, we are going to start with the subtraction as it is the first test we should pass to move to the next one. On this exercise the order we decide to write the new functions does not matter, but in more complex projects we should have a specific order to write the new functions because some tests might have multiple dependencies.

testcalc3

The next step is to write the test for the divide function. Here we have a problem, we don’t know how to implement this function because it can return a double or an integer. To follow the TDD correctly first we have to make the test pass, right now the test only ask for an integer so we will write just the code to make that correct.

testcalc4

At last, the multiply function. Here again we ask ourselves if this function should be capable of accepting float values, but it is not specified on our tests so by now we will handle only integers for the input and output.

testcalc5

 

Now lets imagine some guys from testing added new tests, now with more specific cases trying to find faults in our code. They included a test that divides two integers but the result is non integer, a test for a division by zero,  and a test with negative integers. After running the tests this happens:

testcalc6

As we can notice, only the division function is not passing the new tests. On one test it is expecting float numbers, on the other test, it is expecting us to catch an error when the divisor is zero. This means that we need a refactor to return doubles and to detect divisions by zero. This will change some of our previous tests, the first division test is working but it is expecting integers, the new test is expecting  doubles, there is an inconsistency in the tests and we should define this with the team first. The final decision is that a division should return doubles because it gives a more exact result. With that information we can go back to the tests, modify them accordingly and now start working on the refactor.

testcalc7

With this exercise we practiced TDD and refactoring. We learned how important it is to have good tests that defines the requirements for each function and that show how specific behavior should be handled. Also, it showed us how in TDD the tests are the guideline of our code and that it can show design errors or inconsistencies before we start writing the code, a huge advantage over other methodologies.

The code of the exercise is available here:

Calc.java

CalcTest.java

Planning Week 4

At the start of the semester we planned that during this week we will be setting everything up to test the project continuously in the development. The project will now be only a web application, so we need to install all the essential tools to test JavaScript code, front-end functionality and data base communication.

At the end of this week we should have everything ready in the repository and some demo tests working. This includes the Travis set-up that will allow us control pull requests automatically.

How to use JUnit with NetBeans

Hello to all the readers. In this post I’m going to show you how simple it is to test in Java using JUnit in NetBeans. First of all, I would like to say that this tutorial is only going to cover what we need to do to start testing simple programs for the TC3045 class in ITESM Guadalajara, that’s why we I’m going to use JUnit 4. The objective is to show how simple it is to setup the programs and start coding right away. Similar tutorials might be around the web and I’m not an expert on this subject, so if you would like to take a deeper look I recommend you going to the documentation in the official site. Everything in this tutorial is just the steps I did to have everything running in my PC(Linux). I hope you find it useful.

Step 0: Install Java

If you don’t have Java installed all you need to do is to run this commands con your console to install the JavaDevelopmentKit(JDK) in Ubuntu.

sudo add-apt-repository ppa:webupd8team/java
sudo apt install oracle-java8-installer
java -version
javac -version

Thanks to this tutorial for the help. If you are in other OS like Windows I recommend you to do the installation as any normal application, try the official java site for the JDK. Remember that sometimes you have to configure the PATH manually.

Step 1: Install NetBeans

  • Go to the official site of NetBeans and download the newest version.

1.png

As NetBeans 10 does not have an installer we must do it manually with the binary files. Download the zip file and decompress it.

2.png

3

  • Do the manual install for the bin files

For this I need you to open the terminal and go to the location of your netbeans folder. In my case I moved it to Desktop.

4

From here to do the installation insert the following commands:

sudo mv netbeans/ /opt
sudo ln -s /opt/netbeans/bin/netbeans netbeans

The first command moved the bin folder to /opt and the second command created a new command “netbeans” for the console that executes the program.

Now to open netbeans just write netbeans in your console.

5

Step 2: Create a project and write a java class

Lets star creating a new project. To avoid complications I like to create a project for the whole class and every file and exercise will be in this project.

  • Click on the “New Project” button

6

  • Select Maven and click next. We want a Java Application in Maven because it’s easier to handle dependencies.

7

  • Write the project name and choose the location. Then click “Finish”.

8

Now it’s time to create a class and start coding.

  • Click on the “New File” button and create a new Java Class.

9

  • Choose the class name and click finish. I’m going to call it Operator because the class will have functions to do simple operations.

10

  • Code

Now we should have the class in out screen. I’m going to start adding simple function for sum, multiplication and a hello world.

public class Operator {
    public int sum(int a, int b){
        return a+b;
    }
    
    public int multiplication(int a, int b){
        return a*b;
    }
    
    public String helloWorld(){
        return "Hello World";
    }
}

Step 3: Create a test for that specific class

NetBeans creates automatically a testing template for the class you want, all you have to do is to modify the tests or create more.

  • Click on Tools and Create/Update Tests(You have to be on the Operator class)

11.png

**Ignore the typo in the picture 🙂

  • Deselect the options on the image(we are not going to need that), and click OK. You can change the name of the new class if you want.

12.png

Now as you can see in the new test file all the functions from Operator have a testing function. If we run the tests now it will fail because we have to remove the fail() call and modify the test.  The “assertEquals” at the end is what defines if the test passes, if the two paramenters are equal the test will pass. There are more options to create more specific tests but as I’m covering just the basics I’ll leave the code here with simple assertions that should pass.

public class OperatorTest {
    /**
     * Test of sum method, of class Operator.
     */
    @Test
    public void testSum() {
        System.out.println("sum");
        int a = 2;
        int b = 3;
        Operator instance = new Operator();
        int result = instance.sum(a, b);
        assertEquals(result, 5);
    }

    /**
     * Test of multiplication method, of class Operator.
     */
    @Test
    public void testMultiplication() {
        System.out.println("multiplication");
        int a = 5;
        int b = 3;
        Operator instance = new Operator();
        int result = instance.multiplication(a, b);
        assertEquals(result, 15);
    }

    /**
     * Test of helloWorld method, of class Operator.
     */
    @Test
    public void testHelloWorld() {
        System.out.println("helloWorld");
        Operator instance = new Operator();
        String result = instance.helloWorld();
        assertEquals(result, "Hello World");
    }
}

To run the test all you have to do is right-click on the code editor and click on “Test File” or press the buttons Ctrl+F6. The output should be something like this:

13.png

For the purpose of the class, this configurations is still not enough because NetBeans 10 uses by default JUnit5 and the book of our lecture focuses on JUnit4. The final step is to install JUnit4. Remember that I said that we are going to use Maven because is easier to handle dependencies? This is the reason, with only a few steps more everything will be ready to go.

  • Add the JUnit4 dependency.

For this you have to find the file pom.xml and open it.

14

Now inside the <dependencies> between any dependency add the next chunk of code:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

15.png

Save the file and then wait for the automatic installation.

  • Change the import of the tests.

Check the imports of your test file, it’s using the library for JUnit5. All is left to do is to use the imports from JUnit4. Remove the import lines and copy paste this ones:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

16

Now it’s all done!!

You are ready to go try your own classes and tests. If you want to learn useful tricks to do the activities check out this file, where you will learn to handle exceptions in testing. Remember that sometimes it is expected that the code fails, this tests are useful to test edge cases and avoid weird inputs.

If you want to see the sample code in GitHub here is the link to our project repository. It’s on a separate branch because is not related to the main testing, but it’s good to have samples somewhere in case we need them.

I hope this tutorial was helpful to you. Feel free to share this content or any content referenced here to help others as well. Keep learning and have fun.