GREASE Core Types

The GREASE Command

class tgt_grease.core.Types.Command(Logger=None)

Bases: object

Abstract class for commands in GREASE

__metaclass__

Metadata class object

Type:ABCMeta
purpose

The purpose of the command

Type:str
help

Help string for the command line

Type:str
__author__

Authorship string

Type:str
__version__

Command Version

Type:str
os_needed

If a specific OS is needed then set this

Type:str
ioc

IOC container for access to system resources

Type:GreaseContainer
variable_storage

collection object for command

Type:pymongo.collection
execute(context)

Base Execute Method

This method should always be overridden in child classes. This is the code that will run when your command is called. If this method is not implemented then the class will fail loading.

Parameters:context (dict) – context for the command to use
Returns:Command Success
Return type:bool
failures
getData()

Get any data the execute method wanted to put into telemetry

Returns:The Key/Value pairs from the execute method execution
Return type:dict
getExecVal()

Get the execution attempt success

Returns:If the command executed without exception
Return type:bool
getRetVal()

Get the execution boolean return state

Returns:the boolean return value of execute
Return type:bool
help = '\n No Help Information Provided\n '
os_needed = None
prevent_retries()

Sets a flag in the command’s return data that will signal to stop retrying, even before the default retry limit is met.

purpose = 'Default'
safe_execute(context=None)

Attempt execution and prevent MOST exceptions

Parameters:context (dict) – context for the command to use
Returns:Void method to attempt exceptions
Return type:None
setData(Key, Data)

Put Data into the data object to be inserted into telemetry

Parameters:
  • Key (str) – Key for the data to be stored
  • Data (object) – JSON-able object to store
Returns:

Void Method to put data

Return type:

None

The Scheduled GREASE Command

Note: This command type doesn’t need a prototype configuration. Just to be run as a prototype on a GREASE node

class tgt_grease.core.Types.ScheduledCommand(logger=None)

Bases: tgt_grease.core.Types.Command.Command

Scheduled Commands Run as Prototypes

This type is used for commands that need to be run cyclically. They will be run as prototypes (always running). Make sure to fill out the timeToRun and run methods

execute(context)

Command execute method

This will run continuously waiting for timeToRun to return true to call run

Parameters:context (dict) – context for the command to use Not Used Here
run()

Put your code here to run whenever the conditions in timeToRun are defined

Note

We recommend returning something valuable since the engine logs the result of the method in verbose mode

timeToRun()

Checks to ensure it is time to run

Returns:If time to run then true else false
Return type:bool

The Automation Tester

Test your automation configurations and commands with this class! Extend it to get started.

class tgt_grease.core.Types.AutomationTest(*args, **kwargs)

Bases: unittest.case.TestCase

Automation Test Class

Version II of GREASE was all about proving stability. Automation testing is critically important to ensure reliability during fault isolation. This class is an abstract class your tests can implement to ensure they will perform exactly as you expect in production.

Make sure you set the configuration class attribute to ensure your configuration is tested, the mock_data class attribute with your mock data dictionary you expect to be sourced in production, and the expected_data with what you expect detection to find from your mocked source data. Then implement the test_command method to write standard unittests around your automation. The Platform will test your configuration for you, and execute test_command with python setup.py test is executed.

configuration

Configuration to load for this test

Type:str|dict
mock_data

String Key -> Int/Float/String Value pair to mock source data

Type:dict
expected_data

data you expect context for your command to look like

Type:dict
enabled

set to true to enable your test to run

Type:bool

Here is an example:

class TestAutomationTest(AutomationTest):

    def __init__(self, *args, **kwargs):
        AutomationTest.__init__(self, *args, **kwargs)
        self.configuration = "mongo://test_automation_test"
        self.mock_data = {'ver': 'var'}
        self.expected_data = {'ver': ['var']}
        self.enabled = True

    def test_command(self):
        myCommand = myCommand()
        self.assertTrue(myCommand.execute({'hostname': 'localhost'}))

This is a pretty basic example but it will help you get started automatically testing your automation!

Note

YOU MUST SET THE PROPERTY `ENABLED` TO BOOLEAN TRUE IN ORDER FOR YOUR TEST TO BE PICKED UP

Note

To use a static configuration set configuration to a dictionary

Note

To use a MongoDB configuration for a test prefix your configuration’s name with mongo://

Note

To use a package configuration for a test prefix your configuration’s name with pkg://

Note

to use a filesystem configuration for a test prefix your configuration’s path with fs://

test_command()

This method is for you to fill out to test your command

Note

The more tests the better! Make sure to add as many tests as you need to ensure your automation is always successful

test_configuration()

Configuration Test

This method tests your configuration and validates that detection will return as you expect