Skip to content

Commit

Permalink
Fix tests (oliver006#254)
Browse files Browse the repository at this point in the history
* properly case build info variables

* don't skip test if host 2nd redis instance connection fails
  • Loading branch information
oliver006 authored Apr 23, 2019
1 parent 3be99b0 commit 48b934d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ services:
- 6379
- name: moar-redis
image: redis:5
commands:
- /usr/local/bin/redis-server --port 6380
ports:
- 6380
- name: redis-cluster
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ENV TAG=$TAG

RUN apk --no-cache add ca-certificates
RUN BUILD_DATE=$(date +%F-%T) && CGO_ENABLED=0 GOOS=linux go build -o /redis_exporter \
-ldflags "-s -w -extldflags \"-static\" -X main.VERSION=$TAG -X main.COMMIT_SHA1=$SHA1 -X main.BUILD_DATE=$BUILD_DATE" .
-ldflags "-s -w -extldflags \"-static\" -X main.BuildVersion=$TAG -X main.BuildCommitSha=$SHA1 -X main.BuildDate=$BUILD_DATE" .

RUN /redis_exporter -version

Expand Down
2 changes: 1 addition & 1 deletion build-github-binaries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fi
echo "Building binaries for Github"
echo ""
export CGO_ENABLED=0
export GO_LDFLAGS="-s -w -extldflags \"-static\" -X main.VERSION=$DRONE_TAG -X main.COMMIT_SHA1=$DRONE_COMMIT_SHA -X main.BUILD_DATE=$(date +%F-%T)"
export GO_LDFLAGS="-s -w -extldflags \"-static\" -X main.BuildVersion=$DRONE_TAG -X main.BuildCommitSha=$DRONE_COMMIT_SHA -X main.BuildDate=$(date +%F-%T)"
echo "GO_LDFLAGS: $GO_LDFLAGS"

go get github.com/mitchellh/gox
Expand Down
17 changes: 7 additions & 10 deletions exporter/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1104,25 +1104,24 @@ func TestNonExistingHost(t *testing.T) {
}

func TestMoreThanOneHost(t *testing.T) {

firstHost := defaultRedisHost.Addrs[0]
secondHostURI := os.Getenv("TEST_SECOND_REDIS_URI")
if secondHostURI == "" {
secondHostURI = "redis://localhost:6380"
log.Printf("TEST_SECOND_REDIS_URI not set - skipping test")
t.SkipNow()
return
}

c, err := redis.DialURL(secondHostURI)
if err != nil {
log.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
t.SkipNow()
t.Errorf("couldn't connect to second redis host, err: %s - skipping test \n", err)
return
}
defer c.Close()

_, err = c.Do("PING")
if err != nil {
log.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
t.SkipNow()
t.Errorf("couldn't connect to second redis host, err: %s - skipping test \n", err)
return
}
defer c.Close()
Expand All @@ -1135,16 +1134,14 @@ func TestMoreThanOneHost(t *testing.T) {

_, err = c.Do("SELECT", dbNumStr)
if err != nil {
log.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
t.SkipNow()
t.Errorf("couldn't connect to second redis host, err: %s - skipping test \n", err)
return
}

secondHostValue := float64(5678.9)
_, err = c.Do("SET", keys[0], secondHostValue)
if err != nil {
log.Printf("couldn't connect to second redis host, err: %s - skipping test \n", err)
t.SkipNow()
t.Errorf("couldn't connect to second redis host, err: %s - skipping test \n", err)
return
}

Expand Down
16 changes: 8 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
)

var (
// VERSION, BUILD_DATE, GIT_COMMIT are filled in by the build script
VERSION = "<<< filled in by build >>>"
BUILD_DATE = "<<< filled in by build >>>"
COMMIT_SHA1 = "<<< filled in by build >>>"
// BuildVersion, BuildDate, BuildCommitSha are filled in by the build script
BuildVersion = "<<< filled in by build >>>"
BuildDate = "<<< filled in by build >>>"
BuildCommitSha = "<<< filled in by build >>>"
)

func getEnv(key string, defaultVal string) string {
Expand Down Expand Up @@ -64,7 +64,7 @@ func main() {
log.SetFormatter(&log.TextFormatter{})
}
log.Printf("Redis Metrics Exporter %s build date: %s sha1: %s Go: %s",
VERSION, BUILD_DATE, COMMIT_SHA1,
BuildVersion, BuildDate, BuildCommitSha,
runtime.Version(),
)
if *isDebug {
Expand Down Expand Up @@ -131,7 +131,7 @@ func main() {
Name: "redis_exporter_build_info",
Help: "redis exporter build_info",
}, []string{"version", "commit_sha", "build_date", "golang_version"})
buildInfo.WithLabelValues(VERSION, COMMIT_SHA1, BUILD_DATE, runtime.Version()).Set(1)
buildInfo.WithLabelValues(BuildVersion, BuildCommitSha, BuildDate, runtime.Version()).Set(1)

if *redisMetricsOnly {
registry := prometheus.NewRegistry()
Expand All @@ -147,9 +147,9 @@ func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Redis Exporter v` + VERSION + `</title></head>
<head><title>Redis Exporter v` + BuildVersion + `</title></head>
<body>
<h1>Redis Exporter ` + VERSION + `</h1>
<h1>Redis Exporter ` + BuildVersion + `</h1>
<p><a href='` + *metricPath + `'>Metrics</a></p>
</body>
</html>
Expand Down

0 comments on commit 48b934d

Please sign in to comment.