sigs.k8s.io/cluster-api-provider-aws@v1.5.5/api/v1beta1/sshkeyname_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 v1beta1 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/aws/aws-sdk-go/aws" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 ) 27 28 func Test_SSHKeyName(t *testing.T) { 29 tests := []struct { 30 name string 31 sshKeyName *string 32 wantErr bool 33 }{ 34 { 35 name: "SSH key name is nil is valid", 36 sshKeyName: nil, 37 wantErr: false, 38 }, 39 { 40 name: "SSH key name is an empty string is valid", 41 sshKeyName: aws.String(""), 42 wantErr: false, 43 }, 44 { 45 name: "SSH key name with alphanumeric characters is valid", 46 sshKeyName: aws.String("test123"), 47 wantErr: false, 48 }, 49 { 50 name: "SSH key name with underscore is valid", 51 sshKeyName: aws.String("test_key"), 52 wantErr: false, 53 }, 54 { 55 name: "SSH key name with dash is valid", 56 sshKeyName: aws.String(`test-key`), 57 wantErr: false, 58 }, 59 { 60 name: "SSH key name with tab is not valid", 61 sshKeyName: aws.String("test-capi\t"), 62 wantErr: true, 63 }, 64 } 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 cluster := &AWSCluster{ 68 ObjectMeta: metav1.ObjectMeta{ 69 GenerateName: "cluster-", 70 Namespace: "default", 71 }, 72 Spec: AWSClusterSpec{ 73 SSHKeyName: tt.sshKeyName, 74 }, 75 } 76 machine := &AWSMachine{ 77 ObjectMeta: metav1.ObjectMeta{ 78 GenerateName: "machine-", 79 Namespace: "default", 80 }, 81 Spec: AWSMachineSpec{ 82 SSHKeyName: tt.sshKeyName, 83 InstanceType: "test", 84 }, 85 } 86 for _, obj := range []client.Object{cluster, machine} { 87 ctx := context.TODO() 88 if err := testEnv.Create(ctx, obj); (err != nil) != tt.wantErr { 89 t.Errorf("ValidateCreate() error = %v, wantErr %v", err, tt.wantErr) 90 } 91 } 92 }) 93 } 94 }