Skip to content

Commit

Permalink
#42: Fixed bug during preparation of the CI environment (#43)
Browse files Browse the repository at this point in the history
* Extract preparation of the CI environment into its own step and add that it creates the logging directory.
  • Loading branch information
tkilias authored Jul 6, 2023
1 parent e3da072 commit 5e7ae75
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 224 deletions.
1 change: 1 addition & 0 deletions doc/changes/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changes

* [1.3.1](changes_1.3.1.md)
* [1.3.0](changes_1.3.0.md)
* [1.2.0](changes_1.2.0.md)
* [1.1.0](changes_1.1.0.md)
Expand Down
23 changes: 23 additions & 0 deletions doc/changes/changes_1.3.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Script-Languages-Container-CI 1.3.1, 2023-07-05

Code name: Fix bug during preparation of the CI environment

## Summary

This release fixes a bug during preparation of the CI environment.

## Bug Fixes

- #42: Fix bug during preparation of the CI environment

## Features / Enhancements

n/a

## Documentation

n/a

## Refactoring

n/a
15 changes: 5 additions & 10 deletions exasol_script_languages_container_ci/lib/ci.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import logging
import os
from pathlib import Path
from typing import Set

from exasol_integration_test_docker_environment.cli.options.system_options import DEFAULT_OUTPUT_DIRECTORY
from exasol_integration_test_docker_environment.lib.base import luigi_log_config
from typing import Set, Callable

from exasol_script_languages_container_ci.lib.branch_config import BranchConfig
from exasol_script_languages_container_ci.lib.ci_build import CIBuild
from exasol_script_languages_container_ci.lib.ci_prepare import CIPrepare
from exasol_script_languages_container_ci.lib.ci_push import CIPush
from exasol_script_languages_container_ci.lib.ci_security_scan import CISecurityScan
from exasol_script_languages_container_ci.lib.ci_test import CIExecuteTest
Expand Down Expand Up @@ -56,7 +52,8 @@ def ci(flavor: str,
ci_build: CIBuild = CIBuild(),
ci_execute_tests: CIExecuteTest = CIExecuteTest(),
ci_push: CIPush = CIPush(),
ci_security_scan: CISecurityScan = CISecurityScan()):
ci_security_scan: CISecurityScan = CISecurityScan(),
ci_prepare: CIPrepare = CIPrepare()):
"""
Run CI build:
1. Build image
Expand All @@ -71,9 +68,7 @@ def ci(flavor: str,
rebuild = BranchConfig.rebuild(branch_name)
needs_to_build = check_if_need_to_build(branch_name, build_config, flavor, git_access)
if needs_to_build:
log_path = Path(DEFAULT_OUTPUT_DIRECTORY) / "jobs" / "logs" / "main.log"
os.environ[luigi_log_config.LOG_ENV_VARIABLE_NAME] = f"{log_path.absolute()}"

ci_prepare.prepare()
ci_build.build(flavor_path=flavor_path,
rebuild=rebuild,
build_docker_repository=docker_build_repository,
Expand Down
13 changes: 13 additions & 0 deletions exasol_script_languages_container_ci/lib/ci_prepare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
from pathlib import Path

from exasol_integration_test_docker_environment.cli.options.system_options import DEFAULT_OUTPUT_DIRECTORY
from exasol_integration_test_docker_environment.lib.base import luigi_log_config


class CIPrepare:

def prepare(self):
log_path = Path(DEFAULT_OUTPUT_DIRECTORY) / "jobs" / "logs" / "main.log"
log_path.mkdir(parents=True, exist_ok=True)
os.environ[luigi_log_config.LOG_ENV_VARIABLE_NAME] = f"{log_path.absolute()}"
9 changes: 5 additions & 4 deletions exasol_script_languages_container_ci/lib/release.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import logging
import os
from pathlib import Path
from typing import Callable

from exasol_integration_test_docker_environment.cli.options.system_options import DEFAULT_OUTPUT_DIRECTORY
from exasol_integration_test_docker_environment.lib.base import luigi_log_config

from exasol_script_languages_container_ci.lib.ci_build import CIBuild
from exasol_script_languages_container_ci.lib.ci_prepare import CIPrepare
from exasol_script_languages_container_ci.lib.ci_push import CIPush
from exasol_script_languages_container_ci.lib.ci_security_scan import CISecurityScan
from exasol_script_languages_container_ci.lib.ci_test import CIExecuteTest
Expand All @@ -25,7 +27,8 @@ def release(flavor: str,
ci_build: CIBuild = CIBuild(),
ci_execute_tests: CIExecuteTest = CIExecuteTest(),
ci_push: CIPush = CIPush(),
ci_security_scan: CISecurityScan = CISecurityScan()):
ci_security_scan: CISecurityScan = CISecurityScan(),
ci_prepare: CIPrepare = CIPrepare()):
"""
Run Release build:
1. Build image
Expand All @@ -37,9 +40,7 @@ def release(flavor: str,

flavor_path = (f"flavors/{flavor}",)
test_container_folder = "test_container"
log_path = Path(DEFAULT_OUTPUT_DIRECTORY) / "jobs" / "logs" / "main.log"
os.environ[luigi_log_config.LOG_ENV_VARIABLE_NAME] = f"{log_path.absolute()}"

ci_prepare.prepare()
ci_build.build(flavor_path=flavor_path, rebuild=True,
build_docker_repository=None,
commit_sha="",
Expand Down
249 changes: 90 additions & 159 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "exasol-script-languages-container-ci"
version = "1.3.0"
version = "1.3.1"
description = "Implements CI builds for script-language-container."

license = "MIT"
Expand Down
5 changes: 5 additions & 0 deletions test/integration_tests/test_ci_build.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import logging
import os
from pathlib import Path

import pytest

from exasol_script_languages_container_ci.lib.ci_build import CIBuild
Expand All @@ -21,6 +25,7 @@ def test(input_docker_build_repository,
test_type = "successful"
flavor_path = str(flavors_path / test_type)
test_container_folder = str(test_containers_folder / test_type)
print("cwd",Path(".").absolute())
with not_raises(Exception):
CIBuild().build(
flavor_path=(flavor_path,),
Expand Down
7 changes: 4 additions & 3 deletions test/unit_tests/ci_calls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from unittest.mock import call

from exasol_integration_test_docker_environment.lib.api.build_test_container import build_test_container
from exasol_script_languages_container_tool.cli.commands import build, run_db_test, security_scan, push

from test.unit_tests.test_env import test_env


def prepare():
return call.prepare()


def build_ci_call(force_rebuild: bool):
return call.build(flavor_path=("flavors/TEST_FLAVOR",),
rebuild=force_rebuild,
Expand Down
77 changes: 43 additions & 34 deletions test/unit_tests/test_ci.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Union
from unittest.mock import create_autospec, MagicMock, Mock
from unittest.mock import Mock

import pytest

Expand All @@ -8,48 +8,56 @@
from exasol_script_languages_container_ci.lib.ci_push import CIPush
from exasol_script_languages_container_ci.lib.ci_security_scan import CISecurityScan
from exasol_script_languages_container_ci.lib.ci_test import CIExecuteTest

from test.unit_tests import ci_calls

from test.unit_tests.test_env import test_env

# Testdata contain tuples of (branch, list(calls to CICommands))
# The goal is to test that for specific branches the correct list of calls (with expected arguments) is passed to the CICommands
testdata_ci = [
("refs/heads/feature/test_branch", [ci_calls.build_ci_call(force_rebuild=False),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_build_repo_with_sha_call(),
ci_calls.push_build_repo_without_sha_call()]
("refs/heads/feature/test_branch", [
ci_calls.prepare(),
ci_calls.build_ci_call(force_rebuild=False),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_build_repo_with_sha_call(),
ci_calls.push_build_repo_without_sha_call()]
),
("refs/heads/rebuild/feature/test_branch", [ci_calls.build_ci_call(force_rebuild=True),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_build_repo_with_sha_call(),
ci_calls.push_build_repo_without_sha_call()]
("refs/heads/rebuild/feature/test_branch", [
ci_calls.prepare(),
ci_calls.build_ci_call(force_rebuild=True),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_build_repo_with_sha_call(),
ci_calls.push_build_repo_without_sha_call()]
),
("refs/heads/master", [ci_calls.build_ci_call(force_rebuild=True),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_build_repo_with_sha_call(),
ci_calls.push_build_repo_without_sha_call(),
ci_calls.push_release_repo()
]
("refs/heads/master", [
ci_calls.prepare(),
ci_calls.build_ci_call(force_rebuild=True),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_build_repo_with_sha_call(),
ci_calls.push_build_repo_without_sha_call(),
ci_calls.push_release_repo()
]
),
("refs/heads/main", [ci_calls.build_ci_call(force_rebuild=True),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_build_repo_with_sha_call(),
ci_calls.push_build_repo_without_sha_call(),
ci_calls.push_release_repo()
]
("refs/heads/main", [
ci_calls.prepare(),
ci_calls.build_ci_call(force_rebuild=True),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_build_repo_with_sha_call(),
ci_calls.push_build_repo_without_sha_call(),
ci_calls.push_release_repo()
]
),
("refs/heads/develop", [ci_calls.build_ci_call(force_rebuild=True),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_build_repo_with_sha_call(),
ci_calls.push_build_repo_without_sha_call()
]
("refs/heads/develop", [
ci_calls.prepare(),
ci_calls.build_ci_call(force_rebuild=True),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_build_repo_with_sha_call(),
ci_calls.push_build_repo_without_sha_call()
]
),

]
Expand Down Expand Up @@ -78,5 +86,6 @@ def test_branches(branch, git_access_mock, expected_calls, build_config):
ci_build=ci_commands_mock,
ci_push=ci_commands_mock,
ci_execute_tests=ci_commands_mock,
ci_security_scan=ci_commands_mock)
ci_security_scan=ci_commands_mock,
ci_prepare=ci_commands_mock)
assert ci_commands_mock.mock_calls == expected_calls
27 changes: 27 additions & 0 deletions test/unit_tests/test_ci_prepare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from pathlib import Path
from unittest import mock

import pytest
from exasol_integration_test_docker_environment.cli.options.system_options import DEFAULT_OUTPUT_DIRECTORY
from exasol_integration_test_docker_environment.lib.base import luigi_log_config

from exasol_script_languages_container_ci.lib.ci_prepare import CIPrepare


@pytest.fixture(autouse=True)
def mock_settings_env_vars():
with mock.patch.dict(os.environ, {}):
yield


def test_ci_prepare_log_environment_variable_is_set():
CIPrepare().prepare()
expected_log_path = str(Path(DEFAULT_OUTPUT_DIRECTORY) / "jobs" / "logs" / "main.log")
assert luigi_log_config.LOG_ENV_VARIABLE_NAME in os.environ \
and os.environ[luigi_log_config.LOG_ENV_VARIABLE_NAME].endswith(expected_log_path)


def test_ci_prepare_log_path_exists():
CIPrepare().prepare()
assert Path(os.environ[luigi_log_config.LOG_ENV_VARIABLE_NAME]).parent.is_dir()
31 changes: 18 additions & 13 deletions test/unit_tests/test_release.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,28 @@

# Testdata contain tuples of (dry_run, list(calls to CICommands))
testdata_ci = [
(True, [ci_calls.build_release_call(),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.release_upload()
]
(True, [
ci_calls.prepare(),
ci_calls.build_release_call(),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.release_upload()
]
),
(False, [ci_calls.build_release_call(),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_release_repo(),
ci_calls.release_upload()
]
(False, [
ci_calls.prepare(),
ci_calls.build_release_call(),
ci_calls.run_db_test_call(),
ci_calls.security_scan_call(),
ci_calls.push_release_repo(),
ci_calls.release_upload()
]
),
]


@pytest.mark.parametrize("is_dry_run,expected_calls", testdata_ci)
def test(is_dry_run: bool, expected_calls, build_config:Config):
def test(is_dry_run: bool, expected_calls, build_config: Config):
"""
Test that the correct steps are executed for the release:
1. Build Image (force_rebuild = true/false)
Expand All @@ -57,5 +61,6 @@ def test(is_dry_run: bool, expected_calls, build_config:Config):
ci_build=ci_commands_mock,
ci_push=ci_commands_mock,
ci_execute_tests=ci_commands_mock,
ci_security_scan=ci_commands_mock)
ci_security_scan=ci_commands_mock,
ci_prepare=ci_commands_mock)
assert ci_commands_mock.mock_calls == expected_calls

0 comments on commit 5e7ae75

Please sign in to comment.