44import com .github .containersolutions .operator .sample .TestCustomResourceSpec ;
55import io .fabric8 .kubernetes .api .model .ConfigMap ;
66import io .fabric8 .kubernetes .api .model .ObjectMetaBuilder ;
7- import org .junit .jupiter .api .BeforeAll ;
8- import org .junit .jupiter .api .BeforeEach ;
97import org .junit .jupiter .api .Test ;
108import org .junit .jupiter .api .TestInstance ;
119import org .slf4j .Logger ;
1513
1614import static com .github .containersolutions .operator .IntegrationTestSupport .TEST_NAMESPACE ;
1715import static org .assertj .core .api .Assertions .assertThat ;
18- import static org .assertj .core .api .Assertions .fail ;
1916import static org .awaitility .Awaitility .await ;
2017
2118@ TestInstance (TestInstance .Lifecycle .PER_CLASS )
@@ -24,51 +21,69 @@ public class ControllerExecutionIT {
2421 private final static Logger log = LoggerFactory .getLogger (ControllerExecutionIT .class );
2522 private IntegrationTestSupport integrationTestSupport = new IntegrationTestSupport ();
2623
27- @ BeforeAll
28- public void setup () {
29- integrationTestSupport .initialize ();
30- }
31-
32- @ BeforeEach
33- public void cleanup () {
24+ public void initAndCleanup (boolean controllerStatusUpdate ) {
25+ integrationTestSupport .initialize (controllerStatusUpdate );
3426 integrationTestSupport .cleanup ();
3527 }
3628
3729 @ Test
38- public void configMapGetsCreatedForTestCustomResource () throws Exception {
30+ public void configMapGetsCreatedForTestCustomResource () {
31+ initAndCleanup (true );
3932 integrationTestSupport .teardownIfSuccess (() -> {
40- TestCustomResource resource = new TestCustomResource ();
41- resource .setMetadata (new ObjectMetaBuilder ()
42- .withName ("test-custom-resource" )
43- .withNamespace (TEST_NAMESPACE )
44- .build ());
45- resource .setKind ("CustomService" );
46- resource .setSpec (new TestCustomResourceSpec ());
47- resource .getSpec ().setConfigMapName ("test-config-map" );
48- resource .getSpec ().setKey ("test-key" );
49- resource .getSpec ().setValue ("test-value" );
33+ TestCustomResource resource = testCustomResource ();
34+
5035 integrationTestSupport .getCrOperations ().inNamespace (TEST_NAMESPACE ).create (resource );
5136
52- await ("configmap created" ).atMost (5 , TimeUnit .SECONDS )
53- .untilAsserted (() -> {
54- ConfigMap configMap = integrationTestSupport .getK8sClient ().configMaps ().inNamespace (TEST_NAMESPACE )
55- .withName ("test-config-map" ).get ();
56- assertThat (configMap ).isNotNull ();
57- assertThat (configMap .getData ().get ("test-key" )).isEqualTo ("test-value" );
58- });
59- await ("cr status updated" ).atMost (5 , TimeUnit .SECONDS )
60- .untilAsserted (() -> {
61- TestCustomResource cr = integrationTestSupport .getCrOperations ().inNamespace (TEST_NAMESPACE ).withName ("test-custom-resource" ).get ();
62- assertThat (cr ).isNotNull ();
63- assertThat (cr .getStatus ()).isNotNull ();
64- assertThat (cr .getStatus ().getConfigMapStatus ()).isEqualTo ("ConfigMap Ready" );
65- });
37+ awaitResourcesCreatedOrUpdated ();
38+ awaitStatusUpdated ();
6639 });
6740 }
6841
6942 @ Test
7043 public void eventIsSkippedChangedOnMetadataOnlyUpdate () {
71- fail ("TODO" );
44+ initAndCleanup (false );
45+ integrationTestSupport .teardownIfSuccess (() -> {
46+ TestCustomResource resource = testCustomResource ();
47+
48+ integrationTestSupport .getCrOperations ().inNamespace (TEST_NAMESPACE ).create (resource );
49+
50+ awaitResourcesCreatedOrUpdated ();
51+ assertThat (integrationTestSupport .numberOfControllerExecutions ()).isEqualTo (1 );
52+ });
53+ }
54+
55+ void awaitResourcesCreatedOrUpdated () {
56+ await ("configmap created" ).atMost (5 , TimeUnit .SECONDS )
57+ .untilAsserted (() -> {
58+ ConfigMap configMap = integrationTestSupport .getK8sClient ().configMaps ().inNamespace (TEST_NAMESPACE )
59+ .withName ("test-config-map" ).get ();
60+ assertThat (configMap ).isNotNull ();
61+ assertThat (configMap .getData ().get ("test-key" )).isEqualTo ("test-value" );
62+ });
63+ }
64+
65+ void awaitStatusUpdated () {
66+ await ("cr status updated" ).atMost (5 , TimeUnit .SECONDS )
67+ .untilAsserted (() -> {
68+ TestCustomResource cr = integrationTestSupport .getCrOperations ().inNamespace (TEST_NAMESPACE ).withName ("test-custom-resource" ).get ();
69+ assertThat (cr ).isNotNull ();
70+ assertThat (cr .getStatus ()).isNotNull ();
71+ assertThat (cr .getStatus ().getConfigMapStatus ()).isEqualTo ("ConfigMap Ready" );
72+ });
73+ }
74+
75+ private TestCustomResource testCustomResource () {
76+ TestCustomResource resource = new TestCustomResource ();
77+ resource .setMetadata (new ObjectMetaBuilder ()
78+ .withName ("test-custom-resource" )
79+ .withNamespace (TEST_NAMESPACE )
80+ .build ());
81+ resource .setKind ("CustomService" );
82+ resource .setSpec (new TestCustomResourceSpec ());
83+ resource .getSpec ().setConfigMapName ("test-config-map" );
84+ resource .getSpec ().setKey ("test-key" );
85+ resource .getSpec ().setValue ("test-value" );
86+ return resource ;
7287 }
7388
7489}
0 commit comments