DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Testing System Event Processing for ECS Instances

Learn how to use OpenAPIs to make testing of ECS instance system events easier.

Leona Zhang user avatar by
Leona Zhang
·
Oct. 21, 18 · Tutorial
Like (2)
Save
Tweet
Share
4.48K Views

Join the DZone community and get the full member experience.

Join For Free

ECS instance system events are scheduled or unexpected events that affect the running status of an instance. The best approach to ensure stable running of services on Elastic Compute Service (ECS) instances is to automatically process the events through a program. The problem, however, is that the program can be difficult to test. System events are generated in specific scenarios and therefore may fail to be manually triggered. This makes testing the system event processing program quite tricky. ECS instances can remain stable and run for months without any errors. We need to devise a new way to test the system event processing program.

ECS OpenAPIs for Convenient Testing

To test the system event processing program, ECS provides the OpenAPIs CreateSimulatedSystemEvents and CancelSimulatedSystemEvents to create and cancel simulated system events.

What are simulated system events? Simulated system events are specifically created for testing the system event processing program. After configuring a simulated system event, you can view the data of the event (which is just the same as that of a real event) through various event consumption channels such as open APIs, consoles, and CloudMonitor.

Apart from generating event data, a simulated event also simulates changes throughout the lifecycle.

  1. After a simulated event is configured, it is in the Scheduled state.
  2. When the scheduled time specified by NotBefore arrives, the state of the event first changes to Executing, and then quickly changes to Executed.
  3. Simulated events also respond to users' operations. Take the SystemMaintenance.Reboot event as an example. If a user restarts the instance before the scheduled time specified by NotBefore, the state of the event changes to Avoided.
  4. If a user calls the CancelSimulatedSystemEvents API before an event is completed, the state of the event changes to Canceled.

Simulated System Event Lifecycle

The following figure shows the lifecycle of a simulated event.

Image title

Sample Code

The following describes how to create two simulated SystemMaintenance.Reboot events and then cancel one of them.

The output of code execution is as follows:

#  coding=utf-8

# make sure the sdk version is 4.10.0 or upper, you can use command 'pip show aliyun-python-sdk-ecs' to check
# if the python sdk is not installed, use 'sudo pip install aliyun-python-sdk-ecs'
# if the python sdk is installed, use 'sudo pip install --upgrade aliyun-python-sdk-ecs'

import json
import logging

from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526.CancelSimulatedSystemEventsRequest import CancelSimulatedSystemEventsRequest
from aliyunsdkecs.request.v20140526.CreateSimulatedSystemEventsRequest import CreateSimulatedSystemEventsRequest

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S')

# your access key Id
ak_id = "YOU_ACCESS_KEY_ID"
# your access key secret
ak_secret = "YOU_ACCESS_SECRET"
region_id = "cn-beijing"

client = client.AcsClient(ak_id, ak_secret, region_id)


# send open api request
def _send_request(request):
    request.set_accept_format('json')
    try:
        response_str = client.do_action_with_exception(request)
        logging.info(response_str)
        response_detail = json.loads(response_str)
        return response_detail
    except Exception as e:
        logging.error(e)


def build_create_request(event_type, not_before, instance_ids):
    request = CreateSimulatedSystemEventsRequest()
    request.set_EventType(event_type)
    request.set_NotBefore(not_before)
    request.set_InstanceIds(instance_ids)
    return request


def print_created_event_id(response):
    error_code = response.get('Code')
    if error_code is None:
        event_id_list = response.get('EventIdSet').get('EventId')
        print("Created %s simulated events: %s", len(event_id_list), event_id_list)
    else:
        print("Creating failed, error code: %s", error_code)


def get_created_event_id(response):
    error_code = response.get('Code')
    if error_code is None:
        event_id_list = response.get('EventIdSet').get('EventId')
        return event_id_list
    else:
        return []


def build_cancel_request(event_id):
    request = CancelSimulatedSystemEventsRequest()
    request.set_EventIds([event_id])
    return request


if __name__ == '__main__':
    request = build_create_request("SystemMaintenance.Reboot", "2018-09-01T00:00:00Z", ["i-2zegswzznxbp168sc5c9", "i-2zeimxypwhnj04sbgf5t"])
    response = _send_request(request)
    event_ids = get_created_event_id(response)
    if event_ids:
        print("Created %s simulated events: %s"%(len(event_ids), event_ids))
        cancel_event_id = event_ids[0]
        print("Now we cancel one event %s" % (cancel_event_id))
        cancel_request = build_cancel_request(cancel_event_id)
        cancel_response = _send_request(cancel_request)
        if not cancel_response.get('Code'):
            print("Cancel succeeded")

Output after code execution:

Created 2 simulated events: [u'e-2zec65b85gi9zwcv1kpz', u'e-2zec65b85gi9zwcv1kq0']
Wed, 22 Aug 2018 18:39:49 simulate_system_event.py[line:35] INFO {"EventIdSet":{"EventId":["e-2zec65b85gi9zwcv1kpz","e-2zec65b85gi9zwcv1kq0"]},"RequestId":"C1762464-CCC2-46EC-B233-92A4D9C1782C"}
Now we cancel one event e-2zec65b85gi9zwcv1kpz
Cancel succeeded
Wed, 22 Aug 2018 18:39:49 simulate_system_event.py[line:35] INFO {"RequestId":"44286901-1BC3-4BA0-AAAF-C3CF20578E0F"}

Constraints and Cautions

  1. Users can only create and cancel simulated system events on their own instances.
  2. One user can create a maximum of 1000 simulated system events in the Scheduled state.
Event Entity component system Processing

Published at DZone with permission of Leona Zhang. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Observability Is Redefining Developer Roles
  • What Is a Kubernetes CI/CD Pipeline?
  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL
  • 7 Awesome Libraries for Java Unit and Integration Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: