|
| 1 | +//go:build all || cassandra |
| 2 | +// +build all cassandra |
| 3 | + |
| 4 | +/* |
| 5 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 6 | + * or more contributor license agreements. See the NOTICE file |
| 7 | + * distributed with this work for additional information |
| 8 | + * regarding copyright ownership. The ASF licenses this file |
| 9 | + * to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance |
| 11 | + * with the License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + */ |
| 21 | +/* |
| 22 | + * Content before git sha 34fdeebefcbf183ed7f916f931aa0586fdaa1b40 |
| 23 | + * Copyright (c) 2016, The Gocql authors, |
| 24 | + * provided under the BSD-3-Clause License. |
| 25 | + * See the NOTICE file distributed with this work for additional information. |
| 26 | + */ |
| 27 | + |
| 28 | +package gocql |
| 29 | + |
| 30 | +import ( |
| 31 | + "fmt" |
| 32 | + "os/exec" |
| 33 | + "strings" |
| 34 | + "testing" |
| 35 | + "time" |
| 36 | + |
| 37 | + "github.com/stretchr/testify/assert" |
| 38 | +) |
| 39 | + |
| 40 | +// This test tests that gocql is able to connect to a C* node provisioned with Docker |
| 41 | +// This is useful to make sure we don't break common testing configurations |
| 42 | +func TestDocker(t *testing.T) { |
| 43 | + version := "3.11.11" |
| 44 | + randomUuid := MustRandomUUID().String() |
| 45 | + err := exec.Command("docker", "run", "-d", "--name", randomUuid, "-p", "9080:9042", fmt.Sprintf("cassandra:%s", version)).Run() |
| 46 | + defer exec.Command("docker", "rm", "-f", randomUuid).Run() |
| 47 | + |
| 48 | + if err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + |
| 52 | + cluster := NewCluster("localhost:9080") |
| 53 | + cluster.Logger = NewLogger(LogLevelDebug) |
| 54 | + |
| 55 | + timer := time.After(60 * time.Second) |
| 56 | + var session *Session |
| 57 | + done := false |
| 58 | + for !done { |
| 59 | + select { |
| 60 | + case <-timer: |
| 61 | + t.Fatalf("timed out, last err: %v", err) |
| 62 | + default: |
| 63 | + session, err = cluster.CreateSession() |
| 64 | + if err == nil { |
| 65 | + done = true |
| 66 | + } else if strings.Contains(err.Error(), "unable to discover protocol version") { |
| 67 | + time.Sleep(5 * time.Second) |
| 68 | + } else { |
| 69 | + t.Fatal(err) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + defer session.Close() |
| 75 | + var parsedVersion string |
| 76 | + err = session.Query("SELECT release_version FROM system.local").Scan(&parsedVersion) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("failed to query: %s", err) |
| 79 | + } |
| 80 | + |
| 81 | + assert.Equal(t, version, parsedVersion) |
| 82 | +} |
0 commit comments