StackStorm / st2

StackStorm (aka "IFTTT for Ops") is event-driven automation for auto-remediation, incident responses, troubleshooting, deployments, and more for DevOps and SREs. Includes rules engine, workflow, 160 integration packs with 6000+ actions (see https://exchange.stackstorm.org) and ChatOps. Installer at https://docs.stackstorm.com/install/index.html
https://stackstorm.com/
Apache License 2.0
5.96k stars 744 forks source link

st2-run-pack-tests not setting environment variables for ST2 API #5270

Open tonybaloney opened 3 years ago

tonybaloney commented 3 years ago

I'm trying to write some tests for the msexchange pack and hitting some issues with the BaseActionTestCase because the pack uses the st2 client to read and write cache entries...

from collections import namedtuple
import os

from st2common.runners.base_action import Action
from st2client.client import Client
from st2client.models import KeyValuePair
from exchangelib import Account, Credentials, Configuration, DELEGATE, EWSTimeZone

CacheEntry = namedtuple('CacheEntry', 'ews_url ews_auth_type primary_smtp_address')

class BaseExchangeAction(Action):
    def __init__(self, config):
        super(BaseExchangeAction, self).__init__(config)
        api_url = os.environ.get('ST2_ACTION_API_URL', None)
        token = os.environ.get('ST2_ACTION_AUTH_TOKEN', None)
        self.client = Client(api_url=api_url, token=token)

On both CircleCI for the pack and when I test using the Vagrant image, because the ST2_ACTION_AUTH_TOKEN is not set on the runner. What is the correct way to get around this?

======================================================================
5) ERROR: test_run_get_test_folder (search_items_test_case.SearchItemsActionTestCase)
----------------------------------------------------------------------
   Traceback (most recent call last):
    tests/search_items_test_case.py line 9 in test_run_get_test_folder
      result = self.get_action_instance(config=self._test_config).run(
    /opt/stackstorm/st2/lib/python3.6/site-packages/st2tests/actions.py line 48 in get_action_instance
      action_service=self.action_service)
    /opt/stackstorm/st2/lib/python3.6/site-packages/st2common/runners/utils.py line 143 in get_action_class_instance
      action_instance = action_cls(**kwargs)
    actions/base/action.py line 27 in __init__
      cache = self._get_cache()
    actions/base/action.py line 70 in _get_cache
      name='exchange_ews_url')
    /opt/stackstorm/st2/lib/python3.6/site-packages/st2client/models/core.py line 42 in decorate
      return func(*args, **kwargs)
    /opt/stackstorm/st2/lib/python3.6/site-packages/st2client/models/core.py line 294 in get_by_name
      instances = self.query(name=name, **kwargs)
    /opt/stackstorm/st2/lib/python3.6/site-packages/st2client/models/core.py line 42 in decorate
      return func(*args, **kwargs)
    /opt/stackstorm/st2/lib/python3.6/site-packages/st2client/models/core.py line 281 in query
      instances, _ = self._query_details(**kwargs)
    /opt/stackstorm/st2/lib/python3.6/site-packages/st2client/models/core.py line 274 in _query_details
      self.handle_error(response)
    /opt/stackstorm/st2/lib/python3.6/site-packages/st2client/models/core.py line 168 in handle_error
      response.raise_for_status()
    /opt/stackstorm/st2/lib/python3.6/site-packages/requests/models.py line 941 in raise_for_status
      raise HTTPError(http_error_msg, response=self)
   HTTPError: 401 Client Error: Unauthorized
   MESSAGE: Unauthorized - One of Token or API key required. for url: http://127.0.0.1:9101/v1/keys/?name=exchange_ews_url
   -------------------- >> begin captured logging << --------------------
   st2.st2common.runners.utils: DEBUG: Action class (ABCMeta) constructor doesn't take "action_service" argument, falling back to late assignment...
   urllib3.connectionpool: DEBUG: Starting new HTTP connection (1): 127.0.0.1:9101
   urllib3.connectionpool: DEBUG: http://127.0.0.1:9101 "GET /v1/keys/?name=exchange_ews_url HTTP/1.1" 401 73
   --------------------- >> end captured logging << ---------------------
tonybaloney commented 3 years ago

@Kami or @armab do you know the answer to this?

Kami commented 3 years ago

IIRC, ST2_ACTION_AUTH_TOKEN was never set for the unit tests and they way to test client / datastore functionality is to use mock datastore client (st2tests.mocks.datastore.MockDatastoreService).

That method only mocks datastore related API client methods though.

If you need to access other client functionality, we should probably also provide mock version of the Client class - right now we don't have a good set up for running pack tests end to end so we rely on unit tests + mocked methods (providing end to end test environment wouldn't be so trivial, would be slow and require quite some set up time).

So right now, at least for unit tests probably best option is to mock the Client methods you need to use.

Kami commented 3 years ago

Update - I had a look at https://github.com/StackStorm-Exchange/stackstorm-msexchange/pull/16.

It appears client is only used to manage datastore items - in this case it probably shouldn't be too hard for us to provide initial MockClient version which offers mocked methods for handling that since we already have MockDatastoreService abstraction.

Having said that, I noticed that pack code uses client directly.

Is there a specific reason why it uses that and not self.datastore_service which also allows datastore items management and we also have MockDatastoreService version of it for unit tests (https://github.com/StackStorm/st2/blob/master/st2tests/st2tests/mocks/datastore.py#L28).

If there is no specific need to use Client and DatastoreService can be used instead, this should likely do the trick for tests.

stale[bot] commented 2 years ago

Thanks for contributing to this issue. As it has been 90 days since the last activity, we are automatically marking is as stale. If this issue is not relevant or applicable anymore (problem has been fixed in a new version or similar), please close the issue or let us know so we can close it. On the contrary, if the issue is still relevant, there is nothing you need to do, but if you have any additional details or context which would help us when working on this issue, please include it as a comment to this issue.