Pull JSON schemas at build/test not run time
[dcaegen2/platform.git] / mod / onboardingapi / dcae_cli / conftest.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2018-2020 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20 """
21 This module is actually for pytesting. This contains fixtures.
22 """
23
24 import pytest
25 import dcae_cli
26
27 # REVIEW: Having issues trying to share this amongst all the tests. Putting this
28 # fixture here allows it to be shared when running tests over the entire project.
29 # The pytest recommendation was to place this file high up in the project.
30
31 @pytest.fixture
32 def mock_schemas(monkeypatch):
33     import os
34     cwd = os.getcwd()
35     schemadir = cwd[:cwd.find('/onboardingapi')] + '/component-json-schemas'
36     monkeypatch.setattr(dcae_cli.catalog.mock.schema.component_schema, 'path', schemadir + '/component-specification/dcae-cli-v2/component-spec-schema.json')
37     monkeypatch.setattr(dcae_cli.catalog.mock.schema.dataformat_schema, 'path', schemadir + '/data-format/dcae-cli-v1/data-format-schema.json')
38     
39 @pytest.fixture
40 def mock_cli_config(mock_schemas, monkeypatch):
41     """Fixture to provide a mock dcae-cli configuration and profiles
42
43     This fixture monkeypatches the respective get calls to return mock objects
44     """
45     # NOTE: The component spec and data format in gerrit moved once already.
46     # Might move again..
47     fake_config = { "active_profile": "default",
48             "server_url": "https://git.onap.org/dcaegen2/platform/cli/plain",
49             "db_url": "postgresql://postgres:abc123@localhost:5432/dcae_onboarding_db",
50             "path_component_spec": "/component-json-schemas/component-specification/dcae-cli-v2/component-spec-schema.json",
51             "path_data_format": "/component-json-schemas/data-format/dcae-cli-v1/data-format-schema.json"
52             }
53
54     fake_profiles = { "default": { "consul_host": "consul",
55         "cdap_broker": "cdap_broker",
56         "config_binding_service": "config_binding_service",
57         "docker_host": "docker_host" }
58         }
59     fake_profiles["active"] = fake_profiles["default"]
60
61     def fake_get_config():
62         return fake_config
63
64     def fake_get_profiles(user_only=False, include_active=True):
65         return fake_profiles
66
67     from dcae_cli.util import config, profiles
68     monkeypatch.setattr(dcae_cli.util.config, "get_config", fake_get_config)
69     monkeypatch.setattr(dcae_cli.util.profiles, "get_profiles", fake_get_profiles)
70
71
72 @pytest.fixture
73 def mock_db_url(tmpdir):
74     """Fixture to provide mock db url
75
76     This url is intended to be the location of where to place the local sqlite
77     databases for each unit test"""
78     dbname="dcae_cli.test.db"
79     config_dir = tmpdir.mkdir("config")
80     return "/".join(["sqlite://", str(config_dir), dbname])