Skip to content

Commit

Permalink
[FAB-5007] Using a Normal user by default in e2e tests.
Browse files Browse the repository at this point in the history
Change-Id: I7ad65df9368397aceda4f2511cdaa0e1f7d6ec64
Signed-off-by: Baha Shaaban <[email protected]>
  • Loading branch information
Baha Shaaban committed Jun 26, 2017
1 parent 9e10dd3 commit 85fa310
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
21 changes: 20 additions & 1 deletion test/integration/base_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type BaseSetupImpl struct {
ChainCodeID string
Initialized bool
ChannelConfig string
AdminUser api.User
NormalUser api.User
}

// Initialize reads configuration from file and sets up client, channel and event hub
Expand All @@ -56,10 +58,14 @@ func (setup *BaseSetupImpl) Initialize() error {
setup.Client = client

org1Admin, err := GetAdmin(client, "org1")
org1User, err := GetUser(client, "org1")
if err != nil {
return fmt.Errorf("Error getting org admin user: %v", err)
}

setup.AdminUser = org1Admin
setup.NormalUser = org1User

channel, err := fcutil.GetChannel(setup.Client, setup.ChannelID)
if err != nil {
return fmt.Errorf("Create channel (%s) failed: %v", setup.ChannelID, err)
Expand All @@ -76,7 +82,9 @@ func (setup *BaseSetupImpl) Initialize() error {
return fmt.Errorf("CreateAndJoinChannel return error: %v", err)
}

client.SetUserContext(org1Admin)
//by default client's user context should use regular user, for admin actions, UserContext must be set to AdminUser
client.SetUserContext(org1User)

if err := setup.setupEventHub(client); err != nil {
return err
}
Expand Down Expand Up @@ -113,6 +121,11 @@ func (setup *BaseSetupImpl) InitConfig() (api.Config, error) {

// InstantiateCC ...
func (setup *BaseSetupImpl) InstantiateCC(chainCodeID string, channelID string, chainCodePath string, chainCodeVersion string, args []string) error {
// InstantiateCC requires AdminUser privileges so setting user context with Admin User
setup.Client.SetUserContext(setup.AdminUser)

// must reset client user context to normal user once done with Admin privilieges
defer setup.Client.SetUserContext(setup.NormalUser)
if err := fcutil.SendInstantiateCC(setup.Channel, chainCodeID, channelID, args, chainCodePath, chainCodeVersion, []api.Peer{setup.Channel.GetPrimaryPeer()}, setup.EventHub); err != nil {
return err
}
Expand All @@ -121,9 +134,15 @@ func (setup *BaseSetupImpl) InstantiateCC(chainCodeID string, channelID string,

// InstallCC ...
func (setup *BaseSetupImpl) InstallCC(chainCodeID string, chainCodePath string, chainCodeVersion string, chaincodePackage []byte) error {
// installCC requires AdminUser privileges so setting user context with Admin User
setup.Client.SetUserContext(setup.AdminUser)

// must reset client user context to normal user once done with Admin privilieges
defer setup.Client.SetUserContext(setup.NormalUser)
if err := fcutil.SendInstallCC(setup.Client, setup.Channel, chainCodeID, chainCodePath, chainCodeVersion, chaincodePackage, setup.Channel.GetPeers(), setup.GetDeployPath()); err != nil {
return fmt.Errorf("SendInstallProposal return error: %v", err)
}

return nil
}

Expand Down
20 changes: 13 additions & 7 deletions test/integration/channel_queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func TestChannelQueries(t *testing.T) {

testQueryChannels(t, channel, client)

testInstalledChaincodes(t, channel, client)
testInstalledChaincodes(t, channel, client, testSetup)

testQueryByChaincode(t, channel, client.GetConfig())
testQueryByChaincode(t, channel, client.GetConfig(), testSetup)

// TODO: Synch with test in node SDK when it becomes available
// testInstantiatedChaincodes(t, channel)
Expand Down Expand Up @@ -175,25 +175,28 @@ func testQueryChannels(t *testing.T, channel api.Channel, client api.FabricClien
}

for _, channel := range channelQueryResponse.Channels {
fmt.Printf("**Channel: %s\n", channel)
fmt.Printf("**Channel: %s", channel)
}

}

func testInstalledChaincodes(t *testing.T, channel api.Channel, client api.FabricClient) {
func testInstalledChaincodes(t *testing.T, channel api.Channel, client api.FabricClient, testSetup *BaseSetupImpl) {

// Our target will be primary peer on this channel
target := channel.GetPrimaryPeer()

fmt.Printf("****QueryInstalledChaincodes for %s\n", target.URL())
// Test Query Installed chaincodes for target (primary)
// set Client User Context to Admin first
testSetup.Client.SetUserContext(testSetup.AdminUser)
defer testSetup.Client.SetUserContext(testSetup.NormalUser)

chaincodeQueryResponse, err := client.QueryInstalledChaincodes(target)
if err != nil {
t.Fatalf("QueryInstalledChaincodes return error: %v", err)
}

for _, chaincode := range chaincodeQueryResponse.Chaincodes {
fmt.Printf("**InstalledCC: %s\n", chaincode)
fmt.Printf("**InstalledCC: %s", chaincode)
}

}
Expand All @@ -217,11 +220,13 @@ func testInstantiatedChaincodes(t *testing.T, channel api.Channel) {

}

func testQueryByChaincode(t *testing.T, channel api.Channel, config api.Config) {
func testQueryByChaincode(t *testing.T, channel api.Channel, config api.Config, testSetup *BaseSetupImpl) {

// Test valid targets
targets := channel.GetPeers()

// set Client User Context to Admin before calling QueryByChaincode
testSetup.Client.SetUserContext(testSetup.AdminUser)
queryResponses, err := channel.QueryByChaincode("lscc", []string{"getinstalledchaincodes"}, targets)
if err != nil {
t.Fatalf("QueryByChaincode failed %s", err)
Expand Down Expand Up @@ -272,6 +277,7 @@ func testQueryByChaincode(t *testing.T, channel api.Channel, config api.Config)
t.Fatalf("QueryByChaincode number of results mismatch. Expected: %d Got: %d", len(targets), len(queryResponses))
}

testSetup.Client.SetUserContext(testSetup.NormalUser)
channel.RemovePeer(firstInvalidTarget)
channel.RemovePeer(secondInvalidTarget)
}
11 changes: 5 additions & 6 deletions test/integration/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ func testFailedTx(t *testing.T, testSetup BaseSetupImpl) {

tpResponses1, tx1, err := util.CreateAndSendTransactionProposal(testSetup.Channel, testSetup.ChainCodeID, testSetup.ChannelID, args, []api.Peer{testSetup.Channel.GetPrimaryPeer()}, nil)
if err != nil {
t.Fatalf("CreateAndSendTransactionProposal return error: %v \n", err)
t.Fatalf("CreateAndSendTransactionProposal return error: %v", err)
}

tpResponses2, tx2, err := util.CreateAndSendTransactionProposal(testSetup.Channel, testSetup.ChainCodeID, testSetup.ChannelID, args, []api.Peer{testSetup.Channel.GetPrimaryPeer()}, nil)
if err != nil {
t.Fatalf("CreateAndSendTransactionProposal return error: %v \n", err)
t.Fatalf("CreateAndSendTransactionProposal return error: %v", err)
}

// Register tx1 and tx2 for commit/block event(s)
Expand Down Expand Up @@ -138,15 +138,14 @@ func testFailedTxErrorCode(t *testing.T, testSetup BaseSetupImpl) {
args = append(args, "invoke")
args = append(args, "invoke")
args = append(args, "SEVERE")

tpResponses1, tx1, err := util.CreateAndSendTransactionProposal(testSetup.Channel, testSetup.ChainCodeID, testSetup.ChannelID, args, []api.Peer{testSetup.Channel.GetPrimaryPeer()}, nil)
if err != nil {
t.Fatalf("CreateAndSendTransactionProposal return error: %v \n", err)
t.Fatalf("CreateAndSendTransactionProposal return error: %v", err)
}

tpResponses2, tx2, err := util.CreateAndSendTransactionProposal(testSetup.Channel, testSetup.ChainCodeID, testSetup.ChannelID, args, []api.Peer{testSetup.Channel.GetPrimaryPeer()}, nil)
if err != nil {
t.Fatalf("CreateAndSendTransactionProposal return error: %v \n", err)
t.Fatalf("CreateAndSendTransactionProposal return error: %v", err)
}

done := make(chan bool)
Expand Down Expand Up @@ -256,7 +255,7 @@ func testMultipleBlockEventCallbacks(t *testing.T, testSetup BaseSetupImpl) {

tpResponses, tx, err := util.CreateAndSendTransactionProposal(testSetup.Channel, testSetup.ChainCodeID, testSetup.ChannelID, args, []api.Peer{testSetup.Channel.GetPrimaryPeer()}, nil)
if err != nil {
t.Fatalf("CreateAndSendTransactionProposal returned error: %v \n", err)
t.Fatalf("CreateAndSendTransactionProposal returned error: %v", err)
}

// Register tx for commit/block event(s)
Expand Down
4 changes: 4 additions & 0 deletions test/integration/install_chaincode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func testChaincodeInstallUsingChaincodePath(t *testing.T, testSetup *BaseSetupIm
if err := testSetup.InstallCC(chainCodeName, chainCodePath, chainCodeVersion, nil); err != nil {
t.Fatalf("installCC return error: %v", err)
}

// set Client User Context to Admin
testSetup.Client.SetUserContext(testSetup.AdminUser)
defer testSetup.Client.SetUserContext(testSetup.NormalUser)
chaincodeQueryResponse, err := client.QueryInstalledChaincodes(testSetup.Channel.GetPrimaryPeer())
if err != nil {
t.Fatalf("QueryInstalledChaincodes return error: %v", err)
Expand Down
8 changes: 8 additions & 0 deletions test/integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ func GetAdmin(c api.FabricClient, userOrg string) (api.User, error) {
username := fmt.Sprintf("peer%sAdmin", userOrg)
return util.GetPreEnrolledUser(c, keyDir, certDir, username)
}

// GetUser ...
func GetUser(c api.FabricClient, userOrg string) (api.User, error) {
keyDir := fmt.Sprintf("peerOrganizations/%s.example.com/users/User1@%s.example.com/keystore", userOrg, userOrg)
certDir := fmt.Sprintf("peerOrganizations/%s.example.com/users/User1@%s.example.com/signcerts", userOrg, userOrg)
username := fmt.Sprintf("peer%sUser1", userOrg)
return util.GetPreEnrolledUser(c, keyDir, certDir, username)
}

0 comments on commit 85fa310

Please sign in to comment.