Beispiel #1
0
    def test_does_not_render_with_missing_stack_name_in_message(
            self, stack_service, mock_message):
        stack, service = stack_service
        del mock_message['data']['resource']['labels']['io.rancher.stack.name']

        access_key = os.getenv('RANCHER_ACCESS_KEY')
        secret_key = os.getenv('RANCHER_SECRET_KEY')
        api_token = b64encode("{0}:{1}".format(access_key, secret_key))
        template = os.path.join(os.path.dirname(__file__), 'fixtures',
                                'template.j2')
        config = {
            'message': mock_message,
            'host': os.getenv('RANCHER_HOST'),
            'port': int(os.getenv('RANCHER_PORT', 80)),
            'project_id': stack['accountId'],
            'api_token': api_token,
            'templates': ['{0}:{1}'.format(template, self.out_file)],
            'ssl': False,
            'stack': 'teststack',
            'services': ['badservice'],
            'notify': None
        }

        handler = MessageHandler(**config)
        handler.run()
        time.sleep(1)
        assert not os.path.exists(self.out_file)
Beispiel #2
0
    def test_get_instances(self, stack_service):
        stack, service = stack_service
        host = os.getenv('RANCHER_HOST')
        port = int(os.getenv('RANCHER_PORT', 80))
        access_key = os.getenv('RANCHER_ACCESS_KEY')
        secret_key = os.getenv('RANCHER_SECRET_KEY')
        project_id = stack['accountId']
        api_token = b64encode("{0}:{1}".format(access_key, secret_key))

        api = API(host, port, project_id, api_token, False)
        serv = api.get_service(None, stack['name'], 'hello')
        instances = api.get_instances(serv)
        assert len(instances) == 1
Beispiel #3
0
def stack_service(request):
    host = os.getenv('RANCHER_HOST')
    port = int(os.getenv('RANCHER_PORT', 80))
    access_key = os.getenv('RANCHER_ACCESS_KEY')
    secret_key = os.getenv('RANCHER_SECRET_KEY')
    api_token = b64encode("{0}:{1}".format(access_key, secret_key))

    with open(os.path.join(os.path.dirname(__file__), 'compose.yaml')) as fh:
        compose_string = fh.read()

    # Create the stack and service
    headers = {'Authorization': 'Basic {0}'.format(api_token)}
    url = 'http://{0}:{1}/v1/environments'.format(host, port)
    stack_name = 'teststack'
    res = requests.post(url,
                        headers=headers,
                        data={
                            'name': stack_name,
                            "dockerCompose": compose_string,
                            "startOnCreate": True
                        })
    stack = res.json()

    # Wait for stack to be active
    state = stack['state']
    url = '{0}?name={1}'.format(url, stack_name)
    while state != 'active':
        res = requests.get(url, headers=headers)
        stack = res.json()['data'][0]
        state = stack['state']
        time.sleep(1)

    # Wait for services to be active
    services = []
    for i in [1, 2]:
        url = '{0}?name={1}'.format(stack['links']['services'], 'hello%d' % i)
        res = requests.get(url, headers=headers)
        state = ''
        service = None
        while state != 'active':
            service = res.json()['data'][0]
            state = service['state']
            time.sleep(1)
        services.append(service)

    def teardown():
        requests.delete('{0}/{1}'.format(url, stack['id']), headers=headers)

    request.addfinalizer(teardown)

    return stack, services
    def test_does_not_render_with_invalid_filter(self, stack_service,
                                                 mock_message):
        stack, service = stack_service

        access_key = os.getenv('RANCHER_ACCESS_KEY')
        secret_key = os.getenv('RANCHER_SECRET_KEY')
        api_token = b64encode("{0}:{1}".format(access_key, secret_key))
        config = {
            'message':
            mock_message,
            'host':
            os.getenv('RANCHER_HOST'),
            'port':
            int(os.getenv('RANCHER_PORT', 80)),
            'project_id':
            None,
            'api_token':
            api_token,
            'template':
            os.path.join(os.path.dirname(__file__), 'fixtures', 'template.j2'),
            'dest':
            self.out_file,
            'ssl':
            False,
            'stack':
            'teststack',
            'service':
            'badservice',
            'notify':
            None
        }

        # Test with bad service name
        handler1 = MessageHandler(**config)
        handler1.start()
        time.sleep(1)
        assert not os.path.exists(self.out_file)

        # test with back stack name
        config['stack'] = 'bad'
        config['service'] = None

        handler2 = MessageHandler(**config)
        handler2.start()
        time.sleep(1)
        assert not os.path.exists(self.out_file)
Beispiel #5
0
    def test_get_service(self, stack_service):
        stack, services = stack_service
        host = os.getenv('RANCHER_HOST')
        port = int(os.getenv('RANCHER_PORT', 80))
        access_key = os.getenv('RANCHER_ACCESS_KEY')
        secret_key = os.getenv('RANCHER_SECRET_KEY')
        project_id = stack['accountId']
        api_token = b64encode("{0}:{1}".format(access_key, secret_key))

        api = API(host, port, project_id, api_token, False)

        # Test with stack and service name
        serv = api.get_service(None, stack['name'], "hello1")
        assert serv is not None

        # Test with bad stack name
        assert api.get_service(None, 'bad', 'name') is None

        # Test with running resource
        resource = {
            'state': 'running',
            'services': [serv]
        }
        serv = api.get_service(resource)
        assert serv is not None

        # Test with deleted resource
        resource['state'] = 'removed'
        resource['labels'] = {
            'io.rancher.stack.name': stack['name'],
            'io.rancher.stack_service.name': '{0}/{1}'
            .format(stack['name'], 'hello1')
        }
        serv = api.get_service(resource)
        assert serv is not None

        # Test with invalid resource state
        assert api.get_service({'state': 'bad'}) is None

        # Test with None values
        assert api.get_service() is None
    def test_renders_template(self, stack_service, mock_message):
        stack, service = stack_service

        access_key = os.getenv('RANCHER_ACCESS_KEY')
        secret_key = os.getenv('RANCHER_SECRET_KEY')
        api_token = b64encode("{0}:{1}".format(access_key, secret_key))
        config = {
            'message':
            mock_message,
            'host':
            os.getenv('RANCHER_HOST'),
            'port':
            int(os.getenv('RANCHER_PORT', 80)),
            'project_id':
            None,
            'api_token':
            api_token,
            'template':
            os.path.join(os.path.dirname(__file__), 'fixtures', 'template.j2'),
            'dest':
            self.out_file,
            'ssl':
            False,
            'stack':
            'teststack',
            'service':
            'hello',
            'notify':
            None
        }

        # Test with stack and service filter
        handler = MessageHandler(**config)
        handler.run()

        while not os.path.exists(self.out_file):
            time.sleep(1)

        with open(self.out_file) as fh:
            output = fh.read().replace('\n', '').strip()

        assert output == '10.42.232.33;'

        # Test with stack only filter
        config['project_id'] = stack['accountId']
        config['service'] = None
        handler = MessageHandler(**config)
        handler.run()

        while not os.path.exists(self.out_file):
            time.sleep(1)

        with open(self.out_file) as fh:
            output = fh.read().replace('\n', '').strip()

        assert output == '10.42.232.33;'

        # Test without filter
        config['stack'] = None
        handler = MessageHandler(**config)
        handler.run()

        while not os.path.exists(self.out_file):
            time.sleep(1)

        with open(self.out_file) as fh:
            output = fh.read().replace('\n', '').strip()

        assert '10.42.232.33' in output