sigs.k8s.io/external-dns@v0.14.1/endpoint/target_filter_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 endpoint
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  type targetFilterTest struct {
    26  	targetFilter []string
    27  	exclusions   []string
    28  	targets      []string
    29  	expected     bool
    30  }
    31  
    32  var targetFilterTests = []targetFilterTest{
    33  	{
    34  		[]string{"10.0.0.0/8"},
    35  		[]string{},
    36  		[]string{"10.1.2.3"},
    37  		true,
    38  	},
    39  	{
    40  		[]string{" 10.0.0.0/8 "},
    41  		[]string{},
    42  		[]string{"10.1.2.3"},
    43  		true,
    44  	},
    45  	{
    46  		[]string{"0"},
    47  		[]string{},
    48  		[]string{"10.1.2.3"},
    49  		true,
    50  	},
    51  	{
    52  		[]string{"10.0.0.0/8"},
    53  		[]string{},
    54  		[]string{"1.1.1.1"},
    55  		false,
    56  	},
    57  	{
    58  		[]string{},
    59  		[]string{"10.0.0.0/8"},
    60  		[]string{"1.1.1.1"},
    61  		true,
    62  	},
    63  	{
    64  		[]string{},
    65  		[]string{"10.0.0.0/8"},
    66  		[]string{"10.1.2.3"},
    67  		false,
    68  	},
    69  }
    70  
    71  func TestTargetFilterWithExclusions(t *testing.T) {
    72  	for i, tt := range targetFilterTests {
    73  		if len(tt.exclusions) == 0 {
    74  			tt.exclusions = append(tt.exclusions, "")
    75  		}
    76  		targetFilter := NewTargetNetFilterWithExclusions(tt.targetFilter, tt.exclusions)
    77  		for _, target := range tt.targets {
    78  			assert.Equal(t, tt.expected, targetFilter.Match(target), "should not fail: %v in test-case #%v", target, i)
    79  		}
    80  	}
    81  }
    82  
    83  func TestTargetFilterMatchWithEmptyFilter(t *testing.T) {
    84  	for _, tt := range targetFilterTests {
    85  		targetFilter := TargetNetFilter{}
    86  		for i, target := range tt.targets {
    87  			assert.True(t, targetFilter.Match(target), "should not fail: %v in test-case #%v", target, i)
    88  		}
    89  	}
    90  }
    91  
    92  func TestMatchTargetFilterReturnsProperEmptyVal(t *testing.T) {
    93  	emptyFilters := []string{}
    94  	assert.Equal(t, true, matchFilter(emptyFilters, "sometarget.com", true))
    95  	assert.Equal(t, false, matchFilter(emptyFilters, "sometarget.com", false))
    96  }