Skip to content

Trim the connecting string and add new tests#3629

Merged
heyams merged 3 commits into
microsoft:mainfrom
progxaker:fix/ignore-cr-and-lf
Apr 11, 2024
Merged

Trim the connecting string and add new tests#3629
heyams merged 3 commits into
microsoft:mainfrom
progxaker:fix/ignore-cr-and-lf

Conversation

@progxaker

Copy link
Copy Markdown
Contributor

Fix #3628. The original approach does not ignore newline or carriage return.

As I mentioned in the issue, I created the following files and reproduced them in the tests:

  • (Success) No line terminator
    progxaker@pc-name# cat -A applicationinsights-connection-string_no-eol.txt 
    InstrumentationKey=<ikey>;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/progxaker@pc-name#
    
  • (Failed) CRLF line terminator (Windows)
    progxaker@pc-name# cat -A applicationinsights-connection-string_windows.txt 
    InstrumentationKey=<ikey>;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/^M$
    progxaker@pc-name#
    
  • (Failed) CR line terminator (Mac OS)
    progxaker@pc-name# cat -A applicationinsights-connection-string_cr-eol.txt 
    InstrumentationKey=<ikey>;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/^Mprogxaker@pc-name#
    
  • (Failed) LF line terminator (Linux)
    progxaker@pc-name# cat -A applicationinsights-connection-string.txt 
    InstrumentationKey=<ikey>;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com$
    progxaker@pc-name#
    

Decisions

  • At first I wanted to replace line terminators with "", but I decided to use the trim() function.
  • I prefer to add line terminators to the test file so we don't have to check if the file exists or rewrite it:
    Writer writer = Files.newBufferedWriter(file.toPath(), UTF_8);
    writer.write(CONNECTION_STRING+"\r\n");
    writer.close();
    but if you don't want to add the StandardOpenOption class import, I can rewrite that block.
  • I'm not quite sure if I should use absolute path or relative path, but I added the tests to the end, thinking the tests would run in order.
  • I prefer to create three tests instead of one with a loop.

Tests

  • If we run tests without the fix, we get the following results:
Failed test results
# ./gradlew test                                                                                    
                                                
> Task :agent:agent-tooling:compileTestJava                                                                                  
Note: Some input files use or override a deprecated API.                                          
Note: Recompile with -Xlint:deprecation for details.                                                       
Note: Some input files use unchecked or unsafe operations.                                                                        
Note: Recompile with -Xlint:unchecked for details.                                                                                                                                                
                                                                                                                                                                                                  
> Task :agent:agent-tooling:test                                                                                                                                                                  

FileStringLookupTest > testFileWithCRLineTerminators() FAILED
    org.opentest4j.AssertionFailedError:                                                          
    expected: "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://fake-ingestion-endpoint.example.com/"
     but was: "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://fake-ingestion-endpoint.example.com/
"
        at java.base@17.0.10/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base@17.0.10/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base@17.0.10/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base@17.0.10/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at app//com.microsoft.applicationinsights.agent.internal.configuration.FileStringLookupTest.testFileWithCRLineTerminators(FileStringLookupTest.java:150)

FileStringLookupTest > testFileWithLFLineTerminators() FAILED
    org.opentest4j.AssertionFailedError: 
    expected: 
      "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://fake-ingestion-endpoint.example.com/"
     but was: 
      "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://fake-ingestion-endpoint.example.com/
      "
        at java.base@17.0.10/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base@17.0.10/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base@17.0.10/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base@17.0.10/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at app//com.microsoft.applicationinsights.agent.internal.configuration.FileStringLookupTest.testFileWithLFLineTerminators(FileStringLookupTest.java:139)

FileStringLookupTest > testFileWithCRLFLineTerminators() FAILED
    org.opentest4j.AssertionFailedError: 
    expected: 
      "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://fake-ingestion-endpoint.example.com/"
     but was: 
      "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://fake-ingestion-endpoint.example.com/
      "
        at java.base@17.0.10/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base@17.0.10/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
        at java.base@17.0.10/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base@17.0.10/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
        at app//com.microsoft.applicationinsights.agent.internal.configuration.FileStringLookupTest.testFileWithCRLFLineTerminators(FileStringLookupTest.java:128)

...

258 tests completed, 3 failed, 10 skipped
  • If we run with the tests with the fix, the build will succeed.

The PR is open as a draft, as I'd like to get your opinion on it and make changes if necessary.
(I'm just not a developer, so I might be missing something).

The original approach does not ignore newline or carriage return,
what cause errors that will be described in a GitHub issue.
@progxaker progxaker marked this pull request as draft April 8, 2024 10:16
@progxaker

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@jeanbisutti

Copy link
Copy Markdown
Contributor

/spotless

@jeanbisutti

Copy link
Copy Markdown
Contributor

@progxaker Thank you for having reported the issue and created a PR to fix it!

The PR is marked as draft. Is it ready for review?

@progxaker

Copy link
Copy Markdown
Contributor Author

Hello @jeanbisutti. If there are no crucial mistakes, then yes, I can open for review.

@jeanbisutti

Copy link
Copy Markdown
Contributor

@progxaker It looks good to me!

Could you please run ./gradlew :agent:agent-tooling:spotlessApply to fix a code format issue, and then commit and push?

@jeanbisutti jeanbisutti marked this pull request as ready for review April 10, 2024 14:51
@progxaker

Copy link
Copy Markdown
Contributor Author

Done.

@jeanbisutti jeanbisutti left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could you please accept these modifications to fix other code format issues?

@progxaker

progxaker commented Apr 10, 2024

Copy link
Copy Markdown
Contributor Author

Abbreviation in name 'testFileWithCRLFLineTerminators' must contain no more than '1' consecutive capital letters.

Should I rename the tests to testFileWithCrLfLineTerminators or something like that?


Never mind, just didn't refresh the page.

Co-authored-by: Jean Bisutti <jean.bisutti@gmail.com>
@jeanbisutti

Copy link
Copy Markdown
Contributor

@progxaker I don't think this PR will fix #3628. connection-string.txt should only contain one line for the connection string.

@progxaker

progxaker commented Apr 10, 2024

Copy link
Copy Markdown
Contributor Author

@jeanbisutti, but I didn't say we need to support multi-line. There is the problem that even if I specify a single line (see failed files), App Insights reads \n, \r\n, \r as a new line, which causes the errors I provided in the issue.
With this PR, I allow the user to put the connection string in a file and use it without worrying about line terminator configurations.

@jeanbisutti

Copy link
Copy Markdown
Contributor

@progxaker I have reproduced the issue. Thank you!

@heyams

heyams commented Apr 11, 2024

Copy link
Copy Markdown
Contributor

thank you @progxaker!

@heyams heyams merged commit 75645f1 into microsoft:main Apr 11, 2024
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.

Ignore newline and carriage return in the connection string file

3 participants