sigs.k8s.io/cluster-api-provider-azure@v1.14.3/internal/test/matchers/gomega/matchers_test.go (about)

     1  /*
     2  Copyright 2020 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 gomega
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/onsi/gomega"
    23  	"sigs.k8s.io/cluster-api-provider-azure/internal/test/record"
    24  )
    25  
    26  var (
    27  	defaultLogEntry = record.LogEntry{
    28  		Values: []interface{}{
    29  			"foo",
    30  			"bin",
    31  			"bax",
    32  		},
    33  		LogFunc: "Info",
    34  		Level:   2,
    35  	}
    36  )
    37  
    38  func TestLogContains(t *testing.T) {
    39  	cases := []struct {
    40  		Name        string
    41  		LogEntry    record.LogEntry
    42  		Matcher     LogMatcher
    43  		ShouldMatch bool
    44  	}{
    45  		{
    46  			Name:     "MatchesCompletely",
    47  			LogEntry: defaultLogEntry,
    48  			Matcher: LogContains(
    49  				"foo",
    50  				"bin",
    51  				"bax").WithLevel(2).WithLogFunc("Info"),
    52  			ShouldMatch: true,
    53  		},
    54  		{
    55  			Name:     "MatchesWithoutSpecifyingLevel",
    56  			LogEntry: defaultLogEntry,
    57  			Matcher: LogContains(
    58  				"foo",
    59  				"bin",
    60  				"bax").WithLogFunc("Info"),
    61  			ShouldMatch: true,
    62  		},
    63  		{
    64  			Name:     "MatchesWithoutSpecifyingLogFunc",
    65  			LogEntry: defaultLogEntry,
    66  			Matcher: LogContains(
    67  				"foo",
    68  				"bin",
    69  				"bax"),
    70  			ShouldMatch: true,
    71  		},
    72  		{
    73  			Name:        "MatchesWithoutSpecifyingAllValues",
    74  			LogEntry:    defaultLogEntry,
    75  			Matcher:     LogContains("foo", "bax"),
    76  			ShouldMatch: true,
    77  		},
    78  	}
    79  
    80  	for _, c := range cases {
    81  		c := c
    82  		t.Run(c.Name, func(t *testing.T) {
    83  			t.Parallel()
    84  			g := gomega.NewWithT(t)
    85  			success, err := c.Matcher.Match(c.LogEntry)
    86  			g.Expect(err).NotTo(gomega.HaveOccurred())
    87  			g.Expect(success).To(gomega.Equal(c.ShouldMatch))
    88  		})
    89  	}
    90  }
    91  
    92  func TestLogContainsEntries(t *testing.T) {
    93  	entries := []record.LogEntry{
    94  		defaultLogEntry,
    95  		{
    96  			Values: []interface{}{
    97  				"controller",
    98  				"AzureCluster",
    99  				"predicate",
   100  				"ClusterUnpaused",
   101  				"predicate",
   102  				"ClusterCreateNotPaused",
   103  				"eventType",
   104  				"create",
   105  				"namespace",
   106  				"default",
   107  				"cluster",
   108  				"foo-52824hhgdv",
   109  				"eventType",
   110  				"create",
   111  				"namespace",
   112  				"default",
   113  				"cluster",
   114  				"cluster-xxnmwzz2wz",
   115  				"eventType",
   116  				"create",
   117  				"namespace",
   118  				"default",
   119  				"cluster",
   120  				"foo-zljvddw5c2",
   121  				"msg",
   122  				"Cluster is not paused, allowing further processing",
   123  			},
   124  			LogFunc: "Error",
   125  			Level:   6,
   126  		},
   127  	}
   128  
   129  	g := gomega.NewWithT(t)
   130  	g.Expect(entries).To(gomega.ContainElements([]LogMatcher{
   131  		LogContains("bin"),
   132  		LogContains("controller",
   133  			"AzureCluster",
   134  			"predicate",
   135  			"ClusterUnpaused",
   136  			"predicate",
   137  			"ClusterCreateNotPaused",
   138  			"eventType",
   139  			"create",
   140  			"namespace",
   141  			"default",
   142  			"cluster",
   143  			"foo-zljvddw5c2",
   144  			"msg",
   145  			"Cluster is not paused, allowing further processing"),
   146  	}))
   147  }