1

When using the local development server, the port for my default service always defaults to 8080. However, when I use aetest, the port number always changes. The command used to start the local server when unit testing specifies --port=0. However, since this is in the app engine package, I do not want to modify it. I also can't specify the port number manually since tests are run using go test and not dev_appserver.py.

What I need

The code I am testing requires a particular response from a different microservice to continue execution successfully. To keep testing to this microservice, I am trying to set up a fake endpoint to provide me with the response I need.

Sample of code being tested

func Sample(c *gin.Context) {
    ...
    url := os.Getenv("some_service") + "/some-endpoint"
    req, err := http.NewRequest("POST", url, r.Body)
    if err != nil {
        // error handling
    }
    ...
}

I need the host of the current service being tested to be able to set dummy environment variables for another microservice it interacts with. The host url should be the url of the service currently undergoing tests. This way I can use factory responses since I am not testing the other services in this set of tests.

What I've Tried

  • Using appengine.DefaultVersionHostname(), however this returns an empty string
  • Using the http request I create to get the url from that, however it does not include the host as needed (just the path)

Question

Is there any way to obtain the host for the local development server when running unit tests in GAE? Or, is there a way to specify the port number for app engine tests?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
smandrus
  • 305
  • 1
  • 2
  • 16
  • 1
    Try [DefaultVersion](https://cloud.google.com/appengine/docs/standard/go/modules/reference#DefaultVersion), this appears to be the equivalent of the python's `modules.get_hostname()` that I'm using for such goal, as described in https://stackoverflow.com/questions/31141247/resolve-discovery-path-on-app-engine-module/31145647#31145647 – Dan Cornilescu Jul 17 '18 at 09:48

1 Answers1

1

I was able to achieve my goal by first getting the list of modules. (It is likely the service name you are looking for will be default, however I chose to use the first name in the list anyways). Using the module name, I got the default version for the module. Using both of these (and an empty instance name), I was able to get the module host name which provided the value I was looking for.

A code sample is shown below:

    ml, - := module.List(ctx)
    mv, _ := module.DefaultVersion(ctx, ml[0])
    hn, _ := appengine.ModuleHostname(ctx, ml[0], mv, "")

Thanks to Dan for pointing me in the right direction.

smandrus
  • 305
  • 1
  • 2
  • 16