github.com/verrazzano/verrazzano@v1.7.0/pkg/k8s/ready/ingresses_present_test.go (about)

     1  // Copyright (c) 2021, 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  package ready
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
     9  
    10  	v1 "k8s.io/api/networking/v1"
    11  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    12  	"k8s.io/apimachinery/pkg/types"
    13  	k8scheme "k8s.io/client-go/kubernetes/scheme"
    14  	"sigs.k8s.io/controller-runtime/pkg/client"
    15  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    16  )
    17  
    18  func TestIngressesPresent(t *testing.T) {
    19  	ingress := v1.Ingress{
    20  		ObjectMeta: metav1.ObjectMeta{
    21  			Name:      "foo",
    22  			Namespace: "foobar",
    23  		},
    24  	}
    25  	oneName := []types.NamespacedName{
    26  		{
    27  			Name:      "foo",
    28  			Namespace: "foobar",
    29  		},
    30  	}
    31  	multipleNames := append(oneName, types.NamespacedName{Name: "anotherIng", Namespace: "foobar"})
    32  
    33  	var noName []types.NamespacedName
    34  	var tests = []struct {
    35  		name    string
    36  		c       client.Client
    37  		n       []types.NamespacedName
    38  		present bool
    39  	}{
    40  		{
    41  			"should be false when ingress not found",
    42  			fake.NewClientBuilder().WithScheme(k8scheme.Scheme).Build(),
    43  			oneName,
    44  			false,
    45  		},
    46  		{
    47  			"should be false when only some ingresses are found",
    48  			fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(&ingress).Build(),
    49  			multipleNames,
    50  			false,
    51  		},
    52  		{
    53  			"should be true when ingress exists",
    54  			fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(&ingress).Build(),
    55  			oneName,
    56  			true,
    57  		},
    58  		{
    59  			"should be true when no ingress names provided",
    60  			fake.NewClientBuilder().WithScheme(k8scheme.Scheme).Build(),
    61  			noName,
    62  			true,
    63  		},
    64  		{
    65  			"should be present when ingress exists",
    66  			fake.NewClientBuilder().WithScheme(k8scheme.Scheme).WithObjects(&ingress).Build(),
    67  			oneName,
    68  			true,
    69  		},
    70  	}
    71  	for _, tt := range tests {
    72  		t.Run(tt.name, func(t *testing.T) {
    73  			if present := IngressesPresent(vzlog.DefaultLogger(), tt.c, tt.n, ""); present != tt.present {
    74  				t.Errorf("IngressesPresent() = %v, want %v", present, tt.present)
    75  			}
    76  		})
    77  	}
    78  }