Skip to content

Commit

Permalink
Add all tests
Browse files Browse the repository at this point in the history
Signed-off-by: Divya Madala <[email protected]>
  • Loading branch information
Divyaasm committed Apr 30, 2024
1 parent 9e46c1d commit 2038692
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 137 deletions.
3 changes: 1 addition & 2 deletions src/run_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import sys

from system import console
from validation_workflow.validation_args import ValidationArgs
from system.temporary_directory import TemporaryDirectory

from validation_workflow.validation_args import ValidationArgs
from validation_workflow.validation_test_runner import ValidationTestRunner # type: ignore


Expand Down
1 change: 0 additions & 1 deletion src/validation_workflow/deb/validation_deb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from system.execute import execute
from system.temporary_directory import TemporaryDirectory

from test_workflow.integ_test.utils import get_password
from validation_workflow.api_test_cases import ApiTestCases
from validation_workflow.download_utils import DownloadUtils
Expand Down
1 change: 0 additions & 1 deletion src/validation_workflow/docker/validation_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from typing import Any

from system.temporary_directory import TemporaryDirectory

from test_workflow.integ_test.utils import get_password
from validation_workflow.api_test_cases import ApiTestCases
from validation_workflow.docker.inspect_docker_image import InspectDockerImage
Expand Down
5 changes: 3 additions & 2 deletions src/validation_workflow/download_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.

import logging

import os

import requests

from system.temporary_directory import TemporaryDirectory


Expand All @@ -22,7 +24,6 @@ def is_url_valid(url: str) -> bool:
def download(url: str, tmp_dir: TemporaryDirectory) -> bool:
# This method writes the contents from the response object into temporary directory file name fetched from the end of the url.
response = requests.get(url, stream=True)
logging.info(tmp_dir.name)
path = os.path.join(tmp_dir.name, os.path.basename(url))
status = bool(open(path, "wb").write(response.content))
return status
3 changes: 1 addition & 2 deletions src/validation_workflow/rpm/validation_rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import logging
import os

from system.temporary_directory import TemporaryDirectory

from system.execute import execute
from system.temporary_directory import TemporaryDirectory
from test_workflow.integ_test.utils import get_password
from validation_workflow.api_test_cases import ApiTestCases
from validation_workflow.download_utils import DownloadUtils
Expand Down
1 change: 0 additions & 1 deletion src/validation_workflow/tar/validation_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from system.execute import execute
from system.process import Process
from system.temporary_directory import TemporaryDirectory

from test_workflow.integ_test.utils import get_password
from validation_workflow.api_test_cases import ApiTestCases
from validation_workflow.download_utils import DownloadUtils
Expand Down
1 change: 0 additions & 1 deletion src/validation_workflow/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import requests

from system.temporary_directory import TemporaryDirectory

from validation_workflow.api_request import ApiTest
from validation_workflow.download_utils import DownloadUtils
from validation_workflow.validation_args import ValidationArgs
Expand Down
1 change: 0 additions & 1 deletion src/validation_workflow/validation_test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
# type: ignore

from system.temporary_directory import TemporaryDirectory

from validation_workflow.deb.validation_deb import ValidateDeb
from validation_workflow.docker.validation_docker import ValidateDocker
from validation_workflow.rpm.validation_rpm import ValidateRpm
Expand Down
3 changes: 1 addition & 2 deletions src/validation_workflow/yum/validation_yum.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
import logging
import os

from system.temporary_directory import TemporaryDirectory

from system.execute import execute
from system.temporary_directory import TemporaryDirectory
from test_workflow.integ_test.utils import get_password
from validation_workflow.api_test_cases import ApiTestCases
from validation_workflow.download_utils import DownloadUtils
Expand Down
8 changes: 3 additions & 5 deletions src/validation_workflow/zip/validation_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import os

from system.process import Process
from system.zip_file import ZipFile
from system.temporary_directory import TemporaryDirectory

from system.zip_file import ZipFile
from test_workflow.integ_test.utils import get_password
from validation_workflow.api_test_cases import ApiTestCases
from validation_workflow.download_utils import DownloadUtils
Expand Down Expand Up @@ -61,9 +60,8 @@ def validation(self) -> bool:

def cleanup(self) -> bool:
try:
if self.succesful_checks > 0:
self.os_process.terminate()
if ("opensearch-dashboards" in self.args.projects) and self.succesful_checks == 2:
self.os_process.terminate()
if ("opensearch-dashboards" in self.args.projects):
self.osd_process.terminate()
except:
raise Exception('Failed to terminate the processes that started OpenSearch and OpenSearch-Dashboards')
Expand Down
74 changes: 50 additions & 24 deletions tests/tests_validation_workflow/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# compatible open source license.

import unittest
from unittest import mock
from unittest.mock import Mock, patch

import requests
Expand Down Expand Up @@ -39,97 +40,122 @@ class TestValidation(unittest.TestCase):

@patch('validation_workflow.download_utils.DownloadUtils')
@patch('validation_workflow.tar.validation_tar.ValidationArgs')
def test_check_url_valid(self, mock_validation_args: Mock, mock_download_utils: Mock) -> None:
@patch('system.temporary_directory.TemporaryDirectory')
def test_check_url_valid(self, mock_temporary_directory: Mock, mock_validation_args: Mock, mock_download_utils: Mock) -> None:
mock_validation_args.projects.return_value = ["opensearch"]
mock_temporary_directory.return_value.path = "/tmp/trytytyuit/"
mock_temporary_directory.return_value.name = "/tmp/trytytyuit/"
mock_validation = ValidateTar(mock_validation_args.return_value, mock_temporary_directory.return_value)

mock_validation = ValidateTar(mock_validation_args.return_value)
mock_download_utils_download = mock_download_utils.return_value
mock_download_utils_download.download.return_value = True
mock_download_utils_download.is_url_valid.return_value = True
url = "https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.12/latest/linux/x64/rpm/dist/opensearch/opensearch-1.3.12.staging.repo"

result = mock_validation.check_url(url)
self.assertTrue(result)
with mock.patch("os.path.join") as mock_join, \
mock.patch("os.path.basename") as mock_basename, \
mock.patch("builtins.open") as mock_open:
mock_join.return_value = "mocked_path"
mock_basename.return_value = "mocked_filename"
response_content = "Mocked content"
mock_open.return_value.write.return_value = len(response_content)

mock_download_utils_download = mock_download_utils.return_value
mock_download_utils_download.download.return_value = True
mock_download_utils_download.is_url_valid.return_value = True

result = mock_validation.check_url(url)

self.assertTrue(result)

@patch('shutil.copy2', return_value=True)
@patch('validation_workflow.tar.validation_tar.ValidationArgs')
def test_copy_artifact(self, mock_validation_args: Mock, mock_copy: Mock) -> None:
@patch('system.temporary_directory.TemporaryDirectory')
def test_copy_artifact(self, mock_temporary_directory: Mock, mock_validation_args: Mock, mock_copy: Mock) -> None:
mock_validation_args.projects.return_value = ["opensearch"]
mock_validation = ValidateTar(mock_validation_args.return_value)
mock_temporary_directory.return_value.path = "/tmp/trytytyuit/"
mock_validation = ValidateTar(mock_validation_args.return_value, mock_temporary_directory.return_value)

url = "https://ci.opensearch.org/ci/dbc/distribution-build-opensearch/1.3.12/latest/linux/x64/rpm/dist/opensearch/opensearch-1.3.12.staging.repo"

result = mock_validation.copy_artifact(url, "tmp/tthcdhfh/")
result = mock_validation.copy_artifact(url, mock_temporary_directory.return_value.path)
self.assertTrue(result)

@patch('os.path.exists')
@patch('validation_workflow.tar.validation_tar.ValidationArgs')
def test_check_for_security_plugin(self, mock_validation_args: Mock, mock_path_exists: Mock) -> None:
@patch('system.temporary_directory.TemporaryDirectory')
def test_check_for_security_plugin(self, mock_temporary_directory: Mock, mock_validation_args: Mock, mock_path_exists: Mock) -> None:
mock_path_exists.return_value = True

mock_validation_args.projects.return_value = ["opensearch"]
mock_validation = ValidateTar(mock_validation_args.return_value)
mock_temporary_directory.return_value.path = "/tmp/trytytyuit/"

mock_validation = ValidateTar(mock_validation_args.return_value, mock_temporary_directory.return_value)

result = mock_validation.check_for_security_plugin("/tmp/tmkuiuo/opensearch")
result = mock_validation.check_for_security_plugin("/tmp/trytytyuit/opensearch")

self.assertTrue(result)

@patch("time.sleep")
@patch('validation_workflow.validation.Validation.check_http_request')
@patch('validation_workflow.validation.ValidationArgs')
def test_check_cluster_readiness_error(self, mock_validation_args: Mock, mock_check_http: Mock, mock_sleep: Mock) -> None:
@patch('system.temporary_directory.TemporaryDirectory')
def test_check_cluster_readiness_error(self, mock_temporary_directory: Mock, mock_validation_args: Mock, mock_check_http: Mock, mock_sleep: Mock) -> None:
mock_validation_args.return_value.version = '1.0.0.1000'
mock_validation_args.return_value.validate_digest_only = False
mock_validation_args.return_value.allow_http = False
mock_validation_args.return_value.projects = ["opensearch"]
mock_temporary_directory.return_value.path = "/tmp/trytytyuit/"
mock_check_http.return_value = False

validate_docker = ValidateTar(mock_validation_args.return_value)
result = validate_docker.check_cluster_readiness()
validate_tar = ValidateTar(mock_validation_args.return_value, mock_temporary_directory.return_value)
result = validate_tar.check_cluster_readiness()

self.assertFalse(result)

@patch("time.sleep")
@patch('validation_workflow.validation.ValidationArgs')
@patch('system.temporary_directory.TemporaryDirectory')
@patch.object(ApiTest, "api_get")
def test_check_http_request(self, mock_api_get: Mock, mock_validation_args: Mock, mock_sleep: Mock) -> None:
def test_check_http_request(self, mock_api_get: Mock, mock_temporary_directory: Mock, mock_validation_args: Mock, mock_sleep: Mock) -> None:
mock_validation_args.return_value.version = '1.3.13'
mock_validation_args.return_value.validate_digest_only = False
mock_validation_args.return_value.allow_http = False
mock_validation_args.return_value.projects = ["opensearch", "opensearch-dashboards"]
mock_api_get.return_value = (200, "text")
mock_temporary_directory.return_value.path = "/tmp/trytytyuit/"

validate_docker = ValidateTar(mock_validation_args.return_value)
result = validate_docker.check_http_request()
validate_tar = ValidateTar(mock_validation_args.return_value, mock_temporary_directory.return_value)
result = validate_tar.check_http_request()

self.assertTrue(result)

@patch("time.sleep")
@patch('validation_workflow.validation.ValidationArgs')
@patch('system.temporary_directory.TemporaryDirectory')
@patch.object(ApiTest, "api_get")
def test_check_http_request_error(self, mock_api_get: Mock, mock_validation_args: Mock, mock_sleep: Mock) -> None:
def test_check_http_request_error(self, mock_api_get: Mock, mock_temporary_directory: Mock, mock_validation_args: Mock, mock_sleep: Mock) -> None:
mock_validation_args.return_value.version = '1.3.14'
mock_validation_args.return_value.validate_digest_only = False
mock_validation_args.return_value.allow_http = False
mock_validation_args.return_value.projects = ["opensearch"]
mock_api_get.return_value = (400, "text")
mock_temporary_directory.return_value.path = "/tmp/trytytyuit/"

validate_docker = ValidateTar(mock_validation_args.return_value)
validate_docker = ValidateTar(mock_validation_args.return_value, mock_temporary_directory.return_value)
result = validate_docker.check_http_request()

self.assertFalse(result)

@patch("time.sleep")
@patch('validation_workflow.validation.ValidationArgs')
@patch('system.temporary_directory.TemporaryDirectory')
@patch.object(ApiTest, "api_get")
def test_check_http_request_connection_error(self, mock_api_get: Mock, mock_validation_args: Mock, mock_sleep: Mock) -> None:
def test_check_http_request_connection_error(self, mock_api_get: Mock, mock_temporary_directory: Mock, mock_validation_args: Mock, mock_sleep: Mock) -> None:
mock_validation_args.return_value.version = '2.3.0'
mock_validation_args.return_value.validate_digest_only = False
mock_validation_args.return_value.allow_http = False
mock_validation_args.return_value.projects = ["opensearch"]
mock_temporary_directory.return_value.path = "/tmp/trytytyuit/"
mock_api_get.side_effect = requests.exceptions.ConnectionError

validate_docker = ValidateDocker(mock_validation_args.return_value)
validate_docker = ValidateDocker(mock_validation_args.return_value, mock_temporary_directory.return_value)

result = validate_docker.check_http_request()

Expand Down
Loading

0 comments on commit 2038692

Please sign in to comment.