@@ -1522,6 +1522,155 @@ func TestPadEqualSigns(t *testing.T) {
1522
1522
})
1523
1523
}
1524
1524
}
1525
+ func testKVUndeleteCommand (tb testing.TB ) (* cli.MockUi , * KVUndeleteCommand ) {
1526
+ tb .Helper ()
1527
+
1528
+ ui := cli .NewMockUi ()
1529
+ return ui , & KVUndeleteCommand {
1530
+ BaseCommand : & BaseCommand {
1531
+ UI : ui ,
1532
+ },
1533
+ }
1534
+ }
1535
+
1536
+ func TestKVUndeleteCommand (t * testing.T ) {
1537
+ t .Parallel ()
1538
+
1539
+ cases := []struct {
1540
+ name string
1541
+ args []string
1542
+ outStrings []string
1543
+ code int
1544
+ }{
1545
+ {
1546
+ "not_enough_args" ,
1547
+ []string {},
1548
+ []string {"Not enough arguments" },
1549
+ 1 ,
1550
+ },
1551
+ {
1552
+ "too_many_args" ,
1553
+ []string {"foo" , "bar" },
1554
+ []string {"Too many arguments" },
1555
+ 1 ,
1556
+ },
1557
+ {
1558
+ "no_versions" ,
1559
+ []string {"-mount" , "kv" , "/read/foo" },
1560
+ []string {"No versions provided" },
1561
+ 1 ,
1562
+ },
1563
+ {
1564
+ "v2_mount_flag_syntax" ,
1565
+ []string {"-versions" , "1" , "-mount" , "kv" , "read/foo" },
1566
+ []string {"Success! Data written to: kv/undelete/read/foo" },
1567
+ 0 ,
1568
+ },
1569
+ {
1570
+ "v2_mount_flag_syntax_complex_1" ,
1571
+ []string {"-versions" , "1" , "-mount" , "secrets/testapp" , "test" },
1572
+ []string {"Success! Data written to: secrets/testapp/undelete/test" },
1573
+ 0 ,
1574
+ },
1575
+ {
1576
+ "v2_mount_flag_syntax_complex_2" ,
1577
+ []string {"-versions" , "1" , "-mount" , "secrets/x/testapp" , "test" },
1578
+ []string {"Success! Data written to: secrets/x/testapp/undelete/test" },
1579
+ 0 ,
1580
+ },
1581
+ }
1582
+
1583
+ t .Run ("validations" , func (t * testing.T ) {
1584
+ t .Parallel ()
1585
+
1586
+ for _ , tc := range cases {
1587
+ tc := tc
1588
+
1589
+ t .Run (tc .name , func (t * testing.T ) {
1590
+ t .Parallel ()
1591
+
1592
+ client , closer := testVaultServer (t )
1593
+ defer closer ()
1594
+ if err := client .Sys ().Mount ("kv/" , & api.MountInput {
1595
+ Type : "kv-v2" ,
1596
+ }); err != nil {
1597
+ t .Fatal (err )
1598
+ }
1599
+
1600
+ if err := client .Sys ().Mount ("secrets/testapp" , & api.MountInput {
1601
+ Type : "kv-v2" ,
1602
+ }); err != nil {
1603
+ t .Fatal (err )
1604
+ }
1605
+
1606
+ // Additional layer of mount path
1607
+ if err := client .Sys ().Mount ("secrets/x/testapp" , & api.MountInput {
1608
+ Type : "kv-v2" ,
1609
+ }); err != nil {
1610
+ t .Fatal (err )
1611
+ }
1612
+
1613
+ // Give time for the upgrade code to run/finish
1614
+ time .Sleep (time .Second )
1615
+
1616
+ if _ , err := client .Logical ().Write ("kv/data/read/foo" , map [string ]interface {}{
1617
+ "data" : map [string ]interface {}{
1618
+ "foo" : "bar" ,
1619
+ },
1620
+ }); err != nil {
1621
+ t .Fatal (err )
1622
+ }
1623
+
1624
+ // Delete the entry so we can undelete it
1625
+ if _ , err := client .Logical ().Delete ("kv/data/read/foo" ); err != nil {
1626
+ t .Fatal (err )
1627
+ }
1628
+
1629
+ if _ , err := client .Logical ().Write ("secrets/testapp/data/test" , map [string ]interface {}{
1630
+ "data" : map [string ]interface {}{
1631
+ "complex" : "yes" ,
1632
+ },
1633
+ }); err != nil {
1634
+ t .Fatal (err )
1635
+ }
1636
+
1637
+ if _ , err := client .Logical ().Write ("secrets/x/testapp/data/test" , map [string ]interface {}{
1638
+ "data" : map [string ]interface {}{
1639
+ "complex" : "yes" ,
1640
+ },
1641
+ }); err != nil {
1642
+ t .Fatal (err )
1643
+ }
1644
+
1645
+ // Delete the entry so we can undelete it
1646
+ if _ , err := client .Logical ().Delete ("secrets/x/testapp/data/test" ); err != nil {
1647
+ t .Fatal (err )
1648
+ }
1649
+
1650
+ // Delete the entry so we can undelete it
1651
+ if _ , err := client .Logical ().Delete ("secrets/testapp/data/test" ); err != nil {
1652
+ t .Fatal (err )
1653
+ }
1654
+
1655
+ ui , cmd := testKVUndeleteCommand (t )
1656
+ cmd .client = client
1657
+
1658
+ code := cmd .Run (tc .args )
1659
+ if code != tc .code {
1660
+ t .Errorf ("expected %d to be %d" , code , tc .code )
1661
+ }
1662
+
1663
+ combined := ui .OutputWriter .String () + ui .ErrorWriter .String ()
1664
+
1665
+ for _ , str := range tc .outStrings {
1666
+ if ! strings .Contains (combined , str ) {
1667
+ t .Errorf ("expected %q to contain %q" , combined , str )
1668
+ }
1669
+ }
1670
+ })
1671
+ }
1672
+ })
1673
+ }
1525
1674
1526
1675
func createTokenForPolicy (t * testing.T , client * api.Client , policy string ) (* api.SecretAuth , error ) {
1527
1676
t .Helper ()
0 commit comments