github.com/GoogleCloudPlatform/testgrid@v0.0.174/pkg/api/v1/config_cache_test.go (about)

     1  /*
     2  Copyright 2022 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 v1
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/GoogleCloudPlatform/testgrid/config/snapshot"
    23  	configpb "github.com/GoogleCloudPlatform/testgrid/pb/config"
    24  	"github.com/GoogleCloudPlatform/testgrid/util/gcs"
    25  	"github.com/google/go-cmp/cmp"
    26  	"github.com/google/go-cmp/cmp/cmpopts"
    27  )
    28  
    29  func TestGenerateNormalCache(t *testing.T) {
    30  	tests := []struct {
    31  		name     string
    32  		cfg      *snapshot.Config
    33  		expected *cachedConfig
    34  	}{
    35  		{
    36  			name:     "Generate nothing for nil config",
    37  			expected: &cachedConfig{},
    38  		},
    39  		{
    40  			name: "Generate empty sets for empty config",
    41  			cfg:  &snapshot.Config{},
    42  			expected: &cachedConfig{
    43  				NormalDashboardGroup: map[string]string{},
    44  				NormalDashboard:      map[string]string{},
    45  				NormalTestGroup:      map[string]string{},
    46  				NormalDashboardTab:   map[string]map[string]string{},
    47  			},
    48  		},
    49  		{
    50  			name: "Generate normalized names for all things",
    51  			cfg: &snapshot.Config{
    52  				DashboardGroups: map[string]*configpb.DashboardGroup{
    53  					"My Dashboard Group": {Name: "My Dashboard Group"},
    54  				},
    55  				Dashboards: map[string]*configpb.Dashboard{
    56  					"Chessboard ♟️♙": {
    57  						Name: "Chessboard ♟️♙",
    58  						DashboardTab: []*configpb.DashboardTab{
    59  							{Name: "Pawns ♟️"},
    60  							{Name: "Knights ♞"},
    61  						},
    62  					},
    63  				},
    64  				Groups: map[string]*configpb.TestGroup{
    65  					"Groups: 一緒にあるいくつかのこと": {Name: "Groups: 一緒にあるいくつかのこと"},
    66  				},
    67  			},
    68  			expected: &cachedConfig{
    69  				NormalDashboardGroup: map[string]string{
    70  					"mydashboardgroup": "My Dashboard Group",
    71  				},
    72  				NormalDashboard: map[string]string{
    73  					"chessboard": "Chessboard ♟️♙",
    74  				},
    75  				NormalTestGroup: map[string]string{
    76  					"groups": "Groups: 一緒にあるいくつかのこと",
    77  				},
    78  				NormalDashboardTab: map[string]map[string]string{
    79  					"chessboard": {
    80  						"pawns":   "Pawns ♟️",
    81  						"knights": "Knights ♞",
    82  					},
    83  				},
    84  			},
    85  		},
    86  		{
    87  			name: "Tolerate dashboards with the same normalized tab name",
    88  			cfg: &snapshot.Config{
    89  				Dashboards: map[string]*configpb.Dashboard{
    90  					"Black Pieces": {
    91  						Name: "Black Pieces",
    92  						DashboardTab: []*configpb.DashboardTab{
    93  							{Name: "Pawns ♟️"},
    94  							{Name: "Knights ♞"},
    95  						},
    96  					},
    97  					"White Pieces": {
    98  						Name: "White Pieces",
    99  						DashboardTab: []*configpb.DashboardTab{
   100  							{Name: "Pawns ♙"},
   101  							{Name: "Knights ♘"},
   102  						},
   103  					},
   104  				},
   105  			},
   106  			expected: &cachedConfig{
   107  				NormalDashboardGroup: map[string]string{},
   108  				NormalTestGroup:      map[string]string{},
   109  				NormalDashboard: map[string]string{
   110  					"blackpieces": "Black Pieces",
   111  					"whitepieces": "White Pieces",
   112  				},
   113  				NormalDashboardTab: map[string]map[string]string{
   114  					"blackpieces": {
   115  						"pawns":   "Pawns ♟️",
   116  						"knights": "Knights ♞",
   117  					},
   118  					"whitepieces": {
   119  						"pawns":   "Pawns ♙",
   120  						"knights": "Knights ♘",
   121  					},
   122  				},
   123  			},
   124  		},
   125  	}
   126  
   127  	for _, tc := range tests {
   128  		t.Run(tc.name, func(t *testing.T) {
   129  			cache := cachedConfig{
   130  				Config: tc.cfg,
   131  			}
   132  			cache.generateNormalCache()
   133  
   134  			if diff := cmp.Diff(&cache, tc.expected, cmpopts.IgnoreFields(cachedConfig{}, "Config", "Mutex")); diff != "" {
   135  				t.Errorf("Unexpected Diff: (-got, +want): %s", diff)
   136  			}
   137  		})
   138  	}
   139  }
   140  
   141  func TestConfigPath(t *testing.T) {
   142  	tests := []struct {
   143  		name          string
   144  		defaultBucket string
   145  		scopeParam    string
   146  		expected      *gcs.Path
   147  		expectDefault bool
   148  	}{
   149  		{
   150  			name:          "Defaults to default",
   151  			defaultBucket: "gs://example",
   152  			expected:      getPathOrDie(t, "gs://example/config"),
   153  			expectDefault: true,
   154  		},
   155  		{
   156  			name:          "Use config if specified",
   157  			defaultBucket: "gs://wrong",
   158  			scopeParam:    "gs://example/path",
   159  			expected:      getPathOrDie(t, "gs://example/path/config"),
   160  		},
   161  		{
   162  			name:       "Do not require a default",
   163  			scopeParam: "gs://example/path",
   164  			expected:   getPathOrDie(t, "gs://example/path/config"),
   165  		},
   166  		{
   167  			name: "Return error if no way to find config",
   168  		},
   169  	}
   170  	for _, test := range tests {
   171  		t.Run(test.name, func(t *testing.T) {
   172  			s := Server{
   173  				DefaultBucket: test.defaultBucket,
   174  			}
   175  
   176  			result, isDefault, err := s.configPath(test.scopeParam)
   177  			if test.expected == nil && err == nil {
   178  				t.Fatalf("Expected an error, but got none")
   179  			}
   180  
   181  			if test.expectDefault != isDefault {
   182  				t.Errorf("Default Flag: Want %t, got %t", test.expectDefault, isDefault)
   183  			}
   184  
   185  			if test.expected != nil && result.String() != test.expected.String() {
   186  				t.Errorf("Want %s, but got %s", test.expected.String(), result.String())
   187  			}
   188  		})
   189  	}
   190  }