|
| 1 | +use Test::Nginx::Socket::Lua; |
| 2 | + |
| 3 | +log_level('info'); |
| 4 | +no_long_string(); |
| 5 | +repeat_each(1); |
| 6 | + |
| 7 | +my $enable_tls = $ENV{ETCD_ENABLE_TLS}; |
| 8 | +if ($enable_tls eq "TRUE") { |
| 9 | + plan 'no_plan'; |
| 10 | +} else { |
| 11 | + plan(skip_all => "etcd is not capable for TLS connection"); |
| 12 | +} |
| 13 | + |
| 14 | +our $HttpConfig = <<'_EOC_'; |
| 15 | + lua_socket_log_errors off; |
| 16 | + lua_package_path 'lib/?.lua;/usr/local/share/lua/5.3/?.lua;/usr/share/lua/5.1/?.lua;;'; |
| 17 | + init_by_lua_block { |
| 18 | + local cjson = require("cjson.safe") |
| 19 | +
|
| 20 | + function check_res(data, err, val, status) |
| 21 | + if err then |
| 22 | + ngx.say("err: ", err) |
| 23 | + ngx.exit(200) |
| 24 | + end |
| 25 | +
|
| 26 | + if val then |
| 27 | + if data.body.kvs==nil then |
| 28 | + ngx.exit(404) |
| 29 | + end |
| 30 | + if data.body.kvs and val ~= data.body.kvs[1].value then |
| 31 | + ngx.say("failed to check value") |
| 32 | + ngx.log(ngx.ERR, "failed to check value, got: ", data.body.kvs[1].value, |
| 33 | + ", expect: ", val) |
| 34 | + ngx.exit(200) |
| 35 | + else |
| 36 | + ngx.say("checked val as expect: ", val) |
| 37 | + end |
| 38 | + end |
| 39 | +
|
| 40 | + if status and status ~= data.status then |
| 41 | + ngx.exit(data.status) |
| 42 | + end |
| 43 | + end |
| 44 | + } |
| 45 | +_EOC_ |
| 46 | +
|
| 47 | +run_tests(); |
| 48 | +
|
| 49 | +__DATA__ |
| 50 | +
|
| 51 | +=== TEST 1: TLS no verify |
| 52 | +--- http_config eval: $::HttpConfig |
| 53 | +--- config |
| 54 | + location /t { |
| 55 | + content_by_lua_block { |
| 56 | + local etcd, err = require "resty.etcd" .new({ |
| 57 | + protocol = "v3", |
| 58 | + http_host = { |
| 59 | + "https://127.0.0.1:12379", |
| 60 | + "https://127.0.0.1:22379", |
| 61 | + "https://127.0.0.1:32379", |
| 62 | + }, |
| 63 | + ssl_verify = false, |
| 64 | + }) |
| 65 | + check_res(etcd, err) |
| 66 | +
|
| 67 | + local res, err = etcd:set("/test", { a='abc'}) |
| 68 | + check_res(res, err) |
| 69 | + ngx.say("done") |
| 70 | + } |
| 71 | + } |
| 72 | +--- request |
| 73 | +GET /t |
| 74 | +--- no_error_log |
| 75 | +[error] |
| 76 | +--- response_body |
| 77 | +done |
| 78 | +
|
| 79 | +
|
| 80 | +
|
| 81 | +=== TEST 2: TLS verify |
| 82 | +--- http_config eval: $::HttpConfig |
| 83 | +--- config |
| 84 | + location /t { |
| 85 | + content_by_lua_block { |
| 86 | + local etcd, err = require "resty.etcd" .new({ |
| 87 | + protocol = "v3", |
| 88 | + http_host = { |
| 89 | + "https://127.0.0.1:12379", |
| 90 | + "https://127.0.0.1:22379", |
| 91 | + "https://127.0.0.1:32379", |
| 92 | + }, |
| 93 | + }) |
| 94 | + check_res(etcd, err) |
| 95 | +
|
| 96 | + local res, err = etcd:set("/test", { a='abc'}) |
| 97 | + check_res(res, err) |
| 98 | + ngx.say("done") |
| 99 | + } |
| 100 | + } |
| 101 | +--- request |
| 102 | +GET /t |
| 103 | +--- no_error_log |
| 104 | +[error] |
| 105 | +--- response_body |
| 106 | +err: 18: self signed certificate |
0 commit comments