Skip to content

Commit

Permalink
dispersion: add error test case
Browse files Browse the repository at this point in the history
  • Loading branch information
talal committed Jan 11, 2020
1 parent 164cb4f commit 44d21aa
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ static-check: FORCE
@$(GO) vet $(GO_BUILDFLAGS) $(GO_ALLPKGS)

# detailed unit test run (incl. test coverage)
build/%.cover.out: FORCE build/mock-swift-dispersion-report build/mock-swift-recon
build/%.cover.out: FORCE build/mock-swift-dispersion build/mock-swift-recon
@printf "\e[1;36m>> go test $(subst _,/,$*)\e[0m\n"
$(GO) test $(GO_BUILDFLAGS) -ldflags '$(GO_LDFLAGS)' -coverprofile=$@ -covermode=count -coverpkg=$(subst $(space),$(comma),$(GO_COVERPKGS)) $(subst _,/,$*)
build/cover.out: $(GO_COVERFILES)
$(GO) run $(GO_BUILDFLAGS) test/cmd/gocovcat/main.go $(GO_COVERFILES) > $@
build/cover.html: build/cover.out
$(GO) tool cover -html $< -o $@

build/mock-swift-dispersion-report: FORCE
build/mock-swift-dispersion: FORCE
$(GO) install $(GO_BUILDFLAGS) -ldflags '$(GO_LDFLAGS)' '$(PKG)/test/cmd/mock-swift-dispersion-report'
$(GO) install $(GO_BUILDFLAGS) -ldflags '$(GO_LDFLAGS)' '$(PKG)/test/cmd/mock-swift-dispersion-report-with-errors'

build/mock-swift-recon: FORCE
$(GO) install $(GO_BUILDFLAGS) -ldflags '$(GO_LDFLAGS)' '$(PKG)/test/cmd/mock-swift-recon'
Expand Down
18 changes: 17 additions & 1 deletion collectors/dispersion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ func TestDispersionCollector(t *testing.T) {
Method: "GET",
Path: "/metrics",
ExpectStatus: 200,
ExpectBody: assert.FixtureFile("fixtures/dispersion_metrics.prom"),
ExpectBody: assert.FixtureFile("fixtures/dispersion_successful_collect.prom"),
}.Check(t, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
}

func TestDispersionCollectorWithErrors(t *testing.T) {
pathToExecutable, err := filepath.Abs("../build/mock-swift-dispersion-report-with-errors")
if err != nil {
t.Error(err)
}

registry := prometheus.NewPedanticRegistry()
registry.MustRegister(NewDispersionCollector(pathToExecutable))
assert.HTTPRequest{
Method: "GET",
Path: "/metrics",
ExpectStatus: 200,
ExpectBody: assert.FixtureFile("fixtures/dispersion_failed_collect.prom"),
}.Check(t, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
}
3 changes: 3 additions & 0 deletions collectors/fixtures/dispersion_failed_collect.prom
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# HELP swift_dispersion_task_exit_code The exit code for a Swift Dispersion Report query execution.
# TYPE swift_dispersion_task_exit_code gauge
swift_dispersion_task_exit_code{failed_query="--dump-json",task="reportDump"} 1
File renamed without changes.
41 changes: 41 additions & 0 deletions test/cmd/mock-swift-dispersion-report-with-errors/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2019 SAP SE
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"os"

"github.com/alecthomas/kingpin"
)

var reportData = []byte(
`ERROR: 10.0.0.1:6001/sdb-01: [Errno 111] ECONNREFUSED
ERROR: 10.0.0.1:6001/sdb-02: [Errno 111] ECONNREFUSED
ERROR: 10.0.0.1:6001/sdb-03: [Errno 111] ECONNREFUSED
ERROR: 10.0.0.1:6001/sdb-04: [Errno 111] ECONNREFUSED
ERROR: 10.0.0.1:6001/sdb-05: [Errno 111] ECONNREFUSED
ERROR: 10.0.0.2:6001/sdb-01: [Errno 111] ECONNREFUSED
ERROR: 10.0.0.2:6001/sdb-02: [Errno 111] ECONNREFUSED
ERROR: 10.0.0.2:6001/sdb-03: [Errno 111] ECONNREFUSED
ERROR: 10.0.0.2:6001/sdb-04: [Errno 111] ECONNREFUSED
ERROR: 10.0.0.2:6001/sdb-05: [Errno 111] ECONNREFUSED`)

func main() {
dumpJSONFlag := kingpin.Flag("dump-json", "Dump dispersion report in json format.").Short('j').Required().Bool()
kingpin.Parse()
if *dumpJSONFlag {
os.Stdout.Write(reportData)
}
}

0 comments on commit 44d21aa

Please sign in to comment.