open-vault/builtin/logical/postgresql/backend_test.go

132 lines
2.8 KiB
Go
Raw Normal View History

2015-04-19 00:34:36 +00:00
package postgresql
import (
"fmt"
"log"
2015-04-19 00:34:36 +00:00
"os"
"testing"
"github.com/hashicorp/vault/logical"
logicaltest "github.com/hashicorp/vault/logical/testing"
"github.com/mitchellh/mapstructure"
2015-04-19 00:34:36 +00:00
)
func TestBackend_basic(t *testing.T) {
2015-04-19 04:45:05 +00:00
b := Backend()
2015-04-19 00:34:36 +00:00
logicaltest.Test(t, logicaltest.TestCase{
PreCheck: func() { testAccPreCheck(t) },
2015-04-19 04:45:05 +00:00
Backend: b,
2015-04-19 00:34:36 +00:00
Steps: []logicaltest.TestStep{
testAccStepConfig(t),
2015-04-19 01:09:33 +00:00
testAccStepRole(t),
testAccStepReadCreds(t, "web"),
2015-04-19 00:34:36 +00:00
},
})
}
func TestBackend_roleCrud(t *testing.T) {
b := Backend()
logicaltest.Test(t, logicaltest.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Backend: b,
Steps: []logicaltest.TestStep{
testAccStepConfig(t),
testAccStepRole(t),
testAccStepReadRole(t, "web", testRole),
testAccStepDeleteRole(t, "web"),
testAccStepReadRole(t, "web", ""),
},
})
}
2015-04-19 00:34:36 +00:00
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("PG_URL"); v == "" {
t.Fatal("PG_URL must be set for acceptance tests")
}
}
func testAccStepConfig(t *testing.T) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.WriteOperation,
Path: "config/connection",
Data: map[string]interface{}{
"value": os.Getenv("PG_URL"),
},
}
}
2015-04-19 01:09:33 +00:00
func testAccStepRole(t *testing.T) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.WriteOperation,
Path: "roles/web",
Data: map[string]interface{}{
"sql": testRole,
},
}
}
func testAccStepDeleteRole(t *testing.T, n string) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.DeleteOperation,
Path: "roles/" + n,
}
}
func testAccStepReadCreds(t *testing.T, name string) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.ReadOperation,
Path: "creds/" + name,
Check: func(resp *logical.Response) error {
var d struct {
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
}
if err := mapstructure.Decode(resp.Data, &d); err != nil {
return err
}
log.Printf("[WARN] Generated credentials: %v", d)
return nil
},
}
}
func testAccStepReadRole(t *testing.T, name string, sql string) logicaltest.TestStep {
return logicaltest.TestStep{
Operation: logical.ReadOperation,
Path: "roles/" + name,
Check: func(resp *logical.Response) error {
if resp == nil {
if sql == "" {
return nil
}
return fmt.Errorf("bad: %#v", resp)
}
var d struct {
SQL string `mapstructure:"sql"`
}
if err := mapstructure.Decode(resp.Data, &d); err != nil {
return err
}
if d.SQL != sql {
return fmt.Errorf("bad: %#v", resp)
}
return nil
},
}
}
2015-04-19 01:09:33 +00:00
const testRole = `
CREATE ROLE "{{name}}" WITH
2015-04-19 01:09:33 +00:00
LOGIN
2015-04-19 04:45:05 +00:00
PASSWORD '{{password}}'
VALID UNTIL '{{expiration}}';
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "{{name}}";
2015-04-19 01:09:33 +00:00
`