Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Extract helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
duck8823 committed Oct 5, 2018
1 parent b75f24a commit 4248722
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions application/service/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,33 +42,7 @@ func TestNew(t *testing.T) {

func TestSshGitService_Clone(t *testing.T) {
// setup
privateKey, err := rsa.GenerateKey(rand.Reader, 256)
if err != nil {
t.Fatalf("error occur: %+v", err)
}
privateKeyDer := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: privateKeyDer,
}
privateKeyPem := string(pem.EncodeToMemory(&privateKeyBlock))

tempDir := path.Join(os.TempDir(), random.String(16, random.Alphanumeric))
if err := os.MkdirAll(tempDir, 0700); err != nil {
t.Fatalf("error occur: %+v", err)
}
keyPath := path.Join(tempDir, "id_rsa")
file, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
t.Fatalf("error occur: %+v", err)
}

if _, err := file.WriteString(privateKeyPem); err != nil {
t.Fatalf("error occur: %+v", err)
}

application.Config.GitHub.SSHKeyPath = keyPath
application.Config.GitHub.SSHKeyPath = createTemporaryKey(t)

t.Run("when failure git clone", func(t *testing.T) {
// given
Expand Down Expand Up @@ -307,3 +281,35 @@ func TestHttpGitService_Clone(t *testing.T) {
git.SetPlainCloneFunc(go_git.PlainClone)
})
}

func createTemporaryKey(t *testing.T) string {
t.Helper()

privateKey, err := rsa.GenerateKey(rand.Reader, 256)
if err != nil {
t.Fatalf("error occur: %+v", err)
}
privateKeyDer := x509.MarshalPKCS1PrivateKey(privateKey)
privateKeyBlock := pem.Block{
Type: "RSA PRIVATE KEY",
Headers: nil,
Bytes: privateKeyDer,
}
privateKeyPem := string(pem.EncodeToMemory(&privateKeyBlock))

tempDir := path.Join(os.TempDir(), random.String(16, random.Alphanumeric))
if err := os.MkdirAll(tempDir, 0700); err != nil {
t.Fatalf("error occur: %+v", err)
}
keyPath := path.Join(tempDir, "id_rsa")
file, err := os.OpenFile(keyPath, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
t.Fatalf("error occur: %+v", err)
}

if _, err := file.WriteString(privateKeyPem); err != nil {
t.Fatalf("error occur: %+v", err)
}

return keyPath
}

0 comments on commit 4248722

Please sign in to comment.