aboutsummaryrefslogtreecommitdiffstats
path: root/.github/scripts/tests/create_new_muted_ya.py
blob: 99d6168e1c5176af809376f67364062f130bef50 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
#!/usr/bin/env python3
import argparse
import configparser
import datetime
import os
import re
import ydb
import logging

from transform_ya_junit import YaMuteCheck
from update_mute_issues import (
    create_and_add_issue_to_project,
    generate_github_issue_title_and_body,
    get_muted_tests_from_issues,
    close_unmuted_issues,
)

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

dir = os.path.dirname(__file__)
config = configparser.ConfigParser()
config_file_path = f"{dir}/../../config/ydb_qa_db.ini"
repo_path = f"{dir}/../../../"
muted_ya_path = '.github/config/muted_ya.txt'
config.read(config_file_path)

DATABASE_ENDPOINT = config["QA_DB"]["DATABASE_ENDPOINT"]
DATABASE_PATH = config["QA_DB"]["DATABASE_PATH"]


def execute_query(driver, branch='main', build_type='relwithdebinfo'):
    query_string = f'''
    SELECT * from (
        SELECT data.*,
        CASE WHEN new_flaky.full_name IS NOT NULL THEN True ELSE False END AS new_flaky_today,
        CASE WHEN flaky.full_name IS NOT NULL THEN True ELSE False END AS flaky_today,
        CASE WHEN muted_stable.full_name IS NOT NULL THEN True ELSE False END AS muted_stable_today,
        CASE WHEN muted_stable_n_days.full_name IS NOT NULL THEN True ELSE False END AS muted_stable_n_days_today,
        CASE WHEN deleted.full_name IS NOT NULL THEN True ELSE False END AS deleted_today

        FROM
        (SELECT test_name, suite_folder, full_name, date_window, build_type, branch, days_ago_window, history, history_class, pass_count, mute_count, fail_count, skip_count, success_rate, summary, owner, is_muted, is_test_chunk, state, previous_state, state_change_date, days_in_state, previous_state_filtered, state_change_date_filtered, days_in_state_filtered, state_filtered
        FROM `test_results/analytics/tests_monitor`) as data
        left JOIN 
        (SELECT full_name, build_type, branch
            FROM `test_results/analytics/tests_monitor`
            WHERE state = 'Flaky'
            AND days_in_state = 1
            AND date_window = CurrentUtcDate()
            )as new_flaky
        ON 
            data.full_name = new_flaky.full_name
            and data.build_type = new_flaky.build_type
            and data.branch = new_flaky.branch
        LEFT JOIN 
        (SELECT full_name, build_type, branch
            FROM `test_results/analytics/tests_monitor`
            WHERE state = 'Flaky'
            AND date_window = CurrentUtcDate()
            )as flaky
        ON 
            data.full_name = flaky.full_name
            and data.build_type = flaky.build_type
            and data.branch = flaky.branch
        LEFT JOIN 
        (SELECT full_name, build_type, branch
            FROM `test_results/analytics/tests_monitor`
            WHERE state = 'Muted Stable'
            AND date_window = CurrentUtcDate()
            )as muted_stable

        ON 
            data.full_name = muted_stable.full_name
            and data.build_type = muted_stable.build_type
            and data.branch = muted_stable.branch
        LEFT JOIN 
        (SELECT full_name, build_type, branch
            FROM `test_results/analytics/tests_monitor`
            WHERE state= 'Muted Stable'
            AND days_in_state >= 14
            AND date_window = CurrentUtcDate()
            and is_test_chunk = 0
            )as muted_stable_n_days

        ON 
            data.full_name = muted_stable_n_days.full_name
            and data.build_type = muted_stable_n_days.build_type
            and data.branch = muted_stable_n_days.branch
       
        LEFT JOIN 
        (SELECT full_name, build_type, branch
            FROM `test_results/analytics/tests_monitor`
            WHERE state = 'no_runs'
            AND days_in_state >= 14
            AND date_window = CurrentUtcDate()
            and is_test_chunk = 0
            )as deleted

        ON 
            data.full_name = deleted.full_name
            and data.build_type = deleted.build_type
            and data.branch = deleted.branch
        ) 
        where date_window = CurrentUtcDate() and branch = '{branch}' and build_type = '{build_type}'
    
    '''

    query = ydb.ScanQuery(query_string, {})
    table_client = ydb.TableClient(driver, ydb.TableClientSettings())
    it = table_client.scan_query(query)
    results = []
    while True:
        try:
            result = next(it)
            results = results + result.result_set.rows
        except StopIteration:
            break

    return results


def add_lines_to_file(file_path, lines_to_add):
    try:
        os.makedirs(os.path.dirname(file_path), exist_ok=True)
        with open(file_path, 'w') as f:
            f.writelines(lines_to_add)
        logging.info(f"Lines added to {file_path}")
    except Exception as e:
        logging.error(f"Error adding lines to {file_path}: {e}")


def apply_and_add_mutes(all_tests, output_path, mute_check):

    output_path = os.path.join(output_path, 'mute_update')

    all_tests = sorted(all_tests, key=lambda d: d['full_name'])

    try:

        deleted_tests = set(
            f"{test.get('suite_folder')} {test.get('test_name')}\n" for test in all_tests if test.get('deleted_today')
        )

        deleted_tests = sorted(deleted_tests)
        add_lines_to_file(os.path.join(output_path, 'deleted.txt'), deleted_tests)

        deleted_tests_debug = set(
            f"{test.get('suite_folder')} {test.get('test_name')} # owner {test.get('owner')} success_rate {test.get('success_rate')}%, state {test.get('state')} days in state {test.get('days_in_state')}\n"
            for test in all_tests
            if test.get('deleted_today')
        )

        deleted_tests_debug = sorted(deleted_tests_debug)
        add_lines_to_file(os.path.join(output_path, 'deleted_debug.txt'), deleted_tests_debug)

        muted_stable_tests = set(
            f"{test.get('suite_folder')} {test.get('test_name')}\n"
            for test in all_tests
            if test.get('muted_stable_n_days_today')
        )

        muted_stable_tests = sorted(muted_stable_tests)
        add_lines_to_file(os.path.join(output_path, 'muted_stable.txt'), muted_stable_tests)

        muted_stable_tests_debug = set(
            f"{test.get('suite_folder')} {test.get('test_name')} "
            + f"# owner {test.get('owner')} success_rate {test.get('success_rate')}%, state {test.get('state')} days in state {test.get('days_in_state')}\n"
            for test in all_tests
            if test.get('muted_stable_n_days_today')
        )

        muted_stable_tests_debug = sorted(muted_stable_tests_debug)
        add_lines_to_file(os.path.join(output_path, 'muted_stable_debug.txt'), muted_stable_tests_debug)

        # Add all flaky tests
        flaky_tests = set(
            re.sub(r'\d+/(\d+)\]', r'*/*]', f"{test.get('suite_folder')} {test.get('test_name')}\n")
            for test in all_tests
            if test.get('days_in_state') >= 1
            and test.get('flaky_today')
            and (test.get('pass_count') + test.get('fail_count')) >= 2
            and test.get('fail_count') >= 2
            and test.get('fail_count')/(test.get('pass_count') + test.get('fail_count')) > 0.2 # <=80% success rate
        )
        flaky_tests = sorted(flaky_tests)
        add_lines_to_file(os.path.join(output_path, 'flaky.txt'), flaky_tests)

        flaky_tests_debug = set(
            re.sub(r'\d+/(\d+)\]', r'*/*]', f"{test.get('suite_folder')} {test.get('test_name')}")
            + f" # owner {test.get('owner')} success_rate {test.get('success_rate')}%, state {test.get('state')}, days in state {test.get('days_in_state')}, pass_count {test.get('pass_count')}, fail count {test.get('fail_count')}\n"
            for test in all_tests
            if test.get('days_in_state') >= 1
            and test.get('flaky_today')
            and (test.get('pass_count') + test.get('fail_count')) >=2
            and test.get('fail_count') >= 2
            and test.get('fail_count')/(test.get('pass_count') + test.get('fail_count')) > 0.2 # <=80% success rate
        )
        ## тесты может запускаться 1 раз в день. если за последние 7 дней набирается трешход то мьютим
        ## падения сегодня более весомы ??  
        ## за 7 дней смотреть?
        #----
        ## Mute Flaky редко запускаемых тестов
        ##   Разобраться почему 90 % флакающих тестов имеют только 1 падение и в статусе Flaky только 2 дня      
        flaky_tests_debug = sorted(flaky_tests_debug)
        add_lines_to_file(os.path.join(output_path, 'flaky_debug.txt'), flaky_tests_debug)

        new_muted_ya_tests_debug = []
        new_muted_ya_tests = []
        new_muted_ya_tests_with_flaky = []
        new_muted_ya_tests_with_flaky_debug = []
        unmuted_tests_debug = []
        muted_ya_tests_sorted = []
        muted_ya_tests_sorted_debug = []
        deleted_tests_in_mute = []
        deleted_tests_in_mute_debug = []
        muted_before_count = 0
        unmuted_stable = 0
        unmuted_deleted = 0
        # Apply mute check and filter out already muted tests
        for test in all_tests:
            testsuite = test.get('suite_folder')
            testcase = test.get('test_name')
            success_rate = test.get('success_rate')
            days_in_state = test.get('days_in_state')
            owner = test.get('owner')
            state = test.get('state')
            test_string = f"{testsuite} {testcase}\n"
            test_string_debug = f"{testsuite} {testcase} # owner {owner} success_rate {success_rate}%, state {state} days in state {days_in_state}\n"
            test_string = re.sub(r'\d+/(\d+)\]', r'*/*]', test_string)
            if (
                testsuite and testcase and mute_check(testsuite, testcase) or test_string in flaky_tests
            ) and test_string not in new_muted_ya_tests_with_flaky:
                if test_string not in muted_stable_tests and test_string not in deleted_tests:
                    new_muted_ya_tests_with_flaky.append(test_string)
                    new_muted_ya_tests_with_flaky_debug.append(test_string_debug)

            if testsuite and testcase and mute_check(testsuite, testcase):
               
                if test_string not in muted_ya_tests_sorted:
                    muted_ya_tests_sorted.append(test_string)
                    muted_ya_tests_sorted_debug.append(test_string_debug)
                    muted_before_count += 1
                if test_string not in new_muted_ya_tests:
                    if test_string not in muted_stable_tests and test_string not in deleted_tests:
                        new_muted_ya_tests.append(test_string)
                        new_muted_ya_tests_debug.append(test_string_debug)
                    if test_string in muted_stable_tests:
                        unmuted_stable += 1
                    if test_string in deleted_tests:
                        unmuted_deleted += 1
                        deleted_tests_in_mute.append(test_string)
                        deleted_tests_in_mute_debug.append(test_string_debug)
                    unmuted_tests_debug.append(test_string_debug)

        muted_ya_tests_sorted = sorted(muted_ya_tests_sorted)
        add_lines_to_file(os.path.join(output_path, 'muted_ya_sorted.txt'), muted_ya_tests_sorted)
        muted_ya_tests_sorted_debug = sorted(muted_ya_tests_sorted_debug)
        add_lines_to_file(os.path.join(output_path, 'muted_ya_sorted_debug.txt'), muted_ya_tests_sorted_debug)
        new_muted_ya_tests = sorted(new_muted_ya_tests)
        add_lines_to_file(os.path.join(output_path, 'new_muted_ya.txt'), new_muted_ya_tests)
        new_muted_ya_tests_debug = sorted(new_muted_ya_tests_debug)
        add_lines_to_file(os.path.join(output_path, 'new_muted_ya_debug.txt'), new_muted_ya_tests_debug)
        new_muted_ya_tests_with_flaky = sorted(new_muted_ya_tests_with_flaky)
        add_lines_to_file(os.path.join(output_path, 'new_muted_ya_with_flaky.txt'), new_muted_ya_tests_with_flaky)
        new_muted_ya_tests_with_flaky_debug = sorted(new_muted_ya_tests_with_flaky_debug)
        add_lines_to_file(
            os.path.join(output_path, 'new_muted_ya_with_flaky_debug.txt'), new_muted_ya_tests_with_flaky_debug
        )
        unmuted_tests_debug = sorted(unmuted_tests_debug)
        add_lines_to_file(os.path.join(output_path, 'unmuted_debug.txt'), unmuted_tests_debug)
        deleted_tests_in_mute = sorted(deleted_tests_in_mute)
        add_lines_to_file(os.path.join(output_path, 'deleted_tests_in_mute.txt'), deleted_tests_in_mute)
        deleted_tests_in_mute_debug = sorted(deleted_tests_in_mute_debug)
        add_lines_to_file(os.path.join(output_path, 'deleted_tests_in_mute_debug.txt'), deleted_tests_in_mute_debug)

        logging.info(f"Muted before script: {muted_before_count} tests")
        logging.info(f"Muted stable : {len(muted_stable_tests)}")
        logging.info(f"Flaky tests : {len(flaky_tests)}")
        logging.info(f"Result: Muted without deleted and stable : {len(new_muted_ya_tests)}")
        logging.info(f"Result: Muted without deleted and stable, with flaky : {len(new_muted_ya_tests_with_flaky)}")
        logging.info(f"Result: Unmuted tests : stable {unmuted_stable} and deleted {unmuted_deleted}")
    except (KeyError, TypeError) as e:
        logging.error(f"Error processing test data: {e}. Check your query results for valid keys.")
        return []

    return len(new_muted_ya_tests)


def read_tests_from_file(file_path):
    result = []
    with open(file_path, "r") as fp:
        for line in fp:
            line = line.strip()
            try:
                testsuite, testcase = line.split(" ", maxsplit=1)
                result.append({'testsuite': testsuite, 'testcase': testcase, 'full_name': f"{testsuite}/{testcase}"})
            except ValueError:
                logging.warning(f"cant parse line: {line!r}")
                continue
    return result


def create_mute_issues(all_tests, file_path, close_issues=True):
    base_date = datetime.datetime(1970, 1, 1)
    tests_from_file = read_tests_from_file(file_path)
    muted_tests_in_issues = get_muted_tests_from_issues()
    prepared_tests_by_suite = {}
    temp_tests_by_suite = {}
    
    # Create set of muted tests for faster lookup
    muted_tests_set = {test['full_name'] for test in tests_from_file}
    
    # Check and close issues if needed
    closed_issues = []
    if close_issues:
        closed_issues = close_unmuted_issues(muted_tests_set)
    
    # First, collect all tests into temporary dictionary
    for test in all_tests:
        for test_from_file in tests_from_file:
            if test['full_name'] == test_from_file['full_name']:
                if test['full_name'] in muted_tests_in_issues:
                    logging.info(
                        f"test {test['full_name']} already have issue, {muted_tests_in_issues[test['full_name']][0]['url']}"
                    )
                else:
                    key = f"{test_from_file['testsuite']}:{test['owner']}"
                    if not temp_tests_by_suite.get(key):
                        temp_tests_by_suite[key] = []
                    temp_tests_by_suite[key].append(
                        {
                            'mute_string': f"{ test.get('suite_folder')} {test.get('test_name')}",
                            'test_name': test.get('test_name'),
                            'suite_folder': test.get('suite_folder'),
                            'full_name': test.get('full_name'),
                            'success_rate': test.get('success_rate'),
                            'days_in_state': test.get('days_in_state'),
                            'date_window': (base_date + datetime.timedelta(days=test.get('date_window'))).date() ,
                            'owner': test.get('owner'),
                            'state': test.get('state'),
                            'summary': test.get('summary'),
                            'fail_count': test.get('fail_count'),
                            'pass_count': test.get('pass_count'),
                            'branch': test.get('branch'),
                        }
                    )
    
    # Split groups larger than 20 tests
    for key, tests in temp_tests_by_suite.items():
        if len(tests) <= 40:
            prepared_tests_by_suite[key] = tests
        else:
            # Split into groups of 40
            for i in range(0, len(tests), 40):
                chunk = tests[i:i+40]
                chunk_key = f"{key}_{i//40 + 1}"  # Add iterator to key starting from 1
                prepared_tests_by_suite[chunk_key] = chunk

    results = []
    for item in prepared_tests_by_suite:
        title, body = generate_github_issue_title_and_body(prepared_tests_by_suite[item])
        owner_value = prepared_tests_by_suite[item][0]['owner'].split('/', 1)[1] if '/' in prepared_tests_by_suite[item][0]['owner'] else prepared_tests_by_suite[item][0]['owner']
        result = create_and_add_issue_to_project(title, body, state='Muted', owner=owner_value)
        if not result:
            break
        else:
            results.append(
                {
                    'message': f"Created issue '{title}' for TEAM:@ydb-platform/{owner_value}, url {result['issue_url']}",
                    'owner': owner_value
                }
            )

    # Sort results by owner
    results.sort(key=lambda x: x['owner'])
    
    # Group results by owner and add spacing and headers
    formatted_results = []
    
    # Add closed issues section if any
    if closed_issues:
        formatted_results.append("CLOSED ISSUES:")
        for issue in closed_issues:
            formatted_results.append(f"Closed {issue['url']}")
            formatted_results.append("Unmuted tests:")
            for test in issue['tests']:
                formatted_results.append(f"  - {test}")
        formatted_results.append("")
        formatted_results.append("CREATED ISSUES:")
    
    # Add created issues
    current_owner = None
    for result in results:
        if current_owner != result['owner']:
            if formatted_results and formatted_results[-1] != "":  # Add blank line between owner groups if last line is not empty
                formatted_results.append('')
            current_owner = result['owner']
            # Add owner header with team URL
            formatted_results.append(f"TEAM:@ydb-platform/{current_owner} @https://github.com/orgs/ydb-platform/teams/{current_owner}")
        formatted_results.append(result['message'])

    print("\n\n")
    print("\n".join(formatted_results))
    if 'GITHUB_OUTPUT' in os.environ:
        if 'GITHUB_WORKSPACE' not in os.environ:
            raise EnvironmentError("GITHUB_WORKSPACE environment variable is not set.")
        
        file_path = os.path.join(os.environ['GITHUB_WORKSPACE'], "created_issues.txt")
        print(f"Writing results to {file_path}")
        
        with open(file_path, 'w') as f:
            f.write("\n")
            f.write("\n".join(formatted_results))
            f.write("\n")
            
        with open(os.environ['GITHUB_OUTPUT'], 'a') as gh_out:
            gh_out.write(f"created_issues_file={file_path}")
            
        print(f"Result saved to env variable GITHUB_OUTPUT by key created_issues_file")


def mute_worker(args):

    # Simplified Connection
    if "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS" not in os.environ:
        print("Error: Env variable CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS is missing, skipping")
        return 1
    else:
        # Do not set up 'real' variable from gh workflows because it interfere with ydb tests
        # So, set up it locally
        os.environ["YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"] = os.environ[
            "CI_YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS"
        ]

    mute_check = YaMuteCheck()
    mute_check.load(muted_ya_path)

    with ydb.Driver(
        endpoint=DATABASE_ENDPOINT,
        database=DATABASE_PATH,
        credentials=ydb.credentials_from_env_variables(),
    ) as driver:
        driver.wait(timeout=10, fail_fast=True)

        all_tests = execute_query(driver, args.branch)
    if args.mode == 'update_muted_ya':
        output_path = args.output_folder
        os.makedirs(output_path, exist_ok=True)
        apply_and_add_mutes(all_tests, output_path, mute_check)

    elif args.mode == 'create_issues':
        file_path = args.file_path
        create_mute_issues(all_tests, file_path, close_issues=args.close_issues)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Add tests to mutes files based on flaky_today condition")

    subparsers = parser.add_subparsers(dest='mode', help="Mode to perform")

    update_muted_ya_parser = subparsers.add_parser('update_muted_ya', help='create new muted_ya')
    update_muted_ya_parser.add_argument('--output_folder', default=repo_path, required=False, help='Output folder.')
    update_muted_ya_parser.add_argument('--branch', default='main', help='Branch to get history')

    create_issues_parser = subparsers.add_parser(
        'create_issues',
        help='create issues by muted_ya like files',
    )
    create_issues_parser.add_argument(
        '--file_path', default=f'{repo_path}/mute_update/flaky.txt', required=False, help='file path'
    )
    create_issues_parser.add_argument('--branch', default='main', help='Branch to get history')
    create_issues_parser.add_argument('--close_issues', action='store_true', default=True, help='Close issues when all tests are unmuted (default: True)')

    args = parser.parse_args()

    mute_worker(args)