@@ -58,7 +58,7 @@ def test_logs_requests(self, emit):
58
58
"""
59
59
60
60
emit .return_value = True
61
- self .useFixture (fixtures .ConfFixture ())
61
+ conf = self .useFixture (fixtures .ConfFixture ()). conf
62
62
self .useFixture (fixtures .RPCFixture ('nova.test' ))
63
63
api = self .useFixture (fixtures .OSAPIFixture ()).api
64
64
@@ -73,6 +73,25 @@ def test_logs_requests(self, emit):
73
73
'"GET /" status: 200 len: %s' % content_length )
74
74
self .assertIn (log1 , self .stdlog .logger .output )
75
75
76
+ # Verify handling of X-Forwarded-For header, example: load balancer.
77
+ # First, try without setting CONF.api.use_forwarded_for, it should not
78
+ # use the header value.
79
+ headers = {'X-Forwarded-For' : '1.2.3.4' }
80
+ resp = api .api_request ('/' , strip_version = True , headers = headers )
81
+ content_length = resp .headers ['content-length' ]
82
+ log2 = ('INFO [nova.api.openstack.requestlog] 127.0.0.1 '
83
+ '"GET /" status: 200 len: %s' % content_length )
84
+ self .assertIn (log2 , self .stdlog .logger .output )
85
+
86
+ # Now set CONF.api.use_forwarded_for, it should use the header value.
87
+ conf .set_override ('use_forwarded_for' , True , 'api' )
88
+ headers = {'X-Forwarded-For' : '1.2.3.4' }
89
+ resp = api .api_request ('/' , strip_version = True , headers = headers )
90
+ content_length = resp .headers ['content-length' ]
91
+ log3 = ('INFO [nova.api.openstack.requestlog] 1.2.3.4 '
92
+ '"GET /" status: 200 len: %s' % content_length )
93
+ self .assertIn (log3 , self .stdlog .logger .output )
94
+
76
95
@mock .patch ('nova.api.openstack.requestlog.RequestLog._should_emit' )
77
96
def test_logs_mv (self , emit ):
78
97
"""Ensure logs register microversion if passed.
0 commit comments