package vault import ( "reflect" "testing" "github.com/hashicorp/vault/logical" "github.com/hashicorp/vault/physical" ) func TestCore_Init(t *testing.T) { inm := physical.NewInmem() conf := &CoreConfig{ Physical: inm, DisableMlock: true, LogicalBackends: map[string]logical.Factory{ "generic": LeasedPassthroughBackendFactory, }, } c, err := NewCore(conf) if err != nil { t.Fatalf("err: %v", err) } init, err := c.Initialized() if err != nil { t.Fatalf("err: %v", err) } if init { t.Fatalf("should not be init") } // Check the seal configuration outConf, err := c.seal.BarrierConfig() if err != nil { t.Fatalf("err: %v", err) } if outConf != nil { t.Fatalf("bad: %v", outConf) } sealConf := &SealConfig{ SecretShares: 1, SecretThreshold: 1, } res, err := c.Initialize(sealConf, nil) if err != nil { t.Fatalf("err: %v", err) } if len(res.SecretShares) != 1 { t.Fatalf("Bad: %v", res) } if res.RootToken == "" { t.Fatalf("Bad: %v", res) } _, err = c.Initialize(sealConf, nil) if err != ErrAlreadyInit { t.Fatalf("err: %v", err) } init, err = c.Initialized() if err != nil { t.Fatalf("err: %v", err) } if !init { t.Fatalf("should be init") } // Check the seal configuration outConf, err = c.seal.BarrierConfig() if err != nil { t.Fatalf("err: %v", err) } if !reflect.DeepEqual(outConf, sealConf) { t.Fatalf("bad: %v expect: %v", outConf, sealConf) } // New Core, same backend c2, err := NewCore(conf) if err != nil { t.Fatalf("err: %v", err) } _, err = c2.Initialize(sealConf, nil) if err != ErrAlreadyInit { t.Fatalf("err: %v", err) } init, err = c2.Initialized() if err != nil { t.Fatalf("err: %v", err) } if !init { t.Fatalf("should be init") } // Check the seal configuration outConf, err = c2.seal.BarrierConfig() if err != nil { t.Fatalf("err: %v", err) } if !reflect.DeepEqual(outConf, sealConf) { t.Fatalf("bad: %v expect: %v", outConf, sealConf) } }