sigs.k8s.io/external-dns@v0.14.1/provider/azure/common_test.go (about)

     1  /*
     2  Copyright 2017 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 azure
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
    24  	dns "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/dns/armdns"
    25  	privatedns "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  func Test_parseMxTarget(t *testing.T) {
    31  	type testCase[T interface {
    32  		dns.MxRecord | privatedns.MxRecord
    33  	}] struct {
    34  		name    string
    35  		args    string
    36  		want    T
    37  		wantErr assert.ErrorAssertionFunc
    38  	}
    39  
    40  	tests := []testCase[dns.MxRecord]{
    41  		{
    42  			name: "valid mx target",
    43  			args: "10 example.com",
    44  			want: dns.MxRecord{
    45  				Preference: to.Ptr(int32(10)),
    46  				Exchange:   to.Ptr("example.com"),
    47  			},
    48  			wantErr: assert.NoError,
    49  		},
    50  		{
    51  			name: "valid mx target with a subdomain",
    52  			args: "99 foo-bar.example.com",
    53  			want: dns.MxRecord{
    54  				Preference: to.Ptr(int32(99)),
    55  				Exchange:   to.Ptr("foo-bar.example.com"),
    56  			},
    57  			wantErr: assert.NoError,
    58  		},
    59  		{
    60  			name:    "invalid mx target with misplaced preference and exchange",
    61  			args:    "example.com 10",
    62  			want:    dns.MxRecord{},
    63  			wantErr: assert.Error,
    64  		},
    65  		{
    66  			name:    "invalid mx target without preference",
    67  			args:    "example.com",
    68  			want:    dns.MxRecord{},
    69  			wantErr: assert.Error,
    70  		},
    71  		{
    72  			name:    "invalid mx target with non numeric preference",
    73  			args:    "aa example.com",
    74  			want:    dns.MxRecord{},
    75  			wantErr: assert.Error,
    76  		},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			got, err := parseMxTarget[dns.MxRecord](tt.args)
    81  			if !tt.wantErr(t, err, fmt.Sprintf("parseMxTarget(%v)", tt.args)) {
    82  				return
    83  			}
    84  			assert.Equalf(t, tt.want, got, "parseMxTarget(%v)", tt.args)
    85  		})
    86  	}
    87  }