Beispiel #1
0
    def test_tasks(self):
        playbook = os.path.expanduser(
            '~/ansible-ws/tests/data/playbooks/tags.yml')
        parameters = {
            'playbook': playbook,
        }
        request = tests.get_sw2_request('tasks', parameters, cache='refresh')
        #         pprint.pprint(request)
        sw2 = ScriptWebServiceWrapper(request, self.config)
        response = sw2.get_result()
        #         pprint.pprint(response)
        expected = [{
            'name': value,
            'value': value
        } for value in [
            'task11',
            'task12 with long name',
            'task13',
            'debug',
            'task21',
            'task22',
        ]]
        self.assertEqual(response['results'], expected)

        ### test with cache
        request['sw2'].pop('cache')
        sw2 = ScriptWebServiceWrapper(request, self.config)
        response = sw2.get_result()
        self.assertEqual(response['results'], expected)
Beispiel #2
0
    def test_sw2(self):
        id = 'ansible-ws-unittest'

        # KILL
        parameters = {
            'id': id,
        }
        request = tests.get_sw2_request('SSHAgentKill', parameters)
        #         pprint.pprint(request)
        sw2 = ScriptWebServiceWrapper(request, self.config)
        response = sw2.get_result()
        #         pprint.pprint(response)
        self.assertEqual(response['results']['keys'], None)
        self.assertEqual(response['results']['action'], 'kill')
        self.assertEqual(response['results']['rc'], 0)

        # INFO
        parameters = {
            'id': id,
        }
        request = tests.get_sw2_request('SSHAgent', parameters)
        #         pprint.pprint(request)
        sw2 = ScriptWebServiceWrapper(request, self.config)
        response = sw2.get_result()
        #         pprint.pprint(response)
        self.assertEqual(response['results']['keys'], [])
        self.assertEqual(response['results']['action'], 'init')

        # ADD
        parameters = {
            'id': id,
            'private_key': self.key1,
            'passphrase': 'key1',
        }
        request = tests.get_sw2_request('SSHAgentAdd', parameters)
        #         pprint.pprint(request)
        sw2 = ScriptWebServiceWrapper(request, self.config)
        response = sw2.get_result()
        #         pprint.pprint(response)
        self.assertEqual(response['results']['action'], 'add')
        self.assertTrue(response['results']['keys'][0].startswith('ssh-rsa '))

        # KILL
        parameters = {
            'id': id,
        }
        request = tests.get_sw2_request('SSHAgentKill', parameters)
        #         pprint.pprint(request)
        sw2 = ScriptWebServiceWrapper(request, self.config)
        response = sw2.get_result()
        #         pprint.pprint(response)
        self.assertEqual(response['results']['keys'], None)
        self.assertEqual(response['results']['action'], 'kill')
        self.assertEqual(response['results']['rc'], 0)
Beispiel #3
0
 def test_groups_list(self):
     parameters = {
         'pattern': '(database_app1_dev|database_app2_prod)',
         'groups_selection': 'yes',
         'sources': self.sources
     }
     request = tests.get_sw2_request(self.query, parameters)
     #         pprint.pprint(request)
     sw2 = ScriptWebServiceWrapper(request, self.config)
     response = sw2.get_result()
     #         pprint.pprint(response)
     values = [option['value'] for option in response['results']]
     expected = [
         'database_app1_dev',
         'server_dev_x1',
         'server_dev_x2',
         'database_app2_prod',
         'db_prod_21',
         'db_prod_22',
         'db_prod_23',
     ]
     #         pprint.pprint(values)
     self.assertEqual(values, expected)
     self.assertEqual(response['results'][0]['disabled'], False)
     self.assertEqual(response['results'][3]['disabled'], False)
Beispiel #4
0
 def test_demo(self):
     parameters = {'demo1': 'test', 'demo2': ['foo', 'bar']}
     request = tests.get_sw2_request('demo', parameters)
     #         pprint.pprint(request)
     sw2 = ScriptWebServiceWrapper(request, self.config)
     response = sw2.get_result()
     #         pprint.pprint(response)
     self.assertEqual(response['results'][3]['name'], 'test')
     self.assertEqual(response['results'][4]['name'], 'foo')
Beispiel #5
0
 def test_default(self):
     parameters = {
         'pattern': 'all',
     }
     request = tests.get_sw2_request(self.query, parameters)
     #         pprint.pprint(request)
     sw2 = ScriptWebServiceWrapper(request, self.config)
     response = sw2.get_result()
     #         pprint.pprint(response)
     self.assertIsInstance(response['results'], list)
Beispiel #6
0
 def test_search_basic(self):
     parameters = {
         'from': 'January 1, 2019',
     }
     request = tests.get_sw2_request('runs', parameters)
     #         pprint.pprint(request)
     sw2 = ScriptWebServiceWrapper(request, self.ansible_ws_config)
     response = sw2.get_result()
     #         pprint.pprint(response)
     #         print(len(response['results']))
     self.assertEqual(len(response['results']), 6)
Beispiel #7
0
    def test_default(self):
        parameters = {
            'group': 'foo',
            'key': 'foo.barr',
        }
        request = tests.get_sw2_request(self.query, parameters)
#         pprint.pprint(request)
        sw2 = ScriptWebServiceWrapper(request, self.config)
        response = sw2.get_result()
#         pprint.pprint(response)
        expected = []
        self.assertEqual(response['results'], expected)
Beispiel #8
0
 def test_no_tags(self):
     playbook = os.path.expanduser(
         '~/ansible-ws/tests/data/playbooks/notags.yml')
     parameters = {
         'playbook': playbook,
     }
     request = tests.get_sw2_request('tags', parameters)
     #         pprint.pprint(request)
     sw2 = ScriptWebServiceWrapper(request, self.config)
     response = sw2.get_result()
     #         pprint.pprint(response)
     expected = []
     self.assertEqual(response['results'], expected)
Beispiel #9
0
 def test_search_state(self):
     parameters = {'from': 'January 1, 2019', 'states': 'failed'}
     request = tests.get_sw2_request('runs', parameters)
     #         pprint.pprint(request)
     sw2 = ScriptWebServiceWrapper(request, self.ansible_ws_config)
     response = sw2.get_result()
     #         pprint.pprint(response)
     #         print(len(response['results']))
     self.assertEqual(response['results'][0]['runid'],
                      'f14529a2-cd83-4d2c-b885-a6184b83f7bc')
     self.assertEqual(response['results'][1]['runid'],
                      'f61aef56-9700-48a8-ab1e-f049df76ec0b')
     self.assertEqual(len(response['results']), 2)
Beispiel #10
0
 def test_grapher(self):
     grapher_output = '/tmp'
     self.config.config['grapher']['output'] = grapher_output
     expected_file = '/tmp/db_prod_11.png'
     if os.path.isfile(expected_file):
         os.remove(expected_file)
     parameters = {'host': 'db_prod_11', 'inventory': self.inventory}
     request = tests.get_sw2_request(self.query, parameters)
     #         pprint.pprint(request)
     sw2 = ScriptWebServiceWrapper(request, self.config)
     response = sw2.get_result()
     #         pprint.pprint(response)
     self.assertTrue(os.path.isfile(expected_file))
     os.remove(expected_file)
Beispiel #11
0
    def test_sw2(self):
        parameters = {
            'cmdline': f'ansible-playbook  {self.playbook} -v',
            'playbook': self.playbook,
        }
        request = tests.get_sw2_request('launch', parameters)
#         pprint.pprint(request)
        sw2 = ScriptWebServiceWrapper(request, self.config)
        response = sw2.get_result()
#         pprint.pprint(response)
        results = response['results']
#         pprint.pprint(results)
        self.assertIsNone(results['return_code'])
        self.assertEqual(results['state'], PlaybookContext.STATE_RUNNING)
        self.assertEqual(results['status'], PlaybookContext.STATUS_READY)
        runid = results['runid']

        parameters = {
            'runid': runid
        }
        request = tests.get_sw2_request('run', parameters)
#         pprint.pprint(request)
        sw2 = ScriptWebServiceWrapper(request, self.config)
        response = sw2.get_result()
#         pprint.pprint(response)
        status = response['results']['status']
#         pprint.pprint(status)
        self.assertEqual(status['runid'], runid)
        while status['status'] != PlaybookContext.STATUS_FINISHED:
            time.sleep(5)
            sw2 = ScriptWebServiceWrapper(request, self.config)
            response = sw2.get_result()
#             pprint.pprint(response)
            status = response['results']['status']
        self.assertEqual(status['return_code'], 0)
        self.assertEqual(status['state'], PlaybookContext.STATE_SUCCEEDED)
        self.assertEqual(status['status'], PlaybookContext.STATUS_FINISHED)
Beispiel #12
0
    def test_dict(self):
        parameters = {
            'group': 'database_app1_prod',
            'key': 'countries.dict',
            'inventories': self.inventories
        }
        request = tests.get_sw2_request(self.query, parameters)
#         pprint.pprint(request)
        sw2 = ScriptWebServiceWrapper(request, self.config)
        response = sw2.get_result()
#         pprint.pprint(response)
        expected = [
            {'name': value, 'value': value}
            for value in ['es', 'fr', 'it']
        ]
        self.assertEqual(response['results'], expected)
Beispiel #13
0
def application(environ, start_response):

    try:
        method = environ['REQUEST_METHOD']
        content_type = environ.get('CONTENT_TYPE')
        if content_type in (wsgi_utils.CONTENT_TYPE_JSON, None):
            if method == 'GET':
                raw_qs = environ.get('QUERY_STRING')
                if raw_qs == '':
                    raw_qs = '{}'
                qs = urllib.parse.unquote_plus(raw_qs, encoding='utf-8')
                parameters = json.loads(qs)
            elif method == 'POST':
                request_body_size = int(environ.get('CONTENT_LENGTH', 0))
                request_body = environ['wsgi.input'].read(request_body_size)
                parameters = json.loads(request_body)
            service = ScriptWebServiceWrapper(parameters, config)
            response = service.get_result()
            if content_type is None:
                response['warnings'] = [
                    f'No content-type found in request. Assume content-type was {wsgi_utils.CONTENT_TYPE_JSON}'
                ]
            if service.is_valid():
                status = wsgi_utils.HTTP_200
            else:
                status = wsgi_utils.HTTP_400
        else:
            status = wsgi_utils.HTTP_415
            response = {
                'errors': [
                    f'Unexpected content-type, {content_type} found instead {wsgi_utils.CONTENT_TYPE_JSON}'
                ]
            }
        response_headers, output = wsgi_utils.get_json_response(response)
    except:
        status, response_headers, output = wsgi_utils.get_500_response()
    start_response(status, response_headers)
    return [output]
Beispiel #14
0
 def test(self):
     sw2 = ScriptWebServiceWrapper({}, self.config)
     response = sw2.get_result()
     #         pprint.pprint(response)
     self.assertIsInstance(response['errors'], list)
     self.assertIsInstance(response['errors'][0], str)