11
11
import io .javaoperatorsdk .operator .ReconcilerUtils ;
12
12
import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
13
13
import io .javaoperatorsdk .operator .api .reconciler .Context ;
14
+ import io .javaoperatorsdk .operator .processing .dependent .Matcher ;
14
15
15
16
import static org .assertj .core .api .Assertions .assertThat ;
16
17
import static org .mockito .Mockito .mock ;
@@ -21,44 +22,94 @@ class GenericKubernetesResourceMatcherTest {
21
22
22
23
private static final Context context = mock (Context .class );
23
24
25
+ Deployment actual = createDeployment ();
26
+ Deployment desired = createDeployment ();
27
+ TestDependentResource dependentResource = new TestDependentResource (desired );
28
+ Matcher matcher =
29
+ GenericKubernetesResourceMatcher .matcherFor (Deployment .class , dependentResource );
30
+
24
31
@ BeforeAll
25
32
static void setUp () {
26
33
final var controllerConfiguration = mock (ControllerConfiguration .class );
27
34
when (context .getControllerConfiguration ()).thenReturn (controllerConfiguration );
28
35
}
29
36
30
37
@ Test
31
- void checksIfDesiredValuesAreTheSame () {
32
- var actual = createDeployment ();
33
- final var desired = createDeployment ();
34
- final var dependentResource = new TestDependentResource (desired );
35
- final var matcher =
36
- GenericKubernetesResourceMatcher .matcherFor (Deployment .class , dependentResource );
38
+ void matchesTrivialCases () {
37
39
assertThat (matcher .match (actual , null , context ).matched ()).isTrue ();
38
- assertThat (matcher .match (actual , null , context ).computedDesired ().isPresent ()).isTrue ();
39
- assertThat (matcher .match (actual , null , context ).computedDesired ().get ()).isEqualTo (desired );
40
+ assertThat (matcher .match (actual , null , context ).computedDesired ()).isPresent ();
41
+ assertThat (matcher .match (actual , null , context ).computedDesired ()).contains (desired );
42
+ }
40
43
44
+ @ Test
45
+ void matchesAdditiveOnlyChanges () {
41
46
actual .getSpec ().getTemplate ().getMetadata ().getLabels ().put ("new-key" , "val" );
42
47
assertThat (matcher .match (actual , null , context ).matched ())
43
- .withFailMessage ("Additive changes should be ok " )
48
+ .withFailMessage ("Additive changes should not cause a mismatch by default " )
44
49
.isTrue ();
50
+ }
45
51
52
+ @ Test
53
+ void matchesWithStrongSpecEquality () {
54
+ actual .getSpec ().getTemplate ().getMetadata ().getLabels ().put ("new-key" , "val" );
46
55
assertThat (GenericKubernetesResourceMatcher
47
- .match (dependentResource , actual , null , context , true , true ).matched ())
48
- .withFailMessage ("Strong equality does not ignore additive changes on spec" )
56
+ .match (dependentResource , actual , null , context , true , true ,
57
+ true )
58
+ .matched ())
59
+ .withFailMessage ("Adding values should fail matching when strong equality is required" )
49
60
.isFalse ();
61
+ }
50
62
63
+ @ Test
64
+ void doesNotMatchRemovedValues () {
51
65
actual = createDeployment ();
52
66
assertThat (matcher .match (actual , createPrimary ("removed" ), context ).matched ())
53
- .withFailMessage ("Removed value should not be ok " )
67
+ .withFailMessage ("Removing values in metadata should lead to a mismatch " )
54
68
.isFalse ();
69
+ }
55
70
71
+ @ Test
72
+ void doesNotMatchChangedValues () {
56
73
actual = createDeployment ();
57
74
actual .getSpec ().setReplicas (2 );
58
75
assertThat (matcher .match (actual , null , context ).matched ())
59
- .withFailMessage ("Changed values are not ok " )
76
+ .withFailMessage ("Should not have matched because values have changed " )
60
77
.isFalse ();
78
+ }
79
+
80
+ @ Test
81
+ void doesNotMatchChangedValuesWhenNoIgnoredPathsAreProvided () {
82
+ actual = createDeployment ();
83
+ actual .getSpec ().setReplicas (2 );
84
+ assertThat (GenericKubernetesResourceMatcher
85
+ .match (dependentResource , actual , null , context , true ).matched ())
86
+ .withFailMessage (
87
+ "Should not have matched because values have changed and no ignored path is provided" )
88
+ .isFalse ();
89
+ }
61
90
91
+ @ Test
92
+ void doesNotAttemptToMatchIgnoredPaths () {
93
+ actual = createDeployment ();
94
+ actual .getSpec ().setReplicas (2 );
95
+ assertThat (GenericKubernetesResourceMatcher
96
+ .match (dependentResource , actual , null , context , false , "/spec/replicas" ).matched ())
97
+ .withFailMessage ("Should not have compared ignored paths" )
98
+ .isTrue ();
99
+ }
100
+
101
+ @ Test
102
+ void ignoresWholeSubPath () {
103
+ actual = createDeployment ();
104
+ actual .getSpec ().getTemplate ().getMetadata ().getLabels ().put ("additional-key" , "val" );
105
+ assertThat (GenericKubernetesResourceMatcher
106
+ .match (dependentResource , actual , null , context , false , "/spec/template" ).matched ())
107
+ .withFailMessage ("Should match when only changes impact ignored sub-paths" )
108
+ .isTrue ();
109
+ }
110
+
111
+ @ Test
112
+ void matchesMetadata () {
62
113
actual = new DeploymentBuilder (createDeployment ())
63
114
.editOrNewMetadata ()
64
115
.addToAnnotations ("test" , "value" )
@@ -70,9 +121,16 @@ void checksIfDesiredValuesAreTheSame() {
70
121
.isTrue ();
71
122
72
123
assertThat (GenericKubernetesResourceMatcher
73
- .match (dependentResource , actual , null , context , true ).matched ())
124
+ .match (dependentResource , actual , null , context , true , true , true ).matched ())
74
125
.withFailMessage ("Annotations should matter when metadata is considered" )
75
126
.isFalse ();
127
+
128
+ assertThat (GenericKubernetesResourceMatcher
129
+ .match (dependentResource , actual , null , context , true , false ).matched ())
130
+ .withFailMessage (
131
+ "Should match when strong equality is not considered and only additive changes are made" )
132
+ .isTrue ();
133
+
76
134
}
77
135
78
136
Deployment createDeployment () {
0 commit comments