Search code examples
ftptestcontainers

Testcontainers ftp and real server different navigation


I want to test my simple class that connected to public Ftp and get list of fileNames from there.

And i found very strange issue - navigation on real server and on test conteiner with same dirs are not same

there is a dir structure

enter image description here

Well if i connect to real server and use ftp.changeWorkingDirectory("/out/published") everything is good - directory changed and i retrive file from there

if i trying to do same in testconteiner ftp.changeWorkingDirectory("/out/published") it return false ( cant find this directory) and if i remove slash like this ftp.changeWorkingDirectory("out/published") it will work

and second issue if i will do ftp.listnames("/out/published/tverskaya_obl/purchaseNotice/daily) on real server it will work good, but in testconteiner it work only if i use this ftp.listNames("tverskaya_obl/purchaseNotice/daily"

)

is there any way to fix it

here is the code to create testcontainer and folders

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@SpringBootTest(properties = "ftp.host = localhost")
class PurchaseFZ223FtpServiceTest {

    private static final int PORT = 21;
    private static final String USER = "";
    private static final String PASSWORD = "";

    private static final int PASSIVE_MODE_PORT = 21000;

    @Autowired
    FtpService ftpService;

    private static final FixedHostPortGenericContainer ftp = new FixedHostPortGenericContainer<>(
            "delfer/alpine-ftp-server:latest")
            .withFixedExposedPort(PASSIVE_MODE_PORT, PASSIVE_MODE_PORT)
            .withExposedPorts(PORT)
            .withEnv("USERS", USER + "|" + PASSWORD)
            .withEnv("MIN_PORT", String.valueOf(PASSIVE_MODE_PORT))
            .withEnv("MAX_PORT", String.valueOf(PASSIVE_MODE_PORT));

    @BeforeAll
    void init() throws NoSuchFieldException, IllegalAccessException, IOException {
        ftp.start();
  
        FTPClient client =new FTPClient();
        client.connect("localhost", ftp.getMappedPort(PORT));
        client.enterLocalPassiveMode();
        client.login(USER, PASSWORD);
        client.makeDirectory("out");
        client.makeDirectory("out/published");
        client.makeDirectory("out/published/tverskaya_obl");
        client.makeDirectory("out/published/tverskaya_obl/purchaseNotice");
        client.makeDirectory("out/published/tverskaya_obl/purchaseNotice/daily");
        client.changeWorkingDirectory("out/published");
        client.storeFile("list_regions.txt", new ByteArrayInputStream("tverskaya_obl".getBytes(StandardCharsets.UTF_8)));
        client.changeWorkingDirectory("tverskaya_obl/purchaseNotice/daily");
        client.storeFile("purchaseNotice_Tverskaya_obl_20221209_000000_20221209_235959_daily_001.xml.txt", new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)));
    }

Solution

  • Finally found what is going wrong

    When testconteiner create ftp server it just add two folders like this

    ftp/yourLoginName

    so my mistake was to ask list of file name with invalid path

    valid path for me to get list of fileNames is /ftp/***/out/published/tverskaya_obl/purchaseNotice/daily

    same with change working dir

    if you dnt know how to check real path you are at just do this

    ftp.printWorkingDirectory()