How to run shell script on remote machine from Java? - java

I'm creating a test which will perform some stress testing of the app which talks to a Postgres database. In order to make it replicable I'd like to recreate the database on every run. Startup method decorated with org.junit.Before annotation will drop the database (if it exists) and restore it from a dump file. Doing this form shell/command is trivial:
psql -f db.out newdb
or something along these lines. Since the test may be performed on a JVM that's not running on the same machine which hosts the database I'm wondering if it's possible to invoke shell/batch script on remote machine from Java programmaticaly.
Thanks

If you have knowledge of the machine details (hostname, login, password) the only thing I can think of at the moment is using a Java SSH library to login to the box and run that command.
Or you could write a script on your local machine which ran the script on the remote machine given the correct parameters.
That's the simplest way I can think of anyway!

If setting up SSH is too complex, you can write a small Java program which listens for connections on a socket and then runs the script. Install that on the Postgres server and connect to it in the test case.
That said, I suggest to install a copy of Postgres on every developer machine. That would make your tests run much faster and you wouldn't get spurious errors when two developers run the tests at the same time. Also, you won't have errors because of network problems, because someone does maintenance on the server, because developer X has changed the DB schema, etc. etc.

Related

swing Application is slow when I run it from network on server

I have a Swing Application on Java when I run the Jar file after clean and build from my local PC it runs very good and fast but when i move the App folder to the server and make a shurtcut of Jar file to some computers on the network the application runs very slow
I made my application using NetBeans IDE 8.0.2
the Java runtime is Java 8 update 66
thank you in advanced.
It could be that your application is accessing resources that are slow across the network ie. If you are reading s file it will be quick locally but slower access network. Also if you are reading data from a database and the query results are large and or take long to run, this will also be quicker locally than remote.
I would suggest breaking you application upj into "resources", then turn them all off PR don't use them and bring them back up one by one. That way you can find out what is causing the app to run slow
thank you for your replies
I tried to copy all the application folders to local machines and change the DB connection to DB file on the server
but the program still very slow
some one told me that the problem is SQLite and I need to change to SQL Server 2008 or something like it

Simulate network connectivity issues on Linux using Java

I would like to have an automated test for the following scenario:
User logs in and performs some "lengthy" operation. For example tries to upload a file.
In the middle of that lengthy operation, the connection to the server is lost for a period of time, and then restored.
Application does whatever it's supposed to do in this case. For example shows a message to the user asking if they want to retry.
I want steps 1. and 3. to be done by Selenium. Question is: how can I do the step 2. as part of the automated test?
Details on the tools and environment:
Selenium is on Java with Junit
Test must run on Linux and Windows
It will also run on 3 browsers: Firefox, Chrome, IE 11
Couple of solutions I was thinking about:
Call some script that manipulates a connection on the machine.
Manipulate proxy settings of the browser (proxy points to fake address, thus connection is broken).
Use a real proxy, which could be controlled from the code (e.g. by some commands), so it can behave as "working" or "broken" connection.
None of these solutions is ideal for various reasons.
So: did anyone try to solve a similar problem? Would like to hear your solution or alternative ideas, that you think may work. Thanks.
Option 1
Stubbing
You'll have to identify what would be the exception thrown and what will be the component that throws it in a real case scenario. You can do that easily, simulate the scenario in your machine and when the exception is thrown, the stack-trace will tell you exactly what component thrown it.
Then you'll have to extend the component that throws the exception and inject it in the proper place and, ultimately, create an API to trigger that exception.
If you think you need a framework to automate this kind of tests, have a look to to Fitnesse.
Option 2
Simulation
Simulating a real network problem, would be overly complicated and the benefits are not worth the effort in this case (imo).
Anyway... Linux has an excellent built in network emulation layer called netem that allows any kind of seamless interaction with the network traffic. This kernel module is controlled by the tc command line interface.
It's not very easy to get it right when you want to apply those condition to a single port because you'll have to:
Netfilter rule to mark the packets that we want to limit
Traffic Control policy
Filter to bind the packets to the policy
A simple example would be if you want to have 10% packet loss on the whole eth0, in which case you'll use:
tc qdisc change dev eth0 root netem loss 10%
Then you'll have to wrap this functionality in some java controllable way.
If I failed on discouraging you, here is a good guide: TrafficControl
You can execute the following commands on windows on cmd to disconnect and reconnect network.
> ipconfig /release
> ipconfig /renew
Using this you can use the Java Runtime class to execute the command.
Runtime.getRuntime().exec("ipconfig /release");
I have tested this on windows and it works.
The Linux equivalent of the cmd commands are as follows
> sudo ifconfig eth0 up
> sudo ifconfig eth0 down
Note that eth0 here is the name of my Ethernet connection. You can list the names of all the connections using
> ifconfig -a
You can look at the following thread to execute bash through Java - How to execute bash command with sudo privileges in Java?

Run batch file on remote computer inside Java program

I have a program right now that uses the "net use" command to access a shared drive on a network computer at my work. This works fabulously and I'm able to copy a file from the remote machine onto my own using the FileUtil library from apache. Now I'm wanting to run batch file located on the remote machine and I want it executed in that environment not my own local machine. At first I thought I could just use the "net use" command again with no problem but when I execute the batch file, it is run on my own machine not the remote one. However, this is not what I want to happen.
So, is anyone familiar with this process of executing batch files on remote machines. Better yet, from inside Java.
Thank you for any feed back I greatly appreciate it!
I don't know much about java but can you execute external programs from it? If the answer is "yes" you might try with PSEXEC from Sysinternals it runs every kind of things remotely.

Search for files from another machine using Java

I need to create a small utility. Using Java, remotely (from another machine) I need to search for files (whether particular files exist or not). I would know exact names of files, or if not, I would know their partial names (so I will be using wildcards).
The problem is: I cannot install anything on that machine (it's a corporate box), but I am allowed FTP, Telnet, SSH login, etc. I would not want to use FTP because there are hundreds of directories and there's no way to know ahead of time where the files are each time (using ftp command LS on each directory every time is simply unproductive). So the best would be to use internal unix 'find' command. Or a similar approach.
I need the best solution to the problem: how can I search for files (from hundreds of thousands files in many different folders) remotely on another machine.
Jsch will allow you to connect quite easily to a remote machine and execute operations using SSH. There is not a lot of documentation, but these examples shows about everything you can do, from basic Shell operations to SFTP and much more.
You can locally exec ssh and have it execute the find command on another machine.
e.g.
ssh user#host "find /home/foo -name index.*"
You can exec that from within the JVM and read the output from the process.

How to up an aplication on remote linux and wait for answer

Someone can tell me the best way, through a web Java application, how to run an application on a remote machine (linux) and how to know if this application has already completed. I know the application terminates automatically, so just wanted to wait for the answer.
any help or advice are very welcome.
I was confused by the wording, so I will assume the following: the web application and the remote application are running on two different machines, with the remote application on Linux.
If that is the case, then you would be better off using a SSH library for Java (there are several - JSch, sshj, Ganymed SSH-2, to connect to the remote machine and run commands on it. This is a better approach than using ProcessBuilder, for it abstracts you from the problem of providing your password to the terminal (which can get quite tricky).
I'm not sure what you meant by this statement: "I know the application terminates automatically, so just wanted to wait for the answer." so I'll make another assumption that you want to know if the remote process terminated successfully or not. I'm unsure if any of the SSH libraries posted above, will allow you to get the remote process status in a non-trivial way, so you are better off writing a shell wrapper to your remote command that will return a parseable message.
If you are running both on the same machine, use ProcessBuilder.

Categories

Resources