github.com/kunnos/engine@v1.13.1/cli/flags/common_test.go (about)

     1  package flags
     2  
     3  import (
     4  	"path/filepath"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/cliconfig"
     8  	"github.com/docker/docker/pkg/testutil/assert"
     9  	"github.com/spf13/pflag"
    10  )
    11  
    12  func TestCommonOptionsInstallFlags(t *testing.T) {
    13  	flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
    14  	opts := NewCommonOptions()
    15  	opts.InstallFlags(flags)
    16  
    17  	err := flags.Parse([]string{
    18  		"--tlscacert=\"/foo/cafile\"",
    19  		"--tlscert=\"/foo/cert\"",
    20  		"--tlskey=\"/foo/key\"",
    21  	})
    22  	assert.NilError(t, err)
    23  	assert.Equal(t, opts.TLSOptions.CAFile, "/foo/cafile")
    24  	assert.Equal(t, opts.TLSOptions.CertFile, "/foo/cert")
    25  	assert.Equal(t, opts.TLSOptions.KeyFile, "/foo/key")
    26  }
    27  
    28  func defaultPath(filename string) string {
    29  	return filepath.Join(cliconfig.ConfigDir(), filename)
    30  }
    31  
    32  func TestCommonOptionsInstallFlagsWithDefaults(t *testing.T) {
    33  	flags := pflag.NewFlagSet("testing", pflag.ContinueOnError)
    34  	opts := NewCommonOptions()
    35  	opts.InstallFlags(flags)
    36  
    37  	err := flags.Parse([]string{})
    38  	assert.NilError(t, err)
    39  	assert.Equal(t, opts.TLSOptions.CAFile, defaultPath("ca.pem"))
    40  	assert.Equal(t, opts.TLSOptions.CertFile, defaultPath("cert.pem"))
    41  	assert.Equal(t, opts.TLSOptions.KeyFile, defaultPath("key.pem"))
    42  }