forked from elastic/cloud-on-k8s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samples_test.go
100 lines (88 loc) · 3.52 KB
/
samples_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
// 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 mixed e2e
package e2e
import (
"bufio"
"os"
"path/filepath"
"testing"
commonv1 "github.com/elastic/cloud-on-k8s/pkg/apis/common/v1"
"github.com/elastic/cloud-on-k8s/test/e2e/cmd/run"
"github.com/elastic/cloud-on-k8s/test/e2e/test"
"github.com/elastic/cloud-on-k8s/test/e2e/test/apmserver"
"github.com/elastic/cloud-on-k8s/test/e2e/test/elasticsearch"
"github.com/elastic/cloud-on-k8s/test/e2e/test/enterprisesearch"
"github.com/elastic/cloud-on-k8s/test/e2e/test/helper"
"github.com/elastic/cloud-on-k8s/test/e2e/test/kibana"
"github.com/stretchr/testify/require"
"k8s.io/apimachinery/pkg/util/rand"
)
func TestSamples(t *testing.T) {
sampleFiles, err := filepath.Glob("../../config/samples/*/*.yaml")
require.NoError(t, err, "Failed to find samples")
decoder := helper.NewYAMLDecoder()
for _, sample := range sampleFiles {
testName := helper.MkTestName(t, sample)
builders := createBuilders(t, decoder, sample, testName)
t.Run(testName, func(t *testing.T) {
test.Sequence(nil, test.EmptySteps, builders...).RunSequential(t)
})
}
}
func createBuilders(t *testing.T, decoder *helper.YAMLDecoder, sampleFile, testName string) []test.Builder {
t.Helper()
f, err := os.Open(sampleFile)
require.NoError(t, err, "Failed to open file %s", sampleFile)
defer f.Close()
namespace := test.Ctx().ManagedNamespace(0)
suffix := rand.String(4)
transform := func(builder test.Builder) test.Builder {
fullTestName := "TestSamples-" + testName
switch b := builder.(type) {
case elasticsearch.Builder:
return b.WithNamespace(namespace).
WithSuffix(suffix).
WithRestrictedSecurityContext().
WithLabel(run.TestNameLabel, fullTestName).
WithPodLabel(run.TestNameLabel, fullTestName)
case kibana.Builder:
return b.WithNamespace(namespace).
WithSuffix(suffix).
WithElasticsearchRef(tweakServiceRef(b.Kibana.Spec.ElasticsearchRef, suffix)).
WithRestrictedSecurityContext().
WithLabel(run.TestNameLabel, fullTestName).
WithPodLabel(run.TestNameLabel, fullTestName)
case apmserver.Builder:
return b.WithNamespace(namespace).
WithSuffix(suffix).
WithElasticsearchRef(tweakServiceRef(b.ApmServer.Spec.ElasticsearchRef, suffix)).
WithKibanaRef(tweakServiceRef(b.ApmServer.Spec.KibanaRef, suffix)).
WithConfig(map[string]interface{}{"apm-server.ilm.enabled": false}).
WithRestrictedSecurityContext().
WithLabel(run.TestNameLabel, fullTestName).
WithPodLabel(run.TestNameLabel, fullTestName)
case enterprisesearch.Builder:
return b.WithNamespace(namespace).
WithSuffix(suffix).
WithElasticsearchRef(tweakServiceRef(b.EnterpriseSearch.Spec.ElasticsearchRef, suffix)).
WithRestrictedSecurityContext().
WithLabel(run.TestNameLabel, fullTestName).
WithPodLabel(run.TestNameLabel, fullTestName)
default:
return b
}
}
builders, err := decoder.ToBuilders(bufio.NewReader(f), transform)
require.NoError(t, err, "Failed to create builders")
return builders
}
func tweakServiceRef(ref commonv1.ObjectSelector, suffix string) commonv1.ObjectSelector {
// All the objects defined in the YAML file will have a random test suffix added to prevent clashes with previous runs.
// This necessitates changing the Elasticsearch reference to match the suffixed name.
if ref.Name != "" {
ref.Name = ref.Name + "-" + suffix
}
return ref
}