github.com/silveraid/fabric-ca@v1.1.0-preview.0.20180127000700-71974f53ab08/lib/server_whitebox_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package lib
    17  
    18  import (
    19  	"os"
    20  	"testing"
    21  )
    22  
    23  const (
    24  	serverPort      = 7060
    25  	affiliationName = "org1"
    26  )
    27  
    28  // TestGetAffliation checks if there is one record for the
    29  // affilition 'org1' in the database after starting the server
    30  // two times. This test is to make sure server does not create
    31  // duplicate affiliations in the database every time it is
    32  // started.
    33  func TestGetAffliation(t *testing.T) {
    34  	defer func() {
    35  		err := os.RemoveAll("../testdata/ca-cert.pem")
    36  		if err != nil {
    37  			t.Errorf("RemoveAll failed: %s", err)
    38  		}
    39  		err = os.RemoveAll("../testdata/fabric-ca-server.db")
    40  		if err != nil {
    41  			t.Errorf("RemoveAll failed: %s", err)
    42  		}
    43  		err = os.RemoveAll("../testdata/msp")
    44  		if err != nil {
    45  			t.Errorf("RemoveAll failed: %s", err)
    46  		}
    47  	}()
    48  	// Start the server at an available port (using port 0 will make OS to
    49  	// pick an available port)
    50  	srv := getServer(serverPort, testdataDir, "", -1, t)
    51  
    52  	err := srv.Start()
    53  	if err != nil {
    54  		t.Fatalf("Server start failed: %v", err)
    55  	}
    56  	err = srv.Stop()
    57  	if err != nil {
    58  		t.Fatalf("Server stop failed: %v", err)
    59  	}
    60  
    61  	err = srv.Start()
    62  	if err != nil {
    63  		t.Fatalf("Server start failed: %v", err)
    64  	}
    65  	defer func() {
    66  		err = srv.Stop()
    67  		if err != nil {
    68  			t.Errorf("Failed to stop server: %s", err)
    69  		}
    70  	}()
    71  
    72  	afs := []AffiliationRecord{}
    73  	err = srv.db.Select(&afs, srv.db.Rebind(getAffiliationQuery), affiliationName)
    74  	t.Logf("Retrieved %+v for the affiliation %s", afs, affiliationName)
    75  	if err != nil {
    76  		t.Fatalf("Failed to get affiliation %s: %v", affiliationName, err)
    77  	}
    78  	if len(afs) != 1 {
    79  		t.Fatalf("Found 0 or more than one record for the affiliation %s in the database, expected 1 record", affiliationName)
    80  	}
    81  }