-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from airflow-laminar/tkp/start
Initialize common utilities
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
from .common import * | ||
|
||
__version__ = "0.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from subprocess import call | ||
from typing import Callable | ||
|
||
from airflow.exceptions import AirflowFailException, AirflowSkipException | ||
|
||
__all__ = ( | ||
"skip", | ||
"fail", | ||
"pass_", | ||
"ping", | ||
) | ||
|
||
|
||
def skip(): | ||
raise AirflowSkipException | ||
|
||
|
||
def fail(): | ||
raise AirflowFailException | ||
|
||
|
||
def pass_(): | ||
pass | ||
|
||
|
||
def ping(host, *, local=True) -> Callable: | ||
if host != "localhost" and host.count(".") == 0 and local: | ||
host = f"{host}.local" | ||
|
||
def _ping(hostname=host): | ||
if call(["ping", "-c", "1", f"{hostname}"]) == 0: | ||
return True | ||
raise AirflowSkipException | ||
|
||
return _ping |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
from airflow.exceptions import AirflowFailException, AirflowSkipException | ||
|
||
from airflow_common_operators import fail, pass_, ping, skip | ||
|
||
|
||
class TestCommon: | ||
def test_pass(self): | ||
pass_() | ||
|
||
def test_skip(self): | ||
with pytest.raises(AirflowSkipException): | ||
skip() | ||
|
||
def test_fail(self): | ||
with pytest.raises(AirflowFailException): | ||
fail() | ||
|
||
def test_ping(self): | ||
assert ping("localhost")() | ||
with pytest.raises(AirflowSkipException): | ||
ping("nonexistent")() | ||
|
||
def test_ping_localappend(self): | ||
with patch("airflow_common_operators.common.call") as call: | ||
call.return_value = 0 | ||
ping("blerg")() | ||
assert call.call_args[0][0] == ["ping", "-c", "1", "blerg.local"] |