github.com/s7techlab/cckit@v0.10.5/extensions/debug/handler_test.go (about)

     1  package debug_test
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo"
     5  	. "github.com/onsi/gomega"
     6  
     7  	"strconv"
     8  	"testing"
     9  
    10  	"github.com/s7techlab/cckit/extensions/debug"
    11  	"github.com/s7techlab/cckit/extensions/owner"
    12  	"github.com/s7techlab/cckit/identity/testdata"
    13  	"github.com/s7techlab/cckit/router"
    14  	statetest "github.com/s7techlab/cckit/state/testdata"
    15  	testcc "github.com/s7techlab/cckit/testing"
    16  	expectcc "github.com/s7techlab/cckit/testing/expect"
    17  )
    18  
    19  func TestDebug(t *testing.T) {
    20  	RegisterFailHandler(Fail)
    21  	RunSpecs(t, "Debug suite")
    22  }
    23  
    24  var (
    25  	Owner = testdata.Certificates[0].MustIdentity(`SOME_MSP`)
    26  )
    27  
    28  func NewChaincode() *router.Chaincode {
    29  	r := router.New(`debuggable`).Init(owner.InvokeSetFromCreator)
    30  	debug.AddHandlers(r, `debug`, owner.Only)
    31  	return router.NewChaincode(r)
    32  }
    33  
    34  var _ = Describe(`Debuggable`, func() {
    35  
    36  	//Create chaincode mock
    37  	cc := testcc.NewMockStub(`debuggable`, NewChaincode())
    38  	cc.From(Owner).Init()
    39  
    40  	Describe("Debug", func() {
    41  
    42  		It("Allow to clean empty state", func() {
    43  			emptyResult := expectcc.PayloadIs(cc.From(Owner).Invoke(
    44  				`debugStateClean`,
    45  				[]string{`some`, `non existent`, `key prefixes`}), new(map[string]uint32)).(map[string]uint32)
    46  
    47  			Expect(emptyResult[`some`]).To(Equal(uint32(0)))
    48  			Expect(len(emptyResult)).To(Equal(3))
    49  		})
    50  
    51  		It("Allow put value in state", func() {
    52  			for i := 0; i < 5; i++ {
    53  				expectcc.ResponseOk(cc.From(Owner).Invoke(
    54  					`debugStatePut`,
    55  					[]string{`prefixA`, `key` + strconv.Itoa(i)}, []byte(`value`+strconv.Itoa(i))))
    56  			}
    57  
    58  			for i := 0; i < 7; i++ {
    59  				expectcc.ResponseOk(cc.From(Owner).Invoke(
    60  					`debugStatePut`,
    61  					[]string{`prefixB`, `subprefixA`, `key` + strconv.Itoa(i)}, []byte(`value`+strconv.Itoa(i))))
    62  				expectcc.ResponseOk(cc.From(Owner).Invoke(
    63  					`debugStatePut`,
    64  					[]string{`prefixB`, `subprefixB`, `key` + strconv.Itoa(i)}, []byte(`value`+strconv.Itoa(i))))
    65  			}
    66  
    67  			cc.From(Owner).Invoke(`debugStatePut`, []string{`keyA`}, []byte(`valueKeyA`))
    68  			cc.From(Owner).Invoke(`debugStatePut`, []string{`keyB`}, []byte(`valueKeyB`))
    69  			cc.From(Owner).Invoke(`debugStatePut`, []string{`keyC`}, []byte(`valueKeyC`))
    70  		})
    71  
    72  		It("Allow to get value from state", func() {
    73  			Expect(cc.From(Owner).Invoke(`debugStateGet`, []string{`prefixA`, `key1`}).Payload).To(Equal([]byte(`value1`)))
    74  			Expect(cc.From(Owner).Invoke(`debugStateGet`, []string{`keyA`}).Payload).To(Equal([]byte(`valueKeyA`)))
    75  		})
    76  
    77  		It("Allow to get keys", func() {
    78  
    79  			keys := expectcc.PayloadIs(cc.From(Owner).Invoke(`debugStateKeys`, `prefixA`), &[]string{}).([]string)
    80  			Expect(len(keys)).To(Equal(5))
    81  
    82  			key0, key0rest, _ := cc.SplitCompositeKey(keys[0])
    83  			Expect(key0).To(Equal(`prefixA`))
    84  			Expect(key0rest).To(Equal([]string{`key0`}))
    85  
    86  			keys = expectcc.PayloadIs(cc.From(Owner).Invoke(
    87  				`debugStateKeys`, statetest.MustCreateCompositeKey(`prefixB`, []string{`subprefixB`})),
    88  				&[]string{}).([]string)
    89  			Expect(len(keys)).To(Equal(7))
    90  		})
    91  
    92  		It("Allow to delete state entry", func() {
    93  			expectcc.ResponseOk(cc.From(Owner).Invoke(`debugStateDelete`, []string{`prefixA`, `key0`}))
    94  			keys := expectcc.PayloadIs(cc.From(Owner).Invoke(`debugStateKeys`, `prefixA`), &[]string{}).([]string)
    95  			Expect(len(keys)).To(Equal(4))
    96  
    97  			expectcc.ResponseOk(cc.From(Owner).Invoke(`debugStateDelete`, []string{`prefixA`, `key4`}))
    98  			keys = expectcc.PayloadIs(cc.From(Owner).Invoke(`debugStateKeys`, `prefixA`), &[]string{}).([]string)
    99  			Expect(len(keys)).To(Equal(3))
   100  		})
   101  
   102  		It("Allow to clean state", func() {
   103  			cleanResult := expectcc.PayloadIs(
   104  				cc.From(Owner).Invoke(`debugStateClean`, []string{`prefixA`}), new(map[string]int)).(map[string]int)
   105  
   106  			Expect(cleanResult[`prefixA`]).To(Equal(3))
   107  			Expect(len(cleanResult)).To(Equal(1))
   108  
   109  			keys := expectcc.PayloadIs(cc.From(Owner).Invoke(`debugStateKeys`, `prefixA`), &[]string{}).([]string)
   110  			Expect(len(keys)).To(Equal(0))
   111  		})
   112  
   113  	})
   114  })