Ejemplo n.º 1
0
def test_assign_active_jobs_to_runners(running_jobs_from_project,
                                       counted_jobs_for_runners, gitlabapi):
    running_jobs_from_project.return_value = mock.Mock()
    counted_jobs_for_runners.return_value = {278: 2, 279: 1, 280: 2}
    expected_output = [
        {
            "id": 278,
            "active_jobs": 2
        },
        {
            "id": 279,
            "active_jobs": 1
        },
        {
            "id": 280,
            "active_jobs": 2
        },
        {
            "id": 281,
            "active_jobs": 0
        },
    ]
    list_of_runners = [
        Runner(
            id=runner["id"],
            description="",
            ip_address="",
            active=True,
            is_shared=True,
            name="",
            online=True,
            status="",
        ) for runner in RUNNERS
    ]

    expected_list_of_runners = [
        Runner(
            id=runner["id"],
            description="",
            ip_address="",
            active=True,
            is_shared=True,
            name="",
            online=True,
            status="",
            active_jobs=runner["active_jobs"],
        ) for runner in expected_output
    ]
    output = gitlabapi.assign_active_jobs_to_runners(list_of_runners, 1)
    assert output == expected_list_of_runners
Ejemplo n.º 2
0
def test_ignore_tags(gitlabdatafilter):
    runners = ALL_INFO_RUNNERS[:]
    expected_runners_list = [
        {
            "id": 1,
            "description": "qa-01.01",
            "ip_address": "123.12.12.10",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-x1", "tag-x2"],
        },
        {
            "id": 2,
            "description": "qa-01.02",
            "ip_address": "123.12.12.11",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-x1", "tag-x2"],
        },
    ]
    expected_runners = [Runner(**runner) for runner in expected_runners_list]
    gitlabdatafilter.ignore = ["name", "qa-02"]
    filtered_runners = gitlabdatafilter.ignore_runners(runners)
    assert expected_runners == filtered_runners
    expected_runners_list = [
        {
            "id": 3,
            "description": "qa-02.01",
            "ip_address": "123.12.12.12",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-x1", "tag-x3"],
        },
    ]
    expected_runners = [Runner(**runner) for runner in expected_runners_list]
    gitlabdatafilter.ignore = ["tag", "tag-x2"]
    filtered_runners = gitlabdatafilter.ignore_runners(runners)
    assert expected_runners == filtered_runners
Ejemplo n.º 3
0
def test_retag_runners(gitlabdatafilter):
    runners = ALL_INFO_RUNNERS[:]
    gitlabdatafilter.action = ["retag", "tag-x1:tag-TEST"]
    tags_to_change = gitlabdatafilter.get_tags_to_change()
    runners_after_changes = []
    for runner in runners:
        changed, new_runner = gitlabdatafilter.retag_algorithm(
            runner, tags_to_change)
        runners_after_changes.append((changed, runner, new_runner))
    expected_runners_list = [
        {
            "id": 1,
            "description": "qa-01.01",
            "ip_address": "123.12.12.10",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-TEST", "tag-x2"],
        },
        {
            "id": 2,
            "description": "qa-01.02",
            "ip_address": "123.12.12.11",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-TEST", "tag-x2"],
        },
        {
            "id": 3,
            "description": "qa-02.01",
            "ip_address": "123.12.12.12",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-TEST", "tag-x3"],
        },
        {
            "id": 4,
            "description": "qa-02.02",
            "ip_address": "123.12.12.13",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-TEST", "tag-x2", "tag-x3"],
        },
    ]
    expected_runners = [Runner(**runner) for runner in expected_runners_list]
    assert expected_runners == [runner[2] for runner in runners_after_changes]
Ejemplo n.º 4
0
def project_runners_dict():
    project_runners = [
        1,
        8,
        9,
        11,
        12,
        56,
        128,
        146,
        261,
        262,
        263,
        264,
        265,
        266,
        267,
        268,
        269,
        270,
        271,
        272,
    ]
    mock_runners = []
    for project_id in project_runners[:6]:
        runner = {"id": project_id, "tag_list": ["tag1", "tag2"]}
        mock_runners.append(runner)
    for project_id in project_runners[6:]:
        runner = {"id": project_id, "tag_list": ["tag3"]}
        mock_runners.append(runner)
    return [
        Runner(
            id=mock_runner["id"],
            description="",
            ip_address="",
            active=True,
            is_shared=True,
            name="",
            online=True,
            status="",
            tag_list=mock_runner["tag_list"],
        ) for mock_runner in mock_runners
    ]
Ejemplo n.º 5
0
def test_filtering_cases(gitlabdatafilter):
    # Filter by name
    expected_runners_list = [
        {
            "id": 1,
            "description": "qa-01.01",
            "ip_address": "123.12.12.10",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-x1", "tag-x2"],
        },
        {
            "id": 2,
            "description": "qa-01.02",
            "ip_address": "123.12.12.11",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-x1", "tag-x2"],
        },
    ]
    expected_runners = [Runner(**runner) for runner in expected_runners_list]
    runners = ALL_INFO_RUNNERS[:]
    filtered_runners = gitlabdatafilter.filter_runners(runners,
                                                       Filtering.NAMES,
                                                       ["qa-01"])
    assert expected_runners == filtered_runners
    expected_runners_list = [{
        "id": 1,
        "description": "qa-01.01",
        "ip_address": "123.12.12.10",
        "active": True,
        "is_shared": False,
        "name": "gitlab-runner",
        "online": True,
        "status": "online",
        "tag_list": ["tag-x1", "tag-x2"],
    }]
    expected_runners = [Runner(**runner) for runner in expected_runners_list]
    runners = ALL_INFO_RUNNERS[:]
    filtered_runners = gitlabdatafilter.filter_runners(runners,
                                                       Filtering.NAMES,
                                                       ["qa-01.01"])
    assert expected_runners == filtered_runners
    expected_runners = []
    runners = ALL_INFO_RUNNERS[:]
    filtered_runners = gitlabdatafilter.filter_runners(runners,
                                                       Filtering.NAMES,
                                                       ["qa-01.0111"])
    assert expected_runners == filtered_runners
    # Filter by tag
    expected_runners = ALL_INFO_RUNNERS[:]
    runners = ALL_INFO_RUNNERS[:]
    filtered_runners = gitlabdatafilter.filter_runners(runners, Filtering.TAGS,
                                                       ["tag-x1"])
    assert expected_runners == filtered_runners
    expected_runners_list = [
        {
            "id": 3,
            "description": "qa-02.01",
            "ip_address": "123.12.12.12",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-x1", "tag-x3"],
        },
        {
            "id": 4,
            "description": "qa-02.02",
            "ip_address": "123.12.12.13",
            "active": True,
            "is_shared": False,
            "name": "gitlab-runner",
            "online": True,
            "status": "online",
            "tag_list": ["tag-x1", "tag-x2", "tag-x3"],
        },
    ]
    expected_runners = [Runner(**runner) for runner in expected_runners_list]
    runners = ALL_INFO_RUNNERS[:]
    filtered_runners = gitlabdatafilter.filter_runners(runners, Filtering.TAGS,
                                                       ["tag-x3"])
    assert expected_runners == filtered_runners
    expected_runners = []
    runners = ALL_INFO_RUNNERS[:]
    filtered_runners = gitlabdatafilter.filter_runners(runners, Filtering.TAGS,
                                                       ["tag-x4"])
    assert expected_runners == filtered_runners
    expected_runners = ALL_INFO_RUNNERS[:]
    runners = ALL_INFO_RUNNERS[:]
    # all tags which have tag inside
    filtered_runners = gitlabdatafilter.filter_runners(runners, Filtering.TAGS,
                                                       ["tag"])
    assert expected_runners == filtered_runners
Ejemplo n.º 6
0
        "tag_list": ["tag-x1", "tag-x3"],
    },
    {
        "id": 4,
        "description": "qa-02.02",
        "ip_address": "123.12.12.13",
        "active": True,
        "is_shared": False,
        "name": "gitlab-runner",
        "online": True,
        "status": "online",
        "tag_list": ["tag-x1", "tag-x2", "tag-x3"],
    },
]

ALL_INFO_RUNNERS = [Runner(**runner) for runner in ALL_INFO_RUNNERS_DICT]


@pytest.fixture()
def project_runners_with_names():
    project_runners = [1, 2, 3, 4]
    names = ["qa-01.02", "qa-01.03", "qa-02.01", "qa-03.01"]
    mock_runners = []
    for project_id, name in zip(project_runners, names):
        mock_runner = mock.Mock()
        mock_runner.id = project_id
        mock_runner.description = name
        mock_runners.append(mock_runner)
    return mock_runners