mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-31 18:48:08 +00:00 
			
		
		
		
	vault: public testing methods
This commit is contained in:
		| @@ -8,34 +8,9 @@ import ( | |||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"testing" | 	"testing" | ||||||
|  |  | ||||||
| 	"github.com/hashicorp/vault/physical" |  | ||||||
| 	"github.com/hashicorp/vault/vault" | 	"github.com/hashicorp/vault/vault" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func testCore(t *testing.T) *vault.Core { |  | ||||||
| 	physicalBackend := physical.NewInmem() |  | ||||||
| 	c, err := vault.NewCore(&vault.CoreConfig{ |  | ||||||
| 		Physical: physicalBackend, |  | ||||||
| 	}) |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatalf("err: %s", err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return c |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func testCoreInit(t *testing.T, core *vault.Core) [][]byte { |  | ||||||
| 	result, err := core.Initialize(&vault.SealConfig{ |  | ||||||
| 		SecretShares:    1, |  | ||||||
| 		SecretThreshold: 1, |  | ||||||
| 	}) |  | ||||||
| 	if err != nil { |  | ||||||
| 		t.Fatalf("err: %s", err) |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	return result.SecretShares |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func testServer(t *testing.T, core *vault.Core) (net.Listener, string) { | func testServer(t *testing.T, core *vault.Core) (net.Listener, string) { | ||||||
| 	ln, err := net.Listen("tcp", "127.0.0.1:0") | 	ln, err := net.Listen("tcp", "127.0.0.1:0") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|   | |||||||
| @@ -5,10 +5,12 @@ import ( | |||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| 	"testing" | 	"testing" | ||||||
|  |  | ||||||
|  | 	"github.com/hashicorp/vault/vault" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func TestSysInit_get(t *testing.T) { | func TestSysInit_get(t *testing.T) { | ||||||
| 	core := testCore(t) | 	core := vault.TestCore(t) | ||||||
| 	ln, addr := testServer(t, core) | 	ln, addr := testServer(t, core) | ||||||
| 	defer ln.Close() | 	defer ln.Close() | ||||||
|  |  | ||||||
| @@ -30,7 +32,7 @@ func TestSysInit_get(t *testing.T) { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	testCoreInit(t, core) | 	vault.TestCoreInit(t, core) | ||||||
|  |  | ||||||
| 	{ | 	{ | ||||||
| 		// Post-init | 		// Post-init | ||||||
| @@ -52,7 +54,7 @@ func TestSysInit_get(t *testing.T) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func TestSysInit_put(t *testing.T) { | func TestSysInit_put(t *testing.T) { | ||||||
| 	core := testCore(t) | 	core := vault.TestCore(t) | ||||||
| 	ln, addr := testServer(t, core) | 	ln, addr := testServer(t, core) | ||||||
| 	defer ln.Close() | 	defer ln.Close() | ||||||
|  |  | ||||||
|   | |||||||
| @@ -5,11 +5,13 @@ import ( | |||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| 	"testing" | 	"testing" | ||||||
|  |  | ||||||
|  | 	"github.com/hashicorp/vault/vault" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| func TestSysSealStatus(t *testing.T) { | func TestSysSealStatus(t *testing.T) { | ||||||
| 	core := testCore(t) | 	core := vault.TestCore(t) | ||||||
| 	testCoreInit(t, core) | 	vault.TestCoreInit(t, core) | ||||||
| 	ln, addr := testServer(t, core) | 	ln, addr := testServer(t, core) | ||||||
| 	defer ln.Close() | 	defer ln.Close() | ||||||
|  |  | ||||||
| @@ -33,8 +35,8 @@ func TestSysSealStatus(t *testing.T) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func TestSysSeal(t *testing.T) { | func TestSysSeal(t *testing.T) { | ||||||
| 	core := testCore(t) | 	core := vault.TestCore(t) | ||||||
| 	testCoreInit(t, core) | 	vault.TestCoreInit(t, core) | ||||||
| 	ln, addr := testServer(t, core) | 	ln, addr := testServer(t, core) | ||||||
| 	defer ln.Close() | 	defer ln.Close() | ||||||
|  |  | ||||||
| @@ -51,11 +53,11 @@ func TestSysSeal(t *testing.T) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func TestSysSeal_unsealed(t *testing.T) { | func TestSysSeal_unsealed(t *testing.T) { | ||||||
| 	core := testCore(t) | 	core := vault.TestCore(t) | ||||||
| 	ln, addr := testServer(t, core) | 	ln, addr := testServer(t, core) | ||||||
| 	defer ln.Close() | 	defer ln.Close() | ||||||
|  |  | ||||||
| 	keys := testCoreInit(t, core) | 	keys := vault.TestCoreInit(t, core) | ||||||
| 	if _, err := core.Unseal(keys[0]); err != nil { | 	if _, err := core.Unseal(keys[0]); err != nil { | ||||||
| 		t.Fatalf("err: %s", err) | 		t.Fatalf("err: %s", err) | ||||||
| 	} | 	} | ||||||
| @@ -73,8 +75,8 @@ func TestSysSeal_unsealed(t *testing.T) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func TestSysUnseal(t *testing.T) { | func TestSysUnseal(t *testing.T) { | ||||||
| 	core := testCore(t) | 	core := vault.TestCore(t) | ||||||
| 	keys := testCoreInit(t, core) | 	keys := vault.TestCoreInit(t, core) | ||||||
| 	ln, addr := testServer(t, core) | 	ln, addr := testServer(t, core) | ||||||
| 	defer ln.Close() | 	defer ln.Close() | ||||||
|  |  | ||||||
| @@ -97,8 +99,8 @@ func TestSysUnseal(t *testing.T) { | |||||||
| } | } | ||||||
|  |  | ||||||
| func TestSysUnseal_badKey(t *testing.T) { | func TestSysUnseal_badKey(t *testing.T) { | ||||||
| 	core := testCore(t) | 	core := vault.TestCore(t) | ||||||
| 	testCoreInit(t, core) | 	vault.TestCoreInit(t, core) | ||||||
| 	ln, addr := testServer(t, core) | 	ln, addr := testServer(t, core) | ||||||
| 	defer ln.Close() | 	defer ln.Close() | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										37
									
								
								vault/testing.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								vault/testing.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | |||||||
|  | package vault | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"testing" | ||||||
|  |  | ||||||
|  | 	"github.com/hashicorp/vault/physical" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // This file contains a number of methods that are useful for unit | ||||||
|  | // tests within other packages. | ||||||
|  |  | ||||||
|  | // TestCore returns a pure in-memory, uninitialized core for testing. | ||||||
|  | func TestCore(t *testing.T) *Core { | ||||||
|  | 	physicalBackend := physical.NewInmem() | ||||||
|  | 	c, err := NewCore(&CoreConfig{ | ||||||
|  | 		Physical: physicalBackend, | ||||||
|  | 	}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("err: %s", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return c | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // TestCoreInit initializes the core with a single key, and returns | ||||||
|  | // the list of keys that must be used to unseal the core. | ||||||
|  | func TestCoreInit(t *testing.T, core *Core) [][]byte { | ||||||
|  | 	result, err := core.Initialize(&SealConfig{ | ||||||
|  | 		SecretShares:    1, | ||||||
|  | 		SecretThreshold: 1, | ||||||
|  | 	}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Fatalf("err: %s", err) | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return result.SecretShares | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 Mitchell Hashimoto
					Mitchell Hashimoto