Skip to content

Commit

Permalink
Adding agent integration tests for DptoCp flow
Browse files Browse the repository at this point in the history
  • Loading branch information
O-sura committed May 9, 2024
1 parent a6501c8 commit e7185e4
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package org.wso2.apk.integration.api;

import com.google.common.io.Resources;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.testng.Assert;
import org.wso2.apk.integration.utils.Utils;
import org.wso2.apk.integration.utils.clients.SimpleHTTPClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class K8ResourceGenerateSteps {

private final SharedContext sharedContext;
private static final Log logger = LogFactory.getLog(BaseSteps.class);
private File definitionFile;
private File apkConfFile;

public K8ResourceGenerateSteps(SharedContext sharedContext) {
this.sharedContext = sharedContext;
}

@When("I use the definition file {string}")
public void i_use_the_definition_file(String definitionFilePath) {

URL url = Resources.getResource(definitionFilePath);
definitionFile = new File(url.getPath());
}

@When("I use the apk conf file {string} in resources")
public void i_use_the_apkconf_file_in_resources(String confFilePath) {

URL url = Resources.getResource(confFilePath);
apkConfFile = new File(url.getPath());
}

@When("I generate and apply the K8Artifacts belongs to that API")
public void generate_the_k8artifacts_set() throws Exception {

// Create a MultipartEntityBuilder to build the request entity
MultipartEntityBuilder builder = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addPart("apkConfiguration", new FileBody(apkConfFile))
.addPart("definitionFile", new FileBody(definitionFile));

HttpEntity multipartEntity = builder.build();
HttpResponse httpResponse = sharedContext.getHttpClient().doPostWithMultipart(Utils.getK8ResourceGeneratorURL(),
multipartEntity);

sharedContext.setResponse(httpResponse);

// Process the HTTP response
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
try (InputStream inputStream = entity.getContent()) {
// Create a temporary file and store the zip content
File tempFile = File.createTempFile("k8s_artifacts", ".zip");
try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
inputStream.transferTo(outputStream);
logger.info("Data transformation complete");
}

// Process the zip file
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(tempFile))) {
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
if (!entry.isDirectory()) {
if (entry.getName().endsWith(".yaml") || entry.getName().endsWith(".yml")) {
// Create a temporary file and store the YAML content into it
File yamlFile = File.createTempFile("k8s_yaml", ".yaml");
try (FileOutputStream yamlOutputStream = new FileOutputStream(yamlFile)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = zipInputStream.read(buffer)) != -1) {
yamlOutputStream.write(buffer, 0, bytesRead);
}
}

String command = "kubectl apply -f " + yamlFile.getAbsolutePath() + " -n apk";
Process process = Runtime.getRuntime().exec(command);
int exitCode = process.waitFor();
if (exitCode != 0) {
logger.error("Error applying YAML file: " + entry.getName());
} else {
logger.info("File: " + entry.getName() + " applied successfully.");
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
logger.info("Failed to generate K8s artifacts. HTTP status code: " + httpResponse.getStatusLine().getStatusCode());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static String getConfigGeneratorURL() {
+ Constants.DEFAULT_API_CONFIGURATOR + "apis/generate-configuration";
}

public static String getK8ResourceGeneratorURL() {
return "https://" + Constants.DEFAULT_API_HOST + ":" + Constants.DEFAULT_GW_PORT + "/"
+ Constants.DEFAULT_API_CONFIGURATOR + "apis/generate-k8s-resources?organization=carbon.super";
}

public static String getTokenEndpointURL() {

return "https://" + Constants.DEFAULT_IDP_HOST + ":" + Constants.DEFAULT_GW_PORT + "/"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Feature: Endpoint
Scenario: Testing API level and resource level endpoints
Given The system is ready
And I have a valid subscription
When I use the apk conf file "artifacts/apk-confs/endpoint_conf.yaml" in resources
And I use the definition file "artifacts/definitions/employees_api.json"
Then I generate and apply the K8Artifacts belongs to that API
Then I wait for 40 seconds
And I have a DCR application
And I have a valid Publisher access token
Then I find the apiUUID of the API created with the name "APIResourceEndpoint"
Then the response status code should be 200
And the response body should contain "APIResourceEndpoint"
And make the Change Lifecycle request
Then the response status code should be 200
And I have a valid Devportal access token
And make the Application Creation request with the name "SampleApp"
Then the response status code should be 201
And the response body should contain "SampleApp"
And I have a KeyManager
And make the Generate Keys request
Then the response status code should be 200
And the response body should contain "consumerKey"
And the response body should contain "consumerSecret"
And make the Subscription request
Then the response status code should be 201
And the response body should contain "Unlimited"
And I get "production" oauth keys for application
Then the response status code should be 200
And make the Access Token Generation request for "production"
Then the response status code should be 200
And the response body should contain "accessToken"
Then I set headers
|Authorization|bearer ${accessToken}|
And I send "GET" request to "https://carbon.super.gw.wso2.com:9095/endpoint/3.14/employee" with body ""
And I eventually receive 200 response code, not accepting
|429|
And the response body should contain "https://backend/anything/employee"
And I send "POST" request to "https://carbon.super.gw.wso2.com:9095/endpoint/3.14/employee" with body ""
And I eventually receive 200 response code, not accepting
|429|
And the response body should contain "https://backend/anything/test/employee"


Scenario Outline: Undeploy API
Given The system is ready
And I have a valid subscription
When I undeploy the API whose ID is "<apiID>"
Then the response status code should be <expectedStatusCode>
And I have a DCR application
And I have a valid Devportal access token
Then I delete the application "SampleApp" from devportal
Then the response status code should be 200
And I have a valid Publisher access token
Then I find the apiUUID of the API created with the name "APIResourceEndpoint"
Then I undeploy the selected API
Then the response status code should be 200

Examples:
| apiID | expectedStatusCode |
| endpoint-test | 202 |

0 comments on commit e7185e4

Please sign in to comment.