github.com/apache/beam/sdks/v2@v2.48.2/go/test/integration/io/xlang/debezium/debezium_test.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one or more
     2  // contributor license agreements.  See the NOTICE file distributed with
     3  // this work for additional information regarding copyright ownership.
     4  // The ASF licenses this file to You under the Apache License, Version 2.0
     5  // (the "License"); you may not use this file except in compliance with
     6  // the License.  You may obtain a copy of the License at
     7  //
     8  //    http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package debezium
    17  
    18  import (
    19  	"context"
    20  	"flag"
    21  	"log"
    22  	"testing"
    23  
    24  	"github.com/apache/beam/sdks/v2/go/pkg/beam"
    25  	"github.com/apache/beam/sdks/v2/go/pkg/beam/io/xlang/debeziumio"
    26  	_ "github.com/apache/beam/sdks/v2/go/pkg/beam/runners/dataflow"
    27  	_ "github.com/apache/beam/sdks/v2/go/pkg/beam/runners/flink"
    28  	_ "github.com/apache/beam/sdks/v2/go/pkg/beam/runners/samza"
    29  	_ "github.com/apache/beam/sdks/v2/go/pkg/beam/runners/spark"
    30  	"github.com/apache/beam/sdks/v2/go/pkg/beam/testing/ptest"
    31  	"github.com/apache/beam/sdks/v2/go/test/integration"
    32  	"github.com/apache/beam/sdks/v2/go/test/integration/internal/containers"
    33  	_ "github.com/lib/pq"
    34  )
    35  
    36  const (
    37  	debeziumImage = "debezium/example-postgres:latest"
    38  	debeziumPort  = "5432/tcp"
    39  	maxRetries    = 5
    40  )
    41  
    42  var expansionAddr string // Populate with expansion address labelled "debeziumio".
    43  
    44  func checkFlags(t *testing.T) {
    45  	if expansionAddr == "" {
    46  		t.Skip("No DebeziumIo expansion address provided.")
    47  	}
    48  }
    49  
    50  func setupTestContainer(ctx context.Context, t *testing.T, dbname, username, password string) string {
    51  	t.Helper()
    52  
    53  	env := map[string]string{
    54  		"POSTGRES_PASSWORD": password,
    55  		"POSTGRES_USER":     username,
    56  		"POSTGRES_DB":       dbname,
    57  	}
    58  
    59  	container := containers.NewContainer(
    60  		ctx,
    61  		t,
    62  		debeziumImage,
    63  		maxRetries,
    64  		containers.WithEnv(env),
    65  		containers.WithPorts([]string{debeziumPort}),
    66  	)
    67  
    68  	return containers.Port(ctx, t, container, debeziumPort)
    69  }
    70  
    71  // TestDebeziumIO_BasicRead tests basic read transform from Debezium.
    72  func TestDebeziumIO_BasicRead(t *testing.T) {
    73  	integration.CheckFilters(t)
    74  	checkFlags(t)
    75  
    76  	ctx := context.Background()
    77  	dbname := "inventory"
    78  	username := "debezium"
    79  	password := "dbz"
    80  	port := setupTestContainer(ctx, t, dbname, username, password)
    81  	host := "localhost"
    82  	connectionProperties := []string{
    83  		"database.dbname=inventory",
    84  		"database.server.name=dbserver1",
    85  		"database.include.list=inventory",
    86  		"include.schema.changes=false",
    87  	}
    88  	read := ReadPipeline(expansionAddr, username, password, dbname, host, port, debeziumio.PostgreSQL, 1, connectionProperties)
    89  	ptest.RunAndValidate(t, read)
    90  }
    91  
    92  func TestMain(m *testing.M) {
    93  	flag.Parse()
    94  	beam.Init()
    95  
    96  	services := integration.NewExpansionServices()
    97  	defer func() { services.Shutdown() }()
    98  	addr, err := services.GetAddr("debeziumio")
    99  	if err != nil {
   100  		log.Printf("skipping missing expansion service: %v", err)
   101  	} else {
   102  		expansionAddr = addr
   103  	}
   104  
   105  	ptest.MainRet(m)
   106  }