OpenStack CURL API Commands

I have recently worked with OpenStack and used the OpenStack API to create/delete container and upload objects.

In this post, I am going to give the commands used for that.

You would need to get an auth token first to use the Openstack API’s. To get the Auth token you again need an API call but for which you would need to pass username and password. I am not going to give that API call here. If you want, please refer the openstack documentation. Same goes for getting the OPENSTACK_OBJECT_STORE_URL. The Openstack auth token response header contains both the auth token and object store(Swift) public URL. Get those details and replace it in the below command.

Creating a container


curl -X PUT ${OPENSTACK_OBJECT_STORE_URL}/${CONTAINER_NAME} -H "X-Auth-Token: ${OPENSTACK_AUTH_TOKEN}"

Deleting a container


curl -X DELETE ${OPENSTACK_OBJECT_STORE_URL}/${CONTAINER_NAME} -H "X-Auth-Token: ${OPENSTACK_AUTH_TOKEN}"

 

Uploading an object


curl -X PUT \
"${OPENSTACK_OBJECT_STORE_URL}/${CONTAINER_NAME}/${FILE_NAME}" \
-H "X-Auth-Token: ${OPENSTACK_AUTH_TOKEN}" \
--data-binary "@${LOCAL_FILE_PATH}/${FILE_NAME}"
 

Deleting an object


curl -X DELETE \
"${OPENSTACK_OBJECT_STORE_URL}/${CONTAINER_NAME}/${FILE_NAME}" \
-H "X-Auth-Token: ${OPENSTACK_AUTH_TOKEN}" 

 

Advertisements

JUnit4 Exception testing

In this post, I am going to show how we can write a JUnit test case to test a method which throws an exception.

We can follow any one of the below approaches to test the method throws an exception. Here, I am using JUnit 4.

1. @Test annotation with “expected” attribute
2. try-catch and fail()
3. ExpectedException

Here is my class and contains a method “validateUserData” which has logic to validate the userName and name. If anyone of this has either null or blank then it will throw a RuntimeException with the appropriate message.


import org.apache.commons.lang3.StringUtils;

public class UserDataValidator {

    public void validateUserData(String userName, String name){
        if(StringUtils.isBlank(userName)){
            throw new RuntimeException("userName is null or blank");
        }
        if(StringUtils.isBlank(name)){
            throw new RuntimeException("name is null or blank");
        }
    }
}

Let’s write a JUnit test case to test this method with the above approaches.

@Test annotation with “expected” attribute

Here is the JUnit test to test the “validateUserData” method. I am passing the “userName” as null. So as expected the method throws a RuntimeException.


import org.junit.Test;

public class UserDataValidatorTest {

    @Test(expected = RuntimeException.class)
    public void testValidateUserData_userNameIsNull(){
        UserDataValidator userDataValidator = new UserDataValidator();
        userDataValidator.validateUserData(null, "test");
    }
}

try-catch and fail()

if we want to assert the exception message, then we can follow this approach.


import org.junit.Test;

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

public class UserDataValidatorTest {
    
    @Test
    public void testValidateUserData_userNameIsNull(){
        UserDataValidator userDataValidator = new UserDataValidator();
        try{
            userDataValidator.validateUserData(null, "test");
            fail();
        }catch (Exception e){
            assertEquals("userName is null or blank", e.getMessage());
        }

    }
}

ExpectedException

The ExpectedException rule is used to test the exception type and also the exception message.


import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class UserDataValidatorTest {

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void testValidateUserData_userNameIsNull(){
        thrown.expect(RuntimeException.class);
        thrown.expectMessage("userName is null or blank");
        UserDataValidator userDataValidator = new UserDataValidator();
        userDataValidator.validateUserData(null, "test");
    }
}

Setting the build description for a jenkins job

If you want to set the build description for a Jenkins job, then follow the below steps.

1. Make sure that the “Description Setter Plugin” installed on the Jenkins server.
2. Go to your Jenkins job configuration page.
3. Go to Post-Build Actions section.
4. On the “Set the build description” section, provide the regular expression to look for it and also the regular expression for failed builds.

Refer the below snippet from my Jenkins job.

setBuildDesc

 

I have given the regex as “project tag number is (\d*)”. So Jenkins will look for this string in my job console output and retrieve the number and use that value as build description. If in case, the job is failed, then it will use “failed” text as the build description.

My Jenkins job sample console output is as given below,


....
....
....
Project has been built successfully.
project tag number is 20180330
job finished Date: Fri Mar 30 03:22:13 EDT 2018
[description-setter] Description set: 20180323
....

The build history looks like below,

buildhistory