options_test.go 1.2 KB

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