cancel
Showing results for 
Search instead for 
Did you mean: 

The option --with-mocks doesn't work anymore once credentials config is provided

jhodel18
Active Contributor
878

Original post is here:

https://open.sap.com/courses/cp7/question/7ec44a77-d378-4462-a129-137332f7bc53

Working with SCP Destination Service, and configuring credentials.destination property in package.json file, forces the application to always use the destination configuration even though --with-mocks option is present using the command cds watch.

Is this a bug in the framework? I need to be able to control the behaviour of the service in order to successfully execute unit tests. I know you can manually remove the configuration but this approach defeats the purpose of setting up automated unit tests. Please advise. Thanks!

Terminal:

jhodel: demo > cds watch

[cds] - running nodemon... 
--ext cds,csn,csv,ts,mjs,cjs,js,json,properties,edmx,xml 
--exec cds serve all --with-mocks --in-memory? 

Configuration:

"cds": {
    "requires": {
        "NorthWind": {
            "kind": "odata",
            "model": "srv/external/NorthWind",
            "credentials": {
                "destination": "NorthWind"
            }
        }
    }
}

I tried using the configuration profile from the documentation below:

https://cap.cloud.sap/docs/advanced/config#profiles

But still wit no luck.

Here's the latest configuration after using config profile:

    "cds": {
        "requires": {
            "NorthWind": {
                "kind": "odata",
                "model": "srv/external/NorthWind"
            }
        },
        "[development]": {
            "requires": {
                "NorthWind": {
                    "kind": "odata",
                    "model": "srv/external/NorthWind",
                    "credentials": {
                        "destination": "NorthWind"
                    }
                }
            }
        }
    }

Note that I tried the config profile below for HANA DB case, and it works. The question now is if the external service config supports config profiles?

    "cds": {
        "requires": {
            "db": {
                "kind": "sqlite",
                "credentials": {
                    "database": "bookshop.db"
                }
            }
        },
        "[production]": {
            "requires": {
                "db": {
                    "kind": "hana"
                }
            }
        }
    }

Accepted Solutions (1)

Accepted Solutions (1)

johannesvogel
Advisor
Advisor

Hi Jhodel,

you can do this by providing simply the url in the development profile and by providing the destination in production (using the profiles you use for the database).

    "cds": {
        "requires": {
            "NorthWind": {
                "kind": "odata",
                "model": "srv/external/NorthWind",
                "credentials": {
                    "url": "<NorthWindUrl>"
                }
            }
        },
        "[production]": {
            "requires": {
                "NorthWind": {
                    "kind": "odata",
                    "model": "srv/external/NorthWind",
                    "credentials": {
                        "destination": "NorthWind"
                    }
                }
            }
        }
    }

This should use the URL in development, the "mock" if you start the application --with-mocks and the destination in "production".

Best regards,

Johannes

jhodel18
Active Contributor
0 Kudos

Hi Johannes,

Thanks for the response! I followed your suggestion, but unfortunately, I didn't get the exact results as you described. Here is the updated configuration:

    "cds": {
        "requires": {
            "NorthWind": {
                "kind": "odata",
                "model": "srv/external/NorthWind",
                    "credentials": {
                        "url": "https://services.odata.org/Experimental/OData/OData.svc"
                    }
                }
            }
        },
        "[production]": {
            "requires": {
                "NorthWind": {
                    "kind": "odata",
                    "model": "srv/external/NorthWind",
                    "credentials": {
                        "destination": "NorthWind"
                    }
                }
            }
        }
    }

It seems that when the credentials.url property was added, it will always use the data from the external service even if I specified to use mock data. I even use the below command instead of just using cds watch.

> cds serve all --with-mocks --in-memory

So what I did next, is that I removed configuration credentials.url, and kept the production configuration. Running the service using regular cds watch command provides the mock data -- which is good. Now testing for the production case scenario, I tested with below command:

> NODE_ENV=production cds serve all

And when i started querying for data, it returned these errors:

jhodel: demo > NODE_ENV=production cds serve all 


[cds] - model loaded from 2 file(s):


  srv/catalog-service.cds
  srv/external/NorthWind.csn


[cds] - connect to [object Object] NorthWind { destination: 'NorthWind' }
[cds] - serving CatalogService { at: '/catalog', impl: 'srv/catalog-service.js' }


[cds] - launched in: 1566.399ms
[cds] - server listening on { url: 'http://localhost:4004' }
[ terminate with ^C ]


GET /catalog/Products
[2020-05-29T12:28:10.283Z] INFO     (destination-accessor): Attempting to retrieve destination from environment variable.
[2020-05-29T12:28:10.287Z] INFO     (destination-accessor): Could not retrieve destination from environment variable.
[2020-05-29T12:28:10.287Z] INFO     (destination-accessor): Attempting to retrieve destination from service binding.
[2020-05-29T12:28:10.288Z] INFO     (environment-accessor): Environment variable VCAP_SERVICES is not defined.
[2020-05-29T12:28:10.288Z] WARN     (environment-accessor): Environment variable 'VCAP_SERVICES' is not defined.
[2020-05-29T12:28:10.288Z] INFO     (destination-accessor): No services are bound to the application (environment variable VCAP_SERVICES is not defined)!
[2020-05-29T12:28:10.288Z] INFO     (destination-accessor): Could not retrieve destination from service binding.
[2020-05-29T12:28:10.288Z] INFO     (destination-accessor): If you are not using SAP Extension Factory, this information probably does not concern you.
[2020-05-29T12:28:10.288Z] INFO     (destination-accessor): Attempting to retrieve destination from destination service.
[2020-05-29T12:28:10.289Z] WARN     (environment-accessor): No services of type destination found! This might cause errors in other parts of the application.
[2020-05-29T12:28:10.293Z | ERROR | 1769294]: Failed to build HTTP request for destination: failed to load destination!

I'm sharing here my sample project for reference:

https://github.com/jcailan/azure-devops/tree/cap-es-part2

Note that as far as the Mock data testing is concerned, it is working fine, it's just the production testing done locally is causing some issues. So what I did next is deploy the application into SCP. Surprisingly, it works without any issues.

Although, I'm already contented with below cases that I need:

- Test locally using mock data

- Test in SCP using destination service

There are still two issues that feel a bit of a mystery:

- When testing locally using credentials.url property, even if I'm using --with-mocks, it will always use the external service data

- When testing locally with production environment set, the service crashes with error Failed to build HTTP request for destination: failed to load destination! (which doesn't happen in the actual production scenario in SCP)

Do you have any idea about these odd behaviour?

Thanks and regards,

Jhodel

johannesvogel
Advisor
Advisor

Hi Jhodel,

I double checked and it is like david.kunz2 said it in the openSAP forum. If credentials are provided in the external service configuration, they have precedence and --with-mocks is ignored.

When you test locally with "production" you need to have a default-env.json that contains the credentials for your xsuaa and your destination instance. Otherwise, the destination you configured, cannot be resolved and you observe your error.

You probably use this file already, if you connect locally to your HANA instance.

You can look up the credentials of the service instances in CP Cockpit.

Best regards,

Johannes

Answers (1)

Answers (1)

jhodel18
Active Contributor
0 Kudos

Hi Johannes,

Thanks for the clarification about the precedence of the credentials configuration.

However, I'm already using the right credentials inside the default-env.json and it is already working if i don't use the "production" profile. But once I start using this production profile and test locally it is not working anymore. The same configuration actually works in SCP. It's just puzzling the there is this kind of odd behaviour. Perhaps a bug?

I mentioned this behaviour in my recent blog -- https://blogs.sap.com/2020/05/31/cap-unit-testing-using-mocha-and-chai/

Nevertheless, the original issues in this post has already been resolve. Thanks again!

Thanks and regards,

Jhodel