...

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

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

     1  package cmdopts
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/cybertec-postgresql/pgwatch/v6/internal/metrics"
    10  	"github.com/cybertec-postgresql/pgwatch/v6/internal/sinks"
    11  	"github.com/cybertec-postgresql/pgwatch/v6/internal/sources"
    12  	"github.com/cybertec-postgresql/pgwatch/v6/internal/testutil"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  // TestConfigUpgrade_VerifyNoCircularDependency tests that config upgrade can be run even when the schema
    18  // needs migrations, proving there's no circular dependency
    19  func TestConfigUpgrade_VerifyNoCircularDependency(t *testing.T) {
    20  	if testing.Short() {
    21  		t.Skip("Skipping integration test")
    22  	}
    23  
    24  	// Create a PostgreSQL container with empty database (no schema)
    25  	pgContainer, tearDown, err := testutil.SetupPostgresContainerWithInitScripts()
    26  	require.NoError(t, err)
    27  	defer tearDown()
    28  
    29  	connStr, err := pgContainer.ConnectionString(testutil.TestContext)
    30  	require.NoError(t, err)
    31  
    32  	ctx := context.Background()
    33  
    34  	// Test with only metrics and sinks (sources reader also implements Migrator since #1288 fix)
    35  	opts := &Options{
    36  		Metrics:      metrics.CmdOpts{Metrics: connStr},
    37  		Sinks:        sinks.CmdOpts{Sinks: []string{connStr}, RetentionInterval: "30 days", BatchingDelay: time.Second, PartitionInterval: "1 week", MaintenanceInterval: "12 hours"},
    38  		OutputWriter: io.Discard,
    39  	}
    40  
    41  	// Config upgrade should work on metrics and sinks
    42  	cmd := ConfigUpgradeCommand{owner: opts}
    43  	err = cmd.Execute(nil)
    44  	assert.NoError(t, err)
    45  	assert.Equal(t, ExitCodeOK, opts.ExitCode)
    46  
    47  	// After successful upgrade, InitConfigReaders should succeed
    48  	opts2 := &Options{
    49  		Metrics: metrics.CmdOpts{Metrics: connStr},
    50  		Sources: sources.CmdOpts{Sources: connStr, Refresh: 120, MaxParallelConnectionsPerDb: 1},
    51  		Sinks:   sinks.CmdOpts{Sinks: []string{connStr}},
    52  	}
    53  	err = opts2.InitConfigReaders(ctx)
    54  	assert.NoError(t, err)
    55  	assert.NotNil(t, opts2.MetricsReaderWriter)
    56  	assert.NotNil(t, opts2.SourcesReaderWriter)
    57  }
    58  
    59  // TestInitConfigReaders_SourcesOnlyDatabase exercises the issue #1288 repro where the operator points
    60  // --sources at a brand-new Postgres database while leaving --metrics empty (built-in defaults).
    61  // Before the fix this left the database without the pgwatch schema and any subsequent call would
    62  // fail with `relation "pgwatch.source" does not exist`. After the fix, the sources constructor
    63  // bootstraps the schema on first use.
    64  func TestInitConfigReaders_SourcesOnlyDatabase(t *testing.T) {
    65  	if testing.Short() {
    66  		t.Skip("Skipping integration test")
    67  	}
    68  
    69  	pgContainer, tearDown, err := testutil.SetupPostgresContainer()
    70  	require.NoError(t, err)
    71  	defer tearDown()
    72  
    73  	connStr, err := pgContainer.ConnectionString(testutil.TestContext, "sslmode=disable")
    74  	require.NoError(t, err)
    75  
    76  	ctx := context.Background()
    77  
    78  	opts := &Options{
    79  		Sources: sources.CmdOpts{
    80  			Sources:                     connStr,
    81  			Refresh:                     120,
    82  			MaxParallelConnectionsPerDb: 1,
    83  		},
    84  	}
    85  
    86  	require.NoError(t, opts.InitConfigReaders(ctx))
    87  
    88  	srcs, err := opts.SourcesReaderWriter.GetSources()
    89  	require.NoError(t, err)
    90  	assert.Empty(t, srcs)
    91  
    92  	require.NoError(t, opts.SourcesReaderWriter.CreateSource(sources.Source{
    93  		Name:          "test",
    94  		Group:         "default",
    95  		ConnStr:       connStr,
    96  		Kind:          sources.Kind("postgres"),
    97  		PresetMetrics: "basic",
    98  		IsEnabled:     true,
    99  	}))
   100  
   101  	srcs, err = opts.SourcesReaderWriter.GetSources()
   102  	require.NoError(t, err)
   103  	assert.Len(t, srcs, 1)
   104  	assert.Equal(t, "test", srcs[0].Name)
   105  
   106  	upgrade, err := opts.NeedsSchemaUpgrade()
   107  	require.NoError(t, err)
   108  	assert.False(t, upgrade, "freshly bootstrapped database must be at current schema version")
   109  }
   110