1+ // Copyright (c) 2021, Oracle and/or its affiliates.
2+ // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+ package oracle .weblogic .kubernetes ;
5+
6+ import java .util .ArrayList ;
7+ import java .util .HashMap ;
8+ import java .util .List ;
9+ import java .util .Map ;
10+
11+ import io .kubernetes .client .openapi .models .V1LoadBalancerIngress ;
12+ import io .kubernetes .client .openapi .models .V1Service ;
13+ import oracle .weblogic .kubernetes .actions .impl .primitive .Kubernetes ;
14+ import oracle .weblogic .kubernetes .annotations .IntegrationTest ;
15+ import oracle .weblogic .kubernetes .annotations .Namespaces ;
16+ import oracle .weblogic .kubernetes .logging .LoggingFacade ;
17+ import org .awaitility .core .ConditionFactory ;
18+ import org .junit .jupiter .api .AfterAll ;
19+ import org .junit .jupiter .api .BeforeAll ;
20+ import org .junit .jupiter .api .DisplayName ;
21+ import org .junit .jupiter .api .Test ;
22+
23+ import static java .util .concurrent .TimeUnit .MINUTES ;
24+ import static java .util .concurrent .TimeUnit .SECONDS ;
25+ import static oracle .weblogic .kubernetes .TestConstants .ADMIN_SERVER_NAME_BASE ;
26+ import static oracle .weblogic .kubernetes .TestConstants .MANAGED_SERVER_NAME_BASE ;
27+ import static oracle .weblogic .kubernetes .TestConstants .MII_BASIC_IMAGE_NAME ;
28+ import static oracle .weblogic .kubernetes .TestConstants .MII_BASIC_IMAGE_TAG ;
29+ import static oracle .weblogic .kubernetes .assertions .impl .Kubernetes .getService ;
30+ import static oracle .weblogic .kubernetes .utils .CommonMiiTestUtils .createMiiDomainAndVerify ;
31+ import static oracle .weblogic .kubernetes .utils .CommonTestUtils .createOcirRepoSecret ;
32+ import static oracle .weblogic .kubernetes .utils .CommonTestUtils .installAndVerifyOCILoadBalancer ;
33+ import static oracle .weblogic .kubernetes .utils .CommonTestUtils .installAndVerifyOperator ;
34+ import static oracle .weblogic .kubernetes .utils .TestUtils .callWebAppAndCheckForServerNameInResponse ;
35+ import static oracle .weblogic .kubernetes .utils .ThreadSafeLogger .getLogger ;
36+ import static org .assertj .core .api .Assertions .assertThat ;
37+ import static org .awaitility .Awaitility .with ;
38+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
39+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
40+
41+ /**
42+ * Verify OCI Load Balancer is installed and running.
43+ * Verify sample-war web application be accessed via OCI LoadBalancer.
44+ * Verify Load Balancing between two managed servers in the cluster
45+ */
46+ @ DisplayName ("Verify the sample-app app can be accessed from "
47+ + "all managed servers in the domain through OCI Load Balancer" )
48+ @ IntegrationTest
49+ class ItOCILoadBalancer {
50+ // domain constants
51+ private static final int replicaCount = 2 ;
52+ private static int managedServersCount = 2 ;
53+ private static String domainNamespace = null ;
54+ private static String domainUid = "lboci-domain" ;
55+ private static ConditionFactory withStandardRetryPolicy = null ;
56+
57+ // constants for creating domain image using model in image
58+ private static final String SAMPLE_APP_NAME = "sample-app" ;
59+ private static String clusterName = "cluster-1" ;
60+ private static LoggingFacade logger = null ;
61+ private static String loadBalancerIP = null ;
62+ private static final String OCI_LB_NAME = "ocilb" ;
63+
64+ /**
65+ * Install and verify operator.
66+ *
67+ * @param namespaces list of namespaces created by the IntegrationTestWatcher by the
68+ * JUnit engine parameter resolution mechanism
69+ */
70+ @ BeforeAll
71+ public static void initAll (@ Namespaces (2 ) List <String > namespaces ) {
72+
73+ logger = getLogger ();
74+ // create standard, reusable retry/backoff policy
75+ withStandardRetryPolicy = with ().pollDelay (2 , SECONDS )
76+ .and ().with ().pollInterval (10 , SECONDS )
77+ .atMost (5 , MINUTES ).await ();
78+
79+ logger .info ("Get a unique namespace for operator" );
80+ assertNotNull (namespaces .get (0 ), "Namespace list is null" );
81+ final String opNamespace = namespaces .get (0 );
82+
83+ logger .info ("Get a unique namespace for WebLogic domain1" );
84+ assertNotNull (namespaces .get (1 ), "Namespace list is null" );
85+ domainNamespace = namespaces .get (1 );
86+
87+ logger .info ("install and verify operator" );
88+ installAndVerifyOperator (opNamespace , domainNamespace );
89+ }
90+
91+ @ AfterAll
92+ public void tearDownAll () {
93+ if (System .getenv ("SKIP_CLEANUP" ) == null
94+ || (System .getenv ("SKIP_CLEANUP" ) != null
95+ && System .getenv ("SKIP_CLEANUP" ).equalsIgnoreCase ("false" ))) {
96+ Kubernetes .deleteService (OCI_LB_NAME , domainNamespace );
97+ }
98+ }
99+
100+ /**
101+ * Test covers basic functionality for OCI LoadBalancer .
102+ * Create domain and OCI LoadBalancer.
103+ * Check that application is accessabale via OCI LoadBalancer
104+ */
105+ @ Test
106+ @ DisplayName ("Test the sample-app app can be accessed"
107+ + " from all managed servers in the domain through OCI Load Balancer." )
108+ public void testOCILoadBalancer () throws Exception {
109+
110+ // create and verify one cluster mii domain
111+ logger .info ("Create domain and verify that it's running" );
112+
113+ // create docker registry secret to pull the image from registry
114+ // this secret is used only for non-kind cluster
115+ logger .info ("Create docker registry secret in namespace {0}" , domainNamespace );
116+ createOcirRepoSecret (domainNamespace );
117+ String adminServerPodName = domainUid + "-" + ADMIN_SERVER_NAME_BASE ;
118+ String managedServerPrefix = domainUid + "-" + MANAGED_SERVER_NAME_BASE ;
119+ createMiiDomainAndVerify (domainNamespace ,domainUid ,
120+ MII_BASIC_IMAGE_NAME + ":" + MII_BASIC_IMAGE_TAG , adminServerPodName ,
121+ managedServerPrefix ,replicaCount );
122+
123+ int clusterHttpPort = 8001 ;
124+
125+ assertDoesNotThrow (() -> installAndVerifyOCILoadBalancer (domainNamespace ,
126+ clusterHttpPort , clusterName , domainUid , OCI_LB_NAME ),
127+ "Installation of OCI Load Balancer failed" );
128+ loadBalancerIP = getLoadBalancerIP (domainNamespace ,OCI_LB_NAME );
129+ assertNotNull (loadBalancerIP , "External IP for Load Balancer is undefined" );
130+ logger .info ("LoadBalancer IP is " + loadBalancerIP );
131+ verifyWebAppAccessThroughOCILoadBalancer (loadBalancerIP , 2 , clusterHttpPort );
132+ }
133+
134+ /**
135+ * Retreive external IP from OCI LoadBalancer.
136+ */
137+ private static String getLoadBalancerIP (String namespace , String lbName ) throws Exception {
138+ Map <String , String > labels = new HashMap <>();
139+ labels .put ("loadbalancer" , lbName );
140+ V1Service service = getService (lbName , labels , namespace );
141+ assertNotNull (service , "Can't find service with name " + lbName );
142+ logger .info ("Found service with name {0} in {1} namespace " , lbName , namespace );
143+ List <V1LoadBalancerIngress > ingress = service .getStatus ().getLoadBalancer ().getIngress ();
144+ if (ingress != null ) {
145+ logger .info ("LoadBalancer Ingress " + ingress .toString ());
146+ V1LoadBalancerIngress lbIng = ingress .stream ().filter (c ->
147+ !c .getIp ().equals ("pending" )
148+ ).findAny ().orElse (null );
149+ if (lbIng != null ) {
150+ logger .info ("OCI LoadBalancer is created with external ip" + lbIng .getIp ());
151+ return lbIng .getIp ();
152+ }
153+ }
154+ return null ;
155+ }
156+
157+ /**
158+ * Verify the sample-app app can be accessed from all managed servers in the domain through OCI Load Balancer.
159+ */
160+ private void verifyWebAppAccessThroughOCILoadBalancer (String lbIp , int replicaCount , int httpport ) {
161+
162+ List <String > managedServerNames = new ArrayList <>();
163+ for (int i = 1 ; i <= replicaCount ; i ++) {
164+ managedServerNames .add (MANAGED_SERVER_NAME_BASE + i );
165+ }
166+
167+ // check that NGINX can access the sample apps from all managed servers in the domain
168+ String curlCmd =
169+ String .format ("curl --silent --show-error --noproxy '*' http://%s:%s/sample-war/index.jsp" ,
170+ lbIp ,
171+ httpport );
172+ assertThat (callWebAppAndCheckForServerNameInResponse (curlCmd , managedServerNames , 50 ))
173+ .as ("Verify OCI LB can access the sample-war app "
174+ + "from all managed servers in the domain via http" )
175+ .withFailMessage ("OCI LB can not access the the sample-war app "
176+ + "from one or more of the managed servers via http" )
177+ .isTrue ();
178+ }
179+ }
0 commit comments