Skip to content

Commit

Permalink
[TT-12957] OAS Uptime Tests migrations (#6852)
Browse files Browse the repository at this point in the history
### **PR Type**
Enhancement, Tests


___

### **Description**
- Renamed `Test` struct to `UptimeTests` for clarity.

- Introduced `UptimeTestsServiceDiscovery` with `RecheckWait` field.

- Updated methods and logic to support the new struct names.

- Adjusted and added corresponding unit tests for the changes.


___



### **Changes walkthrough** 📝
<table><thead><tr><th></th><th align="left">Relevant
files</th></tr></thead><tbody><tr><td><strong>Enhancement</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>upstream.go</strong><dd><code>Refactor and enhance
uptime test configuration</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
<hr>

apidef/oas/upstream.go

<li>Renamed <code>Test</code> struct to <code>UptimeTests</code>.<br>
<li> Added <code>UptimeTestsServiceDiscovery</code> with
<code>RecheckWait</code> field.<br> <li> Updated methods
<code>Fill</code> and <code>ExtractTo</code> to handle new struct.<br>
<li> Introduced <code>NewUptimeTestsServiceDiscovery</code> constructor.


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6852/files#diff-7b0941c7f37fe5a2a23047e0822a65519ca11c371660f36555b59a60f000e3f4">+33/-19</a>&nbsp;
</td>

</tr>
</table></td></tr><tr><td><strong>Tests</strong></td><td><table>
<tr>
  <td>
    <details>
<summary><strong>upstream_test.go</strong><dd><code>Update unit tests
for UptimeTests struct</code>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; </dd></summary>
<hr>

apidef/oas/upstream_test.go

<li>Renamed test function <code>TestTest</code> to
<code>TestUptimeTests</code>.<br> <li> Updated test logic to align with
<code>UptimeTests</code> struct.


</details>


  </td>
<td><a
href="https://github.com/TykTechnologies/tyk/pull/6852/files#diff-222cc254c0c6c09fa0cf50087860b837a0873e2aef3c84ec7d80b1014c149057">+3/-3</a>&nbsp;
&nbsp; &nbsp; </td>

</tr>
</table></td></tr></tr></tbody></table>

___

> <details> <summary> Need help?</summary><li>Type <code>/help how to
...</code> in the comments thread for any questions about PR-Agent
usage.</li><li>Check out the <a
href="https://qodo-merge-docs.qodo.ai/usage-guide/">documentation</a>
for more information.</li></details>

---------

Co-authored-by: Tit Petric <[email protected]>
  • Loading branch information
titpetric and Tit Petric authored Feb 12, 2025
1 parent e0b12d7 commit 107fe7e
Show file tree
Hide file tree
Showing 8 changed files with 473 additions and 70 deletions.
45 changes: 29 additions & 16 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,22 +595,6 @@ type ResponseProcessor struct {
Options interface{} `bson:"options" json:"options"`
}

type HostCheckObject struct {
CheckURL string `bson:"url" json:"url"`
Protocol string `bson:"protocol" json:"protocol"`
Timeout time.Duration `bson:"timeout" json:"timeout"`
EnableProxyProtocol bool `bson:"enable_proxy_protocol" json:"enable_proxy_protocol"`
Commands []CheckCommand `bson:"commands" json:"commands"`
Method string `bson:"method" json:"method"`
Headers map[string]string `bson:"headers" json:"headers"`
Body string `bson:"body" json:"body"`
}

type CheckCommand struct {
Name string `bson:"name" json:"name"`
Message string `bson:"message" json:"message"`
}

type ServiceDiscoveryConfiguration struct {
UseDiscoveryService bool `bson:"use_discovery_service" json:"use_discovery_service"`
QueryEndpoint string `bson:"query_endpoint" json:"query_endpoint"`
Expand Down Expand Up @@ -903,11 +887,40 @@ type AnalyticsPluginConfig struct {
FuncName string `bson:"func_name" json:"func_name,omitempty"`
}

// UptimeTests holds the test configuration for uptime tests.
type UptimeTests struct {
CheckList []HostCheckObject `bson:"check_list" json:"check_list"`
Config UptimeTestsConfig `bson:"config" json:"config"`
}

// UptimeTestCommand handles additional checks for tcp connections.
type CheckCommand struct {
Name string `bson:"name" json:"name"`
Message string `bson:"message" json:"message"`
}

// HostCheckObject represents a single uptime test check.
type HostCheckObject struct {
CheckURL string `bson:"url" json:"url"`
Protocol string `bson:"protocol" json:"protocol"`
Timeout time.Duration `bson:"timeout" json:"timeout"`
EnableProxyProtocol bool `bson:"enable_proxy_protocol" json:"enable_proxy_protocol"`
Commands []CheckCommand `bson:"commands" json:"commands"`
Method string `bson:"method" json:"method"`
Headers map[string]string `bson:"headers" json:"headers"`
Body string `bson:"body" json:"body"`
}

// AddCommand will append a new command to the test.
func (h *HostCheckObject) AddCommand(name, message string) {
command := CheckCommand{
Name: name,
Message: message,
}

h.Commands = append(h.Commands, command)
}

type UptimeTestsConfig struct {
ExpireUptimeAnalyticsAfter int64 `bson:"expire_utime_after" json:"expire_utime_after"` // must have an expireAt TTL index set (http://docs.mongodb.org/manual/tutorial/expire-data/)
ServiceDiscovery ServiceDiscoveryConfiguration `bson:"service_discovery" json:"service_discovery"`
Expand Down
1 change: 1 addition & 0 deletions apidef/oas/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tasks:
test:
desc: "Run tests"
cmds:
- cd schema && ./build.sh && cd -
- go fmt ./...
- go test -count=1 ./...

Expand Down
23 changes: 23 additions & 0 deletions apidef/oas/linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ func TestXTykGateway_Lint(t *testing.T) {
OAuth: nil,
}

settings.Upstream.UptimeTests = &UptimeTests{
HostDownRetestPeriod: ReadableDuration(10 * time.Second),
LogRetentionPeriod: ReadableDuration(10 * time.Second),
Tests: []UptimeTest{
{
Timeout: ReadableDuration(10 * time.Second),
Commands: []UptimeTestCommand{
{
Name: "send",
Message: "PING",
},
{
Name: "recv",
Message: "+PONG",
},
},
Headers: map[string]string{
"Request-Id": "1",
},
},
},
}

settings.Upstream.TLSTransport.MinVersion = "1.2"
settings.Upstream.TLSTransport.MaxVersion = "1.2"
settings.Upstream.TLSTransport.Ciphers = []string{"TLS_RSA_WITH_RC4_128_SHA"}
Expand Down
11 changes: 0 additions & 11 deletions apidef/oas/oas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,7 @@ func TestOAS_ExtractTo_ResetAPIDefinition(t *testing.T) {
"APIDefinition.VersionData.Versions[0].ExtendedPaths.PersistGraphQL[0].Operation",
"APIDefinition.VersionData.Versions[0].ExtendedPaths.PersistGraphQL[0].Variables[0]",
"APIDefinition.VersionData.Versions[0].IgnoreEndpointCase",
"APIDefinition.UptimeTests.CheckList[0].CheckURL",
"APIDefinition.UptimeTests.CheckList[0].Protocol",
"APIDefinition.UptimeTests.CheckList[0].Timeout",
"APIDefinition.UptimeTests.CheckList[0].EnableProxyProtocol",
"APIDefinition.UptimeTests.CheckList[0].Commands[0].Name",
"APIDefinition.UptimeTests.CheckList[0].Commands[0].Message",
"APIDefinition.UptimeTests.CheckList[0].Method",
"APIDefinition.UptimeTests.CheckList[0].Headers[0]",
"APIDefinition.UptimeTests.CheckList[0].Body",
"APIDefinition.UptimeTests.Config.ExpireUptimeAnalyticsAfter",
"APIDefinition.UptimeTests.Config.ServiceDiscovery.CacheDisabled",
"APIDefinition.UptimeTests.Config.RecheckWait",
"APIDefinition.Proxy.DisableStripSlash",
"APIDefinition.Proxy.CheckHostAgainstUptimeTests",
"APIDefinition.DisableQuota",
Expand Down
93 changes: 79 additions & 14 deletions apidef/oas/schema/x-tyk-api-gateway.json
Original file line number Diff line number Diff line change
Expand Up @@ -1341,13 +1341,8 @@
"serviceDiscovery": {
"$ref": "#/definitions/X-Tyk-ServiceDiscovery"
},
"test": {
"type": "object",
"properties": {
"serviceDiscovery": {
"$ref": "#/definitions/X-Tyk-ServiceDiscovery"
}
}
"uptimeTests": {
"$ref": "#/definitions/X-Tyk-UptimeTests"
},
"mutualTLS": {
"$ref": "#/definitions/X-Tyk-MutualTLS"
Expand Down Expand Up @@ -2405,14 +2400,84 @@
},
"X-Tyk-PreserveHostHeader": {
"type": "object",
"properties": {
"enabled": {
"type": "boolean"
}
"properties": {
"enabled": {
"type": "boolean"
}
},
"required": [
"enabled"
]
},
"X-Tyk-UptimeTestCommand": {
"type": "object",
"properties": {
"message": {
"type": "string"
},
"required": [
"enabled"
]
"name": {
"type": "string"
}
}
},
"X-Tyk-UptimeTest": {
"type": "object",
"properties": {
"body": {
"type": "string"
},
"commands": {
"type": "array",
"items": {
"$ref": "#/definitions/X-Tyk-UptimeTestCommand"
}
},
"enableProxyProtocol": {
"type": "boolean"
},
"headers": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"method": {
"type": "string"
},
"protocol": {
"type": "string"
},
"timeout": {
"$ref": "#/definitions/X-Tyk-ReadableDuration"
},
"url": {
"type": "string"
}
}
},
"X-Tyk-UptimeTests": {
"type": "object",
"properties": {
"hostDownRetestPeriod": {
"$ref": "#/definitions/X-Tyk-ReadableDuration"
},
"logRetentionPeriod": {
"$ref": "#/definitions/X-Tyk-ReadableDuration"
},
"serviceDiscovery": {
"$ref": "#/definitions/X-Tyk-ServiceDiscovery"
},
"tests": {
"type": "array",
"items": {
"$ref": "#/definitions/X-Tyk-UptimeTest"
}
}
}
},
"X-Tyk-ReadableDuration": {
"type": "string",
"pattern": "^(\\d+h)?(\\d+m)?(\\d+s)?$"
},
"X-Tyk-LoadBalancingTarget": {
"type": "object",
Expand Down
Loading

0 comments on commit 107fe7e

Please sign in to comment.