github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/googlecloudbuild/client/client_test.go (about)

     1  /*
     2  Copyright 2021 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 client
    18  
    19  import (
    20  	"testing"
    21  
    22  	"cloud.google.com/go/cloudbuild/apiv1/v2/cloudbuildpb"
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  func TestProwLabel(t *testing.T) {
    27  	tests := []struct {
    28  		name string
    29  		key  string
    30  		val  string
    31  		want string
    32  	}{
    33  		{
    34  			name: "empty",
    35  			want: " ::: ",
    36  		},
    37  		{
    38  			name: "empty-key",
    39  			val:  "aaa",
    40  			want: " ::: aaa",
    41  		},
    42  		{
    43  			name: "empty-val",
    44  			key:  "aaa",
    45  			want: "aaa ::: ",
    46  		},
    47  		{
    48  			name: "normal",
    49  			key:  "aaa",
    50  			val:  "aaa",
    51  			want: "aaa ::: aaa",
    52  		},
    53  	}
    54  
    55  	for _, tc := range tests {
    56  		t.Run(tc.name, func(t *testing.T) {
    57  			if got, want := ProwLabel(tc.key, tc.val), tc.want; got != want {
    58  				t.Errorf("want: %s, got: %s", want, got)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  func TestKvPairFromProwLabel(t *testing.T) {
    65  	tests := []struct {
    66  		name string
    67  		tag  string
    68  		want []string
    69  	}{
    70  		{
    71  			name: "empty",
    72  			tag:  " ::: ",
    73  			want: []string{"", ""},
    74  		},
    75  		{
    76  			name: "empty-key",
    77  			tag:  " ::: aaa",
    78  			want: []string{"", "aaa"},
    79  		},
    80  		{
    81  			name: "empty-val",
    82  			tag:  "aaa ::: ",
    83  			want: []string{"aaa", ""},
    84  		},
    85  		{
    86  			name: "normal",
    87  			tag:  "aaa ::: aaa",
    88  			want: []string{"aaa", "aaa"},
    89  		},
    90  		{
    91  			name: "invalid-separator",
    92  			tag:  "aaa ;;; aaa",
    93  			want: []string{"aaa ;;; aaa", ""},
    94  		},
    95  	}
    96  
    97  	for _, tc := range tests {
    98  		t.Run(tc.name, func(t *testing.T) {
    99  			gotKey, gotVal := KvPairFromProwLabel(tc.tag)
   100  			wantKey, wantVal := tc.want[0], tc.want[1]
   101  			if gotKey != wantKey {
   102  				t.Errorf("key mismatching. want: %s, got: %s", wantKey, gotKey)
   103  			}
   104  			if gotVal != wantVal {
   105  				t.Errorf("val mismatching. want: %s, got: %s", wantVal, gotVal)
   106  			}
   107  		})
   108  	}
   109  }
   110  
   111  func TestGetProwLabel(t *testing.T) {
   112  	tests := []struct {
   113  		name string
   114  		tags []string
   115  		want map[string]string
   116  	}{
   117  		{
   118  			name: "single-label",
   119  			tags: []string{"aaa ::: aaa"},
   120  			want: map[string]string{"aaa": "aaa"},
   121  		},
   122  		{
   123  			name: "multiple-label",
   124  			tags: []string{"aaa ::: aaa", "bbb ::: bbb"},
   125  			want: map[string]string{"aaa": "aaa", "bbb": "bbb"},
   126  		},
   127  		{
   128  			name: "no-label",
   129  			tags: []string{},
   130  			want: map[string]string{},
   131  		},
   132  	}
   133  
   134  	for _, tc := range tests {
   135  		t.Run(tc.name, func(t *testing.T) {
   136  			bld := &cloudbuildpb.Build{
   137  				Tags: tc.tags,
   138  			}
   139  			got := GetProwLabels(bld)
   140  			if diff := cmp.Diff(tc.want, got); diff != "" {
   141  				t.Errorf("got(+), want(-):\n%s", diff)
   142  			}
   143  		})
   144  	}
   145  }