github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/integration_tests/api_v2/main.go (about)

     1  // Copyright 2023 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package main
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"net/url"
    20  	"os"
    21  	"os/signal"
    22  	"syscall"
    23  
    24  	"github.com/pingcap/errors"
    25  	"github.com/pingcap/log"
    26  	"github.com/pingcap/tiflow/pkg/httputil"
    27  	"go.uber.org/zap"
    28  )
    29  
    30  func main() {
    31  	ctx, cancel := context.WithCancel(context.Background())
    32  	defer cancel()
    33  
    34  	sc := make(chan os.Signal, 1)
    35  	signal.Notify(sc,
    36  		syscall.SIGHUP,
    37  		syscall.SIGINT,
    38  		syscall.SIGTERM,
    39  		syscall.SIGQUIT)
    40  	go func() {
    41  		sig := <-sc
    42  		fmt.Printf("\nGot signal [%v] to exit.\n", sig)
    43  		log.Warn("received signal to exit", zap.Stringer("signal", sig))
    44  		switch sig {
    45  		case syscall.SIGTERM:
    46  			cancel()
    47  			os.Exit(0)
    48  		default:
    49  			cancel()
    50  			os.Exit(1)
    51  		}
    52  	}()
    53  
    54  	if err := run(ctx); err != nil {
    55  		log.Panic("run api v2 integration test failed", zap.Error(err))
    56  		os.Exit(1)
    57  	}
    58  }
    59  
    60  func newAPIClient() (*CDCRESTClient, error) {
    61  	cli, err := httputil.NewClient(nil)
    62  	if err != nil {
    63  		return nil, errors.Trace(err)
    64  	}
    65  	hostURL, err := url.Parse("http://127.0.0.1:8300")
    66  	if err != nil {
    67  		return nil, errors.Trace(err)
    68  	}
    69  	return &CDCRESTClient{
    70  		base:             hostURL,
    71  		versionedAPIPath: "/api/v2",
    72  		Client:           cli,
    73  	}, nil
    74  }
    75  
    76  var cases = []func(ctx context.Context, client *CDCRESTClient) error{
    77  	testStatus,
    78  	testClusterHealth,
    79  	testChangefeed,
    80  	testCreateChangefeed,
    81  	testRemoveChangefeed,
    82  	testCapture,
    83  	testProcessor,
    84  	testResignOwner,
    85  	testSetLogLevel,
    86  }
    87  
    88  func run(ctx context.Context) error {
    89  	client, err := newAPIClient()
    90  	if err != nil {
    91  		return errors.Trace(err)
    92  	}
    93  	for _, cse := range cases {
    94  		if err := cse(ctx, client); err != nil {
    95  			return errors.Trace(err)
    96  		}
    97  	}
    98  	return nil
    99  }