...

Source file src/k8s.io/component-base/version/verflag/verflag_test.go

Documentation: k8s.io/component-base/version/verflag

     1  /*
     2  Copyright 2023 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 verflag
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/spf13/pflag"
    26  
    27  	"k8s.io/component-base/version"
    28  )
    29  
    30  func TestVersionFlag(t *testing.T) {
    31  	initialFlagValue := string(*versionFlag)
    32  	initialVersion := version.Get()
    33  
    34  	testcases := []struct {
    35  		name               string
    36  		flags              []string
    37  		expectError        string
    38  		expectExit         bool
    39  		expectPrintVersion string
    40  		expectGitVersion   string
    41  	}{
    42  		{
    43  			name:             "no flag",
    44  			flags:            []string{},
    45  			expectGitVersion: initialVersion.GitVersion,
    46  		},
    47  		{
    48  			name:             "false",
    49  			flags:            []string{"--version=false"},
    50  			expectGitVersion: initialVersion.GitVersion,
    51  		},
    52  
    53  		{
    54  			name:               "valueless",
    55  			flags:              []string{"--version"},
    56  			expectGitVersion:   initialVersion.GitVersion,
    57  			expectExit:         true,
    58  			expectPrintVersion: "Kubernetes " + initialVersion.GitVersion,
    59  		},
    60  		{
    61  			name:               "true",
    62  			flags:              []string{"--version=true"},
    63  			expectGitVersion:   initialVersion.GitVersion,
    64  			expectExit:         true,
    65  			expectPrintVersion: "Kubernetes " + initialVersion.GitVersion,
    66  		},
    67  		{
    68  			name:               "raw",
    69  			flags:              []string{"--version=raw"},
    70  			expectGitVersion:   initialVersion.GitVersion,
    71  			expectExit:         true,
    72  			expectPrintVersion: fmt.Sprintf("%#v", initialVersion),
    73  		},
    74  		{
    75  			name:               "truthy",
    76  			flags:              []string{"--version=T"},
    77  			expectGitVersion:   initialVersion.GitVersion,
    78  			expectExit:         true,
    79  			expectPrintVersion: "Kubernetes " + initialVersion.GitVersion,
    80  		},
    81  
    82  		{
    83  			name:             "override",
    84  			flags:            []string{"--version=v0.0.0-custom"},
    85  			expectGitVersion: "v0.0.0-custom",
    86  		},
    87  		{
    88  			name:        "invalid override semver",
    89  			flags:       []string{"--version=vX"},
    90  			expectError: `could not parse "vX"`,
    91  		},
    92  		{
    93  			name:        "invalid override major",
    94  			flags:       []string{"--version=v1.0.0"},
    95  			expectError: `must match major/minor/patch`,
    96  		},
    97  		{
    98  			name:        "invalid override minor",
    99  			flags:       []string{"--version=v0.1.0"},
   100  			expectError: `must match major/minor/patch`,
   101  		},
   102  		{
   103  			name:        "invalid override patch",
   104  			flags:       []string{"--version=v0.0.1"},
   105  			expectError: `must match major/minor/patch`,
   106  		},
   107  
   108  		{
   109  			name:               "override and exit",
   110  			flags:              []string{"--version=v0.0.0-custom", "--version"},
   111  			expectGitVersion:   "v0.0.0-custom",
   112  			expectExit:         true,
   113  			expectPrintVersion: "Kubernetes v0.0.0-custom",
   114  		},
   115  	}
   116  
   117  	for _, tc := range testcases {
   118  		t.Run(tc.name, func(t *testing.T) {
   119  
   120  			originalOutput := output
   121  			originalExit := exit
   122  
   123  			outputBuffer := &bytes.Buffer{}
   124  			output = outputBuffer
   125  			exitCalled := false
   126  			exit = func(code int) { exitCalled = true }
   127  
   128  			t.Cleanup(func() {
   129  				output = originalOutput
   130  				exit = originalExit
   131  				*versionFlag = versionValue(initialFlagValue)
   132  				err := version.SetDynamicVersion(initialVersion.GitVersion)
   133  				if err != nil {
   134  					t.Fatal(err)
   135  				}
   136  			})
   137  
   138  			fs := pflag.NewFlagSet("test", pflag.ContinueOnError)
   139  			AddFlags(fs)
   140  			err := fs.Parse(tc.flags)
   141  			if tc.expectError != "" {
   142  				if err == nil {
   143  					t.Fatal("expected error, got none")
   144  				}
   145  				if !strings.Contains(err.Error(), tc.expectError) {
   146  					t.Fatalf("expected error containing %q, got %q", tc.expectError, err.Error())
   147  				}
   148  				return
   149  			} else if err != nil {
   150  				t.Fatalf("unexpected parse error: %v", err)
   151  			}
   152  
   153  			if e, a := tc.expectGitVersion, version.Get().GitVersion; e != a {
   154  				t.Fatalf("gitversion: expected %v, got %v", e, a)
   155  			}
   156  
   157  			PrintAndExitIfRequested()
   158  			if e, a := tc.expectExit, exitCalled; e != a {
   159  				t.Fatalf("exit(): expected %v, got %v", e, a)
   160  			}
   161  			if e, a := tc.expectPrintVersion, strings.TrimSpace(outputBuffer.String()); e != a {
   162  				t.Fatalf("print version: expected %v, got %v", e, a)
   163  			}
   164  		})
   165  	}
   166  }
   167  

View as plain text