ATS : Environment maintenance using Environment cleanup library

Introduction


This page shows how to backup and restore the test environment using Java code only. This is done by using the API of the ATS Environment Cleanup library which is part of the ATS framework.

Describing the environment


First we create a list containing all the units to work with - databases, files and directories

List<EnvironmentUnit> environmentUnits = new ArrayList<EnvironmentUnit>();

Database environment unit

Here is an XML structure describing some database info:

<database host="10.10.10.10" port="33060" name="myproduct_db" user="root" password="123" type="mysql">
     <table name="Banks"/>
     <table name="Users" autoIncrementResetValue="0"/>
     <table name="PartnerCerts" lock="false"/>
     <table name="BusinessUnit" dropTable="true" columnsToExclude="data"/>
</database>

But now we are not using XML, instead here is the alternative using Java code. It is surely longer than the XML:

// create a list with all tables
List<DbTable> dbTables = new ArrayList<DbTable>();

// create a simple table
DbTable banksTable = new DbTable( "Banks" );
dbTables.add( banksTable );

// this table requests to reset the auto-increment value
DbTable usersTable = new DbTable( "Users" ); // additionally you can provide table schema as second argument. If you do not, ATS will use the default schema for the user
usersTable.setAutoIncrementResetValue( "0" );
dbTables.add( usersTable );

// this table requests to not be locked on restore. Generally it is not recommended to disable locking
DbTable partnerCertsTable = new DbTable( "PartnerCerts" );
partnerCertsTable.setLockTable( false );
dbTables.add( partnerCertsTable );

// a column of this table will not be processed
List<String> columnsToExclude = new ArrayList<String>();
columnsToExclude.add( "data" );
DbTable businessUnitTable = new DbTable( "BusinessUnit", columnsToExclude );
businessUnitTable.setDropTable( true ); // drop table and recreate it from scratch instead of deleting existing records. This is better as performance for very big tables
dbTables.add( businessUnitTable );

// create the database connection object
DbConnection dbConnection = new DbConnMySQL( "10.10.10.10", "myproduct_db", "root", "123" );

// create the database environment unit
EnvironmentUnit myProductDbEnvUnit = new DatabaseEnvironmentUnit( "/tmp/agent_component_environment_backup", dbConnection, dbTables );

// If you want to drop and recreate the tables instead of just deleting all of their content - uncomment the next line
// Note that if you set this to true and drop table is set by either EnvironmentUnit.setDropTables(true) or DbTable.setDropTable(true) 
//   is invoked - All of the table(s) data will be lost if an error occurs while performing backup action
// myProductDbEnvUnit.setSkipTablesContent(true)
// add this EnvironmentUnit to the list of all units
environmentUnits.add( myProductDbEnvUnit );


Drop table note: By default tables are not dropped and recreated before restore. Instead just DELETE records operation is performed, i.e. default is myProductDbEnvUnit.setSkipTablesContent(false)  and respectively per table - businessUnitTable.setDropTable(false). This delete is not effective when test (mostly a performance one) had created hundreds of thousands or even millions of records and they should be cleaned up. Now there is added option to specify for the whole table to be dropped first and then recreated with all its previous metadata - columns and indexes. We recommend setDropTable(true) to be used only for very large tables.

File environment units

Here is Java code which describes some important files. You can see the relative XML structure in the comments

// <file path="../../../file1"/>
EnvironmentUnit file1EnvUnit = new FileEnvironmentUnit( "../../../file1", "../../../file1" );
environmentUnits.add( file1EnvUnit );

// <file path="../../file2" backupName="file2_backup">
//     <action command="/etc/init.d/cmd1" sleep="2"/>
// </file>
EnvironmentUnit file2EnvUnit = new FileEnvironmentUnit( "../../file2", "file2_backup" );
List<AdditionalAction> file1AdditionalActions = new ArrayList<AdditionalAction>();
AdditionalAction file1AdditionalAction = new SystemProcessAction( "/etc/init.d/cmd1", 2 );
file1AdditionalActions.add( file1AdditionalAction );
file2EnvUnit.addAdditionalActions( file1AdditionalActions );
environmentUnits.add( file2EnvUnit );

Directory environment units

Here is some Java code which describes some important directories. You can see the relative XML structure in the comments

// <directory path="/usr/dir1/" backupName="dir1_backup"/>
EnvironmentUnit dir1EnvUnit = new DirectoryEnvironmentUnit( "/usr/dir1/", "dir1_backup" );
environmentUnits.add( dir1EnvUnit );

// <directory path="/tmp/dir3/">
//     <action command="/etc/init.d/cmd3" sleep="2"/>
// </directory>
EnvironmentUnit dir3EnvUnit = new DirectoryEnvironmentUnit( "dir3", "dir3" );
List<AdditionalAction> dir3AdditionalActions = new ArrayList<AdditionalAction>();
AdditionalAction dir3AdditionalAction = new SystemProcessAction( "/etc/init.d/cmd3", 2 );
dir3AdditionalActions.add( dir3AdditionalAction );
dir3EnvUnit.addAdditionalActions( dir3AdditionalActions );
environmentUnits.add( dir3EnvUnit );

Backing-up the environment


The following code will backup the environment:

for( EnvironmentUnit environmentUnit : environmentUnits ) {
    environmentUnit.backup();
}

Restoring the environment


The following code will restore the environment:

for( EnvironmentUnit environmentUnit : environmentUnits ) {
    environmentUnit.restore();
}




Back to parent page

Go to Table of Contents