-
Notifications
You must be signed in to change notification settings - Fork 25
Vetiver tests #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Vetiver tests #331
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e47789e
adding initial files to set up testing
isabelizimm cceef65
update github actions
isabelizimm 1f9a4c6
updating config files
isabelizimm a091bdb
Merge branch 'rstudio:master' into vetiver-testing
isabelizimm a71567f
update to match vetiver's strategy
isabelizimm 29db578
dont interfere with other tests
isabelizimm 2f78186
add pytest mark
isabelizimm 7642202
add vetiver specific dependencies
isabelizimm 89350ac
make py3.5 compliant
isabelizimm 0f0b084
run in gha
isabelizimm b0613aa
Merge branch 'master' into vetiver-testing
bcwu a6e010f
update license variable name
bcwu 5f79c15
Revert "update license variable name"
bcwu a8c3dee
replace license env with export
bcwu ae5299b
move export up
bcwu 835d57a
Revert "move export up"
bcwu 6052894
Revert "replace license env with export"
bcwu 4359fa3
add pytest to requirements
bcwu 81997d8
pip install requirements
bcwu 65a5d45
Merge branch 'master' into vetiver-tests
bcwu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,7 +1,22 @@ | ||
import sys | ||
import pytest | ||
|
||
from os.path import abspath, dirname | ||
|
||
|
||
HERE = dirname(abspath(__file__)) | ||
sys.path.insert(0, HERE) | ||
|
||
|
||
def pytest_addoption(parser): | ||
parser.addoption( | ||
"--vetiver", action="store_true", default=False, help="run vetiver tests" | ||
) | ||
|
||
def pytest_configure(config): | ||
config.addinivalue_line("markers", "vetiver: test for vetiver interaction") | ||
|
||
def pytest_collection_modifyitems(config, items): | ||
if config.getoption("--vetiver"): | ||
return | ||
skip_vetiver = pytest.mark.skip(reason="need --vetiver option to run") |
This file contains hidden or 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,17 @@ | ||
version: '3.2' | ||
|
||
services: | ||
|
||
rsconnect: | ||
image: rstudio/rstudio-connect:latest | ||
restart: always | ||
ports: | ||
- 3939:3939 | ||
volumes: | ||
- $PWD/vetiver-testing/setup-rsconnect/users.txt:/etc/users.txt | ||
- $PWD/vetiver-testing/setup-rsconnect/rstudio-connect.gcfg:/etc/rstudio-connect/rstudio-connect.gcfg | ||
# by default, mysql rounds to 4 decimals, but tests require more precision | ||
privileged: true | ||
environment: | ||
RSTUDIO_CONNECT_HASTE: "enabled" | ||
RSC_LICENSE: ${RSC_LICENSE} |
This file contains hidden or 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
This file contains hidden or 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,95 @@ | ||
import pytest | ||
|
||
vetiver = pytest.importorskip("vetiver", reason="vetiver library not installed") | ||
|
||
import json # noqa | ||
import pins # noqa | ||
import pandas as pd # noqa | ||
import numpy as np # noqa | ||
|
||
from pins.boards import BoardRsConnect # noqa | ||
from pins.rsconnect.api import RsConnectApi # noqa | ||
from pins.rsconnect.fs import RsConnectFs # noqa | ||
from rsconnect.api import RSConnectServer, RSConnectClient # noqa | ||
|
||
RSC_SERVER_URL = "http://localhost:3939" | ||
RSC_KEYS_FNAME = "vetiver-testing/rsconnect_api_keys.json" | ||
|
||
pytestmark = pytest.mark.vetiver # noqa | ||
|
||
|
||
def get_key(name): | ||
with open(RSC_KEYS_FNAME) as f: | ||
api_key = json.load(f)[name] | ||
return api_key | ||
|
||
|
||
def rsc_from_key(name): | ||
with open(RSC_KEYS_FNAME) as f: | ||
api_key = json.load(f)[name] | ||
return RsConnectApi(RSC_SERVER_URL, api_key) | ||
|
||
|
||
def rsc_fs_from_key(name): | ||
|
||
rsc = rsc_from_key(name) | ||
|
||
return RsConnectFs(rsc) | ||
|
||
|
||
def rsc_delete_user_content(rsc): | ||
guid = rsc.get_user()["guid"] | ||
content = rsc.get_content(owner_guid=guid) | ||
for entry in content: | ||
rsc.delete_content_item(entry["guid"]) | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def rsc_short(): | ||
# tears down content after each test | ||
fs_susan = rsc_fs_from_key("susan") | ||
|
||
# delete any content that might already exist | ||
rsc_delete_user_content(fs_susan.api) | ||
|
||
yield BoardRsConnect("", fs_susan, allow_pickle_read=True) # fs_susan.ls to list content | ||
|
||
rsc_delete_user_content(fs_susan.api) | ||
|
||
|
||
def test_deploy(rsc_short): | ||
np.random.seed(500) | ||
|
||
# Load data, model | ||
X_df, y = vetiver.mock.get_mock_data() | ||
model = vetiver.mock.get_mock_model().fit(X_df, y) | ||
|
||
v = vetiver.VetiverModel(model=model, ptype_data=X_df, model_name="susan/model") | ||
|
||
board = pins.board_rsconnect(server_url=RSC_SERVER_URL, api_key=get_key("susan"), allow_pickle_read=True) | ||
|
||
vetiver.vetiver_pin_write(board=board, model=v) | ||
connect_server = RSConnectServer(url=RSC_SERVER_URL, api_key=get_key("susan")) | ||
|
||
vetiver.deploy_rsconnect( | ||
connect_server=connect_server, | ||
board=board, | ||
pin_name="susan/model", | ||
title="testapivetiver", | ||
extra_files=["requirements.txt"], | ||
) | ||
|
||
# get url of where content lives | ||
client = RSConnectClient(connect_server) | ||
dicts = client.content_search() | ||
rsc_api = list(filter(lambda x: x["title"] == "testapivetiver", dicts)) | ||
content_url = rsc_api[0].get("content_url") | ||
|
||
h = {"Authorization": 'Key {}'.format(get_key("susan"))} | ||
|
||
endpoint = vetiver.vetiver_endpoint(content_url + "/predict") | ||
response = vetiver.predict(endpoint, X_df, headers=h) | ||
|
||
assert isinstance(response, pd.DataFrame), response | ||
assert response.iloc[0, 0] == 44.47 | ||
assert len(response) == 100 |
This file contains hidden or 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 @@ | ||
awk ' { system("useradd -m -s /bin/bash "$1); system("echo \""$1":"$2"\" | chpasswd"); system("id "$1) } ' /etc/users.txt |
This file contains hidden or 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,21 @@ | ||
import json | ||
import sys | ||
|
||
from pins.rsconnect.api import _HackyConnect | ||
|
||
OUT_FILE = sys.argv[1] | ||
|
||
|
||
def get_api_key(user, password, email): | ||
rsc = _HackyConnect("http://localhost:3939") | ||
|
||
return rsc.create_first_admin(user, password, email).api_key | ||
|
||
|
||
api_keys = { | ||
"admin": get_api_key("admin", "admin0", "admin@example.com"), | ||
"susan": get_api_key("susan", "susan", "susan@example.com"), | ||
"derek": get_api_key("derek", "derek", "derek@example.com"), | ||
} | ||
|
||
json.dump(api_keys, open(OUT_FILE, "w")) |
This file contains hidden or 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,23 @@ | ||
[Server] | ||
DataDir = /data | ||
Address = http://localhost:3939 | ||
|
||
[HTTP] | ||
Listen = :3939 | ||
|
||
[Authentication] | ||
Provider = pam | ||
|
||
[Authorization] | ||
DefaultUserRole = publisher | ||
|
||
[Python] | ||
Enabled = true | ||
Executable = /opt/python/3.8.10/bin/python | ||
Executable = /opt/python/3.9.5/bin/python | ||
|
||
[RPackageRepository "CRAN"] | ||
URL = https://packagemanager.rstudio.com/cran/__linux__/bionic/latest | ||
|
||
[RPackageRepository "RSPM"] | ||
URL = https://packagemanager.rstudio.com/cran/__linux__/bionic/latest |
This file contains hidden or 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,4 @@ | ||
admin admin0 | ||
test test | ||
susan susan | ||
derek derek |
This file contains hidden or 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,5 @@ | ||
pins | ||
pandas | ||
numpy | ||
vetiver | ||
pytest |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.