Browse Source

Test GPG commit signing

Michael Bridgen 11 months ago
parent
commit
0e1eaa2887
3 changed files with 79 additions and 70 deletions
  1. 0
    69
      git/gittest/repo.go
  2. 2
    1
      git/gittest/repo_test.go
  3. 77
    0
      gpg/gpgtest/gpg.go

+ 0
- 69
git/gittest/repo.go View File

@@ -3,84 +3,15 @@ package gittest
3 3
 import (
4 4
 	"context"
5 5
 	"io/ioutil"
6
-	"io"
7 6
 	"os/exec"
8 7
 	"path/filepath"
9 8
 	"testing"
10
-	"bytes"
11
-	"strings"
12 9
 
13 10
 	"github.com/weaveworks/flux"
14 11
 	"github.com/weaveworks/flux/cluster/kubernetes/testfiles"
15 12
 	"github.com/weaveworks/flux/git"
16 13
 )
17 14
 
18
-// GPGKey creates a new, temporary GPG home directory and a public/private key
19
-// pair. It returns the GPG home directory, the ID of the created key, and a
20
-// cleanup function to be called after the caller is finished with this key.
21
-// Since GPG uses /dev/random, this may block while waiting for entropy to
22
-// become available.
23
-func GPGKey(t *testing.T) (string, string, func()) {
24
-	newDir, cleanup := testfiles.TempDir(t)
25
-
26
-	cmd := exec.Command("gpg", "--homedir", newDir, "--batch", "--gen-key")
27
-
28
-	stdin, err := cmd.StdinPipe()
29
-	if err != nil {
30
-		cleanup()
31
-		t.Fatal(err)
32
-	}
33
-
34
-	io.WriteString(stdin, "Key-Type: DSA\n")
35
-	io.WriteString(stdin, "Key-Length: 1024\n")
36
-	io.WriteString(stdin, "Key-Usage: sign\n")
37
-	io.WriteString(stdin, "Name-Real: Weave Flux\n")
38
-	io.WriteString(stdin, "Name-Email: flux@weave.works\n")
39
-	io.WriteString(stdin, "%no-protection\n")
40
-	stdin.Close()
41
-
42
-	if err := cmd.Run(); err != nil {
43
-		cleanup()
44
-		t.Fatal(err)
45
-	}
46
-
47
-	gpgCmd := exec.Command("gpg", "--homedir", newDir, "--list-keys", "--with-colons")
48
-	grepCmd := exec.Command("grep", "^fpr")
49
-	cutCmd := exec.Command("cut", "-d:", "-f10")
50
-
51
-	grepIn, gpgOut := io.Pipe()
52
-	cutIn, grepOut := io.Pipe()
53
-	var cutOut bytes.Buffer
54
-
55
-	gpgCmd.Stdout = gpgOut
56
-	grepCmd.Stdin, grepCmd.Stdout = grepIn, grepOut
57
-	cutCmd.Stdin, cutCmd.Stdout = cutIn, &cutOut
58
-
59
-	gpgCmd.Start()
60
-	grepCmd.Start()
61
-	cutCmd.Start()
62
-
63
-	if err := gpgCmd.Wait(); err != nil {
64
-		cleanup()
65
-		t.Fatal(err)
66
-	}
67
-	gpgOut.Close()
68
-
69
-	if err := grepCmd.Wait(); err != nil {
70
-		cleanup()
71
-		t.Fatal(err)
72
-	}
73
-	grepOut.Close()
74
-
75
-	if err := cutCmd.Wait(); err != nil {
76
-		cleanup()
77
-		t.Fatal(err)
78
-	}
79
-
80
-	fingerprint := strings.TrimSpace(cutOut.String())
81
-	return newDir, fingerprint, cleanup
82
-}
83
-
84 15
 // Repo creates a new clone-able git repo, pre-populated with some kubernetes
85 16
 // files and a few commits. Also returns a cleanup func to clean up after.
86 17
 func Repo(t *testing.T) (*git.Repo, func()) {

+ 2
- 1
git/gittest/repo_test.go View File

@@ -12,6 +12,7 @@ import (
12 12
 
13 13
 	"github.com/weaveworks/flux/cluster/kubernetes/testfiles"
14 14
 	"github.com/weaveworks/flux/git"
15
+	"github.com/weaveworks/flux/gpg/gpgtest"
15 16
 )
16 17
 
17 18
 type Note struct {
@@ -67,7 +68,7 @@ func TestCommit(t *testing.T) {
67 68
 }
68 69
 
69 70
 func TestSignedCommit(t *testing.T) {
70
-	gpgHome, signingKey, gpgCleanup := GPGKey(t)
71
+	gpgHome, signingKey, gpgCleanup := gpgtest.GPGKey(t)
71 72
 	defer gpgCleanup()
72 73
 
73 74
 	config := TestConfig

+ 77
- 0
gpg/gpgtest/gpg.go View File

@@ -0,0 +1,77 @@
1
+package gpgtest
2
+
3
+import (
4
+	"bytes"
5
+	"io"
6
+	"os/exec"
7
+	"strings"
8
+	"testing"
9
+
10
+	"github.com/weaveworks/flux/cluster/kubernetes/testfiles"
11
+)
12
+
13
+// GPGKey creates a new, temporary GPG home directory and a public/private key
14
+// pair. It returns the GPG home directory, the ID of the created key, and a
15
+// cleanup function to be called after the caller is finished with this key.
16
+// Since GPG uses /dev/random, this may block while waiting for entropy to
17
+// become available.
18
+func GPGKey(t *testing.T) (string, string, func()) {
19
+	newDir, cleanup := testfiles.TempDir(t)
20
+
21
+	cmd := exec.Command("gpg", "--homedir", newDir, "--batch", "--gen-key")
22
+
23
+	stdin, err := cmd.StdinPipe()
24
+	if err != nil {
25
+		cleanup()
26
+		t.Fatal(err)
27
+	}
28
+
29
+	io.WriteString(stdin, "Key-Type: DSA\n")
30
+	io.WriteString(stdin, "Key-Length: 1024\n")
31
+	io.WriteString(stdin, "Key-Usage: sign\n")
32
+	io.WriteString(stdin, "Name-Real: Weave Flux\n")
33
+	io.WriteString(stdin, "Name-Email: flux@weave.works\n")
34
+	io.WriteString(stdin, "%no-protection\n")
35
+	stdin.Close()
36
+
37
+	if err := cmd.Run(); err != nil {
38
+		cleanup()
39
+		t.Fatal(err)
40
+	}
41
+
42
+	gpgCmd := exec.Command("gpg", "--homedir", newDir, "--list-keys", "--with-colons")
43
+	grepCmd := exec.Command("grep", "^fpr")
44
+	cutCmd := exec.Command("cut", "-d:", "-f10")
45
+
46
+	grepIn, gpgOut := io.Pipe()
47
+	cutIn, grepOut := io.Pipe()
48
+	var cutOut bytes.Buffer
49
+
50
+	gpgCmd.Stdout = gpgOut
51
+	grepCmd.Stdin, grepCmd.Stdout = grepIn, grepOut
52
+	cutCmd.Stdin, cutCmd.Stdout = cutIn, &cutOut
53
+
54
+	gpgCmd.Start()
55
+	grepCmd.Start()
56
+	cutCmd.Start()
57
+
58
+	if err := gpgCmd.Wait(); err != nil {
59
+		cleanup()
60
+		t.Fatal(err)
61
+	}
62
+	gpgOut.Close()
63
+
64
+	if err := grepCmd.Wait(); err != nil {
65
+		cleanup()
66
+		t.Fatal(err)
67
+	}
68
+	grepOut.Close()
69
+
70
+	if err := cutCmd.Wait(); err != nil {
71
+		cleanup()
72
+		t.Fatal(err)
73
+	}
74
+
75
+	fingerprint := strings.TrimSpace(cutOut.String())
76
+	return newDir, fingerprint, cleanup
77
+}

Loading…
Cancel
Save