Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add new methods according to new approach of keploy serve #159

Merged
merged 2 commits into from
Dec 8, 2023

Conversation

gouravkrosx
Copy link
Member

@gouravkrosx gouravkrosx commented Nov 28, 2023

Related Issue

  • Since the keploy serve command has changed a bit, the SDK also requires some changes to support new implementations.

Closes: keploy/keploy#1139

Describe the changes you've made

  • Now keploy serve command takes a command to run unit test file like mvn test, To support this in JUnit tests, made some changes like running user application along with jacoco agent, dumping coverage executable file, stopping user application. The command to run junit tests along keploy sudo -E keploy serve -c "mvn test" --delay 15 --passThroughPorts 36320
    This I how it should be used in unit test file, Just add this test in your unit test. & run the above serve command.
@Test
    @Order(Integer.MAX_VALUE)
    public void TestKeploy() throws IOException, InterruptedException {

        Boolean testResult = true;
        long MAX_TIMEOUT = 60000; // 1m
        try {

            String[] testSets = KeployCLI.FetchTestSets();
            if (testSets == null) {
                System.err.println("Test sets are null ");
                return;
            }

            System.out.println("TestSets: " + Arrays.asList(testSets));

            System.out.println("starting user application");

            boolean result = true;
            for (String testset : testSets) {

                // running the test set.
                String testRunId = KeployCLI.RunTestSet(testset);

               
                String jarPath = "target/spring-boot-data-mongodb-0.0.1-SNAPSHOT.jar";
                String[] command = {
                        "java",
                        "-jar",
                        jarPath
                };
                String userCmd = String.join(" ", command);

                KeployCLI.StartUserApplication(userCmd);

                KeployCLI.TestRunStatus testRunStatus = KeployCLI.TestRunStatus.PASSED;

                long startTime = System.currentTimeMillis();

                // Check status in every 2 seconds
                while (true) {
                    // Sleep for 2 seconds
                    Thread.sleep(2000);

                    testRunStatus = KeployCLI.FetchTestSetStatus(testRunId);

                    if (testRunStatus == KeployCLI.TestRunStatus.RUNNING) {
                        System.out.println("testRun still in progress");

                        // Check if the current time exceeds the start time by MAX_TIMEOUT
                        if (System.currentTimeMillis() - startTime > MAX_TIMEOUT) {
                            System.out.println("Timeout reached, exiting loop");
                            break;
                        }

                        continue;
                    }

                    break;
                }

                if (testRunStatus == KeployCLI.TestRunStatus.FAILED || testRunStatus == KeployCLI.TestRunStatus.RUNNING) {
                    System.out.println("testrun failed");
                    result = false;
                } else if (testRunStatus == KeployCLI.TestRunStatus.PASSED) {
                    System.out.println("testrun passed");
                    result = true;
                }

                System.out.println("TestResult of [" + testset + "]:" + result);
                testResult = testResult && result;
                KeployCLI.FindCoverage(testset);
                //Change This time if you have bigger codebase. Because it will take more time to dump the coverage
                Thread.sleep(5000);
                KeployCLI.StopUserApplication();
            }
        } catch (Exception e) {
            System.err.println("failed to execute keploy tests:" + e);
        } finally {
            System.out.println("Testing done with status:" + testResult);
        }

        assertTrue(testResult, "Keploy Test Result");
    }

This is what you need to add in the pom.xml file of your application.

	<dependencies>
	<dependency>
		<groupId>io.keploy</groupId>
		<artifactId>v2</artifactId>
		<version>1.0.0-SNAPSHOT</version>
	</dependency>
</dependencies>
	
	....
	
<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<version>${project.parent.version}</version>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-plugin</artifactId>
			<version>2.22.2</version>
			<configuration>
				<!-- <skipTests>true</skipTests> -->
				<argLine>
                    -javaagent:${settings.localRepository}/org/jacoco/org.jacoco.agent/0.8.8/org.jacoco.agent-0.8.8-runtime.jar=destfile=target/jacoco.exec
                </argLine>
				<systemPropertyVariables>
					<jacoco-agent.destfile>target/jacoco.exec
                        </jacoco-agent.destfile>
				</systemPropertyVariables>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.jacoco</groupId>
			<artifactId>jacoco-maven-plugin</artifactId>
			<version>0.8.8</version>
			<executions>
				<!-- Prepare the JaCoCo agent to track coverage during tests -->
				<execution>
					<id>prepare-agent</id>
					<goals>
						<goal>prepare-agent</goal>
					</goals>
				</execution>
				<!-- Prepare execution data for e2e tests generated by keploy-->
				<execution>
					<id>merge-e2e</id>
					<phase>test</phase>
					<goals>
						<goal>merge</goal>
					</goals>
					<configuration>
						<fileSets>
							<fileSet>
								<directory>${project.build.directory}</directory>
								<includes>
									<include>test-set-*.exec</include>
								</includes>
							</fileSet>
						</fileSets>
						<destFile>${project.build.directory}/keploy-e2e.exec</destFile>
						<!-- Output of merged data -->
					</configuration>
				</execution>
				<!-- Merge e2e & u-t execution data files after tests are run -->
				<execution>
					<id>merge-ut-e2e</id>
					<phase>test</phase>
					<goals>
						<goal>merge</goal>
					</goals>
					<configuration>
						<fileSets>
							<fileSet>
								<directory>${project.build.directory}</directory>
								<includes>
									<include>jacoco.exec</include>
									<include>keploy-e2e.exec</include>
								</includes>
							</fileSet>
						</fileSets>
						<destFile>${project.build.directory}/ut-e2e-merged.exec</destFile>
						<!-- Output of merged data -->
					</configuration>
				</execution>
				<!-- Generate report based on the different execution data -->
				<!-- Generate unit test report-->
				<execution>
					<id>post-unit-test</id>
					<phase>test</phase>
					<goals>
						<goal>report</goal>
					</goals>
					<configuration>
						<dataFile>${project.build.directory}/jacoco.exec</dataFile>
						<!-- Use merged data file -->
						<outputDirectory>${project.reporting.outputDirectory}/ut</outputDirectory>
					</configuration>
				</execution>
				<!-- Generate e2e test report-->
				<execution>
					<id>e2e-report</id>
					<phase>test</phase>
					<goals>
						<goal>report</goal>
					</goals>
					<configuration>
						<dataFile>${project.build.directory}/keploy-e2e.exec</dataFile>
						<!-- Use merged data file -->
						<outputDirectory>${project.reporting.outputDirectory}/keployE2E</outputDirectory>
					</configuration>
				</execution>
				<!-- Generate combined (e2e+ut) report test report-->
				<execution>
					<id>combined-ut-e2e</id>
					<phase>test</phase>
					<goals>
						<goal>report</goal>
					</goals>
					<configuration>
						<dataFile>${project.build.directory}/ut-e2e-merged.exec</dataFile>
						<!-- Use merged data file -->
						<outputDirectory>${project.reporting.outputDirectory}/e2e-ut-aggregate</outputDirectory>
					</configuration>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (formatting, local variables)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Describe if there is any unusual behaviour of your code(Write NA if there isn't)

NA

Checklist:

  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas and used java doc.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

Screenshots (if any)

Original Updated
original screenshot updated screenshot

Copy link
Member

@charankamarapu charankamarapu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@gouravkrosx gouravkrosx merged commit baafc13 into keploy:main Dec 8, 2023
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

refactor: need refactoring in java-sdk to support new approach of keploy serve command
2 participants