forked from elastic/package-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_integration_test.go
107 lines (91 loc) · 2.54 KB
/
main_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// 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"
"github.com/stretchr/testify/require"
)
// TestSetup tests if Kibana can be run against the current registry
// and the setup command works as expected.
func TestSetup(t *testing.T) {
// Mage fetchPackageStorage is needed to pull in the packages from package-storage
err := sh.Run("mage", "fetchPackageStorage")
if err != nil {
t.Error(err)
}
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)
if err != nil {
t.Error(err)
}
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
require.NotNil(t, resp)
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))
}