Skip to content

Commit

Permalink
Basic integration test with Kibana (#493)
Browse files Browse the repository at this point in the history
This is a simple integration test to validate changes made against the registry are still working with the current version of Kibana.
  • Loading branch information
ruflin authored Jun 9, 2020
1 parent 955239d commit e56da29
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
22 changes: 22 additions & 0 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,28 @@ pipeline {
}
}
}
/**
Execute integration tests.
*/
stage('TestIntegration') {
environment {
HOME = "${env.WORKSPACE}"
PATH = "${env.HOME}/go/bin:${env.PATH}"
}
steps {
deleteDir()
unstash 'source'
dir("${BASE_DIR}"){
sh(label: 'Runs the (integration) tests',script: '''
eval "$(gvm 1.14.2)"
go get -u github.com/magefile/mage
go get -u github.com/elastic/go-licenser
go get -u -v golang.org/x/tools/cmd/goimports
mage -debug testIntegration
''')
}
}
}
/**
Publish Docker images.
*/
Expand Down
4 changes: 4 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func Test() error {
return sh.RunV("go", "test", "./...", "-v", "2>&1", "|", "go-junit-report", ">", "junit-report.xml")
}

func TestIntegration() error {
return sh.RunV("go", "test", "./...", "-v", "-tags=integration", "2>&1", "|", "go-junit-report", ">", "junit-report.xml")
}

// Format adds license headers, formats .go files with goimports, and formats
// .py files with autopep8.
func Format() {
Expand Down
92 changes: 92 additions & 0 deletions main_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

// +build integration

package main

import (
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"testing"
"time"

"github.com/magefile/mage/sh"
"github.com/stretchr/testify/assert"
)

// TestSetup tests if Kibana can be run against the current registry
// and the setup command works as expected.
func TestSetup(t *testing.T) {

currentDir, err := os.Getwd()
if err != nil {
t.Error(err)
}
defer os.Chdir(currentDir)
err = os.Chdir("testing/environments")
if err != nil {
t.Error(err)
}

// Make sure services are shut down again at the end of the test
defer func() {
err = sh.Run("docker-compose", "-f", "snapshot.yml", "-f", "local.yml", "down", "-v")
if err != nil {
t.Error(err)
}

}()
// Spin up services
go func() {
err = sh.Run("docker-compose", "-f", "snapshot.yml", "pull")
if err != nil {
t.Error(err)
}

err = sh.Run("docker-compose", "-f", "snapshot.yml", "-f", "local.yml", "up", "--force-recreate", "--remove-orphans", "--build")
if err != nil {
t.Error(err)
}
}()

// Check for 5 minutes if service is available
for i := 0; i < 5*60; i++ {
output, _ := sh.Output("docker-compose", "-f", "snapshot.yml", "-f", "local.yml", "ps")
if err != nil {
// Log errors but do not act on it as at first it might not be ready yet
log.Println(err)
}
// 3 services must report healthy
c := strings.Count(output, "healthy")
if c == 3 {
break
}

// Wait 1 second between each iteration
time.Sleep(1 * time.Second)
}

// Run setup in ingest_manager against registry to see if no errors are returned
req, err := http.NewRequest("POST", "http://elastic:changeme@localhost:5601/api/ingest_manager/setup", nil)
if err != nil {
t.Error(err)
}
req.Header.Add("kbn-xsrf", "ingest_manager")
resp, err := http.DefaultClient.Do(req)
defer resp.Body.Close()

assert.Equal(t, 200, resp.StatusCode)

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}

// Leaving this in as it could become useful for debugging purpose
log.Println(string(body))
}
2 changes: 2 additions & 0 deletions testing/environments/local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ services:
- "127.0.0.1:9200:9200"

integrations-registry:
build: ../..
image: docker.elastic.co/package-registry/package-registry:master
ports:
- "127.0.0.1:8080:8080"

0 comments on commit e56da29

Please sign in to comment.