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

Using mocka for Openresty testing #115

Open
syedmhashim opened this issue Feb 18, 2020 · 7 comments
Open

Using mocka for Openresty testing #115

syedmhashim opened this issue Feb 18, 2020 · 7 comments

Comments

@syedmhashim
Copy link

Hello!
The Readme document states that mocka has nginx embedded methods for individual testing of Openresty. However, I cannot find any support material or examples/references for that. Can you please guide me on how to use it for ngx testing.
Thanks!

@atrifan
Copy link
Collaborator

atrifan commented Oct 28, 2020

Sorry for the late answer yes, I will update with a bunch of openresty testing docs. Is that ok?

@syedmhashim
Copy link
Author

Yes please update the docs. Thanks!

@atrifan
Copy link
Collaborator

atrifan commented Nov 12, 2020

@syedmhashim a pull request is ongoing after is done I will merge to dev and master. There is no need for a new version of mocka it works with previous versions as well. If you have any other questions please don't hesitate to contact me at trifan@adobe.com

atrifan added a commit that referenced this issue Nov 12, 2020
* Develop to master (#117)

* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* update code

* making a centos docker

* add loop script

* working nginx/openresty

* add boiler plate

* small refactoring

* update some more

* fixing issues and adding docker-compose for running

* correct mapping

* adding example

* adding examples

* adding examples

* finished example

* support and example + coverage

* update
atrifan added a commit that referenced this issue Nov 12, 2020
* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* [#115][request] Openresty test examples (#119)

* Develop to master (#117)

* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* update code

* making a centos docker

* add loop script

* working nginx/openresty

* add boiler plate

* small refactoring

* update some more

* fixing issues and adding docker-compose for running

* correct mapping

* adding example

* adding examples

* adding examples

* finished example

* support and example + coverage

* update
@syedmhashim
Copy link
Author

Hi Alexandru! If you can also update the README.md with usage examples with regards to testing ngx, then that would be great! Thanks a load!

@atrifan
Copy link
Collaborator

atrifan commented Nov 12, 2020 via email

atrifan added a commit that referenced this issue Nov 19, 2020
* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* [#115][request] Openresty test examples (#119)

* Develop to master (#117)

* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* update code

* making a centos docker

* add loop script

* working nginx/openresty

* add boiler plate

* small refactoring

* update some more

* fixing issues and adding docker-compose for running

* correct mapping

* adding example

* adding examples

* adding examples

* finished example

* support and example + coverage

* update

* [#121] Global before each (#122)

* Develop to master (#117)

* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* update code

* make globalBeforEach and globalAfterEach

* global before each after each and mocka 1.3.2
atrifan added a commit that referenced this issue Nov 19, 2020
* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* [#115][request] Openresty test examples (#119)

* Develop to master (#117)

* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* update code

* making a centos docker

* add loop script

* working nginx/openresty

* add boiler plate

* small refactoring

* update some more

* fixing issues and adding docker-compose for running

* correct mapping

* adding example

* adding examples

* adding examples

* finished example

* support and example + coverage

* update

* make globalBeforEach and globalAfterEach

* global before each after each and mocka 1.3.2

* fixes for centos deployment pipeline
atrifan added a commit that referenced this issue Nov 19, 2020
* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* [#115][request] Openresty test examples (#119)

* Develop to master (#117)

* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* update code

* making a centos docker

* add loop script

* working nginx/openresty

* add boiler plate

* small refactoring

* update some more

* fixing issues and adding docker-compose for running

* correct mapping

* adding example

* adding examples

* adding examples

* finished example

* support and example + coverage

* update

* [#121] Global before each (#122)

* Develop to master (#117)

* new incremental version (#111)

* fixing mocka failing some nginx tests

* version 1.3.0

* update code

* make globalBeforEach and globalAfterEach

* global before each after each and mocka 1.3.2
@syedmhashim
Copy link
Author

Hi Alexandru. I'll show you the way I've have done the testing of the lua code that's used within my nginx configuration files. Can you please verify if this is how it should be done. The tests run successfully tho.

security.lua (This would be used within the nginx configuration)

local module = {}

function module.authenticate()
   --[[
       some lua code that authenticates the requests
   ]]--
end

return module

The sample usage of above code in nginx.conf

server {
  listen 9191
  location /some/path {
    require("security").authenticate()
    proxy_pass ...
  }
}

Now for testing this lua code I created ngx/mocked.lua as the default ngx doesn't fulfill my needs:

local METHOD = nil
local URI_ARGS = {}

local ngx = {
    exit = function(arg) error({status=arg}) end,
    header = {},
    HTTP_OK = 200,
    HTTP_UNAUTHORIZED = 401,
    HTTP_INTERNAL_SERVER_ERROR = 500,
    log = function(...) end,
    req = {
        set_header = function(arg1, arg2) ngx.header[arg1] = arg2 end,
        get_headers = function() return ngx.header end,
        set_method = function(m) METHOD=m end,
        get_method = function() return METHOD end,
        set_uri_args = function(args) URI_ARGS = args end,
        get_uri_args = function() return URI_ARGS, nil end
    },
    say = print,
    var = {}
}

return ngx

And in authenticate_test.lua I'm using it like this:

authenticate = require("security").authenticate

beforeEach(function()
    mockNgx(require("ngx.mocked"))
end)

test("Authentication Successful", function()
    ngx.req.set_method("GET")
   -- some testing code
end)

@atrifan
Copy link
Collaborator

atrifan commented Jan 13, 2021

the above is correct only if you plan to use ngx just inside the testing code, but if you want to replace the global variable you should not use local ngx you should just say ngx.exit = ... and so on since ngx is a global. You also cannot replace var on the global field because it is actual a c code invocation. It would be better if you would show me the actual testing and tell me more on what you want to verify, you want to verify ngx calls if they are executing or what?

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

No branches or pull requests

2 participants