| 2636 | |
| 2637 | |
| 2638 | def test_datastore(node_factory): |
| 2639 | l1 = node_factory.get_node() |
| 2640 | |
| 2641 | # Starts empty |
| 2642 | assert l1.rpc.listdatastore() == {'datastore': []} |
| 2643 | assert l1.rpc.listdatastore('somekey') == {'datastore': []} |
| 2644 | |
| 2645 | # Add entries. |
| 2646 | somedata = b'somedata'.hex() |
| 2647 | somedata_expect = {'key': ['somekey'], |
| 2648 | 'generation': 0, |
| 2649 | 'hex': somedata, |
| 2650 | 'string': 'somedata'} |
| 2651 | assert l1.rpc.datastore(key='somekey', hex=somedata) == somedata_expect |
| 2652 | |
| 2653 | assert l1.rpc.listdatastore() == {'datastore': [somedata_expect]} |
| 2654 | assert l1.rpc.listdatastore('somekey') == {'datastore': [somedata_expect]} |
| 2655 | assert l1.rpc.listdatastore('otherkey') == {'datastore': []} |
| 2656 | |
| 2657 | # Cannot add by default. |
| 2658 | with pytest.raises(RpcError, match='already exists'): |
| 2659 | l1.rpc.datastore(key='somekey', hex=somedata) |
| 2660 | |
| 2661 | with pytest.raises(RpcError, match='already exists'): |
| 2662 | l1.rpc.datastore(key='somekey', hex=somedata, mode="must-create") |
| 2663 | |
| 2664 | # But can insist on replace. |
| 2665 | l1.rpc.datastore(key='somekey', hex=somedata[:-4], mode="must-replace") |
| 2666 | assert only_one(l1.rpc.listdatastore('somekey')['datastore'])['hex'] == somedata[:-4] |
| 2667 | # And append works. |
| 2668 | l1.rpc.datastore(key='somekey', hex=somedata[-4:-2], mode="must-append") |
| 2669 | assert only_one(l1.rpc.listdatastore('somekey')['datastore'])['hex'] == somedata[:-2] |
| 2670 | l1.rpc.datastore(key='somekey', hex=somedata[-2:], mode="create-or-append") |
| 2671 | assert only_one(l1.rpc.listdatastore('somekey')['datastore'])['hex'] == somedata |
| 2672 | |
| 2673 | # Generation will have increased due to three ops above. |
| 2674 | somedata_expect['generation'] += 3 |
| 2675 | assert l1.rpc.listdatastore() == {'datastore': [somedata_expect]} |
| 2676 | |
| 2677 | # Can't replace or append non-existing records if we say not to |
| 2678 | with pytest.raises(RpcError, match='does not exist'): |
| 2679 | l1.rpc.datastore(key='otherkey', hex=somedata, mode="must-replace") |
| 2680 | |
| 2681 | with pytest.raises(RpcError, match='does not exist'): |
| 2682 | l1.rpc.datastore(key='otherkey', hex=somedata, mode="must-append") |
| 2683 | |
| 2684 | otherdata = b'otherdata'.hex() |
| 2685 | otherdata_expect = {'key': ['otherkey'], |
| 2686 | 'generation': 0, |
| 2687 | 'hex': otherdata, |
| 2688 | 'string': 'otherdata'} |
| 2689 | assert l1.rpc.datastore(key='otherkey', string='otherdata', mode="create-or-append") == otherdata_expect |
| 2690 | |
| 2691 | assert l1.rpc.listdatastore('somekey') == {'datastore': [somedata_expect]} |
| 2692 | assert l1.rpc.listdatastore('otherkey') == {'datastore': [otherdata_expect]} |
| 2693 | assert l1.rpc.listdatastore('badkey') == {'datastore': []} |
| 2694 | |
| 2695 | # Order is sorted! |