github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/labels/labels_test.go (about)

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package labels
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  )
    22  
    23  func TestFromMap(t *testing.T) {
    24  	tests := []struct {
    25  		name     string
    26  		labelMap map[string]string
    27  		want     Labels
    28  	}{
    29  		{
    30  			name: "Test all labels given",
    31  			labelMap: map[string]string{
    32  				"type":     "compile",
    33  				"compiler": "clang",
    34  				"lang":     "cpp",
    35  				"tool":     "clang",
    36  			},
    37  			want: Labels{
    38  				ActionType: Compile,
    39  				Compiler:   Clang,
    40  				Lang:       Cpp,
    41  				ActionTool: ClangTool,
    42  			},
    43  		},
    44  		{
    45  			name: "Test some labels missing",
    46  			labelMap: map[string]string{
    47  				"type": "link",
    48  				"tool": "clang",
    49  			},
    50  			want: Labels{
    51  				ActionType: Link,
    52  				ActionTool: ClangTool,
    53  			},
    54  		},
    55  		{
    56  			name:     "Test empty labels",
    57  			labelMap: map[string]string{},
    58  			want:     Labels{},
    59  		},
    60  	}
    61  
    62  	for _, test := range tests {
    63  		t.Run(test.name, func(t *testing.T) {
    64  			if got := FromMap(test.labelMap); test.want != got {
    65  				t.Errorf("FromMap(%v) returned diff, want %v, got %v", test.labelMap, test.want, got)
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestToMap(t *testing.T) {
    72  	tests := []struct {
    73  		name   string
    74  		labels Labels
    75  		want   map[string]string
    76  	}{
    77  		{
    78  			name: "Test all labels given",
    79  			labels: Labels{
    80  				ActionType: Compile,
    81  				Compiler:   Clang,
    82  				Lang:       Cpp,
    83  				ActionTool: ClangTool,
    84  			},
    85  			want: map[string]string{
    86  				"type":     "compile",
    87  				"compiler": "clang",
    88  				"lang":     "cpp",
    89  				"tool":     "clang",
    90  			},
    91  		},
    92  		{
    93  			name: "Test some labels missing",
    94  			labels: Labels{
    95  				ActionType: Link,
    96  				ActionTool: ClangTool,
    97  			},
    98  			want: map[string]string{
    99  				"type": "link",
   100  				"tool": "clang",
   101  			},
   102  		},
   103  		{
   104  			name:   "Test empty labels",
   105  			labels: Labels{},
   106  			want:   map[string]string{},
   107  		},
   108  	}
   109  
   110  	for _, test := range tests {
   111  		t.Run(test.name, func(t *testing.T) {
   112  			got := ToMap(test.labels)
   113  			if diff := cmp.Diff(test.want, got); diff != "" {
   114  				t.Errorf("ToMap(%v) returned diff, (-want +got): %v\n", test.labels, diff)
   115  			}
   116  		})
   117  	}
   118  }
   119  
   120  func TestToDigest(t *testing.T) {
   121  	m := map[string]string{
   122  		"type":     "compile",
   123  		"compiler": "clang",
   124  		"lang":     "cpp",
   125  	}
   126  	want := "1c2a12e4"
   127  
   128  	got, err := ToDigest(m)
   129  	if err != nil {
   130  		t.Fatalf("ToDigest(%+v) failed: %v", m, err)
   131  	}
   132  	if want != got {
   133  		t.Errorf("ToDigest(%+v) returned diff, want %q, got %q", m, want, got)
   134  	}
   135  }
   136  
   137  func TestToDigest_UsesCache(t *testing.T) {
   138  	m := map[string]string{
   139  		"type":     "compile",
   140  		"compiler": "clang",
   141  		"lang":     "cpp",
   142  	}
   143  	lk := ToKey(m)
   144  	want := "foo"
   145  	valFn := func() (interface{}, error) { return want, nil }
   146  	labelsDigestCache.Delete(lk)
   147  	if _, err := labelsDigestCache.LoadOrStore(lk, valFn); err != nil {
   148  		t.Fatalf("LoadOrStore(%v,%v) failed: %v", lk, want, err)
   149  	}
   150  
   151  	got, err := ToDigest(m)
   152  	if err != nil {
   153  		t.Fatalf("ToDigest(%+v) failed: %v", m, err)
   154  	}
   155  	if want != got {
   156  		t.Errorf("ToDigest(%+v) returned diff, want %q, got %q", m, want, got)
   157  	}
   158  }