github.com/GoogleCloudPlatform/testgrid@v0.0.174/cmd/updater/main_test.go (about)

     1  /*
     2  Copyright 2020 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 main
    18  
    19  import (
    20  	"flag"
    21  	"runtime"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/google/go-cmp/cmp"
    26  
    27  	"github.com/GoogleCloudPlatform/testgrid/util"
    28  	"github.com/GoogleCloudPlatform/testgrid/util/gcs"
    29  
    30  	configpb "github.com/GoogleCloudPlatform/testgrid/pb/config"
    31  )
    32  
    33  func newPathOrDie(s string) *gcs.Path {
    34  	p, err := gcs.NewPath(s)
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  	return p
    39  }
    40  
    41  func TestGatherFlagOptions(t *testing.T) {
    42  	cases := []struct {
    43  		name string
    44  		args []string
    45  		want func(*options)
    46  		err  bool
    47  	}{
    48  		{
    49  			name: "config is required",
    50  			err:  true,
    51  		},
    52  		{
    53  			name: "basically works",
    54  			args: []string{"--config=gs://bucket/whatever"},
    55  			want: func(o *options) {
    56  				o.config = *newPathOrDie("gs://bucket/whatever")
    57  			},
    58  		},
    59  		{
    60  			name: "allow --config=gs://k8s-testgrid/config with default grid prefix",
    61  			args: []string{
    62  				"--config=gs://k8s-testgrid/config",
    63  				"--confirm",
    64  			},
    65  			want: func(o *options) {
    66  				o.config = *newPathOrDie("gs://k8s-testgrid/config")
    67  				o.confirm = true
    68  			},
    69  		},
    70  		{
    71  			name: "allow --config=gs://k8s-testgrid/config --grid-prefix= without confirm",
    72  			args: []string{
    73  				"--config=gs://k8s-testgrid/config",
    74  				"--grid-prefix=",
    75  			},
    76  			want: func(o *options) {
    77  				o.config = *newPathOrDie("gs://k8s-testgrid/config")
    78  				o.gridPrefix = ""
    79  			},
    80  		},
    81  		{
    82  			name: "allow --config=gs://k8s-testgrid/config with random grid prefix",
    83  			args: []string{
    84  				"--config=gs://k8s-testgrid/config",
    85  				"--grid-prefix=random",
    86  				"--confirm",
    87  			},
    88  			want: func(o *options) {
    89  				o.config = *newPathOrDie("gs://k8s-testgrid/config")
    90  				o.gridPrefix = "random"
    91  				o.confirm = true
    92  			},
    93  		},
    94  		{
    95  			name: "reject --config=gs://k8s-testgrid/config --grid-prefix=",
    96  			args: []string{
    97  				"--config=gs://k8s-testgrid/config",
    98  				"--grid-prefix=",
    99  				"--confirm",
   100  			},
   101  			err: true,
   102  		},
   103  		{
   104  			name: "allow --config=gs://random/location --grid-prefix=",
   105  			args: []string{
   106  				"--config=gs://random/location",
   107  				"--grid-prefix=",
   108  				"--confirm",
   109  			},
   110  			want: func(o *options) {
   111  				o.config = *newPathOrDie("gs://random/location")
   112  				o.gridPrefix = ""
   113  				o.confirm = true
   114  			},
   115  		},
   116  	}
   117  
   118  	for _, tc := range cases {
   119  		t.Run(tc.name, func(t *testing.T) {
   120  			cores := runtime.NumCPU()
   121  			want := options{
   122  				buildTimeout:     3 * time.Minute,
   123  				buildConcurrency: cores * 4,
   124  				groupConcurrency: cores,
   125  				groupTimeout:     10 * time.Minute,
   126  				gridPrefix:       "grid",
   127  			}
   128  			if tc.want != nil {
   129  				tc.want(&want)
   130  			}
   131  			got := gatherFlagOptions(flag.NewFlagSet(tc.name, flag.ContinueOnError), tc.args...)
   132  			switch err := got.validate(); {
   133  			case err != nil:
   134  				if !tc.err {
   135  					t.Errorf("validate() got an unexpected error: %v", err)
   136  				}
   137  			case tc.err:
   138  				t.Error("validate() failed to return an error")
   139  			default:
   140  				if diff := cmp.Diff(want, got, cmp.AllowUnexported(options{}, gcs.Path{}), cmp.AllowUnexported(options{}, util.Strings{})); diff != "" {
   141  					t.Fatalf("gatherFlagOptions() got unexpected diff (-want +got):\n%s", diff)
   142  				}
   143  
   144  			}
   145  		})
   146  	}
   147  }
   148  
   149  func TestSource(t *testing.T) {
   150  	cases := []struct {
   151  		name string
   152  		tg   *configpb.TestGroup
   153  		want resultSource
   154  	}{
   155  		{
   156  			name: "nil",
   157  			want: unknownSource,
   158  		},
   159  		{
   160  			name: "empty",
   161  			tg:   &configpb.TestGroup{},
   162  			want: unknownSource,
   163  		},
   164  		{
   165  			name: "use_kubernetes_client",
   166  			tg: &configpb.TestGroup{
   167  				UseKubernetesClient: true,
   168  			},
   169  			want: gcsSource,
   170  		},
   171  		{
   172  			name: "GCS source",
   173  			tg: &configpb.TestGroup{
   174  				ResultSource: &configpb.TestGroup_ResultSource{
   175  					ResultSourceConfig: &configpb.TestGroup_ResultSource_GcsConfig{
   176  						GcsConfig: &configpb.GCSConfig{},
   177  					},
   178  				},
   179  			},
   180  			want: gcsSource,
   181  		},
   182  		{
   183  			name: "ResultStore source",
   184  			tg: &configpb.TestGroup{
   185  				ResultSource: &configpb.TestGroup_ResultSource{
   186  					ResultSourceConfig: &configpb.TestGroup_ResultSource_ResultstoreConfig{
   187  						ResultstoreConfig: &configpb.ResultStoreConfig{},
   188  					},
   189  				},
   190  			},
   191  			want: resultStoreSource,
   192  		},
   193  		{
   194  			name: "use_kubernetes_client and GCS source",
   195  			tg: &configpb.TestGroup{
   196  				UseKubernetesClient: true,
   197  				ResultSource: &configpb.TestGroup_ResultSource{
   198  					ResultSourceConfig: &configpb.TestGroup_ResultSource_GcsConfig{
   199  						GcsConfig: &configpb.GCSConfig{},
   200  					},
   201  				},
   202  			},
   203  			want: gcsSource,
   204  		},
   205  		{
   206  			name: "use_kubernetes_client and ResultStore source",
   207  			tg: &configpb.TestGroup{
   208  				UseKubernetesClient: true,
   209  				ResultSource: &configpb.TestGroup_ResultSource{
   210  					ResultSourceConfig: &configpb.TestGroup_ResultSource_ResultstoreConfig{
   211  						ResultstoreConfig: &configpb.ResultStoreConfig{},
   212  					},
   213  				},
   214  			},
   215  			want: gcsSource,
   216  		},
   217  	}
   218  	for _, tc := range cases {
   219  		t.Run(tc.name, func(t *testing.T) {
   220  			if got := source(tc.tg); tc.want != got {
   221  				t.Errorf("Result source not as expected: want %v, got %v", tc.want, got)
   222  			}
   223  		})
   224  	}
   225  }