github.com/verrazzano/verrazzano@v1.7.0/pkg/certs/cabundle_test.go (about)

     1  // Copyright (c) 2023, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package certs
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/verrazzano/verrazzano/pkg/constants"
    13  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    14  	corev1 "k8s.io/api/core/v1"
    15  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    16  	"k8s.io/apimachinery/pkg/runtime"
    17  	clientgoscheme "k8s.io/client-go/kubernetes/scheme"
    18  	"sigs.k8s.io/controller-runtime/pkg/client"
    19  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    20  )
    21  
    22  var testScheme = runtime.NewScheme()
    23  
    24  func init() {
    25  	_ = clientgoscheme.AddToScheme(testScheme)
    26  }
    27  
    28  func TestGetLocalClusterCABundleData(t *testing.T) {
    29  	log := vzlog.DefaultLogger()
    30  	privateCABundleData := []byte("verrazzano-tls-ca-bundle")
    31  	vzTLSBundleData := []byte("verrazzano-tls-bundle")
    32  	tests := []struct {
    33  		name    string
    34  		cli     client.Client
    35  		want    []byte
    36  		wantErr assert.ErrorAssertionFunc
    37  	}{
    38  		{
    39  			name: "verrazzano-tls-ca-only",
    40  			cli: fake.NewClientBuilder().WithScheme(testScheme).WithObjects(
    41  				&corev1.Secret{
    42  					ObjectMeta: metav1.ObjectMeta{
    43  						Name:      constants.PrivateCABundle,
    44  						Namespace: constants.VerrazzanoSystemNamespace,
    45  					},
    46  					Data: map[string][]byte{
    47  						constants.CABundleKey: privateCABundleData,
    48  					},
    49  				}).Build(),
    50  			want:    privateCABundleData,
    51  			wantErr: assert.NoError,
    52  		},
    53  		{
    54  			name: "verrazzano-tls-only",
    55  			cli: fake.NewClientBuilder().WithScheme(testScheme).WithObjects(
    56  				&corev1.Secret{
    57  					ObjectMeta: metav1.ObjectMeta{
    58  						Name:      constants.VerrazzanoIngressTLSSecret,
    59  						Namespace: constants.VerrazzanoSystemNamespace,
    60  					},
    61  					Data: map[string][]byte{
    62  						constants.CACertKey: vzTLSBundleData,
    63  					},
    64  				}).Build(),
    65  			want:    vzTLSBundleData,
    66  			wantErr: assert.NoError,
    67  		},
    68  		{
    69  			name: "verrazzano-tls-ca-and-verrazzano-tls",
    70  			cli: fake.NewClientBuilder().WithScheme(testScheme).WithObjects(
    71  				&corev1.Secret{
    72  					ObjectMeta: metav1.ObjectMeta{
    73  						Name:      constants.VerrazzanoIngressTLSSecret,
    74  						Namespace: constants.VerrazzanoSystemNamespace,
    75  					},
    76  					Data: map[string][]byte{
    77  						constants.CACertKey: vzTLSBundleData,
    78  					},
    79  				},
    80  				&corev1.Secret{
    81  					ObjectMeta: metav1.ObjectMeta{
    82  						Name:      constants.PrivateCABundle,
    83  						Namespace: constants.VerrazzanoSystemNamespace,
    84  					},
    85  					Data: map[string][]byte{
    86  						constants.CABundleKey: privateCABundleData,
    87  					},
    88  				},
    89  			).Build(),
    90  			want:    privateCABundleData,
    91  			wantErr: assert.NoError,
    92  		},
    93  	}
    94  	for _, tt := range tests {
    95  		t.Run(tt.name, func(t *testing.T) {
    96  			ctx := context.TODO()
    97  			got, err := GetLocalClusterCABundleData(log.GetZapLogger(), tt.cli, ctx)
    98  			if !tt.wantErr(t, err, fmt.Sprintf("GetLocalClusterCABundleData(%v, %v, %v)", log, tt.cli, ctx)) {
    99  				return
   100  			}
   101  			assert.Equalf(t, tt.want, got, "GetLocalClusterCABundleData(%v, %v, %v)", log, tt.cli, ctx)
   102  		})
   103  	}
   104  }