<commit_before>from __future__ import absolute_import, unicode_literals
import copy
import inspect
import json
import os

import pytest
import logging
from .fake_api import openshift
from osbs.build import DockJsonManipulator, BuildManager
from tests.constants import TEST_BUILD, TEST_LABEL, TEST_LABEL_VALUE


logger = logging.getLogger("osbs.tests")


def test_set_labels_on_build(openshift):
    l = openshift.set_labels_on_build(TEST_BUILD, {TEST_LABEL: TEST_LABEL_VALUE})
    assert l.json() is not None


def test_get_oauth_token(openshift):
    token = openshift.get_oauth_token()
    assert token is not None


def test_list_builds(openshift):
    l = openshift.list_builds()
    assert l is not None
    assert bool(l.json())  # is there at least something



#####
#
# build/DockJsonManipulator
#
#####

BUILD_JSON = {
    "metadata": {
        "name": "{{NAME}}"
    },
    "kind": "Build",
    "apiVersion": "v1beta1",
    "parameters": {
        "source": {
            "type": "Git",
            "git": {
                "uri": "{{GIT_URI}}"
            }
        },
        "strategy": {
            "type": "Custom",
            "customStrategy": {
                "image": "buildroot",
                "exposeDockerSocket": True,
                "env": [{
                    "name": "DOCK_PLUGINS",
                    "value": "TBD"
                }]
            }
        },
        "output": {
            "imageTag": "{{OUTPUT_IMAGE_TAG}}",
            "registry": "{{REGISTRY_URI}}"
        }
    }
}

INNER_DOCK_JSON = {
    "prebuild_plugins": [
        {
            "name": "change_from_in_dockerfile"
        },
        {
            "args": {
                "key1": {
                    "a": "1",
                    "b": "2"
                },
                "key2": "b"
            },
            "name": "a_plugin"
        },
    ],
    "postbuild_plugins": [
        {
            "args": {
                "image_id": "BUILT_IMAGE_ID"
            },
            "name": "all_rpm_packages"
        },
    ]
}


def test_manipulator():
    m = DockJsonManipulator(BUILD_JSON, INNER_DOCK_JSON)
    assert m is not None

    
def test_manipulator_get_dock_json():
    build_json = copy.deepcopy(BUILD_JSON)
    env_json = build_json['parameters']['strategy']['customStrategy']['env']
    p = [env for env in env_json if env["name"] == "DOCK_PLUGINS"]
    inner = {
        "a": "b"
    }
    p[0]['value'] = json.dumps(inner)
    m = DockJsonManipulator(build_json, None)
    response = m.get_dock_json()
    assert response["a"] == inner["a"]


def test_manipulator_get_dock_json_missing_input():
    build_json = copy.deepcopy(BUILD_JSON)
    build_json['parameters']['strategy']['customStrategy']['env'] = None
    m = DockJsonManipulator(build_json, None)
    with pytest.raises(RuntimeError):
        m.get_dock_json()


def test_manipulator_merge():
    inner = copy.deepcopy(INNER_DOCK_JSON)
    plugin = [x for x in inner['prebuild_plugins'] if x["name"] == "a_plugin"][0]
    m = DockJsonManipulator(None, inner)
    m.dock_json_merge_arg("prebuild_plugins", "a_plugin", "key1", {"a": '3', "z": '9'})
    assert plugin['args']['key1']['a'] == '3'
    assert plugin['args']['key1']['b'] == '2'
    assert plugin['args']['key1']['z'] == '9'


def test_render_prod_request():
    this_file = inspect.getfile(test_render_prod_request)
    this_dir = os.path.dirname(this_file)
    parent_dir = os.path.dirname(this_dir)
    inputs_path = os.path.join(parent_dir, "inputs")
    bm = BuildManager(inputs_path)
    kwargs = {
        'git_uri': "http://git/",
        'git_ref': "master",
        'user': "john-foo",
        'component': "component",
        'registry_uri': "registry.example.com",
        'openshift_uri': "http://openshift/",
        'koji_target': "koji-target",
        'kojiroot': "http://root/",
        'kojihub': "http://hub/",
        'sources_command': "make",
        'architecture': "x86_64",
        'vendor': "Foo Vendor",
        'build_host': "our.build.host.example.com",
        'authoritative_registry': "registry.example.com",
    }
    build = bm.get_build("prod", **kwargs)


def test_get_user(openshift):
    l = openshift.get_user()
    assert l.json() is not None<commit_msg>Test build JSON for implicit labels.<commit_after>from __future__ import absolute_import, unicode_literals
import copy
import inspect
import json
import os

import pytest
import logging
from .fake_api import openshift
from osbs.build import DockJsonManipulator, BuildManager
from tests.constants import TEST_BUILD, TEST_LABEL, TEST_LABEL_VALUE


logger = logging.getLogger("osbs.tests")


def test_set_labels_on_build(openshift):
    l = openshift.set_labels_on_build(TEST_BUILD, {TEST_LABEL: TEST_LABEL_VALUE})
    assert l.json() is not None


def test_get_oauth_token(openshift):
    token = openshift.get_oauth_token()
    assert token is not None


def test_list_builds(openshift):
    l = openshift.list_builds()
    assert l is not None
    assert bool(l.json())  # is there at least something



#####
#
# build/DockJsonManipulator
#
#####

BUILD_JSON = {
    "metadata": {
        "name": "{{NAME}}"
    },
    "kind": "Build",
    "apiVersion": "v1beta1",
    "parameters": {
        "source": {
            "type": "Git",
            "git": {
                "uri": "{{GIT_URI}}"
            }
        },
        "strategy": {
            "type": "Custom",
            "customStrategy": {
                "image": "buildroot",
                "exposeDockerSocket": True,
                "env": [{
                    "name": "DOCK_PLUGINS",
                    "value": "TBD"
                }]
            }
        },
        "output": {
            "imageTag": "{{OUTPUT_IMAGE_TAG}}",
            "registry": "{{REGISTRY_URI}}"
        }
    }
}

INNER_DOCK_JSON = {
    "prebuild_plugins": [
        {
            "name": "change_from_in_dockerfile"
        },
        {
            "args": {
                "key1": {
                    "a": "1",
                    "b": "2"
                },
                "key2": "b"
            },
            "name": "a_plugin"
        },
    ],
    "postbuild_plugins": [
        {
            "args": {
                "image_id": "BUILT_IMAGE_ID"
            },
            "name": "all_rpm_packages"
        },
    ]
}


def test_manipulator():
    m = DockJsonManipulator(BUILD_JSON, INNER_DOCK_JSON)
    assert m is not None

    
def test_manipulator_get_dock_json():
    build_json = copy.deepcopy(BUILD_JSON)
    env_json = build_json['parameters']['strategy']['customStrategy']['env']
    p = [env for env in env_json if env["name"] == "DOCK_PLUGINS"]
    inner = {
        "a": "b"
    }
    p[0]['value'] = json.dumps(inner)
    m = DockJsonManipulator(build_json, None)
    response = m.get_dock_json()
    assert response["a"] == inner["a"]


def test_manipulator_get_dock_json_missing_input():
    build_json = copy.deepcopy(BUILD_JSON)
    build_json['parameters']['strategy']['customStrategy']['env'] = None
    m = DockJsonManipulator(build_json, None)
    with pytest.raises(RuntimeError):
        m.get_dock_json()


def test_manipulator_merge():
    inner = copy.deepcopy(INNER_DOCK_JSON)
    plugin = [x for x in inner['prebuild_plugins'] if x["name"] == "a_plugin"][0]
    m = DockJsonManipulator(None, inner)
    m.dock_json_merge_arg("prebuild_plugins", "a_plugin", "key1", {"a": '3', "z": '9'})
    assert plugin['args']['key1']['a'] == '3'
    assert plugin['args']['key1']['b'] == '2'
    assert plugin['args']['key1']['z'] == '9'


def test_render_prod_request():
    this_file = inspect.getfile(test_render_prod_request)
    this_dir = os.path.dirname(this_file)
    parent_dir = os.path.dirname(this_dir)
    inputs_path = os.path.join(parent_dir, "inputs")
    bm = BuildManager(inputs_path)
    kwargs = {
        'git_uri': "http://git/",
        'git_ref': "master",
        'user': "john-foo",
        'component': "component",
        'registry_uri': "registry.example.com",
        'openshift_uri': "http://openshift/",
        'koji_target': "koji-target",
        'kojiroot': "http://root/",
        'kojihub': "http://hub/",
        'sources_command': "make",
        'architecture': "x86_64",
        'vendor': "Foo Vendor",
        'build_host': "our.build.host.example.com",
        'authoritative_registry': "registry.example.com",
    }
    build = bm.get_build("prod", **kwargs)
    build_json = build.render ()
    strategy = build_json['parameters']['strategy']['customStrategy']['env']
    plugins_json = None
    for d in strategy:
        if d['name'] == 'DOCK_PLUGINS':
            plugins_json = d['value']
            break

    assert plugins_json is not None
    plugins = json.loads(plugins_json)

    labels = None
    for d in plugins['prebuild_plugins']:
        if d['name'] == 'add_labels_in_dockerfile':
            labels = d['args']['labels']

    assert labels is not None
    assert 'Architecture' in labels
    assert 'Authoritative_Registry' in labels
    assert 'Build_Host' in labels
    assert 'Vendor' in labels

def test_get_user(openshift):
    l = openshift.get_user()
    assert l.json() is not None
{'fraction_non_alphanumeric': 0.14867218110579017, 'fraction_numerical': 0.00457117979973879, 'mean_word_length': 2.4185267857142856, 'pattern_counts': {'":': 70, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1025502', 'n_tokens_mistral': 3096, 'n_tokens_neox': 2993, 'n_words': 581}