|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | +// Author Ewout Prangsma |
| 21 | +// |
| 22 | + |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "context" |
| 27 | + "crypto/tls" |
| 28 | + "flag" |
| 29 | + "fmt" |
| 30 | + "log" |
| 31 | + "os" |
| 32 | + "os/signal" |
| 33 | + "strings" |
| 34 | + "syscall" |
| 35 | + "time" |
| 36 | + |
| 37 | + driver "github.com/arangodb/go-driver" |
| 38 | + "github.com/arangodb/go-driver/http" |
| 39 | + "github.com/pkg/errors" |
| 40 | + |
| 41 | + "github.com/arangodb/kube-arangodb/pkg/util/retry" |
| 42 | +) |
| 43 | + |
| 44 | +const ( |
| 45 | + defaultTestDuration = time.Hour * 24 * 7 // 7 days |
| 46 | +) |
| 47 | + |
| 48 | +var ( |
| 49 | + maskAny = errors.WithStack |
| 50 | + userName string |
| 51 | + password string |
| 52 | + clusterEndpoints string |
| 53 | + testDuration time.Duration |
| 54 | +) |
| 55 | + |
| 56 | +func init() { |
| 57 | + flag.StringVar(&userName, "username", "", "Authenticating username") |
| 58 | + flag.StringVar(&password, "password", "", "Authenticating password") |
| 59 | + flag.StringVar(&clusterEndpoints, "cluster", "", "Endpoints for database cluster") |
| 60 | + flag.DurationVar(&testDuration, "duration", defaultTestDuration, "Duration of the test") |
| 61 | +} |
| 62 | + |
| 63 | +func main() { |
| 64 | + flag.Parse() |
| 65 | + |
| 66 | + // Create clients & wait for cluster available |
| 67 | + client, err := createClusterClient(clusterEndpoints, userName, password) |
| 68 | + if err != nil { |
| 69 | + log.Fatalf("Failed to create cluster client: %#v\n", err) |
| 70 | + } |
| 71 | + if err := waitUntilClusterUp(client); err != nil { |
| 72 | + log.Fatalf("Failed to reach cluster: %#v\n", err) |
| 73 | + } |
| 74 | + |
| 75 | + // Start running tests |
| 76 | + ctx, cancel := context.WithCancel(context.Background()) |
| 77 | + sigChannel := make(chan os.Signal) |
| 78 | + signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM) |
| 79 | + go handleSignal(sigChannel, cancel) |
| 80 | + runTestLoop(ctx, client, testDuration) |
| 81 | +} |
| 82 | + |
| 83 | +// createClusterClient creates a configuration, connection and client for |
| 84 | +// one of the two ArangoDB clusters in the test. It uses the go-driver. |
| 85 | +// It needs a list of endpoints. |
| 86 | +func createClusterClient(endpoints string, user string, password string) (driver.Client, error) { |
| 87 | + // This will always use HTTP, and user and password authentication |
| 88 | + config := http.ConnectionConfig{ |
| 89 | + Endpoints: strings.Split(endpoints, ","), |
| 90 | + TLSConfig: &tls.Config{InsecureSkipVerify: true}, |
| 91 | + } |
| 92 | + connection, err := http.NewConnection(config) |
| 93 | + if err != nil { |
| 94 | + return nil, maskAny(err) |
| 95 | + } |
| 96 | + clientCfg := driver.ClientConfig{ |
| 97 | + Connection: connection, |
| 98 | + Authentication: driver.BasicAuthentication(user, password), |
| 99 | + } |
| 100 | + client, err := driver.NewClient(clientCfg) |
| 101 | + if err != nil { |
| 102 | + return nil, maskAny(err) |
| 103 | + } |
| 104 | + return client, nil |
| 105 | +} |
| 106 | + |
| 107 | +func waitUntilClusterUp(c driver.Client) error { |
| 108 | + op := func() error { |
| 109 | + ctx := context.Background() |
| 110 | + if _, err := c.Version(ctx); err != nil { |
| 111 | + return maskAny(err) |
| 112 | + } |
| 113 | + return nil |
| 114 | + } |
| 115 | + if err := retry.Retry(op, time.Minute); err != nil { |
| 116 | + return maskAny(err) |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +// handleSignal listens for termination signals and stops this process on termination. |
| 122 | +func handleSignal(sigChannel chan os.Signal, cancel context.CancelFunc) { |
| 123 | + signalCount := 0 |
| 124 | + for s := range sigChannel { |
| 125 | + signalCount++ |
| 126 | + fmt.Println("Received signal:", s) |
| 127 | + if signalCount > 1 { |
| 128 | + os.Exit(1) |
| 129 | + } |
| 130 | + cancel() |
| 131 | + } |
| 132 | +} |
0 commit comments