sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/config/cert_manager_client_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes 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  
    17  package config
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  
    23  	. "github.com/onsi/gomega"
    24  
    25  	"sigs.k8s.io/cluster-api/cmd/clusterctl/internal/test"
    26  )
    27  
    28  func TestCertManagerGet(t *testing.T) {
    29  	type fields struct {
    30  		reader Reader
    31  	}
    32  	tests := []struct {
    33  		name    string
    34  		fields  fields
    35  		envVars map[string]string
    36  		want    CertManager
    37  		wantErr bool
    38  	}{
    39  		{
    40  			name: "return default url if no custom config is provided",
    41  			fields: fields{
    42  				reader: test.NewFakeReader(),
    43  			},
    44  			want:    NewCertManager(CertManagerDefaultURL, CertManagerDefaultVersion, CertManagerDefaultTimeout.String()),
    45  			wantErr: false,
    46  		},
    47  		{
    48  			name: "return custom url if defined",
    49  			fields: fields{
    50  				reader: test.NewFakeReader().WithCertManager("foo-url", "vX.Y.Z", ""),
    51  			},
    52  			want:    NewCertManager("foo-url", "vX.Y.Z", CertManagerDefaultTimeout.String()),
    53  			wantErr: false,
    54  		},
    55  		{
    56  			name: "return custom url with evaluated env vars if defined",
    57  			fields: fields{
    58  				reader: test.NewFakeReader().WithCertManager("${TEST_REPO_PATH}/foo-url", "vX.Y.Z", ""),
    59  			},
    60  			envVars: map[string]string{
    61  				"TEST_REPO_PATH": "/tmp/test",
    62  			},
    63  			want:    NewCertManager("/tmp/test/foo-url", "vX.Y.Z", CertManagerDefaultTimeout.String()),
    64  			wantErr: false,
    65  		},
    66  		{
    67  			name: "return timeout if defined",
    68  			fields: fields{
    69  				reader: test.NewFakeReader().WithCertManager("", "", "5m"),
    70  			},
    71  			want:    NewCertManager(CertManagerDefaultURL, CertManagerDefaultVersion, "5m"),
    72  			wantErr: false,
    73  		},
    74  	}
    75  	for _, tt := range tests {
    76  		t.Run(tt.name, func(t *testing.T) {
    77  			g := NewWithT(t)
    78  
    79  			for k, v := range tt.envVars {
    80  				g.Expect(os.Setenv(k, v)).To(Succeed())
    81  			}
    82  			defer func() {
    83  				for k := range tt.envVars {
    84  					g.Expect(os.Unsetenv(k)).To(Succeed())
    85  				}
    86  			}()
    87  			p := &certManagerClient{
    88  				reader: tt.fields.reader,
    89  			}
    90  			got, err := p.Get()
    91  			if tt.wantErr {
    92  				g.Expect(err).To(HaveOccurred())
    93  				return
    94  			}
    95  
    96  			g.Expect(err).ToNot(HaveOccurred())
    97  			g.Expect(got).To(Equal(tt.want))
    98  		})
    99  	}
   100  }