Dmytro Chasovskyi
Dmytro Chasovskyi

Reputation: 3651

How to resolve GOOGLE_APPLICATION_CREDENTIALS when running app in test, Spring Boot?

I have a Spring Boot application dependent on Google PubSub. I want to run it with a Google Cloud PubSub emulator. How can I resolve GOOGLE_APPLICATION_CREDENTIALS, so the app will start and consume messages from the local emulator, not an external project?

At the moment, if I set GOOGLE_APPLICATION_CREDENTIALS to dev.json, PubSub doesn't get invoked if I don't set the variable, test crashes. How can I overcome it? I cannot put puzzles together.

NOTE: I am writing an integration test with a full Spring boot startup.

My PubSub implementation:

import com.github.dockerjava.api.exception.DockerClientException
import com.google.api.gax.core.NoCredentialsProvider
import com.google.api.gax.grpc.GrpcTransportChannel
import com.google.api.gax.rpc.FixedTransportChannelProvider
import com.google.api.gax.rpc.TransportChannelProvider
import com.google.cloud.pubsub.v1.*
import com.google.cloud.pubsub.v1.stub.GrpcSubscriberStub
import com.google.cloud.pubsub.v1.stub.SubscriberStubSettings
import com.google.protobuf.ByteString
import com.google.pubsub.v1.*
import com.greenbird.server.contracts.TestServer
import io.grpc.ManagedChannel
import io.grpc.ManagedChannelBuilder
import org.testcontainers.containers.PubSubEmulatorContainer
import org.testcontainers.utility.DockerImageName
import java.util.concurrent.TimeUnit

class PubSubTestServer(private val projectName: ProjectName, private val ports: Array<Int> = arrayOf(8085)) :
    TestServer {

    constructor(projectId: String): this(ProjectName.of(projectId))

    private val projectId = projectName.project

    var emulator: PubSubEmulatorContainer = PubSubEmulatorContainer(
        DockerImageName.parse("gcr.io/google.com/cloudsdktool/cloud-sdk:latest")
    )

    private var channels: MutableList<ManagedChannel> = mutableListOf()

    private fun channel(): ManagedChannel {
        return if (channels.isEmpty()) {
            val endpoint = emulator.emulatorEndpoint
            val channel = ManagedChannelBuilder
                .forTarget(endpoint)
                .usePlaintext()
                .build()
            channels.add(channel)
            channel
        } else {
            channels.first()
        }
    }

    private val channelProvider: TransportChannelProvider
        get() {
            return FixedTransportChannelProvider
                .create(
                    GrpcTransportChannel.create(channel())
                )
        }

    private val credentialsProvider: NoCredentialsProvider = NoCredentialsProvider.create()

    private val topicAdminSettings: TopicAdminSettings
        get() {
            when {
                emulator.isRunning -> {
                    return buildTopicAdminSettings()
                }
                else -> {
                    throw DockerClientException("Topic admin settings attempted to initialize before starting PubSub emulator")
                }
            }
        }

    private fun buildTopicAdminSettings(): TopicAdminSettings {
        return TopicAdminSettings.newBuilder()
            .setTransportChannelProvider(channelProvider)
            .setCredentialsProvider(credentialsProvider)
            .build()
    }

    private val subscriptionAdminSettings: SubscriptionAdminSettings
        get() {
            when {
                emulator.isRunning -> {
                    return buildSubscriptionAdminSettings()
                }
                else -> {
                    throw DockerClientException("Subscription admin settings attempted to initialize before starting PubSub emulator")
                }
            }
        }

    private fun buildSubscriptionAdminSettings(): SubscriptionAdminSettings {
        return SubscriptionAdminSettings.newBuilder()
            .setTransportChannelProvider(channelProvider)
            .setCredentialsProvider(credentialsProvider)
            .build()
    }

    override fun start() {
        emulator.withExposedPorts(*ports).start()
    }

    override fun stop() {
        terminate()
        emulator.stop()
    }

    private fun terminate() {
        for (channel in channels) {
            channel.shutdownNow()
            channel.awaitTermination(5, TimeUnit.SECONDS)
        }
    }

    fun createTopic(topicId: String) {
        TopicAdminClient.create(topicAdminSettings).use { topicAdminClient ->
            val topicName = TopicName.of(projectId, topicId)
            topicAdminClient.createTopic(topicName)
        }
    }

    fun listTopics(): List<String> {
        return TopicAdminClient.create(topicAdminSettings)
            .listTopics(projectName)
            .iterateAll()
            .map { it.name }
            .toList()
    }

    fun createSubscription(subscriptionId: String, topicId: String) {
        val subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId)
        SubscriptionAdminClient.create(subscriptionAdminSettings).createSubscription(
            subscriptionName,
            TopicName.of(projectId, topicId),
            PushConfig.getDefaultInstance(),
            10
        )
    }

    fun listSubscriptions(): List<String> {
        return SubscriptionAdminClient.create(subscriptionAdminSettings)
            .listSubscriptions(projectName)
            .iterateAll()
            .map { it.name }
            .toList()
    }

    fun push(topicId: String, message: String) {
        val publisher: Publisher = Publisher.newBuilder(TopicName.of(projectId, topicId))
            .setChannelProvider(channelProvider)
            .setCredentialsProvider(credentialsProvider)
            .build()


        val pubsubMessage: PubsubMessage = PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(message)).build()
        publisher.publish(pubsubMessage).get()
    }

    fun poll(size: Int, subscriptionId: String): List<String> {
        val subscriberStubSettings: SubscriberStubSettings = SubscriberStubSettings.newBuilder()
            .setTransportChannelProvider(channelProvider)
            .setCredentialsProvider(credentialsProvider)
            .build()
        GrpcSubscriberStub.create(subscriberStubSettings).use { subscriber ->
            val pullRequest: PullRequest = PullRequest.newBuilder()
                .setMaxMessages(size)
                .setSubscription(ProjectSubscriptionName.format(projectId, subscriptionId))
                .build()
            val pullResponse: PullResponse = subscriber.pullCallable().call(pullRequest)

            return pullResponse.receivedMessagesList
                .map { it.message.data.toStringUtf8() }
                .toList()
        }
    }

}

Upvotes: 2

Views: 1913

Answers (1)

Dmytro Chasovskyi
Dmytro Chasovskyi

Reputation: 3651

I couldn't find the answer to my question as it was asked.

I found a workaround for Junit5 with junit-pioneer it's possible to set the env variable to something before the actual test run.

Therefore, the code will be the same as before but annotated with @SetEnvironmentVariable

@SetEnvironmentVariable(key="GOOGLE_APPLICATION_CREDENTIALS", value="dev.json")
class PubSubTestServer {
  ...
}

JUnit-pioneer: Maven central.

Upvotes: 2

Related Questions