Skip to content

Commit

Permalink
Add E2E testing for ES Rollover feature (#1544)
Browse files Browse the repository at this point in the history
* Add E2E testing for ES Rollover feature

Signed-off-by: Israel Blancas <iblancas@redhat.com>

* Add E2E testing for ES Rollover feature

Signed-off-by: Israel Blancas <iblancas@redhat.com>

* Apply changes requested in CR

Signed-off-by: Israel Blancas <iblancas@redhat.com>

* Fix security issue

Signed-off-by: Israel Blancas <iblancas@redhat.com>

* Ensure the data is totally stored before finishing the program

Signed-off-by: Israel Blancas <iblancas@redhat.com>

* Fix condition check

Signed-off-by: Israel Blancas <iblancas@redhat.com>

* Ensure the spans are received

Signed-off-by: Israel Blancas <iblancas@redhat.com>

* Apply new reporter to other tests

Signed-off-by: Israel Blancas <iblancas@redhat.com>

* Fix ES Rollover E2E test

Signed-off-by: Israel Blancas <iblancas@redhat.com>

Co-authored-by: Ruben Vargas <ruben.vp8510@gmail.com>
  • Loading branch information
iblancasa and rubenvp8510 authored Oct 13, 2021
1 parent 93eb3c4 commit 2505d69
Show file tree
Hide file tree
Showing 28 changed files with 1,301 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Dockerfile.asserts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ COPY . /go/src/github.com/jaegertracing/jaeger-operator/
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o ./uiconfig -a ./tests/assert-jobs/uiconfig/main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o ./reporter -a ./tests/assert-jobs/reporter/main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o ./query -a ./tests/assert-jobs/query/main.go
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o ./index -a ./tests/assert-jobs/index/main.go

FROM scratch
WORKDIR /
COPY --from=builder /go/src/github.com/jaegertracing/jaeger-operator/uiconfig .
COPY --from=builder /go/src/github.com/jaegertracing/jaeger-operator/reporter .
COPY --from=builder /go/src/github.com/jaegertracing/jaeger-operator/query .
COPY --from=builder /go/src/github.com/jaegertracing/jaeger-operator/index .
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ prepare-e2e-kuttl-tests: build docker build-assert-job

@cp deploy/crds/jaegertracing.io_jaegers_crd.yaml tests/_build/crds/jaegertracing.io_jaegers_crd.yaml
docker pull jaegertracing/vertx-create-span:operator-e2e-tests
docker pull docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.6

# end-to-tests
.PHONY: kuttl-e2e
Expand All @@ -503,6 +504,7 @@ start-kind:
kind load docker-image local/jaeger-operator:e2e
kind load docker-image local/asserts:e2e
kind load docker-image jaegertracing/vertx-create-span:operator-e2e-tests
kind load docker-image docker.elastic.co/elasticsearch/elasticsearch-oss:6.8.6

.PHONY: build-assert-job
build-assert-job:
Expand Down
2 changes: 1 addition & 1 deletion kuttl-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ kindContainers:
- local/asserts:e2e
- jaegertracing/vertx-create-span:operator-e2e-tests
commands:
- command: kubectl create namespace jaeger-operator-system
- script: kubectl create namespace jaeger-operator-system 2>&1 | grep -v "already exists" || true
- command: kubectl apply -f ./tests/_build/manifests/01-jaeger-operator.yaml -n jaeger-operator-system
- command: kubectl wait --timeout=5m --for=condition=available deployment jaeger-operator -n jaeger-operator-system
- command: kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.1/deploy/static/provider/kind/deploy.yaml
Expand Down
185 changes: 185 additions & 0 deletions tests/assert-jobs/index/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package main

import (
"flag"
"fmt"
"os"
"regexp"
"strings"

"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"

"github.com/jaegertracing/jaeger-operator/tests/assert-jobs/utils"
"github.com/jaegertracing/jaeger-operator/tests/assert-jobs/utils/elasticsearch"
"github.com/jaegertracing/jaeger-operator/tests/assert-jobs/utils/logger"
)

var log logrus.Logger

const (
flagEsNamespace = "es-namespace"
flagEsPort = "es-port"
flagEsURL = "es-url"
flagPattern = "pattern"
flagName = "name"
flagExist = "assert-exist"
flagAssertCountIndices = "assert-count-indices"
flagAssertCountDocs = "assert-count-docs"
flagJaegerService = "jaeger-service"
flagVerbose = "verbose"
)

func filterIndices(indices *[]elasticsearch.EsIndex, pattern string) ([]elasticsearch.EsIndex, error) {
regexPattern, err := regexp.Compile(pattern)
if err != nil {
return nil, fmt.Errorf(fmt.Sprintf("There was a problem with the pattern: %s", err))
}

var matchingIndices []elasticsearch.EsIndex

for _, index := range *indices {
if regexPattern.MatchString(index.Index) {
log.Debugf("Index '%s' matched", index.Index)
matchingIndices = append(matchingIndices, index)
}
}

log.Debugf("%d indices matches the pattern '%s'", len(matchingIndices), pattern)

return matchingIndices, nil

}

// Init the CMD and return error if something didn't go properly
func initCmd() error {
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()

viper.SetDefault(flagEsNamespace, "default")
flag.String(flagEsNamespace, "", "ElasticSearch namespace to use")

viper.SetDefault(flagEsPort, "9200")
flag.String(flagEsPort, "", "ElasticSearch port")

viper.SetDefault(flagEsURL, "http://localhost")
flag.String(flagEsURL, "", "ElasticSearch URL")

viper.SetDefault(flagVerbose, false)
flag.Bool(flagVerbose, false, "Enable verbosity")

viper.SetDefault(flagExist, false)
flag.Bool(flagExist, false, "Assert the pattern matches something")

viper.SetDefault(flagPattern, "")
flag.String(flagPattern, "", "Pattern to use to match indices")

viper.SetDefault(flagName, "")
flag.String(flagName, "", "Name of the desired index (needed for aliases)")

viper.SetDefault(flagJaegerService, "")
flag.String(flagJaegerService, "", "Name of the Jaeger Service")

viper.SetDefault(flagAssertCountIndices, "-1")
flag.Int(flagAssertCountIndices, -1, "Assert the number of matched indices")

viper.SetDefault(flagAssertCountDocs, "-1")
flag.Int(flagAssertCountDocs, -1, "Assert the number of documents")

pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()

err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
return err
}
params := utils.NewParameters()
params.Parse()

if viper.GetString(flagName) != "" && viper.GetString(flagPattern) != "" {
return fmt.Errorf(fmt.Sprintf("--%s and --%s provided. Provide just one", flagName, flagPattern))
} else if viper.GetString(flagName) == "" && viper.GetString(flagPattern) == "" {
return fmt.Errorf(fmt.Sprintf("--%s nor --%s provided. Provide one at least", flagName, flagPattern))
} else if viper.GetBool(flagAssertCountDocs) && viper.GetString(flagJaegerService) == "" {
return fmt.Errorf(fmt.Sprintf("--%s provided. Provide --%s", flagAssertCountDocs, flagJaegerService))
}

return nil
}

func main() {
err := initCmd()
if err != nil {
fmt.Println(err)
os.Exit(1)
}

log = *logger.InitLog(viper.GetBool(flagVerbose))

connection := elasticsearch.EsConnection{
Port: viper.GetString(flagEsPort),
Namespace: viper.GetString(flagEsNamespace),
URL: viper.GetString(flagEsURL),
}
connection.PrettyString(log.Debug)

err = elasticsearch.CheckESConnection(connection)
if err != nil {
log.Fatalln(err)
log.Exit(1)
}

var matchingIndices []elasticsearch.EsIndex
if viper.GetString(flagPattern) != "" {
indices, err := elasticsearch.GetEsIndices(connection)
if err != nil {
log.Fatalln("There was an error while getting the ES indices: ", err)
log.Exit(1)
}

matchingIndices, err = filterIndices(&indices, viper.GetString(flagPattern))
if err != nil {
log.Fatalln(err)
os.Exit(1)
}
} else {
index := elasticsearch.GetEsIndex(connection, viper.GetString(flagName))
matchingIndices = []elasticsearch.EsIndex{index}
}

if viper.GetBool(flagExist) {
if len(matchingIndices) == 0 {
log.Fatalln("No indices match the pattern")
os.Exit(1)
}
}

if viper.GetString(flagName) != "" && viper.GetString(flagAssertCountIndices) != "" {
log.Warnln("Ignoring parameter", flagAssertCountIndices, "because we are checking the info for one index")
} else if viper.GetString(flagPattern) != "" && viper.GetInt(flagAssertCountIndices) > -1 {
if len(matchingIndices) != viper.GetInt(flagAssertCountIndices) {
log.Fatalln(len(matchingIndices), "indices found.", viper.GetInt(flagAssertCountIndices), "expected")
os.Exit(1)
}
}

if viper.GetInt(flagAssertCountDocs) > -1 {
foundDocs := 0
jaegerServiceName := viper.GetString(flagJaegerService)
for _, index := range matchingIndices {
spans, err := index.GetServiceIndexSpans(jaegerServiceName)
if err != nil {
log.Errorln("Something failed while getting the index spans:", err)
}
foundDocs += len(spans)
}
log.Debug(foundDocs, " in ", len(matchingIndices), " indices")

if foundDocs != viper.GetInt(flagAssertCountDocs) {
log.Fatalln(foundDocs, "docs found.", viper.GetInt(flagAssertCountDocs), "expected")
os.Exit(1)
}
}

}
Loading

0 comments on commit 2505d69

Please sign in to comment.