Fix the intermittent TestGPGGit failures (#9360)
* Fix the intermittent TestGPGGit failures Reattempt to open the listener if the port is busy with a delay up to a second Switch from generating a private key each time, just use a known good key
This commit is contained in:
parent
f6b29012e0
commit
60b31c8f01
28 changed files with 11821 additions and 10883 deletions
|
@ -10,7 +10,6 @@ import (
|
|||
"io/ioutil"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
|
@ -40,8 +39,11 @@ func TestGPGGit(t *testing.T) {
|
|||
defer os.Setenv("GNUPGHOME", oldGNUPGHome)
|
||||
|
||||
// Need to create a root key
|
||||
rootKeyPair, err := createGPGKey(tmpDir, "gitea", "gitea@fake.local")
|
||||
rootKeyPair, err := importTestingKey(tmpDir, "gitea", "gitea@fake.local")
|
||||
assert.NoError(t, err)
|
||||
if err != nil {
|
||||
assert.FailNow(t, "Unable to import rootKeyPair")
|
||||
}
|
||||
|
||||
rootKeyID := rootKeyPair.PrimaryKey.KeyIdShortString()
|
||||
|
||||
|
@ -125,6 +127,11 @@ func TestGPGGit(t *testing.T) {
|
|||
testCtx := NewAPITestContext(t, username, "initial-unsigned")
|
||||
t.Run("CreateCRUDFile-Always", crudActionCreateFile(
|
||||
t, testCtx, user, "master", "always", "signed-always.txt", func(t *testing.T, response api.FileResponse) {
|
||||
assert.NotNil(t, response.Verification)
|
||||
if response.Verification == nil {
|
||||
assert.FailNow(t, "no verification provided with response! %v", response)
|
||||
return
|
||||
}
|
||||
assert.True(t, response.Verification.Verified)
|
||||
if !response.Verification.Verified {
|
||||
t.FailNow()
|
||||
|
@ -134,6 +141,11 @@ func TestGPGGit(t *testing.T) {
|
|||
}))
|
||||
t.Run("CreateCRUDFile-ParentSigned-always", crudActionCreateFile(
|
||||
t, testCtx, user, "parentsigned", "parentsigned-always", "signed-parent2.txt", func(t *testing.T, response api.FileResponse) {
|
||||
assert.NotNil(t, response.Verification)
|
||||
if response.Verification == nil {
|
||||
assert.FailNow(t, "no verification provided with response! %v", response)
|
||||
return
|
||||
}
|
||||
assert.True(t, response.Verification.Verified)
|
||||
if !response.Verification.Verified {
|
||||
t.FailNow()
|
||||
|
@ -152,6 +164,11 @@ func TestGPGGit(t *testing.T) {
|
|||
testCtx := NewAPITestContext(t, username, "initial-unsigned")
|
||||
t.Run("CreateCRUDFile-Always-ParentSigned", crudActionCreateFile(
|
||||
t, testCtx, user, "always", "always-parentsigned", "signed-always-parentsigned.txt", func(t *testing.T, response api.FileResponse) {
|
||||
assert.NotNil(t, response.Verification)
|
||||
if response.Verification == nil {
|
||||
assert.FailNow(t, "no verification provided with response! %v", response)
|
||||
return
|
||||
}
|
||||
assert.True(t, response.Verification.Verified)
|
||||
if !response.Verification.Verified {
|
||||
t.FailNow()
|
||||
|
@ -171,7 +188,15 @@ func TestGPGGit(t *testing.T) {
|
|||
t.Run("CreateRepository", doAPICreateRepository(testCtx, false))
|
||||
t.Run("CheckMasterBranchSigned", doAPIGetBranch(testCtx, "master", func(t *testing.T, branch api.Branch) {
|
||||
assert.NotNil(t, branch.Commit)
|
||||
if branch.Commit == nil {
|
||||
assert.FailNow(t, "no commit provided with branch! %v", branch)
|
||||
return
|
||||
}
|
||||
assert.NotNil(t, branch.Commit.Verification)
|
||||
if branch.Commit.Verification == nil {
|
||||
assert.FailNow(t, "no verification provided with branch commit! %v", branch.Commit)
|
||||
return
|
||||
}
|
||||
assert.True(t, branch.Commit.Verification.Verified)
|
||||
if !branch.Commit.Verification.Verified {
|
||||
t.FailNow()
|
||||
|
@ -318,43 +343,26 @@ func crudActionCreateFile(t *testing.T, ctx APITestContext, user *models.User, f
|
|||
}, callback...)
|
||||
}
|
||||
|
||||
func createGPGKey(tmpDir, name, email string) (*openpgp.Entity, error) {
|
||||
keyPair, err := openpgp.NewEntity(name, "test", email, nil)
|
||||
func importTestingKey(tmpDir, name, email string) (*openpgp.Entity, error) {
|
||||
if _, _, err := process.GetManager().Exec("gpg --import integrations/private-testing.key", "gpg", "--import", "integrations/private-testing.key"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keyringFile, err := os.Open("integrations/private-testing.key")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer keyringFile.Close()
|
||||
|
||||
block, err := armor.Decode(keyringFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, id := range keyPair.Identities {
|
||||
err := id.SelfSignature.SignUserId(id.UserId.Id, keyPair.PrimaryKey, keyPair.PrivateKey, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
keyFile := filepath.Join(tmpDir, "temporary.key")
|
||||
keyWriter, err := os.Create(keyFile)
|
||||
keyring, err := openpgp.ReadKeyRing(block.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer keyWriter.Close()
|
||||
defer os.Remove(keyFile)
|
||||
|
||||
w, err := armor.Encode(keyWriter, openpgp.PrivateKeyType, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer w.Close()
|
||||
|
||||
keyPair.SerializePrivate(w, nil)
|
||||
if err := w.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := keyWriter.Close(); err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Keyring access failed: '%v'", err)
|
||||
}
|
||||
|
||||
if _, _, err := process.GetManager().Exec("gpg --import temporary.key", "gpg", "--import", keyFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return keyPair, nil
|
||||
// There should only be one entity in this file.
|
||||
return keyring[0], nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue