...

Source file src/github.com/sigstore/rekor/pkg/client/rekor_client_test.go

Documentation: github.com/sigstore/rekor/pkg/client

     1  //
     2  // Copyright 2021 The Sigstore Authors.
     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 client
    17  
    18  import (
    19  	"net/http"
    20  	"net/http/httptest"
    21  	"strings"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/sigstore/rekor/pkg/generated/client/index"
    26  	"go.uber.org/goleak"
    27  )
    28  
    29  func TestGetRekorClientWithUserAgent(t *testing.T) {
    30  	t.Parallel()
    31  	expectedUserAgent := "test User-Agent"
    32  	requestReceived := false
    33  	testServer := httptest.NewServer(http.HandlerFunc(
    34  		func(w http.ResponseWriter, r *http.Request) {
    35  			requestReceived = true
    36  			file := []byte{}
    37  
    38  			got := r.UserAgent()
    39  			if got != expectedUserAgent {
    40  				t.Errorf("wanted User-Agent %q, got %q", expectedUserAgent, got)
    41  			}
    42  			w.WriteHeader(http.StatusOK)
    43  			_, _ = w.Write(file)
    44  		}))
    45  	defer testServer.Close()
    46  
    47  	client, err := GetRekorClient(testServer.URL, WithUserAgent(expectedUserAgent))
    48  	if err != nil {
    49  		t.Error(err)
    50  	}
    51  	_, _ = client.Tlog.GetLogInfo(nil)
    52  	if !requestReceived {
    53  		t.Fatal("no requests were received")
    54  	}
    55  }
    56  
    57  func TestGetRekorClientWithCustomPath(t *testing.T) {
    58  	t.Parallel()
    59  	requestReceived := false
    60  	pathAdd := "/custom"
    61  
    62  	testServer := httptest.NewServer(http.HandlerFunc(
    63  		func(w http.ResponseWriter, r *http.Request) {
    64  			requestReceived = true
    65  			if !strings.HasPrefix(r.URL.Path, pathAdd) {
    66  				t.Errorf("Expected request to be sent to /test, got %s", r.URL.Path)
    67  			}
    68  			w.WriteHeader(http.StatusOK)
    69  		}))
    70  	defer testServer.Close()
    71  
    72  	testServer.URL += pathAdd
    73  
    74  	client, err := GetRekorClient(testServer.URL)
    75  	if err != nil {
    76  		t.Error(err)
    77  	}
    78  	_, _ = client.Tlog.GetLogInfo(nil)
    79  	if !requestReceived {
    80  		t.Fatal("no requests were received")
    81  	}
    82  }
    83  
    84  func TestGetRekorClientWithRetryCount(t *testing.T) {
    85  	t.Parallel()
    86  	expectedCount := 2
    87  	actualCount := 0
    88  	testServer := httptest.NewServer(http.HandlerFunc(
    89  		func(w http.ResponseWriter, _ *http.Request) {
    90  			actualCount++
    91  			file := []byte{}
    92  
    93  			if actualCount < expectedCount {
    94  				w.WriteHeader(http.StatusInternalServerError)
    95  			} else {
    96  				w.WriteHeader(http.StatusOK)
    97  				_, _ = w.Write(file)
    98  			}
    99  		}))
   100  	defer testServer.Close()
   101  
   102  	client, err := GetRekorClient(testServer.URL, WithRetryCount(2))
   103  	if err != nil {
   104  		t.Error(err)
   105  	}
   106  	_, err = client.Tlog.GetLogInfo(nil)
   107  	if err != nil {
   108  		t.Fatalf("unexpected error: %v", err)
   109  	}
   110  }
   111  
   112  func TestRekorLeakedGoroutine_SearchByHash(t *testing.T) {
   113  	testServer := httptest.NewUnstartedServer(http.HandlerFunc(
   114  		func(w http.ResponseWriter, _ *http.Request) {
   115  			file := []byte("ok")
   116  
   117  			w.WriteHeader(http.StatusOK)
   118  			_, _ = w.Write(file)
   119  		}))
   120  	testServer.EnableHTTP2 = true
   121  	testServer.StartTLS()
   122  	// sleep to allow go routines to start
   123  	time.Sleep(1 * time.Second)
   124  	// store the goroutines launched by the testserver
   125  	opt := goleak.IgnoreCurrent()
   126  	defer func() {
   127  		goleak.VerifyNone(t, opt)
   128  		// this is done after leak detection so that we can test
   129  		testServer.Close()
   130  	}()
   131  	rekor, _ := GetRekorClient(testServer.URL, WithInsecureTLS(true))
   132  	rekor.Index.SearchIndex(index.NewSearchIndexParams())
   133  }
   134  

View as plain text