You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficserver.apache.org by ja...@apache.org on 2015/02/10 22:55:13 UTC

[4/5] trafficserver-qa git commit: Add examples to document various test classes

Add examples to document various test classes


Project: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/commit/80581888
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/tree/80581888
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/diff/80581888

Branch: refs/heads/master
Commit: 80581888dc5b94dd50f442d983202e0d13d54ccc
Parents: c5b0498
Author: Thomas Jackson <ja...@gmail.com>
Authored: Fri Feb 6 17:03:15 2015 -0800
Committer: Thomas Jackson <ja...@gmail.com>
Committed: Tue Feb 10 13:54:08 2015 -0800

----------------------------------------------------------------------
 examples/README.rst             | 19 +++++++++
 examples/endpoint_cases.py      | 56 ++++++++++++++++++++++++++
 examples/environment_cases.py   | 78 ++++++++++++++++++++++++++++++++++++
 examples/environment_helpers.py | 71 ++++++++++++++++++++++++++++++++
 examples/skip_examples.py       | 38 ++++++++++++++++++
 tsqa/test_cases.py              |  7 +++-
 6 files changed, 268 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/80581888/examples/README.rst
----------------------------------------------------------------------
diff --git a/examples/README.rst b/examples/README.rst
new file mode 100644
index 0000000..2c673bd
--- /dev/null
+++ b/examples/README.rst
@@ -0,0 +1,19 @@
+.. Licensed to the Apache Software Foundation (ASF) under one
+   or more contributor license agreements.  See the NOTICE file
+   distributed with this work for additional information
+   regarding copyright ownership.  The ASF licenses this file
+   to you under the Apache License, Version 2.0 (the
+   "License"); you may not use this file except in compliance
+   with the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing,
+   software distributed under the License is distributed on an
+   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+   KIND, either express or implied.  See the License for the
+   specific language governing permissions and limitations
+   under the License.
+
+This directory is for examples to document how to use TSQA. There is no gaurantee
+(or even intent) that these tests work or test anything.

http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/80581888/examples/endpoint_cases.py
----------------------------------------------------------------------
diff --git a/examples/endpoint_cases.py b/examples/endpoint_cases.py
new file mode 100644
index 0000000..66ca6a6
--- /dev/null
+++ b/examples/endpoint_cases.py
@@ -0,0 +1,56 @@
+'''
+Examples of how to use DynamicHTTPEndpointCase
+'''
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+
+class TestDynamicHTTPEndpointCase(tsqa.test_cases.DynamicHTTPEndpointCase):
+    '''
+    DynamicHTTPEndpointCase will set up an instance attribute of "http_endpoint"
+    as well as a method (endpoint_url) to generate http request strings from paths.
+    '''
+    def test_base(self):
+        '''
+        By default there are no handlers registered to the server thread, so
+        all requests will 404
+        '''
+        ret = requests.get(self.endpoint_url('/footest'))
+        self.assertEqual(ret.status_code, 404)
+
+    def test_endpoint_url(self):
+        '''
+        self.endpoint_url is useful for converting paths into http request strings
+        '''
+        self.assertEqual(self.endpoint_url(), 'http://127.0.0.1:{0}'.format(self.http_endpoint.address[1]))
+
+        self.assertEqual(self.endpoint_url('/foo'), 'http://127.0.0.1:{0}/foo'.format(self.http_endpoint.address[1]))
+
+    def test_with_endpoint():
+        '''
+        You can register custom handlers to the http_endpoint
+        '''
+        # create a function which will take the Flask request object (http://werkzeug.pocoo.org/docs/0.10/wrappers/#werkzeug.wrappers.Request)
+        # and return a valid return for Flask (http://flask.pocoo.org/docs/0.10/quickstart/#about-responses)
+        def handler(request):
+            return "hello world"
+
+        # register the endpoint on a context path. Now any request to /hello will
+        # be sent to the handler function specified
+        self.http_endpoint.add_handler('/hello', handler)
+
+        ret = requests.get(self.endpoint_url('/hello'))
+        self.assertEqual(ret.text, 'hello world')

http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/80581888/examples/environment_cases.py
----------------------------------------------------------------------
diff --git a/examples/environment_cases.py b/examples/environment_cases.py
new file mode 100644
index 0000000..8f33b2a
--- /dev/null
+++ b/examples/environment_cases.py
@@ -0,0 +1,78 @@
+'''
+Examples of how to use EnvironmentCase
+'''
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+
+class HelloWorld(tsqa.test_cases.EnvironmentCase):
+    '''
+    This is the trivial example of a TestCase. The parent class (in this case
+    EnvironmentCase) is responsible for all the heavy lifting. By the time
+    test_base is called EnvironmentCase will have aquired a unique environment,
+    configured ATS, and started ATS.
+    '''
+    def test_base(self):
+        # for example, you could send a request to ATS and check the response
+        ret = requests.get('http://127.0.0.1:{0}/'.format(self.configs['records.config']['CONFIG']['proxy.config.http.server_ports']))
+
+        # you also have access to your own logger.
+        self.log('Something interesting to log')
+
+        self.assertEqual(ret.status_code, 404)
+        self.assertIn('ATS', ret.headers['server'], 'message to print on test failure')
+
+
+class OverrideConfigureFlags(tsqa.test_cases.EnvironmentCase):
+    '''
+    The default getEnv() uses EnvironmentFactory to build trafficserver from
+    source with given environment/configure options. You can override these
+    values for a test class using the attribute "environment_factory"
+    '''
+    # Override the build options for environment factory
+    environment_factory = {
+        'env': {'ENV_VAR': 'VALUE'},
+        'configure': {'enable-spdy': None},
+    }
+
+
+class ConfiguredCase(tsqa.test_cases.EnvironmentCase):
+    '''
+    This is the trivial example of a TestCase. The parent class (in this case
+    EnvironmentCase) is responsible for all the heavy lifting. By the time
+    test_base is called EnvironmentCase will have aquired a unique environment,
+    configured ATS, and started ATS.
+    '''
+    @classmethod
+    def setUpEnv(cls, env):
+        '''
+        This funciton is responsible for setting up the environment for this fixture
+        This includes everything pre-daemon start.
+
+        You are passed in cls (which is the instance of this class) and env (which
+        is an environment object)
+        '''
+        # we can modify any/all configs (note: all pre-daemon start)
+        cls.configs['remap.config'].add_line('map / http://http://trafficserver.readthedocs.org/')
+
+        # Some configs have nicer wrapper objects to give you a more pythonic interface
+        cls.configs['records.config']['CONFIG'].update({
+            'proxy.config.log.squid_log_enabled': 1,
+            'proxy.config.log.squid_log_is_ascii': 1,
+        })
+
+
+

http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/80581888/examples/environment_helpers.py
----------------------------------------------------------------------
diff --git a/examples/environment_helpers.py b/examples/environment_helpers.py
new file mode 100644
index 0000000..bb37a31
--- /dev/null
+++ b/examples/environment_helpers.py
@@ -0,0 +1,71 @@
+'''
+Environment cases will by default pull source from ~/trafficserver. For most
+applications you will want to create a sub-class of tsqa.traffic_tests.EnvironmentCase
+which will override the getEnv() method.
+'''
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+
+class HelperEnvironmentCase(tsqa.test_cases.EnvironmentCase):
+    '''
+    This is an example of how to subclass EnviromentCase and return your own
+    environments. This one is pulled from the ATS core.
+    '''
+    @classmethod
+    def getEnv(cls):
+        '''
+        This function is responsible for returning a unique environment
+        '''
+        SOURCE_DIR = os.path.realpath(os.path.join(__file__, '..', '..', '..', '..'))
+        TMP_DIR = os.path.join(tempfile.gettempdir(), 'tsqa')
+        ef = tsqa.environment.EnvironmentFactory(SOURCE_DIR,
+                                                 os.path.join(TMP_DIR, 'base_envs'),
+                                                 default_configure={'enable-example-plugins': None,
+                                                                    'enable-test-tools': None,
+                                                                    'enable-example-plugins': None,
+                                                                    },
+                                                 )
+        # anywhere in this method you can raize SkipTest, and the test will be skipped
+        # in this example we skip any exceptions in building the environment,
+        # but you can put any logic in here to determine when a test should be
+        # skipped (primarily if you can't provide the environment requested)
+        try:
+            return ef.get_environment(cls.environment_factory['configure'], cls.environment_factory['env'])
+        except Exception as e:
+            raise unittest.SkipTest(e)
+
+
+class StaticEnvironmentCase(tsqa.test_cases.EnvironmentCase):
+    '''
+    This is an example which returns a static environment for all tests to run
+    with. This is useful for testing specific builds of ATS or its plugins.
+
+    An example would be to test a new internal plugin against the current internal
+    build of ATS.
+    '''
+    @classmethod
+    def getEnv(cls):
+        '''
+        This function is responsible for returning a unique environment
+        '''
+        # create a layout from the static root we have
+        layout = tsqa.environment.Layout('static/environment/dir')
+        # create a new environment
+        env = tsqa.environment.Environment()
+        # clone the static layout we have
+        env.clone(layout=layout)
+        return env

http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/80581888/examples/skip_examples.py
----------------------------------------------------------------------
diff --git a/examples/skip_examples.py b/examples/skip_examples.py
new file mode 100644
index 0000000..d972e37
--- /dev/null
+++ b/examples/skip_examples.py
@@ -0,0 +1,38 @@
+'''
+Examples of how to skip tests
+    More examples: https://docs.python.org/2/library/unittest.html#unittest-skipping
+'''
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+
+class SkipEntireClass(helpers.EnvironmentCase):
+    @classmethod
+    def setUpClass(cls):
+        '''
+        If you'd like to skip an entire test
+        '''
+        # If you raise SkipTest in setUpClass (or within a test case) the test
+        # will be skipped. You can build logic around this to conditionally
+        # skip tests based on environment conditions.
+        raise unittest.SkipTest('Skip the entire class')
+
+
+class SkipSingleTestCase(helpers.EnvironmentCase):
+   @unittest.skip('Always skip this test with this message')
+   def test_example(self):
+        self.assertTrue(False)
+

http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/80581888/tsqa/test_cases.py
----------------------------------------------------------------------
diff --git a/tsqa/test_cases.py b/tsqa/test_cases.py
index 7b84833..494a4fe 100644
--- a/tsqa/test_cases.py
+++ b/tsqa/test_cases.py
@@ -31,7 +31,12 @@ import os
 # Base environment case
 class EnvironmentCase(unittest.TestCase):
     '''
-    This class will get an environment (which is unique)
+    This class will:
+        - get a unique environment (using getEnv())
+        - create wrappers for ATS configs available in self.configs
+        - setup the environment (setUpEnv())
+        - write out the configs
+        - start the environment (environment.start())
     '''
     # TODO: better naming??
     environment_factory = {'configure': None,