github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/certutils/certutils_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package certutils
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	"testing"
    10  
    11  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    12  	"github.com/SAP/jenkins-library/pkg/mock"
    13  	"github.com/jarcoal/httpmock"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  const (
    18  	caCertsFile = "/kaniko/ssl/certs/ca-certificates.crt"
    19  )
    20  
    21  func TestCertificateUpdate(t *testing.T) {
    22  	certLinks := []string{"https://test-link-1.com/cert.crt", "https://test-link-2.com/cert.crt"}
    23  	httpmock.Activate()
    24  	defer httpmock.DeactivateAndReset()
    25  	httpmock.RegisterResponder(http.MethodGet, "https://test-link-1.com/cert.crt", httpmock.NewStringResponder(200, "testCert"))
    26  	httpmock.RegisterResponder(http.MethodGet, "https://test-link-2.com/cert.crt", httpmock.NewStringResponder(200, "testCert"))
    27  	client := &piperhttp.Client{}
    28  	client.SetOptions(piperhttp.ClientOptions{MaxRetries: -1, UseDefaultTransport: true})
    29  
    30  	t.Run("success case", func(t *testing.T) {
    31  		fileUtils := &mock.FilesMock{}
    32  		fileUtils.AddFile(caCertsFile, []byte("initial cert\n"))
    33  
    34  		err := CertificateUpdate(certLinks, client, fileUtils, caCertsFile)
    35  
    36  		assert.NoError(t, err)
    37  		result, err := fileUtils.FileRead(caCertsFile)
    38  		assert.NoError(t, err)
    39  		assert.Equal(t, "initial cert\ntestCert\ntestCert\n", string(result))
    40  	})
    41  
    42  	t.Run("error case - read certs", func(t *testing.T) {
    43  		client := &piperhttp.Client{}
    44  		fileUtils := &mock.FilesMock{}
    45  
    46  		err := CertificateUpdate(certLinks, client, fileUtils, caCertsFile)
    47  		assert.EqualError(t, err, "failed to load file '/kaniko/ssl/certs/ca-certificates.crt': could not read '/kaniko/ssl/certs/ca-certificates.crt'")
    48  	})
    49  
    50  	t.Run("error case - write certs", func(t *testing.T) {
    51  		fileUtils := &mock.FilesMock{
    52  			FileWriteErrors: map[string]error{
    53  				caCertsFile: fmt.Errorf("write error"),
    54  			},
    55  		}
    56  		fileUtils.AddFile(caCertsFile, []byte("initial cert\n"))
    57  
    58  		err := CertificateUpdate(certLinks, client, fileUtils, caCertsFile)
    59  		assert.EqualError(t, err, "failed to update file '/kaniko/ssl/certs/ca-certificates.crt': write error")
    60  	})
    61  
    62  	t.Run("error case - get cert via http", func(t *testing.T) {
    63  		httpmock.RegisterResponder(http.MethodGet, "http://non-existing-url", httpmock.NewStringResponder(404, "not found"))
    64  
    65  		fileUtils := &mock.FilesMock{}
    66  		fileUtils.AddFile(caCertsFile, []byte("initial cert\n"))
    67  
    68  		err := CertificateUpdate([]string{"http://non-existing-url"}, client, fileUtils, caCertsFile)
    69  		assert.Contains(t, err.Error(), "failed to load certificate from url: request to http://non-existing-url returned with response 404")
    70  	})
    71  
    72  }
    73  
    74  func TestDownloadCACertbunde(t *testing.T) {
    75  	certLinks := []string{"https://test-link-1.com/cert-1.crt", "https://test-link-2.com/cert-2.crt"}
    76  	badCaseLink := "http://non-existing-url"
    77  
    78  	httpmock.Activate()
    79  	defer httpmock.DeactivateAndReset()
    80  	httpmock.RegisterResponder(http.MethodGet, certLinks[0], httpmock.NewStringResponder(http.StatusOK, "testCert1"))
    81  	httpmock.RegisterResponder(http.MethodGet, certLinks[1], httpmock.NewStringResponder(http.StatusOK, "testCert2"))
    82  	httpmock.RegisterResponder(http.MethodGet, badCaseLink, httpmock.NewStringResponder(http.StatusNotFound, "not found"))
    83  
    84  	client := &piperhttp.Client{}
    85  	client.SetOptions(piperhttp.ClientOptions{MaxRetries: -1, UseDefaultTransport: true})
    86  
    87  	testTable := []struct {
    88  		name        string
    89  		certsLinks  []string
    90  		expected    string
    91  		expectedErr string
    92  	}{
    93  		{
    94  			name:       "good case",
    95  			certsLinks: certLinks,
    96  			expected:   "testCert1\ntestCert2\n",
    97  		},
    98  		{
    99  			name: "no links",
   100  		},
   101  		{
   102  			name:        "bad link",
   103  			certsLinks:  []string{badCaseLink},
   104  			expectedErr: fmt.Sprintf("failed to load certificate from url: request to %s returned with response 404", badCaseLink),
   105  		},
   106  	}
   107  
   108  	for _, testCase := range testTable {
   109  		t.Run(testCase.name, func(t *testing.T) {
   110  			certs, err := CertificateDownload(testCase.certsLinks, client)
   111  			if err != nil {
   112  				assert.Contains(t, testCase.expectedErr, err.Error())
   113  			}
   114  			assert.Equal(t, testCase.expected, string(certs))
   115  		})
   116  	}
   117  }