diff options
author | Raoul Hidalgo Charman <raoul.hidalgocharman@codethink.co.uk> | 2019-03-20 15:26:50 +0000 |
---|---|---|
committer | Raoul Hidalgo Charman <raoul.hidalgocharman@codethink.co.uk> | 2019-04-04 17:07:51 +0100 |
commit | cdbd98e19680f1bfd312bb1ae8e0319b3c4467af (patch) | |
tree | 3da5e323eab04eca86e84133b0a9c16b437817a9 /tests | |
parent | 8ded5797999795377906ff1d3c2be5695e4924f6 (diff) | |
download | buildstream-cdbd98e19680f1bfd312bb1ae8e0319b3c4467af.tar.gz |
artifactservice.py: tests for the artifact serviceraoul/965-AaaP-service
Part of #965
Diffstat (limited to 'tests')
-rw-r--r-- | tests/artifactcache/artifactservice.py | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/tests/artifactcache/artifactservice.py b/tests/artifactcache/artifactservice.py new file mode 100644 index 000000000..5a7a3cdd5 --- /dev/null +++ b/tests/artifactcache/artifactservice.py @@ -0,0 +1,109 @@ +# +# Copyright (C) 2019 Bloomberg Finance LP +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see <http://www.gnu.org/licenses/>. +# +# Authors: Raoul Hidalgo Charman <raoul.hidalgocharman@codethink.co.uk> +# +import os +import pytest +from urllib.parse import urlparse + +import grpc + +from buildstream._protos.buildstream.v2.artifact_pb2 \ + import Artifact, GetArtifactRequest, UpdateArtifactRequest +from buildstream._protos.buildstream.v2.artifact_pb2_grpc import ArtifactServiceStub +from buildstream._protos.build.bazel.remote.execution.v2 \ + import remote_execution_pb2 as re_pb2 +from buildstream._protos.build.bazel.remote.execution.v2 \ + import remote_execution_pb2_grpc as re_pb2_grpc +from buildstream import utils + +from tests.testutils.artifactshare import create_artifact_share + + +def test_artifact_get_not_found(tmpdir): + sharedir = os.path.join(str(tmpdir), "share") + with create_artifact_share(sharedir) as share: + # set up artifact service stub + url = urlparse(share.repo) + channel = grpc.insecure_channel("{}:{}".format(url.hostname, url.port)) + artifact_stub = ArtifactServiceStub(channel) + + # Run GetArtifact and check it throws a not found error + request = GetArtifactRequest() + request.cache_key = "@artifact/something/not_there" + try: + artifact_stub.GetArtifact(request) + except grpc.RpcError as e: + assert e.code() == grpc.StatusCode.NOT_FOUND + assert e.details() == "Artifact proto not found" + else: + assert False + +# Successfully getting the artifact +@pytest.mark.parametrize("files", ["present", "absent", "invalid"]) +def test_update_artifact(tmpdir, files): + sharedir = os.path.join(str(tmpdir), "share") + with create_artifact_share(sharedir) as share: + url = urlparse(share.repo) + channel = grpc.insecure_channel("{}:{}".format(url.hostname, url.port)) + artifact_stub = ArtifactServiceStub(channel) + + # initialise an artifact + artifact = Artifact() + artifact.version = 0 + artifact.build_success = True + artifact.strong_key = "abcdefghijklmnop" + artifact.files.hash = "hashymchashash" + artifact.files.size_bytes = 10 + + # put files object + if files == "present": + directory = re_pb2.Directory() + digest = share.cas.add_object(buffer=directory.SerializeToString()) + elif files == "invalid": + digest = share.cas.add_object(buffer="abcdefghijklmnop".encode("utf-8")) + elif files == "absent": + digest = utils._message_digest("abcdefghijklmnop".encode("utf-8")) + + artifact.files.CopyFrom(digest) + + # Put it in the artifact share with an UpdateArtifactRequest + request = UpdateArtifactRequest() + request.artifact.CopyFrom(artifact) + request.cache_key = "a-cache-key" + + # should return the same artifact back + if files == "present": + response = artifact_stub.UpdateArtifact(request) + assert response == artifact + else: + try: + artifact_stub.UpdateArtifact(request) + except grpc.RpcError as e: + assert e.code() == grpc.StatusCode.FAILED_PRECONDITION + if files == "absent": + assert e.details() == "Artifact files specified but no files found" + elif files == "invalid": + assert e.details() == "Artifact files specified but directory not found" + return + + # If we uploaded the artifact check GetArtifact + request = GetArtifactRequest() + request.cache_key = "a-cache-key" + + response = artifact_stub.GetArtifact(request) + assert response == artifact |