...

Source file src/github.com/cybertec-postgresql/pgwatch/v6/internal/cmdopts/cmdoptions_test.go

Documentation: github.com/cybertec-postgresql/pgwatch/v6/internal/cmdopts

     1  package cmdopts
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/cybertec-postgresql/pgwatch/v6/internal/log"
     8  	flags "github.com/jessevdk/go-flags"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // NewCmdOptions returns a new instance of CmdOptions with default values
    13  func NewCmdOptions(args ...string) *Options {
    14  	cmdOpts := new(Options)
    15  	_, _ = flags.NewParser(cmdOpts, flags.PrintErrors).ParseArgs(args)
    16  	return cmdOpts
    17  }
    18  
    19  func TestParseFail(t *testing.T) {
    20  	tests := [][]string{
    21  		{0: "go-test", "--unknown-option"},
    22  		{0: "go-test", "-c", "client01", "-f", "foo"},
    23  	}
    24  	for _, d := range tests {
    25  		os.Args = d
    26  		_, err := New(nil)
    27  		assert.Error(t, err)
    28  	}
    29  }
    30  
    31  func TestParseSuccess(t *testing.T) {
    32  	tests := [][]string{
    33  		{0: "go-test", "--help"},
    34  	}
    35  	for _, d := range tests {
    36  		os.Args = d
    37  		c, err := New(nil)
    38  		assert.True(t, c.Help)
    39  		assert.Error(t, err)
    40  	}
    41  }
    42  
    43  func TestLogLevel(t *testing.T) {
    44  	c := &Options{Logging: log.CmdOpts{LogLevel: "debug"}}
    45  	assert.True(t, c.Verbose())
    46  	c = &Options{Logging: log.CmdOpts{LogLevel: "info"}}
    47  	assert.False(t, c.Verbose())
    48  }
    49  
    50  func TestNewCmdOptions(t *testing.T) {
    51  	c := NewCmdOptions("-c", "config_unit_test", "--password=somestrong")
    52  	assert.NotNil(t, c)
    53  }
    54  
    55  func TestValidateConfig(t *testing.T) {
    56  	tests := []struct {
    57  		name             string
    58  		sources          string
    59  		metrics          string
    60  		wantErr          bool
    61  		wantErrSubstring string
    62  		wantSourcesAfter string
    63  		wantMetricsAfter string
    64  	}{
    65  		{
    66  			name:             "both empty returns error",
    67  			sources:          "",
    68  			metrics:          "",
    69  			wantErr:          true,
    70  			wantErrSubstring: "both --sources and --metrics are empty",
    71  		},
    72  		{
    73  			name:             "only metrics PG inherits sources",
    74  			sources:          "",
    75  			metrics:          "postgres://u@h/config",
    76  			wantErr:          false,
    77  			wantSourcesAfter: "postgres://u@h/config",
    78  			wantMetricsAfter: "postgres://u@h/config",
    79  		},
    80  		{
    81  			name:             "only sources PG inherits metrics",
    82  			sources:          "postgres://u@h/config",
    83  			metrics:          "",
    84  			wantErr:          false,
    85  			wantSourcesAfter: "postgres://u@h/config",
    86  			wantMetricsAfter: "postgres://u@h/config",
    87  		},
    88  		{
    89  			name:    "identical PG connstrs no error",
    90  			sources: "postgres://u@h/config",
    91  			metrics: "postgres://u@h/config",
    92  			wantErr: false,
    93  		},
    94  		{
    95  			name:             "two different PG connstrs rejected",
    96  			sources:          "postgres://u@h/config1",
    97  			metrics:          "postgres://u@h/config2",
    98  			wantErr:          true,
    99  			wantErrSubstring: "--sources and --metrics must use the same configuration database",
   100  		},
   101  		{
   102  			name:    "PG sources with YAML metrics allowed",
   103  			sources: "postgres://u@h/config",
   104  			metrics: "metrics.yaml",
   105  			wantErr: false,
   106  		},
   107  		{
   108  			name:    "YAML sources with PG metrics allowed",
   109  			sources: "sources.yaml",
   110  			metrics: "postgres://u@h/config",
   111  			wantErr: false,
   112  		},
   113  		{
   114  			name:             "YAML sources with empty metrics keeps metrics empty",
   115  			sources:          "sources.yaml",
   116  			metrics:          "",
   117  			wantErr:          false,
   118  			wantSourcesAfter: "sources.yaml",
   119  			wantMetricsAfter: "",
   120  		},
   121  	}
   122  
   123  	for _, tt := range tests {
   124  		t.Run(tt.name, func(t *testing.T) {
   125  			a := assert.New(t)
   126  			c := NewCmdOptions()
   127  			c.Sources.Sources = tt.sources
   128  			c.Metrics.Metrics = tt.metrics
   129  			err := c.ValidateConfig()
   130  			if tt.wantErr {
   131  				a.Error(err)
   132  				if tt.wantErrSubstring != "" {
   133  					a.Contains(err.Error(), tt.wantErrSubstring)
   134  				}
   135  				return
   136  			}
   137  			a.NoError(err)
   138  			if tt.wantSourcesAfter != "" {
   139  				a.Equal(tt.wantSourcesAfter, c.Sources.Sources)
   140  			}
   141  			if tt.wantMetricsAfter != "" {
   142  				a.Equal(tt.wantMetricsAfter, c.Metrics.Metrics)
   143  			}
   144  		})
   145  	}
   146  }
   147  
   148  func TestConfig(t *testing.T) {
   149  	os.Args = []string{0: "config_test", "--sources=sample.config.yaml"}
   150  	_, err := New(nil)
   151  	assert.NoError(t, err)
   152  
   153  	os.Args = []string{0: "config_test", "--unknown"}
   154  	_, err = New(nil)
   155  	assert.Error(t, err)
   156  
   157  	os.Args = []string{0: "config_test"} // sources arg is missing, but set PW_CONFIG
   158  	t.Setenv("PW_SOURCES", "postgresql://foo:baz@bar/test")
   159  	_, err = New(nil)
   160  	assert.NoError(t, err)
   161  }
   162