-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmetrics_trait_defaulter_test.go
84 lines (75 loc) · 3.01 KB
/
metrics_trait_defaulter_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
// Copyright (c) 2020, 2021, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
package webhooks
import (
"encoding/json"
"testing"
"github.com/verrazzano/verrazzano/application-operator/apis/oam/v1alpha1"
oamv1 "github.com/crossplane/oam-kubernetes-runtime/apis/core/v1alpha2"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)
// TestMetricsTraitDefaulter_Default tests adding a default MetricsTrait to an appconfig
// GIVEN a AppConfigDefaulter and an appconfig
// WHEN Default is called with an appconfig
// THEN Default should add a default MetricsTrait to the appconfig
func TestMetricsTraitDefaulter_Default(t *testing.T) {
testDefaulter(t, "hello-comp.yaml", "hello-conf.yaml",
0, 1)
testDefaulter(t, "hello-comp.yaml", "hello-conf_withTrait.yaml",
1, 2)
testDefaulter(t, "hello-comp.yaml", "hello-conf_withMetricsTrait.yaml",
2, 2)
}
// TestMetricsTraitDefaulter_Cleanup tests cleaning up the default MetricsTrait on an appconfig
// GIVEN a AppConfigDefaulter and an appconfig
// WHEN Cleanup is called with an appconfig
// THEN Cleanup should run without error
func TestMetricsTraitDefaulter_Cleanup(t *testing.T) {
testMetricsTraitDefaulterCleanup(t, "hello-conf.yaml", false)
testMetricsTraitDefaulterCleanup(t, "hello-conf.yaml", true)
}
func testDefaulter(t *testing.T, componentPath, configPath string, initTraitsSize, expectedTraitsSize int) {
req := admission.Request{}
req.Object = runtime.RawExtension{Raw: readYaml2Json(t, configPath)}
decoder := decoder()
appConfig := &oamv1.ApplicationConfiguration{}
err := decoder.Decode(req, appConfig)
if err != nil {
t.Fatalf("Error in decoder.Decode %v", err)
}
assert.Equal(t, 1, len(appConfig.Spec.Components))
assert.Equal(t, initTraitsSize, len(appConfig.Spec.Components[0].Traits))
defaulter := &MetricsTraitDefaulter{}
err = defaulter.Default(appConfig, false)
if err != nil {
t.Fatalf("Error in defaulter.Default %v", err)
}
assert.Equal(t, expectedTraitsSize, len(appConfig.Spec.Components[0].Traits))
foundMetricsTrait := false
for _, trait := range appConfig.Spec.Components[0].Traits {
var rawTrait map[string]interface{}
json.Unmarshal(trait.Trait.Raw, &rawTrait)
if rawTrait["apiVersion"] == apiVersion && rawTrait["kind"] == v1alpha1.MetricsTraitKind {
foundMetricsTrait = true
}
}
assert.True(t, foundMetricsTrait)
}
func testMetricsTraitDefaulterCleanup(t *testing.T, configPath string, dryRun bool) {
req := admission.Request{}
req.Object = runtime.RawExtension{Raw: readYaml2Json(t, configPath)}
decoder := decoder()
appConfig := &oamv1.ApplicationConfiguration{}
err := decoder.Decode(req, appConfig)
if err != nil {
t.Fatalf("Error in decoder.Decode %v", err)
}
assert.Equal(t, 1, len(appConfig.Spec.Components))
defaulter := &MetricsTraitDefaulter{}
err = defaulter.Cleanup(appConfig, dryRun)
if err != nil {
t.Fatalf("Error in defaulter.Default %v", err)
}
}