No longer skip environment tests and adapt them to new behavior

This commit is contained in:
Julian Brost 2021-10-14 11:31:49 +02:00
parent 3342191b5e
commit aedfa8aeba

View file

@ -1,12 +1,14 @@
package icingadb_test
import (
"bytes"
"encoding/hex"
"fmt"
"encoding/json"
"github.com/icinga/icinga-testing/services"
"github.com/icinga/icinga-testing/utils/eventually"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"sort"
"testing"
"time"
)
@ -15,26 +17,42 @@ func TestMultipleEnvironments(t *testing.T) {
m := it.MysqlDatabaseT(t)
m.ImportIcingaDbSchema()
envs := []string{"", "some-env", "other-env"}
numEnvs := 3
icinga2Instances := make([]services.Icinga2, numEnvs)
// Start numEnvs icinga2 instances with an icingadb instance each, all writing to the same mysql database.
var g errgroup.Group
for _, env := range envs {
env := env
for i := range icinga2Instances {
i := i
g.Go(func() error {
r := it.RedisServerT(t)
i := it.Icinga2NodeT(t, "master")
conf := bytes.NewBuffer(nil)
_, _ = fmt.Fprintf(conf, "const Environment = %q\n", env)
i.WriteConfig("etc/icinga2/conf.d/testdata.conf", conf.Bytes())
i.EnableIcingaDb(r)
i.Reload()
icinga2Instances[i] = it.Icinga2NodeT(t, "master")
icinga2Instances[i].EnableIcingaDb(r)
icinga2Instances[i].Reload()
it.IcingaDbInstanceT(t, r, m)
return nil
})
}
_ = g.Wait()
// Query the IcingaDB environment_id from each icinga2 instance.
var expectedEnvs []string
for _, instance := range icinga2Instances {
res, err := instance.ApiClient().GetJson("/v1/objects/icingadbs")
require.NoError(t, err, "requesting IcingaDB objects from API should succeed")
var objects ObjectsIcingaDBsResponse
err = json.NewDecoder(res.Body).Decode(&objects)
require.NoError(t, err, "requesting IcingaDB objects from API should succeed")
require.NotEmpty(t, objects.Results, "API response should return an IcingaDB object")
expectedEnvs = append(expectedEnvs, objects.Results[0].Attrs.EnvironmentId)
}
sort.Strings(expectedEnvs)
for i := 0; i < len(expectedEnvs)-1; i++ {
require.NotEqual(t, expectedEnvs[i], expectedEnvs[i+1], "all environment IDs should be distinct")
}
db, err := m.Open()
require.NoError(t, err, "mysql open")
t.Cleanup(func() { _ = db.Close() })
@ -42,39 +60,28 @@ func TestMultipleEnvironments(t *testing.T) {
t.Run("Table", func(t *testing.T) {
t.Parallel()
// TODO(jb): needs fix for issue #292
t.Skip("environment table is currently not populated")
assert.Eventually(t, func() bool {
expectedRows := make(map[string]struct{})
for _, env := range envs {
expectedRows[env] = struct{}{}
}
rows, err := db.Query("SELECT id, name FROM environment")
eventually.Assert(t, func(t require.TestingT) {
rows, err := db.Query("SELECT LOWER(HEX(id)), name FROM environment ORDER BY id")
require.NoError(t, err, "mysql query")
defer rows.Close()
var gotEnvs []string
for rows.Next() {
var id, name string
err := rows.Scan(&id, &name)
require.NoError(t, err, "mysql scan")
if _, ok := expectedRows[name]; ok {
delete(expectedRows, name)
} else {
return false
}
require.Equal(t, id, name, "name should be initialized to the environment id")
gotEnvs = append(gotEnvs, id)
}
return len(expectedRows) == 0
}, 20*time.Second, 1*time.Second, "there should be one row in the environments tables for each one")
require.Equal(t, expectedEnvs, gotEnvs, "each environment should be present in the environments table")
}, 20*time.Second, 250*time.Millisecond)
})
t.Run("HA", func(t *testing.T) {
t.Parallel()
assert.Eventually(t, func() bool {
eventually.Assert(t, func(t require.TestingT) {
rows, err := db.Query("SELECT environment_id, COUNT(*) FROM icingadb_instance WHERE responsible = 'y' GROUP BY environment_id")
require.NoError(t, err, "mysql query")
defer rows.Close()
@ -90,7 +97,15 @@ func TestMultipleEnvironments(t *testing.T) {
"environment %s must have at most one active instance", hex.EncodeToString(env))
numRows++
}
return numRows == len(envs)
}, 20*time.Second, 1*time.Second, "there should be one active instance per environment")
require.Equal(t, numEnvs, numRows, "each environment should have one active instance")
}, 20*time.Second, 250*time.Millisecond)
})
}
type ObjectsIcingaDBsResponse struct {
Results []struct {
Attrs struct {
EnvironmentId string `json:"environment_id"`
} `json:"attrs"`
} `json:"results"`
}