|
| 1 | +// Copyright 2023 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "crypto/sha256" |
| 9 | + "encoding/hex" |
| 10 | + "io" |
| 11 | + "net/http" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "code.gitea.io/gitea/routers/api/actions" |
| 17 | + actions_service "code.gitea.io/gitea/services/actions" |
| 18 | + "code.gitea.io/gitea/tests" |
| 19 | + "google.golang.org/protobuf/encoding/protojson" |
| 20 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 21 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 22 | + "google.golang.org/protobuf/types/known/wrapperspb" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func toProtoJson(m protoreflect.ProtoMessage) io.Reader { |
| 28 | + resp, _ := protojson.Marshal(m) |
| 29 | + buf := bytes.Buffer{} |
| 30 | + buf.Write(resp) |
| 31 | + return &buf |
| 32 | +} |
| 33 | + |
| 34 | +func TestActionsArtifactV4UploadSingleFile(t *testing.T) { |
| 35 | + defer tests.PrepareTestEnv(t)() |
| 36 | + |
| 37 | + token, err := actions_service.CreateAuthorizationToken(48, 792, 193) |
| 38 | + assert.NoError(t, err) |
| 39 | + |
| 40 | + // acquire artifact upload url |
| 41 | + req := NewRequestWithBody(t, "POST", "/twirp/github.actions.results.api.v1.ArtifactService/CreateArtifact", toProtoJson(&actions.CreateArtifactRequest{ |
| 42 | + Version: 4, |
| 43 | + Name: "artifact", |
| 44 | + WorkflowRunBackendId: "792", |
| 45 | + WorkflowJobRunBackendId: "193", |
| 46 | + })).AddTokenAuth(token) |
| 47 | + resp := MakeRequest(t, req, http.StatusOK) |
| 48 | + var uploadResp actions.CreateArtifactResponse |
| 49 | + protojson.Unmarshal(resp.Body.Bytes(), &uploadResp) |
| 50 | + assert.True(t, uploadResp.Ok) |
| 51 | + assert.Contains(t, uploadResp.SignedUploadUrl, "/twirp/github.actions.results.api.v1.ArtifactService/UploadArtifact") |
| 52 | + |
| 53 | + // get upload url |
| 54 | + idx := strings.Index(uploadResp.SignedUploadUrl, "/twirp/") |
| 55 | + url := uploadResp.SignedUploadUrl[idx:] + "&comp=block" |
| 56 | + |
| 57 | + // upload artifact chunk |
| 58 | + body := strings.Repeat("A", 1024) |
| 59 | + req = NewRequestWithBody(t, "PUT", url, strings.NewReader(body)) |
| 60 | + MakeRequest(t, req, http.StatusCreated) |
| 61 | + |
| 62 | + t.Logf("Create artifact confirm") |
| 63 | + |
| 64 | + sha := sha256.Sum256([]byte(body)) |
| 65 | + |
| 66 | + // confirm artifact upload |
| 67 | + req = NewRequestWithBody(t, "POST", "/twirp/github.actions.results.api.v1.ArtifactService/FinalizeArtifact", toProtoJson(&actions.FinalizeArtifactRequest{ |
| 68 | + Name: "artifact", |
| 69 | + Size: 1024, |
| 70 | + Hash: wrapperspb.String("sha256:" + hex.EncodeToString(sha[:])), |
| 71 | + WorkflowRunBackendId: "792", |
| 72 | + WorkflowJobRunBackendId: "193", |
| 73 | + })). |
| 74 | + AddTokenAuth(token) |
| 75 | + resp = MakeRequest(t, req, http.StatusOK) |
| 76 | + var finalizeResp actions.FinalizeArtifactResponse |
| 77 | + protojson.Unmarshal(resp.Body.Bytes(), &finalizeResp) |
| 78 | + assert.True(t, finalizeResp.Ok) |
| 79 | +} |
| 80 | + |
| 81 | +func TestActionsArtifactV4UploadSingleFileWithRetentionDays(t *testing.T) { |
| 82 | + defer tests.PrepareTestEnv(t)() |
| 83 | + |
| 84 | + token, err := actions_service.CreateAuthorizationToken(48, 792, 193) |
| 85 | + assert.NoError(t, err) |
| 86 | + |
| 87 | + // acquire artifact upload url |
| 88 | + req := NewRequestWithBody(t, "POST", "/twirp/github.actions.results.api.v1.ArtifactService/CreateArtifact", toProtoJson(&actions.CreateArtifactRequest{ |
| 89 | + Version: 4, |
| 90 | + ExpiresAt: timestamppb.New(time.Now().Add(5 * 24 * time.Hour)), |
| 91 | + Name: "artifactWithRetentionDays", |
| 92 | + WorkflowRunBackendId: "792", |
| 93 | + WorkflowJobRunBackendId: "193", |
| 94 | + })).AddTokenAuth(token) |
| 95 | + resp := MakeRequest(t, req, http.StatusOK) |
| 96 | + var uploadResp actions.CreateArtifactResponse |
| 97 | + protojson.Unmarshal(resp.Body.Bytes(), &uploadResp) |
| 98 | + assert.True(t, uploadResp.Ok) |
| 99 | + assert.Contains(t, uploadResp.SignedUploadUrl, "/twirp/github.actions.results.api.v1.ArtifactService/UploadArtifact") |
| 100 | + |
| 101 | + // get upload url |
| 102 | + idx := strings.Index(uploadResp.SignedUploadUrl, "/twirp/") |
| 103 | + url := uploadResp.SignedUploadUrl[idx:] + "&comp=block" |
| 104 | + |
| 105 | + // upload artifact chunk |
| 106 | + body := strings.Repeat("A", 1024) |
| 107 | + req = NewRequestWithBody(t, "PUT", url, strings.NewReader(body)) |
| 108 | + MakeRequest(t, req, http.StatusCreated) |
| 109 | + |
| 110 | + t.Logf("Create artifact confirm") |
| 111 | + |
| 112 | + sha := sha256.Sum256([]byte(body)) |
| 113 | + |
| 114 | + // confirm artifact upload |
| 115 | + req = NewRequestWithBody(t, "POST", "/twirp/github.actions.results.api.v1.ArtifactService/FinalizeArtifact", toProtoJson(&actions.FinalizeArtifactRequest{ |
| 116 | + Name: "artifactWithRetentionDays", |
| 117 | + Size: 1024, |
| 118 | + Hash: wrapperspb.String("sha256:" + hex.EncodeToString(sha[:])), |
| 119 | + WorkflowRunBackendId: "792", |
| 120 | + WorkflowJobRunBackendId: "193", |
| 121 | + })). |
| 122 | + AddTokenAuth(token) |
| 123 | + resp = MakeRequest(t, req, http.StatusOK) |
| 124 | + var finalizeResp actions.FinalizeArtifactResponse |
| 125 | + protojson.Unmarshal(resp.Body.Bytes(), &finalizeResp) |
| 126 | + assert.True(t, finalizeResp.Ok) |
| 127 | +} |
| 128 | + |
| 129 | +func TestActionsArtifactV4DownloadSingle(t *testing.T) { |
| 130 | + defer tests.PrepareTestEnv(t)() |
| 131 | + |
| 132 | + token, err := actions_service.CreateAuthorizationToken(48, 792, 193) |
| 133 | + assert.NoError(t, err) |
| 134 | + |
| 135 | + // acquire artifact upload url |
| 136 | + req := NewRequestWithBody(t, "POST", "/twirp/github.actions.results.api.v1.ArtifactService/ListArtifacts", toProtoJson(&actions.ListArtifactsRequest{ |
| 137 | + NameFilter: wrapperspb.String("artifact"), |
| 138 | + WorkflowRunBackendId: "792", |
| 139 | + WorkflowJobRunBackendId: "193", |
| 140 | + })).AddTokenAuth(token) |
| 141 | + resp := MakeRequest(t, req, http.StatusOK) |
| 142 | + var listResp actions.ListArtifactsResponse |
| 143 | + protojson.Unmarshal(resp.Body.Bytes(), &listResp) |
| 144 | + assert.Len(t, listResp.Artifacts, 1) |
| 145 | + |
| 146 | + // confirm artifact upload |
| 147 | + req = NewRequestWithBody(t, "POST", "/twirp/github.actions.results.api.v1.ArtifactService/GetSignedArtifactURL", toProtoJson(&actions.GetSignedArtifactURLRequest{ |
| 148 | + Name: "artifact", |
| 149 | + WorkflowRunBackendId: "792", |
| 150 | + WorkflowJobRunBackendId: "193", |
| 151 | + })). |
| 152 | + AddTokenAuth(token) |
| 153 | + resp = MakeRequest(t, req, http.StatusOK) |
| 154 | + var finalizeResp actions.GetSignedArtifactURLResponse |
| 155 | + protojson.Unmarshal(resp.Body.Bytes(), &finalizeResp) |
| 156 | + assert.NotEmpty(t, finalizeResp.SignedUrl) |
| 157 | + |
| 158 | + req = NewRequest(t, "GET", finalizeResp.SignedUrl) |
| 159 | + resp = MakeRequest(t, req, http.StatusOK) |
| 160 | + body := strings.Repeat("A", 1024) |
| 161 | + assert.Equal(t, resp.Body.String(), body) |
| 162 | +} |
0 commit comments