summaryrefslogtreecommitdiffstats
path: root/tests/environment_test.go
blob: b4cedbccbf4fa43a2fa258a072fdd72a1538f227 (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
package icingadb_test

import (
	"encoding/hex"
	"encoding/json"
	"github.com/icinga/icinga-testing/services"
	"github.com/icinga/icinga-testing/utils/eventually"
	"github.com/jmoiron/sqlx"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"golang.org/x/sync/errgroup"
	"sort"
	"testing"
	"time"
)

func TestMultipleEnvironments(t *testing.T) {
	rdb := getDatabase(t)

	numEnvs := 3
	icinga2Instances := make([]services.Icinga2, numEnvs)

	// Start numEnvs icinga2 instances with an icingadb instance each, all writing to the same SQL database.
	var g errgroup.Group
	for i := range icinga2Instances {
		i := i

		g.Go(func() error {
			r := it.RedisServerT(t)
			icinga2Instances[i] = it.Icinga2NodeT(t, "master")
			icinga2Instances[i].EnableIcingaDb(r)
			icinga2Instances[i].Reload()
			it.IcingaDbInstanceT(t, r, rdb)
			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 := sqlx.Open(rdb.Driver(), rdb.DSN())
	require.NoError(t, err, "SQL database open")
	t.Cleanup(func() { _ = db.Close() })

	t.Run("Table", func(t *testing.T) {
		t.Parallel()

		eventually.Assert(t, func(t require.TestingT) {
			var query string
			switch rdb.Driver() {
			case "mysql":
				query = "SELECT LOWER(HEX(id)), name FROM environment ORDER BY id"
			case "postgres":
				query = "SELECT LOWER(ENCODE(id, 'hex')), name FROM environment ORDER BY id"
			default:
				panic("unknown database driver")
			}
			rows, err := db.Query(query)
			require.NoError(t, err, "SQL query")
			defer rows.Close()

			var gotEnvs []string
			for rows.Next() {
				var id, name string
				err := rows.Scan(&id, &name)
				require.NoError(t, err, "SQL scan")
				require.Equal(t, id, name, "name should be initialized to the environment id")
				gotEnvs = append(gotEnvs, id)
			}

			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()

		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, "SQL query")
			defer rows.Close()

			numRows := 0
			for rows.Next() {
				var env []byte
				var count int
				err := rows.Scan(&env, &count)
				require.NoError(t, err, "SQL scan")

				assert.LessOrEqualf(t, count, 1,
					"environment %s must have at most one active instance", hex.EncodeToString(env))
				numRows++
			}
			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"`
}