github.com/GoogleCloudPlatform/testgrid@v0.0.174/pkg/tabulator/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 tabulator
    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  		want   *string
    41  	}{
    42  		{
    43  			name:   "basically works",
    44  			prefix: mustPath("gs://bucket/prefix/"),
    45  			notice: &pubsub.Notification{},
    46  		},
    47  		{
    48  			name:   "wrong bucket",
    49  			prefix: mustPath("gs://bucket/prefix/"),
    50  			notice: &pubsub.Notification{
    51  				Path: mustPath("gs://elsewhere/prefix/foo"),
    52  			},
    53  		},
    54  		{
    55  			name:   "wrong prefix",
    56  			prefix: mustPath("gs://bucket/prefix/"),
    57  			notice: &pubsub.Notification{
    58  				Path: mustPath("gs://bucket/foo"),
    59  			},
    60  		},
    61  		{
    62  			name:   "require trailing slash",
    63  			prefix: mustPath("gs://bucket/prefix"), // missing /
    64  			notice: &pubsub.Notification{
    65  				Path: mustPath("gs://bucket/prefix/foo"),
    66  			},
    67  		},
    68  		{
    69  			name:   "match",
    70  			prefix: mustPath("gs://bucket/prefix/"),
    71  			notice: &pubsub.Notification{
    72  				Path: mustPath("gs://bucket/prefix/foo"),
    73  			},
    74  			want: pstr("foo"),
    75  		},
    76  	}
    77  
    78  	for _, tc := range cases {
    79  		t.Run(tc.name, func(t *testing.T) {
    80  			got := processNotification(tc.prefix, tc.notice)
    81  			if diff := cmp.Diff(tc.want, got); diff != "" {
    82  				t.Errorf("processNotification() got unexpected diff (-want +got):\n%s", diff)
    83  			}
    84  		})
    85  	}
    86  }