Skip to content

Commit

Permalink
Merge pull request #6 from airflow-laminar/tkp/start
Browse files Browse the repository at this point in the history
Initialize common utilities
  • Loading branch information
timkpaine authored Jan 6, 2025
2 parents 8ffc262 + 04adda6 commit bc9e1b1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions airflow_common_operators/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .common import *

__version__ = "0.1.0"
35 changes: 35 additions & 0 deletions airflow_common_operators/common.py
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
30 changes: 30 additions & 0 deletions airflow_common_operators/tests/test_common.py
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"]

0 comments on commit bc9e1b1

Please sign in to comment.