k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/gopherage/cmd/metadata/metadata_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 metadata
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"reflect"
    23  	"testing"
    24  
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  func TestValidateBase(t *testing.T) {
    29  	var testCases = []struct {
    30  		name        string
    31  		flags       *Flags
    32  		expected    *Flags
    33  		envOwner    string
    34  		expectedErr bool
    35  	}{
    36  		{
    37  			name:     "Flag Set",
    38  			flags:    &Flags{project: "foo"},
    39  			expected: &Flags{project: "foo"},
    40  		},
    41  		{
    42  			name:     "Flag From ENV",
    43  			flags:    &Flags{project: ""},
    44  			envOwner: "foo",
    45  			expected: &Flags{project: "foo"},
    46  		},
    47  		{
    48  			name:        "Flag ENV unset",
    49  			envOwner:    "",
    50  			flags:       &Flags{},
    51  			expected:    &Flags{},
    52  			expectedErr: true,
    53  		},
    54  	}
    55  
    56  	cmd := &cobra.Command{
    57  		Use:   "test",
    58  		Short: "Test",
    59  		Run: func(cmd *cobra.Command, args []string) {
    60  			fmt.Print("Test")
    61  		}}
    62  
    63  	for _, testCase := range testCases {
    64  		testCase := testCase
    65  		t.Run(testCase.name, func(t *testing.T) {
    66  			t.Parallel()
    67  			fakeEnv := func(a string) string {
    68  				return testCase.envOwner
    69  			}
    70  			err := ValidateBase(testCase.flags, cmd, fakeEnv)
    71  			if err == nil && testCase.expectedErr {
    72  				t.Errorf("%s: expected an error but got none", testCase.name)
    73  			}
    74  			if err != nil && !testCase.expectedErr {
    75  				t.Errorf("%s: expected no error but got one: %v", testCase.name, err)
    76  			}
    77  
    78  			if !reflect.DeepEqual(testCase.flags, testCase.expected) {
    79  				t.Errorf("%s: expected match to %v but got %v", testCase.name, testCase.expected, testCase.flags)
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func TestValidateInc(t *testing.T) {
    86  	var testCases = []struct {
    87  		name        string
    88  		flags       *Flags
    89  		expected    *Flags
    90  		expectedErr bool
    91  	}{
    92  		{
    93  			name:     "Flag Set",
    94  			flags:    &Flags{changeNum: "foo", patchSet: "bar"},
    95  			expected: &Flags{changeNum: "foo", patchSet: "bar"},
    96  		},
    97  		{
    98  			name:        "One flag unset",
    99  			flags:       &Flags{changeNum: "foo"},
   100  			expected:    &Flags{changeNum: "foo"},
   101  			expectedErr: true,
   102  		},
   103  		{
   104  			name:        "All flag unset",
   105  			flags:       &Flags{},
   106  			expected:    &Flags{},
   107  			expectedErr: true,
   108  		},
   109  	}
   110  
   111  	for _, testCase := range testCases {
   112  		testCase := testCase
   113  		t.Run(testCase.name, func(t *testing.T) {
   114  			t.Parallel()
   115  			err := ValidateInc(testCase.flags)
   116  			if err == nil && testCase.expectedErr {
   117  				t.Errorf("%s: expected an error but got none", testCase.name)
   118  			}
   119  			if err != nil && !testCase.expectedErr {
   120  				t.Errorf("%s: expected no error but got one: %v", testCase.name, err)
   121  			}
   122  
   123  			if !reflect.DeepEqual(testCase.flags, testCase.expected) {
   124  				t.Errorf("%s: expected match to %v but got %v", testCase.name, testCase.expected, testCase.flags)
   125  			}
   126  		})
   127  	}
   128  }
   129  
   130  func TestValidateAbs(t *testing.T) {
   131  	var testCases = []struct {
   132  		name        string
   133  		flags       *Flags
   134  		expected    *Flags
   135  		gitFunc     gitRunner
   136  		expectedErr bool
   137  	}{
   138  		{
   139  			name:     "Flag Set",
   140  			flags:    &Flags{commitID: "1234", ref: "bar"},
   141  			expected: &Flags{commitID: "1234", ref: "bar"},
   142  		},
   143  		{
   144  			name:     "No CommitID",
   145  			flags:    &Flags{ref: "bar"},
   146  			expected: &Flags{commitID: "1234", ref: "bar"},
   147  			gitFunc:  func(a ...string) (string, error) { return "1234", nil },
   148  		},
   149  		{
   150  			name:        "Error CommitID",
   151  			flags:       &Flags{ref: "bar"},
   152  			expected:    &Flags{ref: "bar"},
   153  			gitFunc:     func(a ...string) (string, error) { return "", errors.New("bad") },
   154  			expectedErr: true,
   155  		},
   156  		{
   157  			name:     "No Ref",
   158  			flags:    &Flags{commitID: "1234"},
   159  			expected: &Flags{commitID: "1234", ref: "bar"},
   160  			gitFunc:  func(a ...string) (string, error) { return "bar", nil },
   161  		},
   162  		{
   163  			name:     "Error Ref",
   164  			flags:    &Flags{commitID: "1234"},
   165  			expected: &Flags{commitID: "1234", ref: "HEAD"},
   166  			gitFunc:  func(a ...string) (string, error) { return "", errors.New("bad") },
   167  		},
   168  	}
   169  
   170  	for _, testCase := range testCases {
   171  		testCase := testCase
   172  		t.Run(testCase.name, func(t *testing.T) {
   173  			t.Parallel()
   174  			err := ValidateAbs(testCase.flags, testCase.gitFunc)
   175  			if err == nil && testCase.expectedErr {
   176  				t.Errorf("%s: expected an error but got none", testCase.name)
   177  			}
   178  			if err != nil && !testCase.expectedErr {
   179  				t.Errorf("%s: expected no error but got one: %v", testCase.name, err)
   180  			}
   181  
   182  			if !reflect.DeepEqual(testCase.flags, testCase.expected) {
   183  				t.Errorf("%s: expected match to %v but got %v", testCase.name, testCase.expected, testCase.flags)
   184  			}
   185  		})
   186  	}
   187  }