sigs.k8s.io/cluster-api@v1.7.1/util/secret/secret_test.go (about)

     1  /*
     2  Copyright 2020 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 secret
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  )
    24  
    25  func TestParseSecretName(t *testing.T) {
    26  	type args struct {
    27  		name string
    28  	}
    29  	tests := []struct {
    30  		name    string
    31  		args    args
    32  		want    string
    33  		want1   Purpose
    34  		wantErr bool
    35  	}{
    36  		{
    37  			name: "A secret for the test cluster",
    38  			args: args{
    39  				name: "test-kubeconfig",
    40  			},
    41  			want:    "test",
    42  			want1:   Kubeconfig,
    43  			wantErr: false,
    44  		},
    45  		{
    46  			name: "A secret for the test-capa cluster (cluster name with - in the middle)",
    47  			args: args{
    48  				name: "test-capa-ca",
    49  			},
    50  			want:    "test-capa",
    51  			want1:   ClusterCA,
    52  			wantErr: false,
    53  		},
    54  		{
    55  			name: "Not a Cluster API secret",
    56  			args: args{
    57  				name: "foo",
    58  			},
    59  			wantErr: true,
    60  		},
    61  		{
    62  			name: "Not a Cluster API secret (secret name with - in the middle)",
    63  			args: args{
    64  				name: "foo-bar",
    65  			},
    66  			wantErr: true,
    67  		},
    68  	}
    69  	for _, tt := range tests {
    70  		t.Run(tt.name, func(t *testing.T) {
    71  			g := NewWithT(t)
    72  			got, got1, err := ParseSecretName(tt.args.name)
    73  			if tt.wantErr {
    74  				g.Expect(err).To(HaveOccurred())
    75  			} else {
    76  				g.Expect(err).ToNot(HaveOccurred())
    77  			}
    78  			g.Expect(got).To(Equal(tt.want))
    79  			g.Expect(got1).To(Equal(tt.want1))
    80  		})
    81  	}
    82  }