gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/lib/server/operations/system_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package operations_test
     8  
     9  import (
    10  	"fmt"
    11  	"io/ioutil"
    12  	"net"
    13  	"os"
    14  	"path/filepath"
    15  	"time"
    16  
    17  	http "gitee.com/zhaochuninhefei/gmgo/gmhttp"
    18  
    19  	"gitee.com/zhaochuninhefei/fabric-ca-gm/lib/server/operations"
    20  	"gitee.com/zhaochuninhefei/fabric-gm/common/metrics/disabled"
    21  	"gitee.com/zhaochuninhefei/fabric-gm/common/metrics/prometheus"
    22  	"gitee.com/zhaochuninhefei/fabric-gm/common/metrics/statsd"
    23  	. "github.com/onsi/ginkgo"
    24  	. "github.com/onsi/gomega"
    25  )
    26  
    27  var _ = Describe("System", func() {
    28  	var (
    29  		tempDir string
    30  
    31  		authClient   *http.Client
    32  		unauthClient *http.Client
    33  		options      operations.Options
    34  		system       *operations.System
    35  	)
    36  
    37  	BeforeEach(func() {
    38  		var err error
    39  		tempDir, err = ioutil.TempDir("", "system")
    40  		Expect(err).NotTo(HaveOccurred())
    41  
    42  		err = generateCertificates(tempDir)
    43  		Expect(err).NotTo(HaveOccurred())
    44  
    45  		options = operations.Options{
    46  			ListenAddress: "127.0.0.1:0",
    47  			Metrics: operations.MetricsOptions{
    48  				Provider: "disabled",
    49  			},
    50  			TLS: operations.TLS{
    51  				Enabled:            true,
    52  				CertFile:           filepath.Join(tempDir, "server-cert.pem"),
    53  				KeyFile:            filepath.Join(tempDir, "server-key.pem"),
    54  				ClientCertRequired: false,
    55  				ClientCACertFiles:  []string{filepath.Join(tempDir, "client-ca.pem")},
    56  			},
    57  		}
    58  
    59  		system = operations.NewSystem(options)
    60  
    61  		authClient = newHTTPClient(tempDir, true)
    62  		unauthClient = newHTTPClient(tempDir, false)
    63  	})
    64  
    65  	AfterEach(func() {
    66  		os.RemoveAll(tempDir)
    67  		if system != nil {
    68  			system.Stop()
    69  		}
    70  	})
    71  
    72  	It("hosts an unsecured endpoint for the version information", func() {
    73  		err := system.Start()
    74  		Expect(err).NotTo(HaveOccurred())
    75  
    76  		versionURL := fmt.Sprintf("https://%s/version", system.Addr())
    77  		resp, err := unauthClient.Get(versionURL)
    78  		Expect(err).NotTo(HaveOccurred())
    79  		Expect(resp.StatusCode).To(Equal(http.StatusOK))
    80  		resp.Body.Close()
    81  	})
    82  
    83  	Context("when ClientCertRequired is true", func() {
    84  		BeforeEach(func() {
    85  			options.TLS.ClientCertRequired = true
    86  			system = operations.NewSystem(options)
    87  		})
    88  
    89  		It("requires a client cert to connect", func() {
    90  			err := system.Start()
    91  			Expect(err).NotTo(HaveOccurred())
    92  
    93  			_, err = unauthClient.Get(fmt.Sprintf("https://%s/metrics", system.Addr()))
    94  			Expect(err).To(HaveOccurred())
    95  			Expect(err.Error()).To(ContainSubstring("remote error: tls: bad certificate"))
    96  		})
    97  	})
    98  
    99  	Context("when listen fails", func() {
   100  		var listener net.Listener
   101  
   102  		BeforeEach(func() {
   103  			var err error
   104  			listener, err = net.Listen("tcp", "127.0.0.1:0")
   105  			Expect(err).NotTo(HaveOccurred())
   106  
   107  			options.ListenAddress = listener.Addr().String()
   108  			system = operations.NewSystem(options)
   109  		})
   110  
   111  		AfterEach(func() {
   112  			listener.Close()
   113  		})
   114  
   115  		It("returns an error", func() {
   116  			err := system.Start()
   117  			Expect(err).To(HaveOccurred())
   118  			Expect(err.Error()).To(ContainSubstring("bind: address already in use"))
   119  		})
   120  	})
   121  
   122  	Context("when a bad TLS configuration is provided", func() {
   123  		BeforeEach(func() {
   124  			options.TLS.CertFile = "cert-file-does-not-exist"
   125  			system = operations.NewSystem(options)
   126  		})
   127  
   128  		It("returns an error", func() {
   129  			err := system.Start()
   130  			Expect(err).To(MatchError("open cert-file-does-not-exist: no such file or directory"))
   131  		})
   132  	})
   133  
   134  	Context("when the metrics provider is disabled", func() {
   135  		BeforeEach(func() {
   136  			options.Metrics = operations.MetricsOptions{
   137  				Provider: "disabled",
   138  			}
   139  			system = operations.NewSystem(options)
   140  			Expect(system).NotTo(BeNil())
   141  		})
   142  
   143  		It("sets up a disabled provider", func() {
   144  			Expect(system.Provider).To(Equal(&disabled.Provider{}))
   145  		})
   146  	})
   147  
   148  	Context("when the metrics provider is prometheus", func() {
   149  		BeforeEach(func() {
   150  			options.Metrics = operations.MetricsOptions{
   151  				Provider: "prometheus",
   152  			}
   153  			system = operations.NewSystem(options)
   154  			Expect(system).NotTo(BeNil())
   155  		})
   156  
   157  		It("sets up prometheus as a provider", func() {
   158  			Expect(system.Provider).To(Equal(&prometheus.Provider{}))
   159  		})
   160  
   161  		It("hosts a secure endpoint for metrics", func() {
   162  			err := system.Start()
   163  			Expect(err).NotTo(HaveOccurred())
   164  
   165  			metricsURL := fmt.Sprintf("https://%s/metrics", system.Addr())
   166  			resp, err := authClient.Get(metricsURL)
   167  			Expect(err).NotTo(HaveOccurred())
   168  			Expect(resp.StatusCode).To(Equal(http.StatusOK))
   169  			body, err := ioutil.ReadAll(resp.Body)
   170  			resp.Body.Close()
   171  			Expect(err).NotTo(HaveOccurred())
   172  			Expect(body).To(ContainSubstring("# TYPE go_gc_duration_seconds summary"))
   173  		})
   174  	})
   175  
   176  	Context("when the metrics provider is statsd", func() {
   177  		var listener net.Listener
   178  
   179  		BeforeEach(func() {
   180  			var err error
   181  			listener, err = net.Listen("tcp", "127.0.0.1:0")
   182  			Expect(err).NotTo(HaveOccurred())
   183  
   184  			options.Metrics = operations.MetricsOptions{
   185  				Provider: "statsd",
   186  				Statsd: &operations.Statsd{
   187  					Network:       "tcp",
   188  					Address:       listener.Addr().String(),
   189  					WriteInterval: 100 * time.Millisecond,
   190  					Prefix:        "prefix",
   191  				},
   192  			}
   193  			system = operations.NewSystem(options)
   194  			Expect(system).NotTo(BeNil())
   195  		})
   196  
   197  		AfterEach(func() {
   198  			listener.Close()
   199  		})
   200  
   201  		It("sets up statsd as a provider", func() {
   202  			provider, ok := system.Provider.(*statsd.Provider)
   203  			Expect(ok).To(BeTrue())
   204  			Expect(provider.Statsd).NotTo(BeNil())
   205  		})
   206  
   207  		Context("when checking the network and address fails", func() {
   208  			BeforeEach(func() {
   209  				options.Metrics.Statsd.Network = "bob-the-network"
   210  				system = operations.NewSystem(options)
   211  			})
   212  
   213  			It("returns an error", func() {
   214  				err := system.Start()
   215  				Expect(err).To(HaveOccurred())
   216  				Expect(err.Error()).To(ContainSubstring("bob-the-network"))
   217  			})
   218  		})
   219  	})
   220  
   221  	Context("when the metrics provider is unknown", func() {
   222  		BeforeEach(func() {
   223  			options.Metrics.Provider = "something-unknown"
   224  			system = operations.NewSystem(options)
   225  		})
   226  
   227  		It("sets up a disabled provider", func() {
   228  			Expect(system.Provider).To(Equal(&disabled.Provider{}))
   229  		})
   230  	})
   231  })