Coverage for osm_mon/tests/unit/collector/vnf_collectors/vmware/mock_http.py: 89%
9 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-05-06 19:04 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-05-06 19:04 +0000
1# -*- coding: utf-8 -*-
2# Copyright 2019 VMware
3# *************************************************************
5# This file is part of OSM Monitoring module
6# All Rights Reserved to VMware
8# Licensed under the Apache License, Version 2.0 (the "License"); you may
9# not use this file except in compliance with the License. You may obtain
10# a copy of the License at
12# http://www.apache.org/licenses/LICENSE-2.0
14# Unless required by applicable law or agreed to in writing, software
15# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17# License for the specific language governing permissions and limitations
18# under the License.
19# For those usages not covered by the Apache License, Version 2.0 please
20# contact: mbeierl@vmware.com
21# #
23import os
24import re
27def mock_http_response(
28 mocker,
29 method="GET",
30 site="https://vrops",
31 url_pattern="",
32 response_file="OK",
33 status_code=200,
34 exception=None,
35):
36 """Helper function to load a canned response from a file."""
37 with open(
38 os.path.join(os.path.dirname(__file__), "vmware_mocks", "%s" % response_file),
39 "r",
40 ) as f:
41 response = f.read()
43 matcher = re.compile(site + url_pattern)
44 if exception is None:
45 mocker.register_uri(method, matcher, text=response, status_code=status_code)
46 else:
47 mocker.register_uri(method, matcher, exc=exception)