sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/internal/util/obj_refs_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 util
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/gomega"
    23  	corev1 "k8s.io/api/core/v1"
    24  )
    25  
    26  func TestGetObjectReferences(t *testing.T) {
    27  	tests := []struct {
    28  		name    string
    29  		args    []string
    30  		want    []corev1.ObjectReference
    31  		wantErr bool
    32  	}{
    33  		{
    34  			name: "valid",
    35  			args: []string{"machinedeployment/foo"},
    36  			want: []corev1.ObjectReference{
    37  				{
    38  					Kind: "machinedeployment",
    39  					Name: "foo",
    40  				},
    41  			},
    42  			wantErr: false,
    43  		},
    44  		{
    45  			name: "valid multiple with name indirection",
    46  			args: []string{"machinedeployment/foo", "machinedeployment/bar"},
    47  			want: []corev1.ObjectReference{
    48  				{
    49  					Kind: "machinedeployment",
    50  					Name: "foo",
    51  				},
    52  				{
    53  					Kind: "machinedeployment",
    54  					Name: "bar",
    55  				},
    56  			},
    57  			wantErr: false,
    58  		},
    59  		{
    60  			name:    "no name but with slash",
    61  			args:    []string{",machinedeployment/"},
    62  			wantErr: true,
    63  		},
    64  		{
    65  			name:    "no name w/o slash",
    66  			args:    []string{",machinedeployment"},
    67  			wantErr: true,
    68  		},
    69  		{
    70  			name:    "trailing slash",
    71  			args:    []string{",foo/"},
    72  			wantErr: true,
    73  		},
    74  		{
    75  			name:    "leading slash",
    76  			args:    []string{"/foo"},
    77  			wantErr: true,
    78  		},
    79  	}
    80  	for _, tt := range tests {
    81  		t.Run(tt.name, func(t *testing.T) {
    82  			g := NewWithT(t)
    83  			got, err := GetObjectReferences("default", tt.args...)
    84  			if tt.wantErr {
    85  				g.Expect(err).To(HaveOccurred())
    86  				return
    87  			}
    88  			g.Expect(err).ToNot(HaveOccurred())
    89  			g.Expect(got).To(HaveLen(len(tt.want)))
    90  			for i := range got {
    91  				g.Expect(got[i].Kind).To(Equal(tt.want[i].Kind))
    92  				g.Expect(got[i].Name).To(Equal(tt.want[i].Name))
    93  				g.Expect(got[i].Namespace).To(Equal("default"))
    94  			}
    95  		})
    96  	}
    97  }