github.com/IBM-Blockchain/fabric-operator@v1.0.4/pkg/certificate/reenroller/reenroller_suite_test.go (about)

     1  /*
     2   * Copyright contributors to the Hyperledger Fabric Operator project
     3   *
     4   * SPDX-License-Identifier: Apache-2.0
     5   *
     6   * Licensed under the Apache License, Version 2.0 (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at:
     9   *
    10   * 	  http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing, software
    13   * distributed under the License is distributed on an "AS IS" BASIS,
    14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15   * See the License for the specific language governing permissions and
    16   * limitations under the License.
    17   */
    18  
    19  package reenroller_test
    20  
    21  import (
    22  	"encoding/pem"
    23  	"io/ioutil"
    24  	"net/http"
    25  	"net/http/httptest"
    26  	"net/url"
    27  	"os"
    28  	"path/filepath"
    29  	"testing"
    30  
    31  	current "github.com/IBM-Blockchain/fabric-operator/api/v1beta1"
    32  	"github.com/IBM-Blockchain/fabric-operator/pkg/certificate/reenroller"
    33  	"github.com/IBM-Blockchain/fabric-operator/pkg/certificate/reenroller/mocks"
    34  	"github.com/IBM-Blockchain/fabric-operator/pkg/util"
    35  	. "github.com/onsi/ginkgo/v2"
    36  	. "github.com/onsi/gomega"
    37  )
    38  
    39  func TestReenroller(t *testing.T) {
    40  	RegisterFailHandler(Fail)
    41  	RunSpecs(t, "Reenroller Suite")
    42  }
    43  
    44  var (
    45  	err error
    46  
    47  	testReenroller *reenroller.Reenroller
    48  	config         *current.Enrollment
    49  	mockIdentity   *mocks.Identity
    50  
    51  	server       *httptest.Server
    52  	serverCert   string
    53  	serverURL    string
    54  	serverUrlObj *url.URL
    55  )
    56  
    57  var _ = BeforeSuite(func() {
    58  	// Start a local HTTP server
    59  	server = httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
    60  		// Test request parameters
    61  		Expect(req.URL.String()).To(Equal("/cainfo"))
    62  		return
    63  	}))
    64  
    65  	serverURL = server.URL
    66  	rawCert := server.Certificate().Raw
    67  	pemCert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: rawCert})
    68  	serverCert = string(util.BytesToBase64(pemCert))
    69  
    70  	urlObj, err := url.Parse(serverURL)
    71  	Expect(err).NotTo(HaveOccurred())
    72  	serverUrlObj = urlObj
    73  
    74  	// Generate temporary key for reenroll test
    75  	keystorePath := filepath.Join(homeDir, "msp", "keystore")
    76  	err = os.MkdirAll(keystorePath, 0755)
    77  	Expect(err).NotTo(HaveOccurred())
    78  
    79  	key, err := util.Base64ToBytes(testkey)
    80  	Expect(err).NotTo(HaveOccurred())
    81  	err = ioutil.WriteFile(filepath.Join(keystorePath, "key.pem"), key, 0755)
    82  })
    83  
    84  var _ = AfterSuite(func() {
    85  	// Close the server when test finishes
    86  	server.Close()
    87  
    88  	err = os.RemoveAll(homeDir)
    89  	Expect(err).NotTo(HaveOccurred())
    90  })