vitess.io/vitess@v0.16.2/go/test/endtoend/clustertest/main_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  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  
    17  package clustertest
    18  
    19  import (
    20  	"flag"
    21  	"net/http"
    22  	"os"
    23  	"testing"
    24  
    25  	"vitess.io/vitess/go/mysql"
    26  	"vitess.io/vitess/go/test/endtoend/cluster"
    27  )
    28  
    29  var (
    30  	clusterInstance *cluster.LocalProcessCluster
    31  	vtParams        mysql.ConnParams
    32  	keyspaceName    = "commerce"
    33  	cell            = "zone1"
    34  	sqlSchema       = `create table product( 
    35  		sku varbinary(128),
    36  			description varbinary(128),
    37  			price bigint,
    38  			primary key(sku)
    39  		) ENGINE=InnoDB;
    40  		create table customer(
    41  			id bigint not null auto_increment,
    42  			email varchar(128),
    43  			primary key(id)
    44  		) ENGINE=InnoDB;
    45  		create table corder(
    46  			order_id bigint not null auto_increment,
    47  			customer_id bigint,
    48  			sku varbinary(128),
    49  			price bigint,
    50  			primary key(order_id)
    51  		) ENGINE=InnoDB;`
    52  
    53  	vSchema = `{
    54  						"tables": {
    55  							"product": {},
    56  							"customer": {},
    57  							"corder": {}
    58  						}
    59  					}`
    60  )
    61  
    62  func TestMain(m *testing.M) {
    63  	defer cluster.PanicHandler(nil)
    64  	flag.Parse()
    65  
    66  	exitCode := func() int {
    67  		clusterInstance = cluster.NewCluster(cell, "localhost")
    68  		defer clusterInstance.Teardown()
    69  
    70  		// Start topo server
    71  		err := clusterInstance.StartTopo()
    72  		if err != nil {
    73  			return 1
    74  		}
    75  
    76  		// Start keyspace
    77  		keyspace := &cluster.Keyspace{
    78  			Name:      keyspaceName,
    79  			SchemaSQL: sqlSchema,
    80  			VSchema:   vSchema,
    81  		}
    82  		err = clusterInstance.StartUnshardedKeyspace(*keyspace, 1, true)
    83  		if err != nil {
    84  			return 1
    85  		}
    86  
    87  		// Start vtgate
    88  		err = clusterInstance.StartVtgate()
    89  		if err != nil {
    90  			return 1
    91  		}
    92  		vtParams = mysql.ConnParams{
    93  			Host: clusterInstance.Hostname,
    94  			Port: clusterInstance.VtgateMySQLPort,
    95  		}
    96  		return m.Run()
    97  	}()
    98  	os.Exit(exitCode)
    99  }
   100  
   101  func testURL(t *testing.T, url string, testCaseName string) {
   102  	statusCode := getStatusForURL(url)
   103  	if got, want := statusCode, 200; got != want {
   104  		t.Errorf("\nurl: %v\nstatus code: %v \nwant %v for %s", url, got, want, testCaseName)
   105  	}
   106  }
   107  
   108  // getStatusForUrl returns the status code for the URL
   109  func getStatusForURL(url string) int {
   110  	resp, err := http.Get(url)
   111  	if err != nil {
   112  		return 0
   113  	}
   114  	defer resp.Body.Close()
   115  	return resp.StatusCode
   116  }