github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/dind/pkg/cluster-up/options/options_test.go (about)

     1  /*
     2  Copyright 2018 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 options
    18  
    19  import (
    20  	"flag"
    21  	"reflect"
    22  	"testing"
    23  )
    24  
    25  // TestCLIOptions tests that the options correctly parses from the CLI.
    26  func TestCLIOptions(t *testing.T) {
    27  	testCases := []struct {
    28  		testName        string
    29  		argv0           string
    30  		args            []string
    31  		expectedSuccess bool
    32  		expectedOptions Options
    33  	}{
    34  		{
    35  			"Expected sample happy path",
    36  			"foo",
    37  			[]string{"--side-load-image=false", "--proxy-addr=192.168.0.1", "--num-nodes=3"},
    38  			true,
    39  			Options{
    40  				SideloadImage: false,
    41  				DinDNodeImage: "k8s.gcr.io/dind-node-amd64",
    42  				ProxyAddr:     "192.168.0.1",
    43  				Version:       "",
    44  				NumNodes:      3,
    45  			},
    46  		},
    47  		{
    48  			"Negative nodes should fail",
    49  			"foo",
    50  			[]string{"--num-nodes=0"},
    51  			false,
    52  			Options{},
    53  		},
    54  	}
    55  
    56  	for _, tc := range testCases {
    57  		set := flag.NewFlagSet(tc.argv0, flag.ContinueOnError)
    58  		o, err := New(set, tc.args)
    59  
    60  		if err == nil && !tc.expectedSuccess {
    61  			t.Errorf("Test %q expected error, but got %v", tc.testName, o)
    62  		}
    63  		if err != nil && tc.expectedSuccess {
    64  			t.Errorf("Test %q expected success, but got error %v", tc.testName, err)
    65  		}
    66  		if err != nil && !tc.expectedSuccess {
    67  			continue
    68  		}
    69  
    70  		if !reflect.DeepEqual(*o, tc.expectedOptions) {
    71  			t.Errorf("Test case %q expected %#v but got %#v", tc.testName, tc.expectedOptions, *o)
    72  		}
    73  	}
    74  }