summaryrefslogtreecommitdiff
path: root/tests/artifactcache/artifactservice.py
blob: 4f7146fe0f265d1ee3d4a3f3e1c66ce26f909364 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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 buildstream2._protos.buildstream.v2.artifact_pb2 \
    import Artifact, GetArtifactRequest, UpdateArtifactRequest
from buildstream2._protos.buildstream.v2.artifact_pb2_grpc import ArtifactServiceStub
from buildstream2._protos.build.bazel.remote.execution.v2 \
    import remote_execution_pb2 as re_pb2
from buildstream2._protos.build.bazel.remote.execution.v2 \
    import remote_execution_pb2_grpc as re_pb2_grpc
from buildstream2 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