Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions aws_lambda_builders/workflows/dotnet_clipackage/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Actions for .NET dependency resolution with CLI Package
"""

import threading
import os
import logging

Expand All @@ -14,6 +15,8 @@


class GlobalToolInstallAction(BaseAction):
__lock = threading.Lock()
__tools_installed = False

"""
A Lambda Builder Action which installs the Amazon.Lambda.Tools .NET Core Global Tool
Expand All @@ -28,15 +31,30 @@ def __init__(self, subprocess_dotnet):
self.subprocess_dotnet = subprocess_dotnet

def execute(self):
try:
LOG.debug("Installing Amazon.Lambda.Tools Global Tool")
self.subprocess_dotnet.run(["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"])
except DotnetCLIExecutionError as ex:
LOG.debug("Error installing probably due to already installed. Attempt to update to latest version.")
# run Amazon.Lambda.Tools update in sync block in case build is triggered in parallel
with GlobalToolInstallAction.__lock:
LOG.debug("Entered synchronized block for updating Amazon.Lambda.Tools")

# check if Amazon.Lambda.Tools updated recently
if GlobalToolInstallAction.__tools_installed:
LOG.info("Skipping to update Amazon.Lambda.Tools install/update, since it is updated recently")
return

try:
self.subprocess_dotnet.run(["tool", "update", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"])
LOG.debug("Installing Amazon.Lambda.Tools Global Tool")
self.subprocess_dotnet.run(["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"])
GlobalToolInstallAction.__tools_installed = True
except DotnetCLIExecutionError as ex:
raise ActionFailedError("Error configuring the Amazon.Lambda.Tools .NET Core Global Tool: " + str(ex))
LOG.debug("Error installing probably due to already installed. Attempt to update to latest version.")
try:
self.subprocess_dotnet.run(
["tool", "update", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"]
)
GlobalToolInstallAction.__tools_installed = True
except DotnetCLIExecutionError as ex:
raise ActionFailedError(
"Error configuring the Amazon.Lambda.Tools .NET Core Global Tool: " + str(ex)
)


class RunPackageAction(BaseAction):
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/workflows/dotnet_clipackage/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest import TestCase
from mock import patch

from concurrent.futures import ThreadPoolExecutor
from mock import patch, call
import os
import platform

Expand All @@ -8,6 +10,7 @@
from aws_lambda_builders.workflows.dotnet_clipackage.actions import GlobalToolInstallAction, RunPackageAction


@patch.object(GlobalToolInstallAction, "_GlobalToolInstallAction__tools_installed", False)
class TestGlobalToolInstallAction(TestCase):
@patch("aws_lambda_builders.workflows.dotnet_clipackage.dotnetcli.SubprocessDotnetCLI")
def setUp(self, MockSubprocessDotnetCLI):
Expand Down Expand Up @@ -42,6 +45,24 @@ def test_global_tool_update_failed(self):
action = GlobalToolInstallAction(self.subprocess_dotnet)
self.assertRaises(ActionFailedError, action.execute)

def test_global_tool_parallel(self):
executor = ThreadPoolExecutor()

actions = [
GlobalToolInstallAction(self.subprocess_dotnet),
GlobalToolInstallAction(self.subprocess_dotnet),
GlobalToolInstallAction(self.subprocess_dotnet),
]

for action in actions:
executor.submit(action.execute)

executor.shutdown()

self.subprocess_dotnet.assert_has_calls(
[call.run(["tool", "install", "-g", "Amazon.Lambda.Tools", "--ignore-failed-sources"])]
)


class TestRunPackageAction(TestCase):
@patch("aws_lambda_builders.workflows.dotnet_clipackage.dotnetcli.SubprocessDotnetCLI")
Expand Down