Debian Codegraph @

tests/unit/network/test_scan.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 """Test scan filters and data provision stuff."""
17
18 from pathlib import Path
19 import re
20 from textwrap import dedent
21
22 from cylc.flow.network.scan import (
23 api_version,
24 contact_info,
25 cylc_version,
26 filter_name,
27 graphql_query,
28 validate_contact_info
29 )
30 from cylc.flow.workflow_files import (
31 ContactFileFields,
32 WorkflowFiles
33 )
34
35
36 SRV_DIR = Path(WorkflowFiles.Service.DIRNAME)
37 CONTACT = Path(WorkflowFiles.Service.CONTACT)
38
39
40 def test_filter_name_preprocess():
41 """It should combine provided patterns and compile them."""
42 pipe = filter_name('^f', '^c')
43 assert pipe.args[0] == re.compile('(^f|^c)')
44
45
46 async def test_filter_name():
47 """It should filter flows by registration name."""
48 pipe = filter_name('^f')
49 assert await pipe.func(
50 {'name': 'foo'},
51 *pipe.args
52 )
53 assert not await pipe.func(
54 {'name': 'bar'},
55 *pipe.args
56 )
57
58
59 async def test_cylc_version():
60 """It should filter flows by cylc version."""
61 version = ContactFileFields.VERSION
62
63 pipe = cylc_version('>= 8.0a1, < 9')
64 assert await pipe.func(
65 {version: '8.0a1'},
66 *pipe.args
67 )
68
69 pipe = cylc_version('>= 8.0a1, < 9')
70 assert not await pipe.func(
71 {version: '7.8.4'},
72 *pipe.args
73 )
74
75
76 async def test_api_version():
77 """It should filter flows by api version."""
78 version = ContactFileFields.API
79
80 pipe = api_version('>= 4, < 5')
81 assert await pipe.func(
82 {version: '4'},
83 *pipe.args
84 )
85
86 pipe = api_version('>= 4, < 5')
87 assert not await pipe.func(
88 {version: '5'},
89 *pipe.args
90 )
91
92
93 async def test_contact_info(tmp_path):
94 """It should load info from the contact file."""
95 # create a dummy flow
96 Path(tmp_path, 'foo', SRV_DIR).mkdir(parents=True)
97 # write a contact file with some junk in it
98 with open(Path(tmp_path, 'foo', SRV_DIR, CONTACT), 'w+') as contact:
99 contact.write(dedent('''
100 foo=1
101 bar=2
102 baz=3
103 ''').strip())
104 # create a flow dict as returned by scan
105 flow = {
106 'name': 'foo',
107 'path': tmp_path / 'foo'
108 }
109 # ensure the contact fields get added to the flow dict
110 assert await contact_info.func(flow) == {
111 **flow,
112 'foo': '1',
113 'bar': '2',
114 'baz': '3'
115 }
116
117
118 def test_graphql_query_preproc():
119 """It should format graphql query fragments from the input data."""
120 pipe = graphql_query(['a', 'b', 'c'])
121 assert pipe.args[0] == dedent('''
122 a
123 b
124 c
125 ''')
126
127 pipe = graphql_query({'a': None, 'b': None, 'c': None})
128 assert pipe.args[0] == dedent('''
129 a
130 b
131 c
132 ''')
133
134 pipe = graphql_query({'a': None, 'b': ['ba', 'bb'], 'c': None})
135 assert pipe.args[0] == dedent('''
136 a
137 c
138 b {
139 ba
140 bb
141 }
142 ''')
143
144
145 async def test_validate_contact_file(tmp_path):
146 """Ensure rejection for missing fields"""
147
148 flow = {
149 'name': 'foo',
150 'path': tmp_path / 'foo',
151 }
152 # contact_info has already loaded info from contact file (missing fields)
153 assert await validate_contact_info.func(flow) is False
154
155
156 async def test_validate_contact_file_no_missing_fields(tmp_path):
157 """Ensure rejection for missing fields"""
158 version = ContactFileFields.API
159 name = ContactFileFields.NAME
160 host = ContactFileFields.HOST
161 flow = {
162 'name': 'foo',
163 'path': tmp_path / 'foo',
164 version: 1,
165 name: 'moo',
166 host: 'hosty'
167 }
168 assert await validate_contact_info.func(flow) == flow