github.com/openshift-online/ocm-sdk-go@v0.1.473/unix_sockets_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  /*
     5  Copyright (c) 2019 Red Hat, Inc.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11    http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  // This file contains tests for the support for Unix sockets.
    21  
    22  package sdk
    23  
    24  import (
    25  	"net/http"
    26  	"os"
    27  	"path/filepath"
    28  	"time"
    29  
    30  	"github.com/onsi/gomega/ghttp"
    31  
    32  	. "github.com/onsi/ginkgo/v2/dsl/core"             // nolint
    33  	. "github.com/onsi/gomega"                         // nolint
    34  	. "github.com/openshift-online/ocm-sdk-go/testing" // nolint
    35  )
    36  
    37  var _ = Describe("Unix sockets", func() {
    38  	var accessToken string
    39  	var refreshToken string
    40  	var oidServer *ghttp.Server
    41  	var oidSocket string
    42  	var oidURL string
    43  
    44  	BeforeEach(func() {
    45  		// Create the tokens:
    46  		accessToken = MakeTokenString("Bearer", 5*time.Minute)
    47  		refreshToken = MakeTokenString("Refresh", 10*time.Hour)
    48  
    49  		// Create the OpenID server:
    50  		oidServer, oidSocket = MakeUnixServer()
    51  		oidServer.AppendHandlers(
    52  			ghttp.CombineHandlers(
    53  				RespondWithAccessAndRefreshTokens(accessToken, refreshToken),
    54  			),
    55  		)
    56  		oidURL = "unix://127.0.0.1" + oidSocket
    57  	})
    58  
    59  	AfterEach(func() {
    60  		// Stop the OpenID server:
    61  		oidServer.Close()
    62  	})
    63  
    64  	Describe("With HTTP", func() {
    65  		var apiServer *ghttp.Server
    66  		var apiURL string
    67  		var apiSocket string
    68  		var connection *Connection
    69  
    70  		BeforeEach(func() {
    71  			var err error
    72  
    73  			// Create the server:
    74  			apiServer, apiSocket = MakeUnixServer()
    75  			apiURL = "unix://127.0.0.1" + apiSocket
    76  
    77  			// Create the connection:
    78  			connection, err = NewConnectionBuilder().
    79  				Logger(logger).
    80  				TokenURL(oidURL).
    81  				URL(apiURL).
    82  				Tokens(accessToken, refreshToken).
    83  				Build()
    84  			Expect(err).ToNot(HaveOccurred())
    85  		})
    86  
    87  		AfterEach(func() {
    88  			var err error
    89  
    90  			// Close the connection:
    91  			err = connection.Close()
    92  			Expect(err).ToNot(HaveOccurred())
    93  
    94  			// Close the server:
    95  			apiServer.Close()
    96  
    97  			// Remore the temporary files and directories:
    98  			err = os.RemoveAll(filepath.Dir(apiSocket))
    99  			Expect(err).ToNot(HaveOccurred())
   100  		})
   101  
   102  		It("Get", func() {
   103  			// Configure the server:
   104  			apiServer.AppendHandlers(
   105  				ghttp.CombineHandlers(
   106  					RespondWithJSON(http.StatusOK, `{
   107  						"href": "/api/clusters_mgmt"
   108  					}`),
   109  				),
   110  			)
   111  
   112  			// Send the request:
   113  			response, err := connection.Get().
   114  				Path("/mypath").
   115  				Send()
   116  			Expect(err).ToNot(HaveOccurred())
   117  			Expect(response).ToNot(BeNil())
   118  			Expect(response.String()).To(MatchJSON(`{
   119  				"href": "/api/clusters_mgmt"
   120  			}`))
   121  		})
   122  	})
   123  
   124  	Describe("With HTTPS", func() {
   125  		var apiServer *ghttp.Server
   126  		var apiURL string
   127  		var apiCA string
   128  		var apiSocket string
   129  		var connection *Connection
   130  
   131  		BeforeEach(func() {
   132  			var err error
   133  
   134  			// Create the server:
   135  			apiServer, apiCA, apiSocket = MakeUnixTLSServer()
   136  			apiURL = "unix+https://127.0.0.1" + apiSocket
   137  
   138  			// Create the connection:
   139  			connection, err = NewConnectionBuilder().
   140  				Logger(logger).
   141  				TokenURL(oidURL).
   142  				URL(apiURL).
   143  				Tokens(accessToken, refreshToken).
   144  				TrustedCAFile(apiCA).
   145  				Build()
   146  			Expect(err).ToNot(HaveOccurred())
   147  		})
   148  
   149  		AfterEach(func() {
   150  			var err error
   151  
   152  			// Close the connection:
   153  			err = connection.Close()
   154  			Expect(err).ToNot(HaveOccurred())
   155  
   156  			// Close the server:
   157  			apiServer.Close()
   158  
   159  			// Remore the temporary files and directories:
   160  			err = os.RemoveAll(apiCA)
   161  			Expect(err).ToNot(HaveOccurred())
   162  			err = os.RemoveAll(filepath.Dir(apiSocket))
   163  			Expect(err).ToNot(HaveOccurred())
   164  		})
   165  
   166  		It("Get", func() {
   167  			// Configure the server:
   168  			apiServer.AppendHandlers(
   169  				ghttp.CombineHandlers(
   170  					RespondWithJSON(http.StatusOK, `{
   171  						"href": "/api/clusters_mgmt"
   172  					}`),
   173  				),
   174  			)
   175  
   176  			// Send the request:
   177  			response, err := connection.Get().
   178  				Path("/mypath").
   179  				Send()
   180  			Expect(err).ToNot(HaveOccurred())
   181  			Expect(response).ToNot(BeNil())
   182  			Expect(response.String()).To(MatchJSON(`{
   183  				"href": "/api/clusters_mgmt"
   184  			}`))
   185  		})
   186  	})
   187  
   188  	Describe("With Unix and TCP simultaneously", func() {
   189  		var unixServer *ghttp.Server
   190  		var unixURL string
   191  		var unixSocket string
   192  		var tcpServer *ghttp.Server
   193  		var tcpURL string
   194  		var connection *Connection
   195  
   196  		BeforeEach(func() {
   197  			var err error
   198  
   199  			// Create the the main server, using Unix sockets:
   200  			unixServer, unixSocket = MakeUnixServer()
   201  			unixURL = "unix://127.0.0.1" + unixSocket
   202  
   203  			// Make the alternative, using TCP:
   204  			tcpServer = MakeTCPServer()
   205  			tcpURL = tcpServer.URL()
   206  
   207  			// Create the connection:
   208  			connection, err = NewConnectionBuilder().
   209  				Logger(logger).
   210  				TokenURL(oidURL).
   211  				AlternativeURL("/api/clusters_mgmt", unixURL).
   212  				AlternativeURL("/api/accounts_mgmt", tcpURL).
   213  				Tokens(accessToken, refreshToken).
   214  				Build()
   215  			Expect(err).ToNot(HaveOccurred())
   216  		})
   217  
   218  		AfterEach(func() {
   219  			var err error
   220  
   221  			// Close the connection:
   222  			err = connection.Close()
   223  			Expect(err).ToNot(HaveOccurred())
   224  
   225  			// Close the servers:
   226  			unixServer.Close()
   227  			tcpServer.Close()
   228  
   229  			// Remore the temporary files and directories:
   230  			err = os.RemoveAll(filepath.Dir(unixSocket))
   231  			Expect(err).ToNot(HaveOccurred())
   232  		})
   233  
   234  		It("Get", func() {
   235  			// Configure the servers:
   236  			unixServer.AppendHandlers(
   237  				ghttp.CombineHandlers(
   238  					RespondWithJSON(http.StatusOK, `{
   239  						"href": "/api/clusters_mgmt"
   240  					}`),
   241  				),
   242  			)
   243  			tcpServer.AppendHandlers(
   244  				ghttp.CombineHandlers(
   245  					RespondWithJSON(http.StatusOK, `{
   246  						"href": "/api/accounts_mgmt"
   247  					}`),
   248  				),
   249  			)
   250  
   251  			// Send a request to the Unix server:
   252  			unixResponse, err := connection.Get().
   253  				Path("/api/clusters_mgmt").
   254  				Send()
   255  			Expect(err).ToNot(HaveOccurred())
   256  			Expect(unixResponse.String()).To(MatchJSON(`{
   257  				"href": "/api/clusters_mgmt"
   258  			}`))
   259  
   260  			// Send a request to the TCP server:
   261  			tcpResponse, err := connection.Get().
   262  				Path("/api/accounts_mgmt").
   263  				Send()
   264  			Expect(err).ToNot(HaveOccurred())
   265  			Expect(tcpResponse.String()).To(MatchJSON(`{
   266  				"href": "/api/accounts_mgmt"
   267  			}`))
   268  		})
   269  	})
   270  })