Beispiel #1
0
    def test_filter_nodes_env(self):
        """Should filter nodes belonging to a given environment"""
        data = chef.filter_nodes(chef.get_nodes_extended(), 'production')
        self.assertEqual(len(data), 8)

        data = chef.filter_nodes(chef.get_nodes_extended(), 'staging')
        self.assertEqual(len(data), 1)

        data = chef.filter_nodes(chef.get_nodes_extended(), 'non_existing_env')
        self.assertEqual(len(data), 0)
Beispiel #2
0
    def test_filter_nodes_virt(self):
        """Should filter nodes acording to their virt value"""
        total_guests = 7
        total_hosts = 3
        data = chef.filter_nodes(chef.get_nodes_extended(), virt_roles='guest')
        self.assertEqual(len(data), total_guests)

        data = chef.filter_nodes(chef.get_nodes_extended(), virt_roles='host')
        self.assertEqual(len(data), total_hosts)

        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 virt_roles='host,guest')
        self.assertEqual(len(data), TOTAL_NODES)
Beispiel #3
0
    def test_filter_nodes_combined(self):
        """Should filter nodes acording to their virt value"""
        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 env='production',
                                 roles='loadbalancer,webserver',
                                 virt_roles='guest')
        self.assertEqual(len(data), 3)
        self.assertEqual(data[0]['name'], "testnode1")
        self.assertEqual(data[1]['name'], "testnode2")
        self.assertEqual(data[2]['name'], "testnode7")

        data = chef.filter_nodes(chef.get_nodes_extended(), env='staging',
                                 roles='webserver', virt_roles='guest')
        self.assertEqual(len(data), 1)
        self.assertEqual(data[0]['name'], "testnode4")
Beispiel #4
0
def _get_data(request, env, roles, virt, group_by_host=False):
    """Returns processed repository data, filtering nodes based on given args
    """
    roles = [role for role in roles.split(',') if role]
    data = {
        'filter_env': env, 'filter_roles': roles, 'filter_virt': virt,
        'show_list': SHOW_LIST_VIEW, 'show_virt': SHOW_VIRT_VIEW,
        'show_graph': SHOW_GRAPH_VIEW, 'show_links': SHOW_LINKS,
        'query_string': request.META['QUERY_STRING']
    }
    data['roles'] = get_roles()
    roles_groups = get_role_groups(data['roles'])
    data['roles_groups'] = roles_groups
    data['virt_roles'] = ['host', 'guest']
    # Get environments before we filter nodes
    data['nodes'] = get_nodes()
    data['nodes_extended'] = get_nodes_extended(data['nodes'])
    data['environments'] = get_environments(data['nodes_extended'])
    if group_by_host:
        data['nodes_extended'] = group_nodes_by_host(data['nodes_extended'],
                                                     roles=data['filter_roles'],
                                                     env=data['filter_env'])
    elif data['filter_env'] or data['filter_roles'] or data['filter_virt']:
        data['nodes_extended'] = filter_nodes(data['nodes_extended'],
                                              data['filter_env'],
                                              data['filter_roles'],
                                              data['filter_virt'])
    inject_plugin_data(data['nodes_extended'])
    if not data['nodes_extended']:
        add_message(request, WARNING,
                    "There are no nodes that fit the supplied criteria.")
    return data
Beispiel #5
0
 def test_build_links_all(self):
     """Should generate all links when nodes define connections"""
     data = chef.filter_nodes(self.nodes, 'production', virt_roles='guest')
     links = graphs._build_links(data)
     expected = {
         'testnode1': {'role_prefix': 'loadbalancer'},
         'testnode2': {
             'role_prefix': 'webserver',
             'client_nodes': [('testnode1', 'apache2')]
         },
         'testnode3.mydomain.com': {
             'role_prefix': 'dbserver',
             'client_nodes': [
                 ('testnode2', 'mysql'), ('testnode7', 'mysql')
             ]
         },
         'testnode7': {
             'role_prefix': 'webserver',
             'client_nodes': [('testnode1', 'apache2')]
         },
         'testnode8': {
             'role_prefix': 'worker',
             'needs_nodes': [
                 ('testnode3.mydomain.com', 'mysql')
             ]
         }
     }
     self.assertEqual(links, expected)
Beispiel #6
0
def _get_data(request, env, roles, virt, group_by_host=False):
    """Returns processed repository data, filtering nodes based on given args
    """
    data = {'filter_env': env, 'filter_roles': roles, 'filter_virt': virt}
    data['roles'] = get_roles()
    roles_groups = get_role_groups(data['roles'])
    data['roles_groups'] = roles_groups
    data['virt_roles'] = ['host', 'guest']
    # Get environments before we filter nodes
    data['nodes'] = get_nodes()
    data['nodes_extended'] = get_nodes_extended(data['nodes'])
    data['environments'] = get_environments(data['nodes_extended'])
    roles_to_filter = '' if group_by_host else data['filter_roles']
    if data['filter_env'] or roles_to_filter or data['filter_virt']:
        data['nodes_extended'] = filter_nodes(data['nodes_extended'],
                                              data['filter_env'],
                                              roles_to_filter,
                                              data['filter_virt'])
    if group_by_host:
        data['nodes_extended'] = group_nodes_by_host(
            data['nodes_extended'], roles=data['filter_roles'])
    inject_plugin_data(data['nodes_extended'])
    if not data['nodes_extended']:
        add_message(request, WARNING,
                    "There are no nodes that fit the supplied criteria.")
    return data
Beispiel #7
0
 def test_build_links_all(self):
     """Should generate all links when nodes define connections"""
     data = chef.filter_nodes(
         self.nodes, 'production')
     links = graphs._build_links(data)
     expected = {
         'testnode2': {'client_nodes': [('testnode1', 'apache2')]},
         'testnode3.mydomain.com': {
             'client_nodes': [
                 ('testnode2', 'mysql'), ('testnode7', 'mysql')
             ]
         },
         'testnode5': {
             'client_nodes': [
                 ('testnode2', 'mysql'), ('testnode7', 'mysql')
             ]
         },
         'testnode7': {'client_nodes': [('testnode1', 'apache2')]},
         'testnode8': {
             'needs_nodes': [
                 ('testnode3.mydomain.com', 'mysql'), ('testnode5', 'mysql')
             ]
         }
     }
     self.assertEqual(links, expected)
Beispiel #8
0
 def test_generate_empty_graph(self):
     """Should generate an empty graph when no nodes are given"""
     data = chef.filter_nodes(self.nodes, "badenv")
     graphs.generate_node_map(data, self.roles)
     self.assertTrue(os.path.exists(self.filepath))
     size = os.path.getsize(self.filepath)
     max_size = 650
     self.assertTrue(size < max_size, "Size greater than {0}: {1}".format(max_size, size))
Beispiel #9
0
 def test_build_links_needs_nodes(self):
     """Should generate links when nodes have needs_nodes set"""
     data = chef.filter_nodes(self.nodes, "production", "dbserver,worker", "guest")
     links = graphs._build_links(data)
     expected = {
         "testnode3.mydomain.com": {"role_prefix": "dbserver"},
         "testnode8": {"role_prefix": "worker", "needs_nodes": [("testnode3.mydomain.com", "mysql")]},
     }
     self.assertEqual(links, expected)
Beispiel #10
0
 def test_get_role_relations(self):
     """Should return role dependencies when roles with relationships are given"""
     prod_nodes = chef.filter_nodes(self.nodes, "production")
     extra_roles = graphs.get_role_relations("production", "dbserver", prod_nodes)
     self.assertEqual(extra_roles, ["webserver", "worker"])
     extra_roles = graphs.get_role_relations("production", "loadbalancer", prod_nodes)
     self.assertEqual(extra_roles, ["webserver"])
     extra_roles = graphs.get_role_relations("production", "worker", prod_nodes)
     self.assertEqual(extra_roles, ["dbserver"])
     extra_roles = graphs.get_role_relations("production", "webserver", prod_nodes)
     self.assertEqual(extra_roles, ["dbserver", "loadbalancer"])
Beispiel #11
0
def get_nodes(request):
    """Returns node files. If 'extended' is given, the extended version is
    returned

    """
    if request.GET.get('extended'):
        data = chef.get_nodes_extended()
    else:
        data = chef.get_nodes()
    data = chef.filter_nodes(data, request.GET.get('env'))
    return HttpResponse(json.dumps(data), content_type="application/json")
Beispiel #12
0
    def test_graph_timeout(self):
        """Should display an error message when GraphViz excesses the timeout
        """
        error_msg = "Unable to draw graph, timeout exceeded"
        data = chef.filter_nodes(self.nodes, "production")

        with patch("kitchen.dashboard.graphs.GraphThread.isAlive", return_value=True):
            with patch("kitchen.dashboard.graphs.GraphThread.kill", return_value=True):
                success, msg = graphs.generate_node_map(data, self.roles)
        self.assertFalse(success)
        self.assertTrue(error_msg in msg)
Beispiel #13
0
    def test_filter_nodes_roles(self):
        """Should filter nodes acording to their virt value"""
        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 roles='dbserver')
        self.assertEqual(len(data), 2)
        self.assertEqual(data[0]['name'], "testnode3.mydomain.com")

        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 roles='loadbalancer')
        self.assertEqual(len(data), 1)
        self.assertEqual(data[0]['name'], "testnode1")

        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 roles='webserver')
        self.assertEqual(len(data), 4)
        self.assertEqual(data[0]['name'], "testnode2")

        data = chef.filter_nodes(chef.get_nodes_extended(),
                                 roles='webserver,dbserver')
        self.assertEqual(len(data), 6)
        self.assertEqual(data[1]['name'], "testnode3.mydomain.com")
Beispiel #14
0
 def test_build_links_needs_nodes(self):
     """Should generate links when nodes have needs_nodes set"""
     data = chef.filter_nodes(
         self.nodes, 'production', 'dbserver,worker')
     links = graphs._build_links(data)
     expected = {
         'testnode8': {
             'needs_nodes': [
                 ('testnode3.mydomain.com', 'mysql'), ('testnode5', 'mysql')
             ]
         }
     }
     self.assertEqual(links, expected)
Beispiel #15
0
 def test_generate_small_graph(self):
     """Should generate a graph when some nodes are given"""
     data = chef.filter_nodes(self.nodes, 'staging', virt_roles='guest')
     graphs.generate_node_map(data, self.roles)
     self.assertTrue(os.path.exists(self.filepath))
     size = os.path.getsize(self.filepath)
     #min_size = 3000  # png
     #max_size = 4000  # png
     min_size = 1000  # svg
     max_size = 1500  # svg
     self.assertTrue(size > min_size and size < max_size,
                     "Size not between {0} and {1}: {2}".format(
                         min_size, max_size, size))
Beispiel #16
0
 def test_generate_connected_graph(self):
     """Should generate a connected graph when connected nodes are given"""
     data = chef.filter_nodes(self.nodes, 'production', virt_roles='guest')
     graphs.generate_node_map(data, self.roles)
     self.assertTrue(os.path.exists(self.filepath))
     size = os.path.getsize(self.filepath)
     # Graph size with connections
     #min_size = 20000  # png
     #max_size = 23000  # png
     min_size = 5000  # svg
     max_size = 7000  # svg
     self.assertTrue(size > min_size and size < max_size,
                     "Size not between {0} and {1}: {2}".format(
                         min_size, max_size, size))
Beispiel #17
0
 def test_get_role_relations(self):
     """Should return role dependencies when roles with relationships are given"""
     prod_nodes = chef.filter_nodes(self.nodes, 'production')
     extra_roles = graphs.get_role_relations('production', 'dbserver',
                                             prod_nodes)
     self.assertEqual(extra_roles, ['webserver', 'worker'])
     extra_roles = graphs.get_role_relations('production', 'loadbalancer',
                                             prod_nodes)
     self.assertEqual(extra_roles, ['webserver'])
     extra_roles = graphs.get_role_relations('production', 'worker',
                                             prod_nodes)
     self.assertEqual(extra_roles, ['dbserver'])
     extra_roles = graphs.get_role_relations('production', 'webserver',
                                             prod_nodes)
     self.assertEqual(extra_roles, ['dbserver', 'loadbalancer'])
Beispiel #18
0
 def test_build_links_client_nodes(self):
     """Should generate links when nodes have client_nodes set"""
     data = chef.filter_nodes(self.nodes, "production", "loadbalancer,webserver,dbserver", "guest")
     links = graphs._build_links(data)
     self.maxDiff = None
     expected = {
         "testnode1": {"role_prefix": "loadbalancer"},
         "testnode2": {"role_prefix": "webserver", "client_nodes": [("testnode1", "apache2")]},
         "testnode3.mydomain.com": {
             "role_prefix": "dbserver",
             "client_nodes": [("testnode2", "mysql"), ("testnode7", "mysql")],
         },
         "testnode7": {"role_prefix": "webserver", "client_nodes": [("testnode1", "apache2")]},
     }
     self.assertEqual(links, expected)
Beispiel #19
0
 def test_build_links_all(self):
     """Should generate all links when nodes define connections"""
     data = chef.filter_nodes(self.nodes, "production", virt_roles="guest")
     links = graphs._build_links(data)
     expected = {
         "testnode1": {"role_prefix": "loadbalancer"},
         "testnode2": {"role_prefix": "webserver", "client_nodes": [("testnode1", "apache2")]},
         "testnode3.mydomain.com": {
             "role_prefix": "dbserver",
             "client_nodes": [("testnode2", "mysql"), ("testnode7", "mysql")],
         },
         "testnode7": {"role_prefix": "webserver", "client_nodes": [("testnode1", "apache2")]},
         "testnode8": {"role_prefix": "worker", "needs_nodes": [("testnode3.mydomain.com", "mysql")]},
     }
     self.assertEqual(links, expected)
Beispiel #20
0
def graph(request):
    """Graph view where users can visualize graphs of their nodes
    generated using Graphviz open source graph visualization library

    """
    _show_repo_sync_date(request)
    data = {}
    options = _set_options(request.GET.get('options'))
    env_filter = request.GET.get('env', REPO['DEFAULT_ENV'])
    roles_filter = request.GET.get('roles', '')
    env_nodes = []
    try:
        data = _get_data(request, env_filter, '', 'guest')
    except RepoError as e:
        add_message(request, ERROR, str(e))
    else:
        if env_filter:
            env_nodes = data['nodes_extended']
            if roles_filter:
                # Filter the env nodes by role
                data['nodes_extended'] = filter_nodes(data['nodes_extended'],
                                                      roles=roles_filter)
                data['filter_roles'] = roles_filter
            success, msg = graphs.generate_node_map(
                data['nodes_extended'], data.get('roles', []),
                'show_hostnames' in options)
            data['draw_graph'] = success
            if not success:
                add_message(request, ERROR, msg)
            else:
                data['related_roles'] = graphs.get_role_relations(env_filter,
                                                                  roles_filter,
                                                                  env_nodes)
        else:
            add_message(request, WARNING, "Please select an environment")

    data['show_hostnames'] = 'show_hostnames' in options
    data['query_string'] = request.META['QUERY_STRING']

    return render_to_response('graph.html',
                              data, context_instance=RequestContext(request))
Beispiel #21
0
 def test_build_links_client_nodes(self):
     """Should generate links when nodes have client_nodes set"""
     data = chef.filter_nodes(
         self.nodes, 'production', 'loadbalancer,webserver,dbserver')
     links = graphs._build_links(data)
     self.maxDiff = None
     expected = {
         'testnode2': {'client_nodes': [('testnode1', 'apache2')]},
         'testnode3.mydomain.com': {
             'client_nodes': [
                 ('testnode2', 'mysql'), ('testnode7', 'mysql')
             ]
         },
         'testnode5': {
             'client_nodes': [
                 ('testnode2', 'mysql'), ('testnode7', 'mysql')
             ]
         },
         'testnode7': {'client_nodes': [('testnode1', 'apache2')]}
     }
     self.assertEqual(links, expected)
Beispiel #22
0
 def test_filter_nodes_all(self):
     """Should return all nodes when empty filters are are given"""
     data = chef.filter_nodes(chef.get_nodes_extended(), '', '')
     self.assertEqual(len(data), TOTAL_NODES)
Beispiel #23
0
 def test_get_role_relations_empty_when_no_roles(self):
     """Should obtain no roles when a role filter list is not given"""
     prod_nodes = chef.filter_nodes(self.nodes, 'staging')
     extra_roles = graphs.get_role_relations('production', '', prod_nodes)
     self.assertEqual(extra_roles, [])
Beispiel #24
0
 def test_get_role_relations_empty_when_roles(self):
     """Should obtain no roles when the given roles have no extra relationships"""
     stag_nodes = chef.filter_nodes(self.nodes, 'staging')
     extra_roles = graphs.get_role_relations('staging', 'webserver',
                                             stag_nodes)
     self.assertEqual(extra_roles, [])
Beispiel #25
0
 def test_build_links_empty(self):
     """Should not generate links when nodes do not have any defined"""
     data = chef.filter_nodes(self.nodes, "staging", virt_roles="guest")
     links = graphs._build_links(data)
     expected = {"testnode4": {"role_prefix": "webserver"}}
     self.assertEqual(links, expected)
Beispiel #26
0
 def test_build_links_empty(self):
     """Should not generate links when nodes do not have any defined"""
     data = chef.filter_nodes(self.nodes, 'staging', virt_roles='guest')
     links = graphs._build_links(data)
     expected = {'testnode4': {'role_prefix': 'webserver'}}
     self.assertEqual(links, expected)
Beispiel #27
0
 def test_build_links_empty(self):
     """Should not generate links when nodes do not have any defined"""
     data = chef.filter_nodes(self.nodes, 'staging', virt_roles='guest')
     links = graphs._build_links(data)
     self.assertEqual(links, {})