Skip to content

Commit

Permalink
Unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: dblock <dblock@dblock.org>
  • Loading branch information
dblock committed Sep 9, 2021
1 parent b6f6167 commit bf10dc5
Show file tree
Hide file tree
Showing 17 changed files with 739 additions and 45 deletions.
2 changes: 1 addition & 1 deletion DEVELOPER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Run from `bundle-workflow` before making pull requests.
cd bundle-workflow
pipenv run isort .
git status -s | cut -c4- | xargs pipenv run black
git status -s | grep -e "[MA?]\s.*.py" | cut -c4- | xargs pipenv run black
pipenv run flake8
pipenv run pytest
pipenv run mypy .
Expand Down
2 changes: 1 addition & 1 deletion bundle-workflow/src/build_workflow/build_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import yaml

from manifests.build_manifest import BuildManifest
from system.properties_file import PropertiesFile # type: ignore
from system.properties_file import PropertiesFile


class BuildRecorder:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re

from ci_workflow.check import Check
from system.properties_file import PropertiesFile # type: ignore
from system.properties_file import PropertiesFile


class CheckGradleDependencies(Check):
Expand Down
2 changes: 1 addition & 1 deletion bundle-workflow/src/ci_workflow/check_gradle_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# compatible open source license.

from ci_workflow.check import Check
from system.properties_file import PropertiesFile # type: ignore
from system.properties_file import PropertiesFile


class CheckGradleProperties(Check):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@


class CheckGradlePublishToMavenLocal(Check):
def __init__(self, component, git_repo, version, arch, snapshot):
super().__init__(component, git_repo, version, arch, snapshot)

def check(self):
cmd = " ".join(
[
Expand All @@ -20,4 +17,4 @@ def check(self):
]
)

self.git_repo.output(cmd)
self.git_repo.execute(cmd)
2 changes: 1 addition & 1 deletion bundle-workflow/src/ci_workflow/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

from ci_workflow.check_gradle_dependencies_open_search import (
from ci_workflow.check_gradle_dependencies_opensearch import (
CheckGradleDependenciesOpenSearchVersion,
CheckGradlePluginDependenciesOpenSearchVersion)
from ci_workflow.check_gradle_properties_version import \
Expand Down
9 changes: 7 additions & 2 deletions bundle-workflow/src/system/properties_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ def __init__(self, key, expected, current=None):
else f"Expected to have {key}=any of {expected}, but none was found."
)

def __init__(self, data):
def __init__(self, data=None):
super().__init__(self)
self.load(data)
if type(data) is str:
self.load(data)
elif type(data) is dict:
self.properties = data
elif data is not None:
raise TypeError()

def get_value(self, key, default_value=None):
try:
Expand Down

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions bundle-workflow/tests/tests_ci_workflow/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
# compatible open source license.

import unittest
from unittest.mock import MagicMock, PropertyMock
from manifests.input_manifest import InputManifest
from unittest.mock import MagicMock

from ci_workflow.check import Check
from manifests.input_manifest import InputManifest


class DummyTestCheck(Check):
def check():
def check(self):
pass


class TestCheck(unittest.TestCase):
def setUp(self):
self.check = DummyTestCheck(
Expand All @@ -31,6 +33,7 @@ def test_properties(self):
self.assertEqual(self.check.opensearch_version, "1.1.0")
self.assertEqual(self.check.component_version, "1.1.0.0")


class TestCheckSnapshot(unittest.TestCase):
def setUp(self):
self.check = DummyTestCheck(
Expand All @@ -46,14 +49,13 @@ def test_snapshot_version(self):
self.assertEqual(self.check.opensearch_version, "1.1.0-SNAPSHOT")
self.assertEqual(self.check.component_version, "1.1.0.0-SNAPSHOT")


class TestCheckOpenSearch(unittest.TestCase):
def setUp(self):
self.check = DummyTestCheck(
component=InputManifest.Component({
"name": "OpenSearch",
"repository": "",
"ref": "",
}),
component=InputManifest.Component(
{"name": "OpenSearch", "repository": "", "ref": ""}
),
git_repo=MagicMock(),
version="1.1.0",
arch="x86",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

import os
import unittest
from unittest.mock import MagicMock

from ci_workflow.check_gradle_dependencies import CheckGradleDependencies


class TestCheckGradleDependencies(unittest.TestCase):
class DummyDependencies(CheckGradleDependencies):
def check(self):
pass

def __mock_dependencies(self, props="", snapshot=False, gradle_project=None):
git_repo = MagicMock()
git_repo.output.return_value = props

return TestCheckGradleDependencies.DummyDependencies(
component=MagicMock(),
git_repo=git_repo,
version="1.1.0",
arch="x86",
snapshot=snapshot,
gradle_project=gradle_project,
)

def test_executes_gradle_dependencies(self):
check = self.__mock_dependencies()
check.git_repo.output.assert_called_once_with(
'./gradlew :dependencies -Dopensearch.version=1.1.0 -Dbuild.snapshot=false | grep -e "---"'
)

def test_executes_gradle_dependencies_snapshot(self):
check = self.__mock_dependencies(snapshot=True)
check.git_repo.output.assert_called_once_with(
'./gradlew :dependencies -Dopensearch.version=1.1.0-SNAPSHOT -Dbuild.snapshot=true | grep -e "---"'
)

def test_executes_gradle_dependencies_project(self):
check = self.__mock_dependencies(snapshot=True, gradle_project="project")
check.git_repo.output.assert_called_once_with(
'./gradlew project:dependencies -Dopensearch.version=1.1.0-SNAPSHOT -Dbuild.snapshot=true | grep -e "---"'
)

def test_loads_tree(self):
data_path = os.path.join(
os.path.dirname(__file__), "data/job_scheduler_dependencies.txt"
)
with open(data_path) as f:
check = self.__mock_dependencies(props=f.read())
self.assertEqual(
check.dependencies.get_value("org.opensearch:opensearch"),
"1.1.0-SNAPSHOT",
)
self.assertEqual(
check.dependencies.get_value(
"org.opensearch:opensearch/org.opensearch:opensearch-core"
),
"1.1.0-SNAPSHOT",
)
self.assertEqual(
check.dependencies.get_value("com.puppycrawl.tools:checkstyle"), "8.29"
)
self.assertEqual(
check.dependencies.get_value(
"com.puppycrawl.tools:checkstyle/antlr:antlr"
),
"2.7.7",
)
self.assertEqual(
check.dependencies.get_value(
"com.puppycrawl.tools:checkstyle/commons-beanutils:commons-beanutils/commons-collections:commons-collections"
),
"3.2.2",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

import unittest
from unittest.mock import MagicMock, patch

from ci_workflow.check_gradle_dependencies_opensearch import (
CheckGradleDependenciesOpenSearchVersion,
CheckGradlePluginDependenciesOpenSearchVersion)
from system.properties_file import PropertiesFile


class TestCheckGradleDependenciesOpenSearchVersion(unittest.TestCase):
def __mock_check(self, props=None):
with patch.object(
CheckGradleDependenciesOpenSearchVersion,
"_CheckGradleDependencies__get_dependencies",
) as mock_dependencies:
mock_dependencies.return_value = PropertiesFile(props)
return CheckGradleDependenciesOpenSearchVersion(
component=MagicMock(),
git_repo=MagicMock(),
version="1.1.0",
arch="x86",
snapshot=True,
)

def test_gradle_project(self):
self.assertIsNone(self.__mock_check().gradle_project)

def test_has_version(self):
self.__mock_check({"org.opensearch:opensearch": "1.1.0-SNAPSHOT"}).check()

def test_missing_version(self):
with self.assertRaises(PropertiesFile.UnexpectedKeyValueError) as err:
self.__mock_check({}).check()
self.assertEqual(
str(err.exception),
"Expected to have org.opensearch:opensearch='1.1.0-SNAPSHOT', but none was found.",
)

def test_invalid_version(self):
with self.assertRaises(PropertiesFile.UnexpectedKeyValueError) as err:
self.__mock_check({"org.opensearch:opensearch": "1.2.0-SNAPSHOT"}).check()
self.assertEqual(
str(err.exception),
"Expected to have org.opensearch:opensearch='1.1.0-SNAPSHOT', but was '1.2.0-SNAPSHOT'.",
)

def test_executes_gradle_command(self):
check = CheckGradleDependenciesOpenSearchVersion(
component=MagicMock(),
git_repo=MagicMock(),
version="1.1.0",
arch="x86",
snapshot=True,
)
check.git_repo.output.assert_called_once_with(
'./gradlew :dependencies -Dopensearch.version=1.1.0-SNAPSHOT -Dbuild.snapshot=true | grep -e "---"'
)


class TestCheckGradlePluginDependenciesOpenSearchVersion(unittest.TestCase):
def test_executes_gradle_plugin_command(self):
check = CheckGradlePluginDependenciesOpenSearchVersion(
component=MagicMock(),
git_repo=MagicMock(),
version="1.1.0",
arch="x86",
snapshot=True,
)
self.assertEqual(check.gradle_project, "plugin")
check.git_repo.output.assert_called_once_with(
'./gradlew plugin:dependencies -Dopensearch.version=1.1.0-SNAPSHOT -Dbuild.snapshot=true | grep -e "---"'
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

import unittest
from unittest.mock import MagicMock

from ci_workflow.check_gradle_properties import CheckGradleProperties


class TestCheckGradleProperties(unittest.TestCase):
class DummyProperties(CheckGradleProperties):
def check(self):
pass

def test_executes_gradle_properties(self):
git_repo = MagicMock()
git_repo.output.return_value = ""

TestCheckGradleProperties.DummyProperties(
component=MagicMock(),
git_repo=git_repo,
version="1.1.0",
arch="x86",
snapshot=False,
)

git_repo.output.assert_called_once_with(
"./gradlew properties -Dopensearch.version=1.1.0 -Dbuild.snapshot=false"
)

def test_executes_gradle_properties_snapshot(self):
git_repo = MagicMock()
git_repo.output.return_value = ""

TestCheckGradleProperties.DummyProperties(
component=MagicMock(),
git_repo=git_repo,
version="1.1.0",
arch="x86",
snapshot=True,
)

git_repo.output.assert_called_once_with(
"./gradlew properties -Dopensearch.version=1.1.0-SNAPSHOT -Dbuild.snapshot=true"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

import unittest
from unittest.mock import MagicMock, patch

from ci_workflow.check_gradle_properties_version import \
CheckGradlePropertiesVersion
from system.properties_file import PropertiesFile


class TestCheckGradlePropertiesVersion(unittest.TestCase):
def __mock_check(self, props=None):
with patch.object(
CheckGradlePropertiesVersion, "_CheckGradleProperties__get_properties"
) as mock_properties:
mock_properties.return_value = PropertiesFile(props)
return CheckGradlePropertiesVersion(
component=MagicMock(),
git_repo=MagicMock(),
version="1.1.0",
arch="x86",
snapshot=True,
)

def test_has_version(self):
self.__mock_check({"version": "1.1.0.0-SNAPSHOT"}).check()

def test_missing_version(self):
with self.assertRaises(PropertiesFile.UnexpectedKeyValueError) as err:
self.__mock_check().check()
self.assertEqual(
str(err.exception),
"Expected to have version='1.1.0.0-SNAPSHOT', but none was found.",
)

def test_invalid_version(self):
with self.assertRaises(PropertiesFile.UnexpectedKeyValueError) as err:
self.__mock_check({"version": "1.2.0-SNAPSHOT"}).check()
self.assertEqual(
str(err.exception),
"Expected to have version='1.1.0.0-SNAPSHOT', but was '1.2.0-SNAPSHOT'.",
)
Loading

0 comments on commit bf10dc5

Please sign in to comment.