github.com/kamilsk/grafaman@v1.0.0-beta3.0.20201207211242-3e0d02dd84ce/internal/cnf/option_test.go (about)

     1  package cnf_test
     2  
     3  import (
     4  	"bytes"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/spf13/cobra"
    10  	"github.com/spf13/viper"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  	"go.octolab.org/safe"
    14  
    15  	. "github.com/kamilsk/grafaman/internal/cnf"
    16  )
    17  
    18  func TestWithConfig(t *testing.T) {
    19  	t.Run("load from dotenv", func(t *testing.T) {
    20  		var (
    21  			box = viper.New()
    22  			cmd = new(cobra.Command)
    23  			cnf = Config{File: "testdata/.env.paas"}
    24  		)
    25  
    26  		src := cnf // copy
    27  		box.RegisterAlias("app", "app_name")
    28  		box.RegisterAlias("grafana", "grafana_url")
    29  		box.RegisterAlias("dashboard", "grafana_dashboard")
    30  		box.RegisterAlias("graphite", "graphite_url")
    31  		box.RegisterAlias("metrics", "graphite_metrics")
    32  
    33  		cmd = Apply(cmd, box, WithConfig(&cnf))
    34  		assert.NoError(t, cmd.PreRunE(cmd, nil))
    35  		assert.NotEqual(t, src, cnf)
    36  		assert.Equal(t, "awesome-service", cnf.App)
    37  		assert.Equal(t, "https://grafana.api/", cnf.Grafana.URL)
    38  		assert.Equal(t, "DTknF4rik", cnf.Grafana.Dashboard)
    39  		assert.Equal(t, "https://graphite.api/", cnf.Graphite.URL)
    40  		assert.Equal(t, "apps.services.awesome-service", cnf.Graphite.Prefix)
    41  	})
    42  
    43  	t.Run("load from old dotenv", func(t *testing.T) {
    44  		var (
    45  			box = viper.New()
    46  			cmd = new(cobra.Command)
    47  			cnf = Config{File: "testdata/.env"}
    48  		)
    49  
    50  		src := cnf // copy
    51  		box.RegisterAlias("app", "app_name")
    52  		box.RegisterAlias("grafana", "grafana_url")
    53  		box.RegisterAlias("dashboard", "grafana_dashboard")
    54  		box.RegisterAlias("graphite", "graphite_url")
    55  		box.RegisterAlias("metrics", "graphite_metrics")
    56  
    57  		cmd = Apply(cmd, box, WithConfig(&cnf))
    58  		assert.NoError(t, cmd.PreRunE(cmd, nil))
    59  		assert.NotEqual(t, src, cnf)
    60  		assert.Empty(t, cnf.App)
    61  		assert.Equal(t, "https://grafana.api/", cnf.Grafana.URL)
    62  		assert.Equal(t, "DTknF4rik", cnf.Grafana.Dashboard)
    63  		assert.Equal(t, "https://graphite.api/", cnf.Graphite.URL)
    64  		assert.Equal(t, "apps.services.awesome-service", cnf.Graphite.Prefix)
    65  	})
    66  
    67  	t.Run("load from app.toml", func(t *testing.T) {
    68  		var (
    69  			box = viper.New()
    70  			cmd = new(cobra.Command)
    71  			cnf = Config{File: ".env.unknown"}
    72  		)
    73  
    74  		src := cnf // copy
    75  		box.RegisterAlias("app", "app_name")
    76  		box.RegisterAlias("grafana", "grafana_url")
    77  		box.RegisterAlias("dashboard", "grafana_dashboard")
    78  		box.RegisterAlias("graphite", "graphite_url")
    79  		box.RegisterAlias("metrics", "graphite_metrics")
    80  
    81  		cmd = Apply(cmd, box, WithConfig(&cnf))
    82  		require.NoError(t, os.Chdir("testdata"))
    83  		assert.NoError(t, cmd.PreRunE(cmd, nil))
    84  		assert.NotEqual(t, src, cnf)
    85  		assert.Equal(t, "awesome-service", cnf.App)
    86  		assert.Equal(t, "https://grafana.api/", cnf.Grafana.URL)
    87  		assert.Equal(t, "DTknF4rik", cnf.Grafana.Dashboard)
    88  		assert.Equal(t, "https://graphite.api/", cnf.Graphite.URL)
    89  		assert.Equal(t, "apps.services.awesome-service", cnf.Graphite.Prefix)
    90  	})
    91  
    92  	t.Run("without config file", func(t *testing.T) {
    93  		var (
    94  			box = viper.New()
    95  			cmd = new(cobra.Command)
    96  			cnf = Config{}
    97  		)
    98  
    99  		src := cnf // copy
   100  		box.RegisterAlias("app", "app_name")
   101  		box.RegisterAlias("grafana", "grafana_url")
   102  		box.RegisterAlias("dashboard", "grafana_dashboard")
   103  		box.RegisterAlias("graphite", "graphite_url")
   104  		box.RegisterAlias("metrics", "graphite_metrics")
   105  
   106  		cmd = Apply(cmd, box, WithConfig(&cnf))
   107  		assert.NoError(t, cmd.PreRunE(cmd, nil))
   108  		assert.Equal(t, src, cnf)
   109  	})
   110  }
   111  
   112  func TestWithDebug(t *testing.T) {
   113  	t.Run("flags and bindings", func(t *testing.T) {
   114  		var (
   115  			box = viper.New()
   116  			buf = bytes.NewBuffer(nil)
   117  			cmd = new(cobra.Command)
   118  			cnf = new(Config)
   119  		)
   120  
   121  		logger := logrus.New()
   122  		cmd.SetErr(buf)
   123  
   124  		cmd = Apply(cmd, box, WithDebug(cnf, logger))
   125  		assert.NoError(t, cmd.ParseFlags([]string{
   126  			"--debug",
   127  			"--debug-host", "127.0.0.1:1234",
   128  			"-vvv",
   129  		}))
   130  		assert.True(t, box.GetBool("debug.enabled"))
   131  		assert.Equal(t, "127.0.0.1:1234", box.GetString("debug.host"))
   132  		assert.Equal(t, 3, box.GetInt("debug.level"))
   133  	})
   134  
   135  	t.Run("debug with defaults", func(t *testing.T) {
   136  		var (
   137  			box = viper.New()
   138  			buf = bytes.NewBuffer(nil)
   139  			cmd = new(cobra.Command)
   140  			cnf = new(Config)
   141  		)
   142  
   143  		logger := logrus.New()
   144  		cmd.SetErr(buf)
   145  
   146  		cmd = Apply(cmd, box, WithDebug(cnf, logger))
   147  		assert.NoError(t, cmd.ParseFlags([]string{"--debug"}))
   148  		assert.NoError(t, box.Unmarshal(cnf))
   149  		assert.NoError(t, cmd.PreRunE(cmd, nil))
   150  		assert.Empty(t, buf.String())
   151  	})
   152  
   153  	t.Run("debug with warnings", func(t *testing.T) {
   154  		var (
   155  			box = viper.New()
   156  			buf = bytes.NewBuffer(nil)
   157  			cmd = new(cobra.Command)
   158  			cnf = new(Config)
   159  		)
   160  
   161  		logger := logrus.New()
   162  		cmd.SetErr(buf)
   163  
   164  		cmd = Apply(cmd, box, WithDebug(cnf, logger))
   165  		assert.NoError(t, cmd.ParseFlags([]string{"--debug", "-v"}))
   166  		assert.NoError(t, box.Unmarshal(cnf))
   167  		assert.NoError(t, cmd.PreRunE(cmd, nil))
   168  		assert.Contains(t, buf.String(), "start listen and serve pprof")
   169  	})
   170  
   171  	t.Run("debug with infos", func(t *testing.T) {
   172  		var (
   173  			box = viper.New()
   174  			buf = bytes.NewBuffer(nil)
   175  			cmd = new(cobra.Command)
   176  			cnf = new(Config)
   177  		)
   178  
   179  		logger := logrus.New()
   180  		cmd.SetErr(buf)
   181  
   182  		cmd = Apply(cmd, box, WithDebug(cnf, logger))
   183  		assert.NoError(t, cmd.ParseFlags([]string{"--debug", "-vv"}))
   184  		assert.NoError(t, box.Unmarshal(cnf))
   185  		assert.NoError(t, cmd.PreRunE(cmd, nil))
   186  		assert.Contains(t, buf.String(), "start listen and serve pprof")
   187  	})
   188  
   189  	t.Run("verbose debug", func(t *testing.T) {
   190  		var (
   191  			box = viper.New()
   192  			buf = bytes.NewBuffer(nil)
   193  			cmd = new(cobra.Command)
   194  			cnf = new(Config)
   195  		)
   196  
   197  		logger := logrus.New()
   198  		cmd.SetErr(buf)
   199  
   200  		cmd = Apply(cmd, box, WithDebug(cnf, logger))
   201  		assert.NoError(t, cmd.ParseFlags([]string{"--debug", "-vvv"}))
   202  		assert.NoError(t, box.Unmarshal(cnf))
   203  		assert.NoError(t, cmd.PreRunE(cmd, nil))
   204  		assert.Contains(t, buf.String(), "start listen and serve pprof")
   205  	})
   206  
   207  	t.Run("invalid host", func(t *testing.T) {
   208  		var (
   209  			box = viper.New()
   210  			buf = bytes.NewBuffer(nil)
   211  			cmd = new(cobra.Command)
   212  			cnf = new(Config)
   213  		)
   214  
   215  		logger := logrus.New()
   216  		cmd.SetErr(buf)
   217  
   218  		cmd = Apply(cmd, box, WithDebug(cnf, logger))
   219  		assert.NoError(t, cmd.ParseFlags([]string{"--debug", "--debug-host", "invalid:host"}))
   220  		assert.NoError(t, box.Unmarshal(cnf))
   221  		assert.Error(t, cmd.PreRunE(cmd, nil))
   222  		assert.Empty(t, buf.String())
   223  	})
   224  }
   225  
   226  func TestWithGrafana(t *testing.T) {
   227  	t.Run("configure by flags", func(t *testing.T) {
   228  		var (
   229  			box = viper.New()
   230  			cmd = new(cobra.Command)
   231  		)
   232  
   233  		cmd = Apply(cmd, box, WithGrafana())
   234  		assert.NoError(t, cmd.ParseFlags([]string{
   235  			"--grafana", "https://grafana.api/",
   236  			"-d", "DTknF4rik",
   237  		}))
   238  		assert.Equal(t, "https://grafana.api/", box.GetString("grafana"))
   239  		assert.Equal(t, "https://grafana.api/", box.GetString("grafana_url"))
   240  		assert.Equal(t, "DTknF4rik", box.GetString("dashboard"))
   241  		assert.Equal(t, "DTknF4rik", box.GetString("grafana_dashboard"))
   242  	})
   243  
   244  	t.Run("configure by environment", func(t *testing.T) {
   245  		var (
   246  			box = viper.New()
   247  			cmd = new(cobra.Command)
   248  		)
   249  
   250  		release, err := safe.SetEnvs(
   251  			"GRAFANA_URL", "https://grafana.api/",
   252  			"GRAFANA_DASHBOARD", "DTknF4rik",
   253  		)
   254  		require.NoError(t, err)
   255  		defer release(func(err error) { require.NoError(t, err) })
   256  
   257  		cmd = Apply(cmd, box, WithGrafana())
   258  		assert.NoError(t, cmd.ParseFlags(nil))
   259  		assert.Equal(t, "https://grafana.api/", box.GetString("grafana"))
   260  		assert.Equal(t, "https://grafana.api/", box.GetString("grafana_url"))
   261  		assert.Equal(t, "DTknF4rik", box.GetString("dashboard"))
   262  		assert.Equal(t, "DTknF4rik", box.GetString("grafana_dashboard"))
   263  	})
   264  }
   265  
   266  func TestWithGraphite(t *testing.T) {
   267  	t.Run("configure by flags", func(t *testing.T) {
   268  		var (
   269  			box = viper.New()
   270  			cmd = new(cobra.Command)
   271  		)
   272  
   273  		cmd = Apply(cmd, box, WithGraphite())
   274  		assert.NoError(t, cmd.ParseFlags([]string{
   275  			"--filter", "metric.*",
   276  			"--graphite", "https://graphite.api/",
   277  		}))
   278  		assert.Equal(t, "metric.*", box.GetString("filter"))
   279  		assert.Equal(t, "https://graphite.api/", box.GetString("graphite"))
   280  		assert.Equal(t, "https://graphite.api/", box.GetString("graphite_url"))
   281  	})
   282  
   283  	t.Run("configure by environment", func(t *testing.T) {
   284  		var (
   285  			box = viper.New()
   286  			cmd = new(cobra.Command)
   287  		)
   288  
   289  		release, err := safe.SetEnvs("GRAPHITE_URL", "https://graphite.api/")
   290  		require.NoError(t, err)
   291  		defer release(func(err error) { require.NoError(t, err) })
   292  
   293  		cmd = Apply(cmd, box, WithGraphite())
   294  		assert.NoError(t, cmd.ParseFlags(nil))
   295  		assert.Empty(t, box.GetString("filter"))
   296  		assert.Equal(t, "https://graphite.api/", box.GetString("graphite"))
   297  		assert.Equal(t, "https://graphite.api/", box.GetString("graphite_url"))
   298  	})
   299  }
   300  
   301  func TestWithGraphiteMetrics(t *testing.T) {
   302  	t.Run("configure by flags", func(t *testing.T) {
   303  		var (
   304  			box = viper.New()
   305  			cmd = new(cobra.Command)
   306  		)
   307  
   308  		cmd = Apply(cmd, box, WithGraphiteMetrics())
   309  		assert.NoError(t, cmd.ParseFlags([]string{"-m", "apps.services.awesome-service"}))
   310  		assert.Empty(t, box.GetString("app"))
   311  		assert.Empty(t, box.GetString("app_name"))
   312  		assert.Equal(t, "apps.services.awesome-service", box.GetString("metrics"))
   313  		assert.Equal(t, "apps.services.awesome-service", box.GetString("graphite_metrics"))
   314  	})
   315  
   316  	t.Run("configure by environment", func(t *testing.T) {
   317  		var (
   318  			box = viper.New()
   319  			cmd = new(cobra.Command)
   320  		)
   321  
   322  		release, err := safe.SetEnvs(
   323  			"APP_NAME", "awesome-service",
   324  			"GRAPHITE_METRICS", "apps.services.awesome-service",
   325  		)
   326  		require.NoError(t, err)
   327  		defer release(func(err error) { require.NoError(t, err) })
   328  
   329  		cmd = Apply(cmd, box, WithGraphiteMetrics())
   330  		assert.NoError(t, cmd.ParseFlags(nil))
   331  		assert.Equal(t, "awesome-service", box.GetString("app"))
   332  		assert.Equal(t, "awesome-service", box.GetString("app_name"))
   333  		assert.Equal(t, "apps.services.awesome-service", box.GetString("metrics"))
   334  		assert.Equal(t, "apps.services.awesome-service", box.GetString("graphite_metrics"))
   335  	})
   336  }
   337  
   338  func TestWithOutputFormat(t *testing.T) {
   339  	var (
   340  		box = viper.New()
   341  		cmd = new(cobra.Command)
   342  	)
   343  
   344  	cmd = Apply(cmd, box, WithOutputFormat())
   345  	assert.NoError(t, cmd.ParseFlags([]string{"-f", "json"}))
   346  	assert.Equal(t, "json", box.GetString("output.format"))
   347  }