2023-07-09 14:52:21 +02:00
// SPDX-License-Identifier: MIT
package actions
import (
"crypto/subtle"
"testing"
auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unittest"
"github.com/stretchr/testify/assert"
2024-07-22 11:55:43 +02:00
"github.com/stretchr/testify/require"
2023-07-09 14:52:21 +02:00
)
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
func TestActions_RegisterRunner_Token ( t * testing . T ) {
2024-07-30 21:41:10 +02:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2023-07-09 14:52:21 +02:00
ownerID := int64 ( 0 )
repoID := int64 ( 0 )
token := "0123456789012345678901234567890123456789"
labels := [ ] string { }
name := "runner"
version := "v1.2.3"
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
runner , err := RegisterRunner ( db . DefaultContext , ownerID , repoID , token , & labels , name , version )
2024-07-30 21:41:10 +02:00
require . NoError ( t , err )
2023-07-09 14:52:21 +02:00
assert . EqualValues ( t , name , runner . Name )
assert . EqualValues ( t , 1 , subtle . ConstantTimeCompare ( [ ] byte ( runner . TokenHash ) , [ ] byte ( auth_model . HashToken ( token , runner . TokenSalt ) ) ) , "the token cannot be verified with the same method as routers/api/actions/runner/interceptor.go as of 8228751c55d6a4263f0fec2932ca16181c09c97d" )
}
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
2024-07-22 11:55:43 +02:00
// TestActions_RegisterRunner_TokenUpdate tests that a token's secret is updated
// when a runner already exists and RegisterRunner is called with a token
// parameter whose first 16 bytes match that record but where the last 24 bytes
// do not match.
func TestActions_RegisterRunner_TokenUpdate ( t * testing . T ) {
const recordID = 12345678
oldToken := "7e577e577e577e57feedfacefeedfacefeedface"
newToken := "7e577e577e577e57deadbeefdeadbeefdeadbeef"
2024-07-30 21:41:10 +02:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
2024-07-22 11:55:43 +02:00
before := unittest . AssertExistsAndLoadBean ( t , & ActionRunner { ID : recordID } )
require . Equal ( t ,
before . TokenHash , auth_model . HashToken ( oldToken , before . TokenSalt ) ,
"the initial token should match the runner's secret" ,
)
RegisterRunner ( db . DefaultContext , before . OwnerID , before . RepoID , newToken , nil , before . Name , before . Version )
after := unittest . AssertExistsAndLoadBean ( t , & ActionRunner { ID : recordID } )
assert . Equal ( t , before . UUID , after . UUID )
assert . NotEqual ( t ,
after . TokenHash , auth_model . HashToken ( oldToken , after . TokenSalt ) ,
"the old token can still be verified" ,
)
assert . Equal ( t ,
after . TokenHash , auth_model . HashToken ( newToken , after . TokenSalt ) ,
"the new token cannot be verified" ,
)
}
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
func TestActions_RegisterRunner_CreateWithLabels ( t * testing . T ) {
2024-07-30 21:41:10 +02:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
ownerID := int64 ( 0 )
repoID := int64 ( 0 )
token := "0123456789012345678901234567890123456789"
name := "runner"
version := "v1.2.3"
labels := [ ] string { "woop" , "doop" }
labelsCopy := labels // labels may be affected by the tested function so we copy them
runner , err := RegisterRunner ( db . DefaultContext , ownerID , repoID , token , & labels , name , version )
2024-07-30 21:41:10 +02:00
require . NoError ( t , err )
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
// Check that the returned record has been updated, except for the labels
assert . EqualValues ( t , ownerID , runner . OwnerID )
assert . EqualValues ( t , repoID , runner . RepoID )
assert . EqualValues ( t , name , runner . Name )
assert . EqualValues ( t , version , runner . Version )
assert . EqualValues ( t , labelsCopy , runner . AgentLabels )
// Check that whatever is in the DB has been updated, except for the labels
after := unittest . AssertExistsAndLoadBean ( t , & ActionRunner { ID : runner . ID } )
assert . EqualValues ( t , ownerID , after . OwnerID )
assert . EqualValues ( t , repoID , after . RepoID )
assert . EqualValues ( t , name , after . Name )
assert . EqualValues ( t , version , after . Version )
assert . EqualValues ( t , labelsCopy , after . AgentLabels )
}
func TestActions_RegisterRunner_CreateWithoutLabels ( t * testing . T ) {
2024-07-30 21:41:10 +02:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
ownerID := int64 ( 0 )
repoID := int64 ( 0 )
token := "0123456789012345678901234567890123456789"
name := "runner"
version := "v1.2.3"
runner , err := RegisterRunner ( db . DefaultContext , ownerID , repoID , token , nil , name , version )
2024-07-30 21:41:10 +02:00
require . NoError ( t , err )
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
// Check that the returned record has been updated, except for the labels
assert . EqualValues ( t , ownerID , runner . OwnerID )
assert . EqualValues ( t , repoID , runner . RepoID )
assert . EqualValues ( t , name , runner . Name )
assert . EqualValues ( t , version , runner . Version )
assert . EqualValues ( t , [ ] string { } , runner . AgentLabels )
// Check that whatever is in the DB has been updated, except for the labels
after := unittest . AssertExistsAndLoadBean ( t , & ActionRunner { ID : runner . ID } )
assert . EqualValues ( t , ownerID , after . OwnerID )
assert . EqualValues ( t , repoID , after . RepoID )
assert . EqualValues ( t , name , after . Name )
assert . EqualValues ( t , version , after . Version )
assert . EqualValues ( t , [ ] string { } , after . AgentLabels )
}
func TestActions_RegisterRunner_UpdateWithLabels ( t * testing . T ) {
const recordID = 12345678
token := "7e577e577e577e57feedfacefeedfacefeedface"
2024-07-30 21:41:10 +02:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
unittest . AssertExistsAndLoadBean ( t , & ActionRunner { ID : recordID } )
newOwnerID := int64 ( 1 )
newRepoID := int64 ( 1 )
newName := "rennur"
newVersion := "v4.5.6"
newLabels := [ ] string { "warp" , "darp" }
labelsCopy := newLabels // labels may be affected by the tested function so we copy them
runner , err := RegisterRunner ( db . DefaultContext , newOwnerID , newRepoID , token , & newLabels , newName , newVersion )
2024-07-30 21:41:10 +02:00
require . NoError ( t , err )
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
// Check that the returned record has been updated
assert . EqualValues ( t , newOwnerID , runner . OwnerID )
assert . EqualValues ( t , newRepoID , runner . RepoID )
assert . EqualValues ( t , newName , runner . Name )
assert . EqualValues ( t , newVersion , runner . Version )
assert . EqualValues ( t , labelsCopy , runner . AgentLabels )
// Check that whatever is in the DB has been updated
after := unittest . AssertExistsAndLoadBean ( t , & ActionRunner { ID : recordID } )
assert . EqualValues ( t , newOwnerID , after . OwnerID )
assert . EqualValues ( t , newRepoID , after . RepoID )
assert . EqualValues ( t , newName , after . Name )
assert . EqualValues ( t , newVersion , after . Version )
assert . EqualValues ( t , labelsCopy , after . AgentLabels )
}
func TestActions_RegisterRunner_UpdateWithoutLabels ( t * testing . T ) {
const recordID = 12345678
token := "7e577e577e577e57feedfacefeedfacefeedface"
2024-07-30 21:41:10 +02:00
require . NoError ( t , unittest . PrepareTestDatabase ( ) )
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
before := unittest . AssertExistsAndLoadBean ( t , & ActionRunner { ID : recordID } )
newOwnerID := int64 ( 1 )
newRepoID := int64 ( 1 )
newName := "rennur"
newVersion := "v4.5.6"
runner , err := RegisterRunner ( db . DefaultContext , newOwnerID , newRepoID , token , nil , newName , newVersion )
2024-07-30 21:41:10 +02:00
require . NoError ( t , err )
feat(cli): add `--keep-labels` flag to `forgejo actions register` (#4610)
This commit adds a new flag, `--keep-labels`, to the runner registration CLI command. If this flag is present and the runner being registered already exists, it will prevent the runners' labels from being reset.
In order to accomplish this, the signature of the `RegisterRunner` function from the `models/actions` package has been modified so that the labels argument can be nil. If it is, the part of the function that updates the record will not change the runner.
Various tests have been added for this function, for the following cases: new runner with labels, new runner without label, existing runner with labels, existing runner without labels.
The flag has been added to the CLI command, the action function has been updated to read the labels parameters through a separate function (`getLabels`), and test cases for this function have been added.
<!--
Before submitting a PR, please read the contributing guidelines:
https://codeberg.org/forgejo/forgejo/src/branch/forgejo/CONTRIBUTING.md
-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4610
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Emmanuel BENOÎT <tseeker@nocternity.net>
Co-committed-by: Emmanuel BENOÎT <tseeker@nocternity.net>
2024-07-22 09:33:45 +02:00
// Check that the returned record has been updated, except for the labels
assert . EqualValues ( t , newOwnerID , runner . OwnerID )
assert . EqualValues ( t , newRepoID , runner . RepoID )
assert . EqualValues ( t , newName , runner . Name )
assert . EqualValues ( t , newVersion , runner . Version )
assert . EqualValues ( t , before . AgentLabels , runner . AgentLabels )
// Check that whatever is in the DB has been updated, except for the labels
after := unittest . AssertExistsAndLoadBean ( t , & ActionRunner { ID : recordID } )
assert . EqualValues ( t , newOwnerID , after . OwnerID )
assert . EqualValues ( t , newRepoID , after . RepoID )
assert . EqualValues ( t , newName , after . Name )
assert . EqualValues ( t , newVersion , after . Version )
assert . EqualValues ( t , before . AgentLabels , after . AgentLabels )
}