github.com/GoogleCloudPlatform/testgrid@v0.0.174/pkg/summarizer/pubsub_test.go (about)

     1  /*
     2  Copyright 2021 The TestGrid 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 summarizer
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/GoogleCloudPlatform/testgrid/pkg/pubsub"
    23  	"github.com/GoogleCloudPlatform/testgrid/util/gcs"
    24  	"github.com/google/go-cmp/cmp"
    25  )
    26  
    27  func TestProcessNotification(t *testing.T) {
    28  	mustPath := func(s string) gcs.Path {
    29  		p, err := gcs.NewPath(s)
    30  		if err != nil {
    31  			t.Fatalf("gcs.NewPath(%q): %v", s, err)
    32  		}
    33  		return *p
    34  	}
    35  	pstr := func(s string) *string { return &s }
    36  	cases := []struct {
    37  		name          string
    38  		prefix        gcs.Path
    39  		notice        *pubsub.Notification
    40  		findDashboard bool
    41  		want          *string
    42  	}{
    43  		{
    44  			name:   "basically works",
    45  			prefix: mustPath("gs://bucket/prefix/"),
    46  			notice: &pubsub.Notification{},
    47  		},
    48  		{
    49  			name:   "wrong bucket",
    50  			prefix: mustPath("gs://bucket/prefix/"),
    51  			notice: &pubsub.Notification{
    52  				Path: mustPath("gs://elsewhere/prefix/foo"),
    53  			},
    54  		},
    55  		{
    56  			name:   "wrong prefix",
    57  			prefix: mustPath("gs://bucket/prefix/"),
    58  			notice: &pubsub.Notification{
    59  				Path: mustPath("gs://bucket/foo"),
    60  			},
    61  		},
    62  		{
    63  			name:   "require trailing slash",
    64  			prefix: mustPath("gs://bucket/prefix"), // missing /
    65  			notice: &pubsub.Notification{
    66  				Path: mustPath("gs://bucket/prefix/foo"),
    67  			},
    68  		},
    69  		{
    70  			name:   "match",
    71  			prefix: mustPath("gs://bucket/prefix/"),
    72  			notice: &pubsub.Notification{
    73  				Path: mustPath("gs://bucket/prefix/foo"),
    74  			},
    75  			want: pstr("foo"),
    76  		},
    77  		{
    78  			name:   "match dashboard via tab notification",
    79  			prefix: mustPath("gs://bucket/prefix/"),
    80  			notice: &pubsub.Notification{
    81  				Path: mustPath("gs://bucket/prefix/good/riddance"),
    82  			},
    83  			findDashboard: true,
    84  			want:          pstr("good"),
    85  		},
    86  		{
    87  			name:   "ignore tabless dashboard",
    88  			prefix: mustPath("gs://bucket/prefix/"),
    89  			notice: &pubsub.Notification{
    90  				Path: mustPath("gs://bucket/prefix/just-dashboard"),
    91  			},
    92  			findDashboard: true,
    93  		},
    94  	}
    95  
    96  	for _, tc := range cases {
    97  		t.Run(tc.name, func(t *testing.T) {
    98  			got := processNotification(tc.prefix, tc.findDashboard, tc.notice)
    99  			if diff := cmp.Diff(tc.want, got); diff != "" {
   100  				t.Errorf("processNotification() got unexpected diff (-want +got):\n%s", diff)
   101  			}
   102  		})
   103  	}
   104  }