forgejo/tests/integration/cmd_forgejo_actions_test.go

214 lines
6.1 KiB
Go
Raw Normal View History

[CLI] implement forgejo-cli (cherry picked from commit 2555e315f7561302484b15576d34c5da0d4cdb12) (cherry picked from commit 51b9c9092e21a451695ee0154e7d49753574f525) [CLI] implement forgejo-cli (squash) support initDB (cherry picked from commit 5c31ae602a45f1d9a90b86bece5393bc9faddf25) (cherry picked from commit bbf76489a73bad83d68ca7c8e7a75cf8e27b2198) Conflicts: because of d0dbe52e76f3038777c3b50066e3636105387ca3 upgrade to https://pkg.go.dev/github.com/urfave/cli/v2 (cherry picked from commit b6c1bcc008fcff0e297d570a0069bf41bc74e53d) [CLI] implement forgejo-cli actions (cherry picked from commit 08be2b226e46d9f41e08f66e936b317bcfb4a257) (cherry picked from commit b6cfa88c6e2ae00e30c832ce4cf93c9e3f2cd6e4) (cherry picked from commit 59704200de59b65a4f37c39569a3b43e1ee38862) [CLI] implement forgejo-cli actions generate-secret (cherry picked from commit 6f7905c8ecf17d5f74ac9a71a453d6768c212b6d) (cherry picked from commit e085d6d2737e6238a4ff00f19f40cf839ac16b34) [CLI] implement forgejo-cli actions generate-secret (squash) NoInit (cherry picked from commit 962c944eb20268a394030495c3caab3e3d4bd8b7) [CLI] implement forgejo-cli actions register (cherry picked from commit 2f95143000e4ccc94ef14332777b58fe778edbd6) (cherry picked from commit 42f2f8731e876564b6627a43a248f262f50c04cd) [CLI] implement forgejo-cli actions register (squash) no private Do not go through the private API, directly modify the database (cherry picked from commit 1ba7c0d39d0ecd190b7d9c517bd26af6c84341aa) [CLI] implement forgejo-cli actions (cherry picked from commit 6f7905c8ecf17d5f74ac9a71a453d6768c212b6d) (cherry picked from commit e085d6d2737e6238a4ff00f19f40cf839ac16b34) [CLI] implement forgejo-cli actions generate-secret (squash) NoInit (cherry picked from commit 962c944eb20268a394030495c3caab3e3d4bd8b7) (cherry picked from commit 4c121ef022597e66d902c17e0f46839c26924b18) Conflicts: cmd/forgejo/actions.go tests/integration/cmd_forgejo_actions_test.go (cherry picked from commit 36997a48e38286579850abe4b55e75a235b56537) [CLI] implement forgejo-cli actions (squash) restore --version Refs: https://codeberg.org/forgejo/forgejo/issues/1134 (cherry picked from commit 9739eb52d8f94d32f61068d7209958e8d2582818) [CI] implement forgejo-cli (squash) the actions subcommand needs config (cherry picked from commit def638475122a26082ab3835842c84cd03839154) Conflicts: cmd/main.go https://codeberg.org/forgejo/forgejo/pulls/1209 (cherry picked from commit a1758a391043123903607338cb11490161ac946d) (cherry picked from commit 935fa650c77b151752a58f621d846b166b97cd79)
2023-07-09 14:52:21 +02:00
// SPDX-License-Identifier: MIT
package integration
import (
gocontext "context"
"net/url"
"os"
"strings"
"testing"
actions_model "code.gitea.io/gitea/models/actions"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
"github.com/stretchr/testify/assert"
)
func Test_CmdForgejo_Actions(t *testing.T) {
onGiteaRun(t, func(*testing.T, *url.URL) {
defer test.MockVariableValue(&setting.Actions.Enabled, true)()
token, err := cmdForgejoCaptureOutput(t, []string{"forgejo", "forgejo-cli", "actions", "generate-runner-token"})
assert.NoError(t, err)
assert.EqualValues(t, 40, len(token))
secret, err := cmdForgejoCaptureOutput(t, []string{"forgejo", "forgejo-cli", "actions", "generate-secret"})
assert.NoError(t, err)
assert.EqualValues(t, 40, len(secret))
_, err = cmdForgejoCaptureOutput(t, []string{"forgejo", "forgejo-cli", "actions", "register"})
assert.ErrorContains(t, err, "at least one of the --secret")
for _, testCase := range []struct {
testName string
scope string
secret string
errorMessage string
}{
{
testName: "bad user",
scope: "baduser",
secret: "0123456789012345678901234567890123456789",
errorMessage: "user does not exist",
},
{
testName: "bad repo",
scope: "org25/badrepo",
secret: "0123456789012345678901234567890123456789",
errorMessage: "repository does not exist",
},
{
testName: "secret length != 40",
scope: "org25",
secret: "0123456789",
errorMessage: "40 characters long",
},
{
testName: "secret is not a hexadecimal string",
scope: "org25",
secret: "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ",
errorMessage: "must be an hexadecimal string",
},
} {
t.Run(testCase.testName, func(t *testing.T) {
cmd := []string{"forgejo", "forgejo-cli", "actions", "register", "--secret", testCase.secret, "--scope", testCase.scope}
output, err := cmdForgejoCaptureOutput(t, cmd)
assert.ErrorContains(t, err, testCase.errorMessage)
assert.EqualValues(t, "", output)
})
}
secret = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
expecteduuid := "44444444-4444-4444-4444-444444444444"
for _, testCase := range []struct {
testName string
secretOption func() string
stdin []string
}{
{
testName: "secret from argument",
secretOption: func() string {
return "--secret=" + secret
},
},
{
testName: "secret from stdin",
secretOption: func() string {
return "--secret-stdin"
},
stdin: []string{secret},
},
{
testName: "secret from file",
secretOption: func() string {
secretFile := t.TempDir() + "/secret"
assert.NoError(t, os.WriteFile(secretFile, []byte(secret), 0o644))
return "--secret-file=" + secretFile
},
},
} {
t.Run(testCase.testName, func(t *testing.T) {
cmd := []string{"forgejo", "forgejo-cli", "actions", "register", testCase.secretOption(), "--scope=org26"}
uuid, err := cmdForgejoCaptureOutput(t, cmd, testCase.stdin...)
assert.NoError(t, err)
assert.EqualValues(t, expecteduuid, uuid)
})
}
secret = "0123456789012345678901234567890123456789"
expecteduuid = "30313233-3435-3637-3839-303132333435"
for _, testCase := range []struct {
testName string
scope string
secret string
name string
labels string
version string
uuid string
}{
{
testName: "org",
scope: "org25",
secret: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
uuid: "41414141-4141-4141-4141-414141414141",
},
{
testName: "user and repo",
scope: "user2/repo2",
secret: "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB",
uuid: "42424242-4242-4242-4242-424242424242",
},
{
testName: "labels",
scope: "org25",
name: "runnerName",
labels: "label1,label2,label3",
version: "v1.2.3",
secret: "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
uuid: "43434343-4343-4343-4343-434343434343",
},
{
testName: "insert a runner",
scope: "user3/repo5",
name: "runnerName",
labels: "label1,label2,label3",
version: "v1.2.3",
secret: secret,
uuid: expecteduuid,
},
{
testName: "update an existing runner",
scope: "user5/repo4",
name: "runnerNameChanged",
labels: "label1,label2,label3,more,label",
version: "v1.2.3-suffix",
secret: secret,
uuid: expecteduuid,
},
} {
t.Run(testCase.testName, func(t *testing.T) {
cmd := []string{
"forgejo", "forgejo-cli", "actions", "register",
"--secret", testCase.secret, "--scope", testCase.scope,
}
if testCase.name != "" {
cmd = append(cmd, "--name", testCase.name)
}
if testCase.labels != "" {
cmd = append(cmd, "--labels", testCase.labels)
}
if testCase.version != "" {
cmd = append(cmd, "--version", testCase.version)
}
//
// Run twice to verify it is idempotent
//
for i := 0; i < 2; i++ {
uuid, err := cmdForgejoCaptureOutput(t, cmd)
assert.NoError(t, err)
if assert.EqualValues(t, testCase.uuid, uuid) {
ownerName, repoName, found := strings.Cut(testCase.scope, "/")
action, err := actions_model.GetRunnerByUUID(gocontext.Background(), uuid)
assert.NoError(t, err)
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: action.OwnerID})
assert.Equal(t, ownerName, user.Name, action.OwnerID)
if found {
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: action.RepoID})
assert.Equal(t, repoName, repo.Name, action.RepoID)
}
if testCase.name != "" {
assert.EqualValues(t, testCase.name, action.Name)
}
if testCase.labels != "" {
labels := strings.Split(testCase.labels, ",")
assert.EqualValues(t, labels, action.AgentLabels)
}
if testCase.version != "" {
assert.EqualValues(t, testCase.version, action.Version)
}
}
}
})
}
})
}