github.com/verrazzano/verrazzano@v1.7.1/application-operator/apis/oam/v1alpha1/ingresstrait_webhook_test.go (about)

     1  // Copyright (C) 2020, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package v1alpha1
     5  
     6  import (
     7  	"github.com/stretchr/testify/assert"
     8  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
     9  	"testing"
    10  )
    11  
    12  var existingTraits = IngressTraitList{}
    13  
    14  // create validation tests
    15  
    16  // TestValidateCreateEmptyHostNoPaths tests validation of an IngressTrait create with empty host and path.
    17  // GIVEN no existing IngressTrait's
    18  // WHEN validate is called on a new IngressTrait with no host and path
    19  // THEN validate is successful and returns no errors
    20  func TestValidateCreateEmptyHostNoPaths(t *testing.T) {
    21  	originalListIngressTraits := getAllIngressTraits
    22  	getAllIngressTraits = testListIngressTraits
    23  	defer func() { getAllIngressTraits = originalListIngressTraits }()
    24  
    25  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{""}}}}}
    26  	err := ingressTrait.ValidateCreate()
    27  	assert.Nil(t, err)
    28  }
    29  
    30  // TestValidateCreateEmptyHostNoPathsCollision tests validation of an IngressTrait create with empty host and path that
    31  // collides with an existing trait with an empty host and path.
    32  // GIVEN an existing IngressTrait with no host and path
    33  // WHEN validate is called on a new IngressTrait with no host and path
    34  // THEN validate fails and returns an error
    35  func TestValidateCreateEmptyHostNoPathsCollision(t *testing.T) {
    36  	originalListIngressTraits := getAllIngressTraits
    37  	getAllIngressTraits = testListIngressTraits
    38  	defer func() { getAllIngressTraits = originalListIngressTraits }()
    39  
    40  	existingTraits.Items = append(existingTraits.Items, IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{""}}}}})
    41  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
    42  
    43  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{""}}}}}
    44  
    45  	err := ingressTrait.ValidateCreate()
    46  	assert.NotNil(t, err)
    47  }
    48  
    49  // TestValidateCreateEmptyHostNoCollisionWithExactHost tests validation of an IngressTrait create with empty host and
    50  // specified path and ensures it doesn't conflict with an existing IngressTrait with an exact host and matching path.
    51  // GIVEN an existing IngressTrait with an exact host and a specified path
    52  // WHEN validate is called on a new IngressTrait with no host and matching path
    53  // THEN validate is successful and returns no error
    54  func TestValidateCreateEmptyHostNoCollisionWithExactHost(t *testing.T) {
    55  	originalListIngressTraits := getAllIngressTraits
    56  	getAllIngressTraits = testListIngressTraits
    57  	defer func() { getAllIngressTraits = originalListIngressTraits }()
    58  
    59  	existingPaths := []IngressPath{{Path: "/test/path"}}
    60  	existingRule := IngressRule{Hosts: []string{"foo.bar.com"}, Paths: existingPaths}
    61  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
    62  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
    63  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
    64  
    65  	paths := []IngressPath{{Path: "/test/path"}}
    66  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{""}, Paths: paths}}}}
    67  
    68  	err := ingressTrait.ValidateCreate()
    69  	assert.Nil(t, err)
    70  }
    71  
    72  // TestValidateCreateEmptyHostNoCollisionWithWildcardHost tests validation of an IngressTrait create with empty host
    73  // and specified path and ensures it doesn't conflict with an existing IngressTrait with a prefix wildcard host
    74  // and matching path.
    75  // GIVEN an existing IngressTrait with a wildcard prefix host and a specified path
    76  // WHEN validate is called on a new IngressTrait with no host and matching path
    77  // THEN validate is successful and returns no error
    78  func TestValidateCreateEmptyHostNoCollisionWithWildcardHost(t *testing.T) {
    79  	originalListIngressTraits := getAllIngressTraits
    80  	getAllIngressTraits = testListIngressTraits
    81  	defer func() { getAllIngressTraits = originalListIngressTraits }()
    82  
    83  	existingPaths := []IngressPath{{Path: "/test/path"}}
    84  	existingRule := IngressRule{Hosts: []string{"*.bar.com"}, Paths: existingPaths}
    85  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
    86  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
    87  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
    88  
    89  	paths := []IngressPath{{Path: "/test/path"}}
    90  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{""}, Paths: paths}}}}
    91  
    92  	err := ingressTrait.ValidateCreate()
    93  	assert.Nil(t, err)
    94  }
    95  
    96  // TestValidateCreateStarHostNoPaths tests validation of an IngressTrait create with a "*" host and empty path.
    97  // GIVEN no existing IngressTrait's
    98  // WHEN validate is called on a new IngressTrait with a "*" host and no path
    99  // THEN validate is successful and returns no errors
   100  func TestValidateCreateStarHostNoPaths(t *testing.T) {
   101  	originalListIngressTraits := getAllIngressTraits
   102  	getAllIngressTraits = testListIngressTraits
   103  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   104  
   105  	rule := IngressRule{Hosts: []string{"*"}, Paths: []IngressPath{}}
   106  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   107  	err := ingressTrait.ValidateCreate()
   108  	assert.Nil(t, err)
   109  }
   110  
   111  // TestValidateCreateStarHostNoPathsCollision tests validation of an IngressTrait create with a "*" host and empty path
   112  // that collides with an existing IngressTrait.
   113  // GIVEN an existing IngressTrait with a "*" host and empty path
   114  // WHEN validate is called on a new IngressTrait with a "*" host and no path
   115  // THEN validate fails and returns an error
   116  func TestValidateCreateStarHostNoPathsCollision(t *testing.T) {
   117  	originalListIngressTraits := getAllIngressTraits
   118  	getAllIngressTraits = testListIngressTraits
   119  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   120  
   121  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"*"}}}}}
   122  
   123  	existingTraits.Items = append(existingTraits.Items, IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"*"}}}}})
   124  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   125  
   126  	err := ingressTrait.ValidateCreate()
   127  	assert.NotNil(t, err)
   128  }
   129  
   130  // TestValidateCreateStarHostCollidesWithEmptyHost tests validation of an IngressTrait create with a "*" host and a
   131  // specified path that collides with an existing IngressTrait with the same path and an empty host.
   132  // GIVEN an existing IngressTrait with an empty host and specified path
   133  // WHEN validate is called on a new IngressTrait with a "*" host and matching path
   134  // THEN validate fails and returns an error
   135  func TestValidateCreateStarHostCollidesWithEmptyHost(t *testing.T) {
   136  	originalListIngressTraits := getAllIngressTraits
   137  	getAllIngressTraits = testListIngressTraits
   138  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   139  
   140  	paths := []IngressPath{{Path: "/test/path"}}
   141  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"*"}, Paths: paths}}}}
   142  
   143  	existingPaths := []IngressPath{{Path: "/test/path"}}
   144  	existingRule := IngressRule{Hosts: []string{""}, Paths: existingPaths}
   145  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
   146  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
   147  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   148  
   149  	err := ingressTrait.ValidateCreate()
   150  	assert.NotNil(t, err)
   151  }
   152  
   153  // TestValidateCreateStarHostNoPathsNoCollision tests validation of an IngressTrait create with a "*" host and
   154  // empty path that doesn't collide with an existing IngressTrait.
   155  // GIVEN an existing IngressTrait with a "*" host and a specified path
   156  // WHEN validate is called on a new IngressTrait with a "*" host and no path
   157  // THEN validate is successful and doesn't return an error
   158  func TestValidateCreateStarHostNoPathsNoCollision(t *testing.T) {
   159  	originalListIngressTraits := getAllIngressTraits
   160  	getAllIngressTraits = testListIngressTraits
   161  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   162  
   163  	paths := []IngressPath{{Path: "/test/path"}}
   164  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"*"}, Paths: paths}}}}
   165  
   166  	existingTraits.Items = append(existingTraits.Items, IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"*"}}}}})
   167  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   168  
   169  	err := ingressTrait.ValidateCreate()
   170  	assert.Nil(t, err)
   171  }
   172  
   173  // TestValidateCreateInvalidHost tests validation of an IngressTrait create with a an invalid host specified
   174  // WHEN validate is called on a new IngressTrait with an invalid host specified
   175  // THEN validate fails and returns an error
   176  func TestValidateCreateInvalidHost(t *testing.T) {
   177  	originalListIngressTraits := getAllIngressTraits
   178  	getAllIngressTraits = testListIngressTraits
   179  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   180  
   181  	rule := IngressRule{Hosts: []string{"This.#is#.a.rea11y.bad/host!"}, Paths: []IngressPath{}}
   182  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   183  	err := ingressTrait.ValidateCreate()
   184  	assert.NotNil(t, err)
   185  }
   186  
   187  // TestValidateCreateHostAndPathNoExisting tests validation of an IngressTrait create with a specified host and path.
   188  // GIVEN no existing IngressTrait's
   189  // WHEN validate is called on a new IngressTrait with a specified host and path
   190  // THEN validate is successful and returns no errors
   191  func TestValidateCreateHostAndPathNoExisting(t *testing.T) {
   192  	originalListIngressTraits := getAllIngressTraits
   193  	getAllIngressTraits = testListIngressTraits
   194  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   195  
   196  	paths := []IngressPath{{Path: "/test/path"}}
   197  	rule := IngressRule{Hosts: []string{"foo.bar.com"}, Paths: paths}
   198  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   199  	err := ingressTrait.ValidateCreate()
   200  	assert.Nil(t, err)
   201  }
   202  
   203  // TestValidateCreateMultipleHosts tests validation of an IngressTrait create with multiple specified hosts
   204  // and and paths.
   205  // GIVEN no existing IngressTrait's
   206  // WHEN validate is called on a new IngressTrait with multiple specified hosts and paths
   207  // THEN validate is successful and returns no errors
   208  func TestValidateCreateMultipleHosts(t *testing.T) {
   209  	originalListIngressTraits := getAllIngressTraits
   210  	getAllIngressTraits = testListIngressTraits
   211  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   212  
   213  	paths := []IngressPath{{Path: "/test/path"}, {Path: "/test/path2"}}
   214  	rule := IngressRule{Hosts: []string{"foo.bar.com", "foo2.bar.com"}, Paths: paths}
   215  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   216  	err := ingressTrait.ValidateCreate()
   217  	assert.Nil(t, err)
   218  }
   219  
   220  // TestValidateCreateMultipleHostsCollision tests validation of an IngressTrait create with multiple specified hosts
   221  // and and paths that collie with an existing IngressTrait.
   222  // GIVEN an existing IngressTrait which conflicts with the IngressTrait to be created
   223  // WHEN validate is called on a new IngressTrait with multiple specified hosts and paths
   224  // THEN validate fails and returns an error
   225  func TestValidateCreateMultipleHostsCollision(t *testing.T) {
   226  	originalListIngressTraits := getAllIngressTraits
   227  	getAllIngressTraits = testListIngressTraits
   228  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   229  
   230  	existingPaths := []IngressPath{{Path: "/test/path2"}}
   231  	existingRule := IngressRule{Hosts: []string{"some.host.com", "foo2.bar.com"}, Paths: existingPaths}
   232  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
   233  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
   234  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   235  
   236  	paths := []IngressPath{{Path: "/test/path"}, {Path: "/test/path2"}}
   237  	rule := IngressRule{Hosts: []string{"foo.bar.com", "foo2.bar.com"}, Paths: paths}
   238  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   239  	err := ingressTrait.ValidateCreate()
   240  	assert.NotNil(t, err)
   241  }
   242  
   243  // TestValidateCreateMatchingHostDifferentPath tests validation of an IngressTrait create with a specified host and path
   244  // that doesn't conflict with an existing IngressTrait with the same host and a different path.
   245  // GIVEN an existing trait with a matching host and different path
   246  // WHEN validate is called on a new IngressTrait with a specified host and path
   247  // THEN validate is successful and returns no errors
   248  func TestValidateCreateMatchingHostDifferentPath(t *testing.T) {
   249  	originalListIngressTraits := getAllIngressTraits
   250  	getAllIngressTraits = testListIngressTraits
   251  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   252  
   253  	paths := []IngressPath{{Path: "/test/path"}}
   254  	rule := IngressRule{Hosts: []string{"foo.bar.com"}, Paths: paths}
   255  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   256  
   257  	existingPaths := []IngressPath{{Path: "/test/path2"}}
   258  	existingRule := IngressRule{Hosts: []string{"foo.bar.com"}, Paths: existingPaths}
   259  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
   260  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
   261  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   262  
   263  	err := ingressTrait.ValidateCreate()
   264  	assert.Nil(t, err)
   265  }
   266  
   267  // TestValidateCreateMatchingPathDifferentHost tests validation of an IngressTrait create with a specified host and path
   268  // that doesn't collide with an existing IngressTrait with a matching path and different host.
   269  // GIVEN an existing trait with a different host and matching path
   270  // WHEN validate is called on a new IngressTrait with a specified host and path
   271  // THEN validate is successful and returns no errors
   272  func TestValidateCreateMatchingPathDifferentHost(t *testing.T) {
   273  	originalListIngressTraits := getAllIngressTraits
   274  	getAllIngressTraits = testListIngressTraits
   275  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   276  
   277  	paths := []IngressPath{{Path: "/test/path"}}
   278  	rule := IngressRule{Hosts: []string{"foo.bar.com"}, Paths: paths}
   279  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   280  
   281  	existingPaths := []IngressPath{{Path: "/test/path"}}
   282  	existingRule := IngressRule{Hosts: []string{"foo.barz.com"}, Paths: existingPaths}
   283  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
   284  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
   285  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   286  
   287  	err := ingressTrait.ValidateCreate()
   288  	assert.Nil(t, err)
   289  }
   290  
   291  // TestValidateCreateHostPathCollision tests validation of an IngressTrait create with a specified host and path
   292  // which collides with an existing IngressTrait with matching host and path.
   293  // GIVEN an existing trait with a matching host and matching path
   294  // WHEN validate is called on a new IngressTrait with a specified host and path
   295  // THEN validate fails and returns an error
   296  func TestValidateCreateHostPathCollision(t *testing.T) {
   297  	originalListIngressTraits := getAllIngressTraits
   298  	getAllIngressTraits = testListIngressTraits
   299  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   300  
   301  	paths := []IngressPath{{Path: "/test/path"}}
   302  	rule := IngressRule{Hosts: []string{"foo.bar.com"}, Paths: paths}
   303  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   304  
   305  	existingPaths := []IngressPath{{Path: "/test/path"}}
   306  	existingRule := IngressRule{Hosts: []string{"foo.bar.com"}, Paths: existingPaths}
   307  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
   308  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
   309  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   310  
   311  	err := ingressTrait.ValidateCreate()
   312  	assert.NotNil(t, err)
   313  }
   314  
   315  // TestValidateCreateHostNoPathCollision tests validation of an IngressTrait create with a specified host and no path
   316  // which collides with an existing IngressTrait with matching host and no path.
   317  // GIVEN an existing trait with a matching host and no path
   318  // WHEN validate is called on a new IngressTrait with a matching host and no path
   319  // THEN validate fails and returns an error
   320  func TestValidateCreateHostNoPathCollision(t *testing.T) {
   321  	originalListIngressTraits := getAllIngressTraits
   322  	getAllIngressTraits = testListIngressTraits
   323  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   324  
   325  	rule := IngressRule{Hosts: []string{"foo.bar.com"}}
   326  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   327  
   328  	existingRule := IngressRule{Hosts: []string{"foo.bar.com"}}
   329  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
   330  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
   331  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   332  
   333  	err := ingressTrait.ValidateCreate()
   334  	assert.NotNil(t, err)
   335  }
   336  
   337  // TestValidateCreateWildcardHostNoPaths tests validation of an IngressTrait create with a wildcard prefix host and
   338  // no path.
   339  // GIVEN no existing IngressTraits's
   340  // WHEN validate is called on a new IngressTrait with a wildcard prefix host and no path
   341  // THEN validate is successful and returns no error
   342  func TestValidateCreateWildcardHostNoPaths(t *testing.T) {
   343  	originalListIngressTraits := getAllIngressTraits
   344  	getAllIngressTraits = testListIngressTraits
   345  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   346  
   347  	rule := IngressRule{Hosts: []string{"*.bar.com"}, Paths: []IngressPath{}}
   348  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   349  	err := ingressTrait.ValidateCreate()
   350  	assert.Nil(t, err)
   351  }
   352  
   353  // TestValidateCreateWildcardHostPath tests validation of an IngressTrait create with a wildcard prefix host
   354  // and specified path.
   355  // GIVEN no existing IngressTraits's
   356  // WHEN validate is called on a new IngressTrait with a wildcard prefix host and specified path
   357  // THEN validate is successful and returns no error
   358  func TestValidateCreateWildcardHostPath(t *testing.T) {
   359  	originalListIngressTraits := getAllIngressTraits
   360  	getAllIngressTraits = testListIngressTraits
   361  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   362  
   363  	paths := []IngressPath{{Path: "/test/path"}}
   364  	rule := IngressRule{Hosts: []string{"*.bar.com"}, Paths: paths}
   365  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   366  
   367  	existingPaths := []IngressPath{{Path: "/test/path2"}}
   368  	existingRule := IngressRule{Hosts: []string{"*.bar.com"}, Paths: existingPaths}
   369  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
   370  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
   371  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   372  
   373  	err := ingressTrait.ValidateCreate()
   374  	assert.Nil(t, err)
   375  }
   376  
   377  // TestValidateCreateWildcardHostPathCollision tests validation of an IngressTrait create with a wildcard prefix host
   378  // and specified path that collides with an existing IngressTrait with matching host and path.
   379  // GIVEN an existing IngressTrait with an identical wildcard prefix host and matching path
   380  // WHEN validate is called on a new IngressTrait with a wildcard prefix host and specified path
   381  // THEN validate fails and returns an error
   382  func TestValidateCreateWildcardHostPathCollision(t *testing.T) {
   383  	originalListIngressTraits := getAllIngressTraits
   384  	getAllIngressTraits = testListIngressTraits
   385  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   386  
   387  	paths := []IngressPath{{Path: "/test/path"}}
   388  	rule := IngressRule{Hosts: []string{"*.bar.com"}, Paths: paths}
   389  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   390  
   391  	existingPaths := []IngressPath{{Path: "/test/path"}}
   392  	existingRule := IngressRule{Hosts: []string{"*.bar.com"}, Paths: existingPaths}
   393  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
   394  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
   395  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   396  
   397  	err := ingressTrait.ValidateCreate()
   398  	assert.NotNil(t, err)
   399  }
   400  
   401  // TestValidateCreateWildcardHostDoesntCollideWithExactHost tests validation of an IngressTrait create with a
   402  // wildcard prefix host and specified path which doesn't collide with an existing IngressTrait with a "matching"
   403  // exact host and matching path.
   404  // GIVEN an existing IngressTraits with an exact host which would be matched by the wildcard host and a matching path
   405  // WHEN validate is called on a new IngressTrait with a wildcard prefix host and specified path
   406  // THEN validate is successful and returns no error
   407  func TestValidateCreateWildcardHostDoesntCollideWithExactHost(t *testing.T) {
   408  	originalListIngressTraits := getAllIngressTraits
   409  	getAllIngressTraits = testListIngressTraits
   410  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   411  
   412  	paths := []IngressPath{{Path: "/test/path"}}
   413  	rule := IngressRule{Hosts: []string{"*.bar.com"}, Paths: paths}
   414  	ingressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{rule}}}
   415  
   416  	existingPaths := []IngressPath{{Path: "/test/path"}}
   417  	existingRule := IngressRule{Hosts: []string{"foo.bar.com"}, Paths: existingPaths}
   418  	existingIngressTrait := IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{existingRule}}}
   419  	existingTraits.Items = append(existingTraits.Items, existingIngressTrait)
   420  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   421  
   422  	err := ingressTrait.ValidateCreate()
   423  	assert.Nil(t, err)
   424  }
   425  
   426  // update validation tests
   427  
   428  // TestValidateUpdateAddPath tests validation of an IngressTrait update where a new path is added to an existing trait.
   429  // GIVEN an existing IngressTrait with a specified host and path
   430  // WHEN validate is called on an updated IngressTrait with an added path
   431  // THEN validate is successful and returns no errors
   432  func TestValidateUpdateAddPath(t *testing.T) {
   433  	originalListIngressTraits := getAllIngressTraits
   434  	getAllIngressTraits = testListIngressTraits
   435  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   436  
   437  	existingPaths := []IngressPath{{Path: "/test/path"}}
   438  	// specify UID as this is used to determine the trait being changed in the list of existing traits
   439  	existingTrait := &IngressTrait{ObjectMeta: v1.ObjectMeta{UID: "100"}, Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo.bar.com"}, Paths: existingPaths}}}}
   440  	existingTraits.Items = append(existingTraits.Items, *existingTrait)
   441  	existingTraits.Items = append(existingTraits.Items, IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{""}}}}})
   442  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   443  
   444  	// add a path
   445  	paths := []IngressPath{{Path: "/test/path"}, {Path: "/test/path2"}}
   446  	ingressTrait := IngressTrait{ObjectMeta: v1.ObjectMeta{UID: "100"}, Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo.bar.com"}, Paths: paths}}}}
   447  
   448  	err := ingressTrait.ValidateUpdate(existingTrait)
   449  	assert.Nil(t, err)
   450  }
   451  
   452  // TestValidateUpdateAddHostCollision tests validation of an IngressTrait update where a new host and path is added to an
   453  // existing trait and that new host and path causes a collision with another existing trait.
   454  // GIVEN an existing IngressTrait with a specified host and path that is going to be updated
   455  // AND another existing IngressTrait with the same different host and path
   456  // WHEN validate is called on an updated IngressTrait with an added host and path that conflicts with another existing trait
   457  // THEN validate fails and returns an error
   458  func TestValidateUpdateAddHostCollision(t *testing.T) {
   459  	originalListIngressTraits := getAllIngressTraits
   460  	getAllIngressTraits = testListIngressTraits
   461  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   462  
   463  	existingPaths := []IngressPath{{Path: "/test/path"}}
   464  	existingPaths2 := []IngressPath{{Path: "/test/path2"}}
   465  	// specify UID as this is used to determine the trait being changed in the list of existing traits
   466  	existingTrait := &IngressTrait{ObjectMeta: v1.ObjectMeta{UID: "100"}, Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo.bar.com"}, Paths: existingPaths}}}}
   467  	existingTraits.Items = append(existingTraits.Items, *existingTrait)
   468  	// the update will conflict with this trait
   469  	existingTraits.Items = append(existingTraits.Items, IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo2.bar.com"}, Paths: existingPaths2}}}})
   470  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   471  
   472  	// add a path
   473  	paths := []IngressPath{{Path: "/test/path"}, {Path: "/test/path2"}}
   474  	ingressTrait := IngressTrait{ObjectMeta: v1.ObjectMeta{UID: "100"}, Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo.bar.com", "foo2.bar.com"}, Paths: paths}}}}
   475  
   476  	err := ingressTrait.ValidateUpdate(existingTrait)
   477  	assert.NotNil(t, err)
   478  }
   479  
   480  // TestValidateUpdateChangeHost tests validation of an IngressTrait update where the host is changed and no conflicts exist.
   481  // GIVEN some existing non-conflicting IngressTrait's
   482  // WHEN validate is called on an updated IngressTrait with the changed host
   483  // THEN validate is successful and no error is returned
   484  func TestValidateUpdateChangeHost(t *testing.T) {
   485  	originalListIngressTraits := getAllIngressTraits
   486  	getAllIngressTraits = testListIngressTraits
   487  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   488  
   489  	existingPath := []IngressPath{{Path: "/test/path"}}
   490  	existingPath2 := []IngressPath{{Path: "/test/path2"}}
   491  	existingPath3 := []IngressPath{{Path: "/test/path3"}}
   492  	// specify UID as this is used to determine the trait being changed in the list of existing traits
   493  	existingTrait := &IngressTrait{ObjectMeta: v1.ObjectMeta{UID: "100"}, Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo.bar.com"}, Paths: existingPath}}}}
   494  	existingTraits.Items = append(existingTraits.Items, *existingTrait)
   495  	// non-conflicting existing traits
   496  	existingTraits.Items = append(existingTraits.Items, IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo2.bar.com"}, Paths: existingPath2}}}})
   497  	existingTraits.Items = append(existingTraits.Items, IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo3.bar.com"}, Paths: existingPath3}}}})
   498  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   499  
   500  	// change the host
   501  	ingressTrait := IngressTrait{ObjectMeta: v1.ObjectMeta{UID: "100"}, Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo3.bar.com"}, Paths: existingPath2}}}}
   502  
   503  	err := ingressTrait.ValidateUpdate(existingTrait)
   504  	assert.Nil(t, err)
   505  }
   506  
   507  // TestValidateUpdateChangeHostCollision tests validation of an IngressTrait update where the host is changed and
   508  // that causes a collision with another existing trait with a matching host and path.
   509  // GIVEN an existing IngressTrait with a specified host and path that is going to be updated
   510  // AND another existing IngressTrait with a different host and the same path
   511  // WHEN validate is called on an updated IngressTrait with the changed host that conflicts with another existing trait
   512  // THEN validate fails and returns an error
   513  func TestValidateUpdateChangeHostCollision(t *testing.T) {
   514  	originalListIngressTraits := getAllIngressTraits
   515  	getAllIngressTraits = testListIngressTraits
   516  	defer func() { getAllIngressTraits = originalListIngressTraits }()
   517  
   518  	existingPath := []IngressPath{{Path: "/test/path"}}
   519  	// specify UID as this is used to determine the trait being changed in the list of existing traits
   520  	existingTrait := &IngressTrait{ObjectMeta: v1.ObjectMeta{UID: "100"}, Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo.bar.com"}, Paths: existingPath}}}}
   521  	existingTraits.Items = append(existingTraits.Items, *existingTrait)
   522  	// the update will conflict with this trait
   523  	existingTraits.Items = append(existingTraits.Items, IngressTrait{Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo2.bar.com"}, Paths: existingPath}}}})
   524  	defer func() { existingTraits.Items = existingTraits.Items[:0] }()
   525  
   526  	// change the host
   527  	ingressTrait := IngressTrait{ObjectMeta: v1.ObjectMeta{UID: "100"}, Spec: IngressTraitSpec{Rules: []IngressRule{{Hosts: []string{"foo2.bar.com"}, Paths: existingPath}}}}
   528  
   529  	err := ingressTrait.ValidateUpdate(existingTrait)
   530  	assert.NotNil(t, err)
   531  }
   532  
   533  func testListIngressTraits(namespace string) (*IngressTraitList, error) {
   534  	return &existingTraits, nil
   535  }