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

fix(gha): Fix github status log and add tests (backport #1316) #1317

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public GithubService githubService(
.setEndpoint(githubEndpoint)
.setConverter(new JacksonConverter())
.setClient(retrofitClient)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setLogLevel(retrofitLogLevel != null ? retrofitLogLevel : RestAdapter.LogLevel.BASIC)
.setLog(new Slf4jRetrofitLogger(GithubService.class))
.build()
.create(GithubService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ package com.netflix.spinnaker.echo.config
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import retrofit.Endpoint
import retrofit.Endpoints
import retrofit.RestAdapter
import retrofit.client.Client
import retrofit.client.Header
import retrofit.client.Response
import retrofit.mime.TypedByteArray
import spock.lang.Specification
import spock.lang.Subject

Expand Down Expand Up @@ -37,6 +43,79 @@ class GithubConfigSpec extends Specification {
then:
endpoint.url == ownEndpoint
}


def 'default log level does not output authorization headers and matches basic API call structure'() {
given:
def systemError = System.out;
def testErr = new ByteArrayOutputStream();
System.setOut(new PrintStream(testErr))

Client mockClient = Stub(Client) {
execute(_) >> {
return new Response("http://example.com", 200, "Success!", new ArrayList<Header>(), new TypedByteArray("", "SOmething workedddd".bytes))
}

}
def ghService = new GithubConfig().githubService(Endpoints.newFixedEndpoint("http://example.com"), mockClient, null)

when:
ghService.getCommit("SECRET", "repo-name", "sha12345");

then:
def logOutput = testErr.toString()
logOutput.contains("HTTP GET http://example.com/repos/repo-name/commits/sha12345")
!logOutput.contains("SECRET")
!logOutput.contains("Authorization")

cleanup:
System.setOut(systemError)
System.out.print(testErr)
}

def 'When no log set, no log output!'() {
given:
def systemError = System.out;
def testErr = new ByteArrayOutputStream();
System.setOut(new PrintStream(testErr))

Client mockClient = Stub(Client) {
execute(_) >> new Response("http://example.com", 200, "Ok", new ArrayList<Header>(), new TypedByteArray("", "response".bytes))
}
def ghService = new GithubConfig().githubService(Endpoints.newFixedEndpoint("http://example.com"), mockClient, RestAdapter.LogLevel.NONE)

when:
ghService.getCommit("", "", "");

then:
!testErr.toString().contains("GET")

cleanup:
System.setOut(systemError)
System.out.print(testErr)
}

def 'Log when full has header information and auth headers- dont do this in prod!'() {
given:
def systemError = System.out;
def testErr = new ByteArrayOutputStream();
System.setOut(new PrintStream(testErr))

Client mockClient = Stub(Client) {
execute(_) >> new Response("http://example.com", 200, "Ok", new ArrayList<Header>(), new TypedByteArray("", "response".bytes))
}
def ghService = new GithubConfig().githubService(Endpoints.newFixedEndpoint("http://example.com"), mockClient, RestAdapter.LogLevel.FULL)

when:
ghService.getCommit("", "", "");

then:
testErr.toString().contains("Authorization")

cleanup:
System.setOut(systemError)
System.out.print(testErr)
}
}

@SpringBootTest(
Expand Down
Loading