def test_index_division_by_zero(client, mocker, mock_puppetdb_environments,
                                mock_puppetdb_default_nodes):
    query_data = {
        'nodes': [[{
            'count': 0
        }]],
        'resources': [[{
            'count': 40
        }]],
    }

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)

    rv = client.get('/')

    assert rv.status_code == 200

    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'

    vals = soup.find_all('h1',
                         {"class": "ui header darkblue no-margin-bottom"})
    assert len(vals) == 3
    assert vals[2].string == '0'
def test_fact_value_view_complex(client, mocker, mock_puppetdb_environments,
                                 mock_puppetdb_default_nodes):
    values = {
        'trusted': {
            'domain': '',
            'certname': 'node-changed',
            'hostname': 'node-changed',
            'extensions': {},
            'authenticated': 'remote',
        }
    }
    query_data = {'facts': []}
    query_data['facts'].append({
        'certname': 'node-changed',
        'name': 'trusted',
        'value': values,
        'environment': 'production'
    })

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)

    rv = client.get('/fact/trusted/' +
                    "{'domain': ''%2C 'certname': 'node-changed'%2C"
                    " 'hostname': 'node-changed'%2C "
                    "'extensions': {}%2C 'authenticated': 'remote'}")
    assert rv.status_code == 200

    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'

    vals = soup.find_all('table', {"id": "facts_table"})
    assert len(vals) == 1
Пример #3
0
def test_json_daily_reports_chart_ok(client, mocker,
                                     mock_puppetdb_environments,
                                     mock_puppetdb_default_nodes):
    query_data = {
        'reports': [[{
            'status': 'changed',
            'count': 1
        }] for i in range(app.app.config['DAILY_REPORTS_CHART_DAYS'])]
    }

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)

    rv = client.get('/daily_reports_chart.json')
    result_json = json.loads(rv.data.decode('utf-8'))

    assert 'result' in result_json
    assert (len(
        result_json['result']) == app.app.config['DAILY_REPORTS_CHART_DAYS'])
    day_format = '%Y-%m-%d'
    cur_day = datetime.strptime(result_json['result'][0]['day'], day_format)
    for day in result_json['result'][1:]:
        next_day = datetime.strptime(day['day'], day_format)
        assert cur_day < next_day
        cur_day = next_day

    assert rv.status_code == 200
def test_node_facts_json(client, mocker, mock_puppetdb_environments,
                         mock_puppetdb_default_nodes):
    values = ['a', 'b', 'b', 'd']
    query_data = {'facts': []}
    query_data['facts'].append([])
    for i, value in enumerate(values):
        query_data['facts'][0].append({
            'certname': 'node-failed',
            'name': 'fact-%s' % i,
            'value': value,
            'environment': 'production'
        })

    dbquery = MockDbQuery(query_data)
    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)

    rv = client.get('/node/node-failed/facts/json')
    assert rv.status_code == 200

    result_json = json.loads(rv.data.decode('utf-8'))

    assert 'data' in result_json
    assert len(result_json['data']) == 4
    for line in result_json['data']:
        assert len(line) == 2

    assert 'chart' not in result_json
def test_fact_json_with_graph(client, mocker, mock_puppetdb_environments,
                              mock_puppetdb_default_nodes):
    values = ['a', 'b', 'b', 'd', True, 'a\nb']
    query_data = {'facts': []}
    query_data['facts'].append([])
    for i, value in enumerate(values):
        query_data['facts'][0].append({
            'certname': 'node-%s' % i,
            'name': 'architecture',
            'value': value,
            'environment': 'production'
        })

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)

    rv = client.get('/fact/architecture/json')
    assert rv.status_code == 200

    result_json = json.loads(rv.data.decode('utf-8'))

    assert 'data' in result_json
    assert len(result_json['data']) == 6
    for line in result_json['data']:
        assert len(line) == 2

    assert 'chart' in result_json
    assert len(result_json['chart']) == 5
    # Test group_by
    assert result_json['chart'][0]['value'] == 2
Пример #6
0
def test_json_report_ok(client, mocker, input_data, mock_puppetdb_environments,
                        mock_puppetdb_default_nodes):
    query_response = json.loads(input_data)

    query_data = {
        'reports': [{
            'validate': {
                'data': query_response[:100],
                'checks': {
                    'limit': 100,
                    'offset': 0
                }
            }
        }]
    }

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)
    app.puppetdb.last_total = 499

    rv = client.get('/reports/json')

    assert rv.status_code == 200
    result_json = json.loads(rv.data.decode('utf-8'))

    assert 'data' in result_json
    assert len(result_json['data']) == 100
def test_index_all(client, mocker, mock_puppetdb_environments,
                   mock_puppetdb_default_nodes):
    # starting with v6.9.1 they changed the metric API to v2
    # and a totally different format
    base_str = 'puppetlabs.puppetdb.population:'
    query_data = {
        'version': [{
            'version': '6.9.1'
        }],
        'metrics': [{
            'validate': {
                'data': {
                    'value': {
                        'Value': 10
                    }
                },
                'checks': {
                    'path': '%sname=num-nodes' % base_str
                }
            }
        }, {
            'validate': {
                'data': {
                    'value': {
                        'Value': 63
                    }
                },
                'checks': {
                    'path': '%sname=num-resources' % base_str
                }
            }
        }, {
            'validate': {
                'data': {
                    'value': {
                        'Value': 6.3
                    }
                },
                'checks': {
                    'path': '%sname=avg-resources-per-node' % base_str
                }
            }
        }]
    }
    dbquery = MockDbQuery(query_data)
    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)
    rv = client.get('/%2A/')

    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'
    vals = soup.find_all('h1',
                         {"class": "ui header darkblue no-margin-bottom"})

    assert len(vals) == 3
    assert vals[0].string == '10'
    assert vals[1].string == '63'
    assert vals[2].string == '         6'

    assert rv.status_code == 200
Пример #8
0
def test_radiator_view_all(client, mocker,
                           mock_puppetdb_environments,
                           mock_puppetdb_default_nodes):
    # starting with v6.9.1 they changed the metric API to v2
    # and a totally different format
    base_str = 'puppetlabs.puppetdb.population:'
    query_data = {
        'version': [{'version': '6.9.1'}],
        'metrics': [
            {
                'validate': {
                    'data': {
                        'value': {'Value': '50'}
                    },
                    'checks': {
                        'path': '%sname=num-nodes' % base_str
                    }
                }
            },
            {
                'validate': {
                    'data': {
                        'value': {'Value': '60'}
                    },
                    'checks': {
                        'path': '%sname=num-resources' % base_str
                    }
                }
            },
            {
                'validate': {
                    'data': {
                        'value': {'Value': 60.3}
                    },
                    'checks': {
                        'path': '%sname=avg-resources-per-node' % base_str
                    }
                }
            }
        ]
    }

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)

    rv = client.get('/%2A/radiator')

    assert rv.status_code == 200

    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'
    assert soup.h1 != 'Not Found'
    total = soup.find(class_='total')

    assert '50' in total.text
Пример #9
0
def test_reports__a_report(
    client,
    mocker,
    mock_puppetdb_environments,
):
    query_data = {
        'reports': [[
            {
                "hash": '1234567',
                "receive_time": '2022-05-11T04:00:00.000Z',
                "report_format": 12,
                "puppet_version": "1.2.3",
                "start_time": '1948-09-07T00:00:01.000Z',
                "end_time": '2022-05-11T04:00:00.000Z',
                "producer_timestamp": '2022-05-11T04:00:00.000Z',
                "producer": 'foobar',
                "transaction_uuid": 'foobar',
                "status": 'failed',
                "noop": False,
                "noop_pending": False,
                "environment": 'production',
                "configuration_version": '123',
                "certname": 'node-failed',
                "code_id": 'foobar',
                "catalog_uuid": 'foobar',
                "cached_catalog_status": 'not_used',
                "resource_events": [],
                "metrics": {
                    "data": []
                },
                "logs": {
                    "data": []
                },
            },
        ]],
        'events': [[]]
    }

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)
    app.puppetdb.last_total = 499

    rv = client.get('/report/node-failed/1234567')
    assert rv.status_code == 200

    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'

    vals = soup.find_all('table', {"id": "logs_table"})
    assert len(vals) == 1

    vals = soup.find_all('table', {"id": "events_table"})
    assert len(vals) == 1
def test_facts_view_empty_when_no_facts(client, mocker,
                                        mock_puppetdb_environments):
    query_data = {'fact-names': [[]]}

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)

    rv = client.get('/facts')
    assert rv.status_code == 200
    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'

    searchable = soup.find('div', {'class': 'searchable'})
    vals = searchable.find_all('div', {'class': 'column'})
    assert len(vals) == 0
def test_get_index(client, mocker, mock_puppetdb_environments,
                   mock_puppetdb_default_nodes):
    query_data = {
        'nodes': [[{
            'count': 10
        }]],
        'resources': [[{
            'count': 40
        }]],
    }

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)
    rv = client.get('/')
    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'
    assert rv.status_code == 200
Пример #12
0
def test_radiator_view_bad_env(client, mocker,
                               mock_puppetdb_environments,
                               mock_puppetdb_default_nodes):
    query_data = {
        'nodes': [[{'count': 10}]],
        'resources': [[{'count': 40}]],
    }

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)

    rv = client.get('/nothere/radiator')

    assert rv.status_code == 404
    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'
    assert soup.h1.text == 'Not Found'
def test_metric_v2_api(client, mocker, mock_puppetdb_environments,
                       mock_puppetdb_default_nodes):
    # starting with v6.9.1 they changed the metric API to v2
    # and a totally different format
    metric_name = 'puppetlabs.puppetdb.population:name=num-nodes'
    query_data = {
        'version': [{
            'version': '6.9.1'
        }],
        'metrics': [{
            'validate': {
                'data': {
                    'value': {
                        'Value': 50
                    },
                },
                'checks': {
                    'path': metric_name,
                }
            }
        }]
    }
    dbquery = MockDbQuery(query_data)
    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)
    rv = client.get('/metric/' + metric_name)

    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'

    small_list = soup.find_all('small')
    assert len(small_list) == 1
    assert small_list[0].string == metric_name

    tbody_list = soup.find_all('tbody')
    assert len(tbody_list) == 1
    rows = tbody_list[0].find_all('tr')

    assert len(rows) == 1
    cols = rows[0].find_all('td')
    assert len(cols) == 2
    assert cols[0].string == 'Value'
    assert cols[1].string == '50'

    assert rv.status_code == 200
Пример #14
0
def test_radiator_view_division_by_zero(client, mocker,
                                        mock_puppetdb_environments,
                                        mock_puppetdb_default_nodes):
    query_data = {
        'nodes': [[{'count': 0}]],
        'resources': [[{'count': 40}]],
    }

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)

    rv = client.get('/radiator')

    assert rv.status_code == 200

    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'

    total = soup.find(class_='total')
    assert '0' in total.text
def test_metrics_v2_api(client, mocker, mock_puppetdb_environments,
                        mock_puppetdb_default_nodes):
    # starting with v6.9.1 they changed the metric API to v2
    # and a totally different format
    query_data = {
        'version': [{
            'version': '6.9.1'
        }],
        'metrics-list': [{
            'validate': {
                'data': {
                    'value': {
                        'java.lang': {
                            'type=Memory': {}
                        },
                        'puppetlabs.puppetdb.population': {
                            'name=num-nodes': {}
                        },
                    }
                },
                'checks': {}
            }
        }]
    }
    dbquery = MockDbQuery(query_data)
    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)
    rv = client.get('/metrics')

    soup = BeautifulSoup(rv.data, 'html.parser')
    assert soup.title.contents[0] == 'Puppetboard'
    ul_list = soup.find_all('ul', attrs={'class': 'ui list searchable'})
    assert len(ul_list) == 1
    vals = ul_list[0].find_all('a')

    assert len(vals) == 2
    assert vals[0].string == 'java.lang:type=Memory'
    assert vals[1].string == 'puppetlabs.puppetdb.population:name=num-nodes'

    assert rv.status_code == 200
Пример #16
0
def test_radiator_view_json(client, mocker,
                            mock_puppetdb_environments,
                            mock_puppetdb_default_nodes):
    query_data = {
        'nodes': [[{'count': 10}]],
        'resources': [[{'count': 40}]],
    }

    dbquery = MockDbQuery(query_data)

    mocker.patch.object(app.puppetdb, '_query', side_effect=dbquery.get)

    rv = client.get('/radiator', headers={'Accept': 'application/json'})

    assert rv.status_code == 200
    json_data = json.loads(rv.data.decode('utf-8'))

    assert json_data['unreported'] == 1
    assert json_data['noop'] == 1
    assert json_data['failed'] == 1
    assert json_data['changed'] == 1
    assert json_data['unchanged'] == 1