Files
akvorado/common/remotedatasource/config_test.go
Vincent Bernat a2339312ac
Some checks failed
CI / 🤖 Check dependabot status (push) Has been cancelled
CI / 🐧 Test on Linux (${{ github.ref_type == 'tag' }}, misc) (push) Has been cancelled
CI / 🐧 Test on Linux (coverage) (push) Has been cancelled
CI / 🐧 Test on Linux (regular) (push) Has been cancelled
CI / ❄️ Build on Nix (push) Has been cancelled
CI / 🍏 Build and test on macOS (push) Has been cancelled
CI / 🧪 End-to-end testing (push) Has been cancelled
CI / 🔍 Upload code coverage (push) Has been cancelled
CI / 🔬 Test only Go (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 20) (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 22) (push) Has been cancelled
CI / 🔬 Test only JS (${{ needs.dependabot.outputs.package-ecosystem }}, 24) (push) Has been cancelled
CI / ⚖️ Check licenses (push) Has been cancelled
CI / 🐋 Build Docker images (push) Has been cancelled
CI / 🐋 Tag Docker images (push) Has been cancelled
CI / 🚀 Publish release (push) Has been cancelled
Update Nix dependency hashes / Update dependency hashes (push) Has been cancelled
common/remotedatasource: accept specific TLS configuration
2025-10-29 22:34:38 +01:00

139 lines
3.2 KiB
Go

// SPDX-FileCopyrightText: 2024 Free Mobile
// SPDX-License-Identifier: AGPL-3.0-only
package remotedatasource
import (
"testing"
"time"
"github.com/gin-gonic/gin"
"akvorado/common/helpers"
)
func TestSourceDecode(t *testing.T) {
helpers.TestConfigurationDecode(t, helpers.ConfigurationDecodeCases{
{
Description: "Empty",
Initial: func() any { return Source{} },
Configuration: func() any {
return gin.H{
"url": "https://example.net",
"interval": "10m",
}
},
Expected: Source{
URL: "https://example.net",
Method: "GET",
Timeout: time.Minute,
Interval: 10 * time.Minute,
TLS: helpers.TLSConfiguration{
Verify: true,
},
},
}, {
Description: "Simple transform",
Initial: func() any { return Source{} },
Configuration: func() any {
return gin.H{
"url": "https://example.net",
"interval": "10m",
"transform": ".[]",
}
},
Expected: Source{
URL: "https://example.net",
Method: "GET",
Timeout: time.Minute,
Interval: 10 * time.Minute,
Transform: MustParseTransformQuery(".[]"),
TLS: helpers.TLSConfiguration{
Verify: true,
},
},
}, {
Description: "Use POST",
Initial: func() any { return Source{} },
Configuration: func() any {
return gin.H{
"url": "https://example.net",
"method": "POST",
"timeout": "2m",
"interval": "10m",
"transform": ".[]",
}
},
Expected: Source{
URL: "https://example.net",
Method: "POST",
Timeout: 2 * time.Minute,
Interval: 10 * time.Minute,
Transform: MustParseTransformQuery(".[]"),
TLS: helpers.TLSConfiguration{
Verify: true,
},
},
}, {
Description: "With TLS configuration",
Initial: func() any { return Source{} },
Configuration: func() any {
return gin.H{
"url": "https://example.net",
"interval": "10m",
"tls": gin.H{
"enable": true,
"ca-file": "something.crt",
},
}
},
Expected: Source{
URL: "https://example.net",
Method: "GET",
Timeout: time.Minute,
Interval: 10 * time.Minute,
TLS: helpers.TLSConfiguration{
Enable: true,
Verify: false, // TODO this should be fixed
CAFile: "something.crt",
},
},
}, {
Description: "Complex transform",
Initial: func() any { return Source{} },
Configuration: func() any {
return gin.H{
"url": "https://example.net",
"interval": "10m",
"transform": `
.prefixes[] | {prefix: .ip_prefix, tenant: "amazon", region: .region, role: .service|ascii_downcase}
`,
}
},
Expected: Source{
URL: "https://example.net",
Method: "GET",
Timeout: time.Minute,
Interval: 10 * time.Minute,
Transform: MustParseTransformQuery(`
.prefixes[] | {prefix: .ip_prefix, tenant: "amazon", region: .region, role: .service|ascii_downcase}
`),
TLS: helpers.TLSConfiguration{
Verify: true,
},
},
}, {
Description: "Incorrect transform",
Initial: func() any { return Source{} },
Configuration: func() any {
return gin.H{
"url": "https://example.net",
"interval": "10m",
"transform": "878778&&",
}
},
Error: true,
},
})
}