...

Source file src/github.com/cybertec-postgresql/pgwatch/v3/internal/cmdopts/cmdconfig_test.go

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

     1  package cmdopts
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestConfigInitCommand_Execute(t *testing.T) {
    13  	a := assert.New(t)
    14  	t.Run("subcommand is missing", func(*testing.T) {
    15  		os.Args = []string{0: "config_test", "config"}
    16  		_, err := New(io.Discard)
    17  		a.Error(err)
    18  	})
    19  
    20  	t.Run("subcommand is invalid", func(*testing.T) {
    21  		os.Args = []string{0: "config_test", "config", "invalid"}
    22  		_, err := New(io.Discard)
    23  		a.Error(err)
    24  	})
    25  
    26  	t.Run("sources and metrics are empty", func(*testing.T) {
    27  		os.Args = []string{0: "config_test", "config", "init"}
    28  		_, err := New(io.Discard)
    29  		a.Error(err)
    30  	})
    31  
    32  	t.Run("metrics is a proper file name", func(*testing.T) {
    33  		fname := t.TempDir() + "/metrics.yaml"
    34  		os.Args = []string{0: "config_test", "--metrics=" + fname, "config", "init"}
    35  		_, err := New(io.Discard)
    36  		a.NoError(err)
    37  		a.FileExists(fname)
    38  		fi, err := os.Stat(fname)
    39  		require.NoError(t, err)
    40  		a.True(fi.Size() > 0)
    41  	})
    42  
    43  	t.Run("sources is a proper file name", func(*testing.T) {
    44  		fname := t.TempDir() + "/sources.yaml"
    45  		os.Args = []string{0: "config_test", "--sources=" + fname, "config", "init"}
    46  		_, err := New(io.Discard)
    47  		a.NoError(err)
    48  		a.FileExists(fname)
    49  		fi, err := os.Stat(fname)
    50  		require.NoError(t, err)
    51  		a.True(fi.Size() > 0)
    52  	})
    53  
    54  	t.Run("metrics is an invalid file name", func(*testing.T) {
    55  		os.Args = []string{0: "config_test", "--metrics=/", "config", "init"}
    56  		opts, err := New(io.Discard)
    57  		a.Error(err)
    58  		a.Equal(ExitCodeConfigError, opts.ExitCode)
    59  	})
    60  
    61  	t.Run("metrics is proper postgres connectin string", func(*testing.T) {
    62  		os.Args = []string{0: "config_test", "--metrics=postgresql://foo@bar/baz", "config", "init"}
    63  		opts, err := New(io.Discard)
    64  		a.Error(err)
    65  		a.Equal(ExitCodeConfigError, opts.ExitCode)
    66  	})
    67  
    68  }
    69  
    70  func TestConfigUpgradeCommand_Execute(t *testing.T) {
    71  	a := assert.New(t)
    72  
    73  	t.Run("sources and metrics are empty", func(*testing.T) {
    74  		os.Args = []string{0: "config_test", "config", "upgrade"}
    75  		_, err := New(io.Discard)
    76  		a.Error(err)
    77  	})
    78  
    79  	t.Run("metrics is a proper file name but files are not upgradable", func(*testing.T) {
    80  		fname := t.TempDir() + "/metrics.yaml"
    81  		os.Args = []string{0: "config_test", "--metrics=" + fname, "config", "upgrade"}
    82  		c, err := New(io.Discard)
    83  		a.Error(err)
    84  		a.True(c.CommandCompleted)
    85  		a.Equal(ExitCodeConfigError, c.ExitCode)
    86  	})
    87  
    88  }
    89