Skip to content

Commit

Permalink
[IMP] client to return mocked server version
Browse files Browse the repository at this point in the history
  • Loading branch information
nilshamerlinck committed Apr 26, 2019
1 parent 0d0a760 commit f8a60bf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ There are two clients:
* a python client, which only depends on the `requests` library.
It should work with any python version supported by `requests`.

### mock server

Odoo [checks for `wkhtmltopdf`'s version on start](https://github.com/odoo/odoo/blob/12.0/odoo/addons/base/models/ir_actions_report.py#L58).

In a CI context, we don't necessarily want to run the `kwkhtmtopdf` container.
But we definitely want Odoo to start.

To support this case, you can set `KWKHTMLTOPDF_SERVER_URL=MOCK`: the client will
then return `wkhtmltopdf 0.12.5 (mock)` when called with `--version` flag.

## Quick start

### Run the server
Expand Down
3 changes: 3 additions & 0 deletions client/go/kwkhtmltopdf_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func do() error {
serverURL := os.Getenv("KWKHTMLTOPDF_SERVER_URL")
if serverURL == "" {
return errors.New("KWKHTMLTOPDF_SERVER_URL not set")
} else if serverURL == "MOCK" {
os.Stdout.WriteString("wkhtmltopdf 0.12.5 (mock)\n")
return nil
}

// detect if last argument is output file, and create it
Expand Down
7 changes: 7 additions & 0 deletions client/python/kwkhtmltopdf_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class ServerError(Error):

def wkhtmltopdf(args):
url = os.getenv("KWKHTMLTOPDF_SERVER_URL")

if url == "":
raise UsageError("KWKHTMLTOPDF_SERVER_URL not set")
elif url == "MOCK":
print("wkhtmltopdf 0.12.5 (mock)")
return

parts = []

def add_option(option):
Expand Down
42 changes: 35 additions & 7 deletions tests/kwkhtmltopdf_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def _wkhtmltopdf_bin():


class Client:
def __init__(self, cmd):
def __init__(self, cmd, env):
self.env = env
self.cmd = cmd
self.version = self._get_version()

Expand All @@ -32,12 +33,15 @@ def _get_version(self):
raise RuntimeError("could not find version in {}".format(out))
return mo.group(1)

def _run_stdout(self, args, check_return_code=True):
def _run_stdout(self, args, check_return_code=True, env_update={}):
env = os.environ.copy()
env.update(env_update)
proc = subprocess.Popen(
self.cmd + args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
env=env,
)
out, _ = proc.communicate()
if check_return_code:
Expand Down Expand Up @@ -76,38 +80,53 @@ def _run_expect_file(self, args, expected_pdf_base_name):
)
def client(request):
if request.param == "native":
yield Client([_wkhtmltopdf_bin()])
yield Client([_wkhtmltopdf_bin()],
request.param)
elif request.param == "client_test_py":
# run the client with same python as test suite
yield Client(
[os.path.join(HERE, "..", "client", "python", "kwkhtmltopdf_client.py")]
[os.path.join(HERE, "..", "client", "python", "kwkhtmltopdf_client.py")],
request.param
)
elif request.param == "client_sys_py2":
# run the client with the system python2
yield Client(
[
"/usr/bin/python2",
os.path.join(HERE, "..", "client", "python", "kwkhtmltopdf_client.py"),
]
],
request.param
)
elif request.param == "client_sys_py3":
# run the client with the system python2
yield Client(
[
"/usr/bin/python3",
os.path.join(HERE, "..", "client", "python", "kwkhtmltopdf_client.py"),
]
],
request.param
)
elif request.param == "client_go":
yield Client(
[
"go",
"run",
os.path.join(HERE, "..", "client", "go", "kwkhtmltopdf_client.go"),
]
],
request.param
)


def test_server_url_not_set(client):
if client.env == "native":
pytest.skip("not relevant for native wkhtmltopdf")

out = client._run_stdout([],
check_return_code=False,
env_update={'KWKHTMLTOPDF_SERVER_URL': ''})
assert re.search(r"KWKHTMLTOPDF_SERVER_URL not set", out)


def test_noargs(client):
out = client._run_stdout([], check_return_code=False)
assert "Synopsis:" in out
Expand All @@ -131,6 +150,15 @@ def test_version(client):
assert re.search(r"wkhtmltopdf [\d\.]+ ", out)


def test_version_mock(client):
if client.env == "native":
pytest.skip("not relevant for native wkhtmltopdf")

out = client._run_stdout(["--version"],
env_update={'KWKHTMLTOPDF_SERVER_URL': 'MOCK'})
assert re.search(r"wkhtmltopdf [\d\.]+ \(mock\)", out)


def test_1(client, tmp_path):
client._run_expect_file(["test1.html", tmp_path / "o.pdf"], "test1")

Expand Down

0 comments on commit f8a60bf

Please sign in to comment.