2021-09-28 13:11:36 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
require_relative '../../test_helper'
|
|
|
|
|
|
|
|
class Test < ChartTest
|
|
|
|
@@chart = Chart.new('charts/library/common-test')
|
|
|
|
|
|
|
|
describe @@chart.name do
|
|
|
|
describe 'service::ports settings' do
|
2021-09-28 22:11:52 +00:00
|
|
|
baseValues = {
|
|
|
|
service: {
|
|
|
|
main: {
|
|
|
|
ports: {
|
|
|
|
main: {
|
|
|
|
port: 8080
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
default_name = 'main'
|
|
|
|
default_port = 8080
|
|
|
|
|
2021-09-28 22:11:52 +00:00
|
|
|
it 'defaults to name "http" on port 8080' do
|
|
|
|
values = baseValues
|
|
|
|
chart.value values
|
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_equal(default_port, service["spec"]["ports"].first["port"])
|
|
|
|
assert_equal(default_name, service["spec"]["ports"].first["targetPort"])
|
|
|
|
assert_equal(default_name, service["spec"]["ports"].first["name"])
|
|
|
|
|
|
|
|
deployment = chart.resources(kind: "Deployment").first
|
|
|
|
containers = deployment["spec"]["template"]["spec"]["containers"]
|
|
|
|
mainContainer = containers.find{ |c| c["name"] == "common-test" }
|
|
|
|
assert_equal(default_port, mainContainer["ports"].first["containerPort"])
|
|
|
|
assert_equal(default_name, mainContainer["ports"].first["name"])
|
|
|
|
end
|
|
|
|
|
2021-09-28 22:11:52 +00:00
|
|
|
it 'port name can be set' do
|
|
|
|
values = baseValues.deep_merge_override({
|
|
|
|
service: {
|
2021-09-28 13:11:36 +00:00
|
|
|
main: {
|
2021-09-28 22:11:52 +00:00
|
|
|
ports: {
|
|
|
|
main: {
|
|
|
|
enabled: false
|
|
|
|
},
|
|
|
|
server: {
|
|
|
|
enabled: true,
|
|
|
|
port: 8080
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 13:11:36 +00:00
|
|
|
chart.value values
|
2021-09-28 22:11:52 +00:00
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_equal(default_port, service["spec"]["ports"].first["port"])
|
2021-09-28 22:11:52 +00:00
|
|
|
assert_equal('server', service["spec"]["ports"].first["targetPort"])
|
|
|
|
assert_equal('server', service["spec"]["ports"].first["name"])
|
2021-09-28 13:11:36 +00:00
|
|
|
|
|
|
|
deployment = chart.resources(kind: "Deployment").first
|
|
|
|
containers = deployment["spec"]["template"]["spec"]["containers"]
|
|
|
|
mainContainer = containers.find{ |c| c["name"] == "common-test" }
|
|
|
|
assert_equal(default_port, mainContainer["ports"].first["containerPort"])
|
2021-09-28 22:11:52 +00:00
|
|
|
assert_equal('server', mainContainer["ports"].first["name"])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'name suffix can be overridden' do
|
|
|
|
values = baseValues.deep_merge_override({
|
|
|
|
service: {
|
|
|
|
main: {
|
|
|
|
nameOverride: 'http'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
chart.value values
|
|
|
|
|
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test-#{values[:service][:main][:nameOverride]}" }
|
|
|
|
refute_nil(service)
|
2021-09-28 13:11:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'targetPort can be overridden' do
|
2021-09-28 22:11:52 +00:00
|
|
|
values = baseValues.deep_merge_override({
|
|
|
|
service: {
|
2021-09-28 13:11:36 +00:00
|
|
|
main: {
|
2021-09-28 22:11:52 +00:00
|
|
|
ports: {
|
|
|
|
main: {
|
|
|
|
targetPort: 80
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 13:11:36 +00:00
|
|
|
chart.value values
|
2021-09-28 22:11:52 +00:00
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_equal(default_port, service["spec"]["ports"].first["port"])
|
2021-09-28 22:11:52 +00:00
|
|
|
assert_equal(80, service["spec"]["ports"].first["targetPort"])
|
2021-09-28 13:11:36 +00:00
|
|
|
assert_equal(default_name, service["spec"]["ports"].first["name"])
|
|
|
|
|
|
|
|
deployment = chart.resources(kind: "Deployment").first
|
|
|
|
containers = deployment["spec"]["template"]["spec"]["containers"]
|
|
|
|
mainContainer = containers.find{ |c| c["name"] == "common-test" }
|
2021-09-28 22:11:52 +00:00
|
|
|
assert_equal(80, mainContainer["ports"].first["containerPort"])
|
2021-09-28 13:11:36 +00:00
|
|
|
assert_equal(default_name, mainContainer["ports"].first["name"])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'targetPort cannot be a named port' do
|
2021-09-28 22:11:52 +00:00
|
|
|
values = baseValues.deep_merge_override({
|
|
|
|
service: {
|
2021-09-28 13:11:36 +00:00
|
|
|
main: {
|
2021-09-28 22:11:52 +00:00
|
|
|
ports: {
|
|
|
|
main: {
|
|
|
|
targetPort: 'test'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 13:11:36 +00:00
|
|
|
chart.value values
|
2021-09-28 22:11:52 +00:00
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
exception = assert_raises HelmCompileError do
|
|
|
|
chart.execute_helm_template!
|
|
|
|
end
|
2021-09-28 22:11:52 +00:00
|
|
|
assert_match("Our charts do not support named ports for targetPort. (port name #{default_name}, targetPort test)", exception.message)
|
2021-09-28 13:11:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'protocol defaults to TCP' do
|
2021-09-28 22:11:52 +00:00
|
|
|
values = baseValues
|
|
|
|
chart.value values
|
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_equal("TCP", service["spec"]["ports"].first["protocol"])
|
|
|
|
|
|
|
|
deployment = chart.resources(kind: "Deployment").first
|
|
|
|
containers = deployment["spec"]["template"]["spec"]["containers"]
|
|
|
|
mainContainer = containers.find{ |c| c["name"] == "common-test" }
|
|
|
|
assert_equal("TCP", mainContainer["ports"].first["protocol"])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'protocol is TCP when set to TCP explicitly' do
|
2021-09-28 22:11:52 +00:00
|
|
|
values = baseValues.deep_merge_override({
|
|
|
|
service: {
|
2021-09-28 13:11:36 +00:00
|
|
|
main: {
|
2021-09-28 22:11:52 +00:00
|
|
|
ports: {
|
|
|
|
main: {
|
|
|
|
protocol: 'TCP'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 13:11:36 +00:00
|
|
|
chart.value values
|
2021-09-28 22:11:52 +00:00
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_equal("TCP", service["spec"]["ports"].first["protocol"])
|
|
|
|
|
|
|
|
deployment = chart.resources(kind: "Deployment").first
|
|
|
|
containers = deployment["spec"]["template"]["spec"]["containers"]
|
|
|
|
mainContainer = containers.find{ |c| c["name"] == "common-test" }
|
|
|
|
assert_equal("TCP", mainContainer["ports"].first["protocol"])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'protocol is TCP when set to HTTP explicitly' do
|
2021-09-28 22:11:52 +00:00
|
|
|
values = baseValues.deep_merge_override({
|
|
|
|
service: {
|
2021-09-28 13:11:36 +00:00
|
|
|
main: {
|
2021-09-28 22:11:52 +00:00
|
|
|
ports: {
|
|
|
|
main: {
|
|
|
|
protocol: 'HTTP'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 13:11:36 +00:00
|
|
|
chart.value values
|
2021-09-28 22:11:52 +00:00
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_equal("TCP", service["spec"]["ports"].first["protocol"])
|
|
|
|
|
|
|
|
deployment = chart.resources(kind: "Deployment").first
|
|
|
|
containers = deployment["spec"]["template"]["spec"]["containers"]
|
|
|
|
mainContainer = containers.find{ |c| c["name"] == "common-test" }
|
|
|
|
assert_equal("TCP", mainContainer["ports"].first["protocol"])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'protocol is TCP when set to HTTPS explicitly' do
|
2021-09-28 22:11:52 +00:00
|
|
|
values = baseValues.deep_merge_override({
|
|
|
|
service: {
|
2021-09-28 13:11:36 +00:00
|
|
|
main: {
|
2021-09-28 22:11:52 +00:00
|
|
|
ports: {
|
|
|
|
main: {
|
|
|
|
protocol: 'HTTPS'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 13:11:36 +00:00
|
|
|
chart.value values
|
2021-09-28 22:11:52 +00:00
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_equal("TCP", service["spec"]["ports"].first["protocol"])
|
|
|
|
|
|
|
|
deployment = chart.resources(kind: "Deployment").first
|
|
|
|
containers = deployment["spec"]["template"]["spec"]["containers"]
|
|
|
|
mainContainer = containers.find{ |c| c["name"] == "common-test" }
|
|
|
|
assert_equal("TCP", mainContainer["ports"].first["protocol"])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'protocol is UDP when set to UDP explicitly' do
|
2021-09-28 22:11:52 +00:00
|
|
|
values = baseValues.deep_merge_override({
|
|
|
|
service: {
|
2021-09-28 13:11:36 +00:00
|
|
|
main: {
|
2021-09-28 22:11:52 +00:00
|
|
|
ports: {
|
|
|
|
main: {
|
|
|
|
protocol: 'UDP'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-09-28 13:11:36 +00:00
|
|
|
chart.value values
|
2021-09-28 22:11:52 +00:00
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_equal("UDP", service["spec"]["ports"].first["protocol"])
|
|
|
|
|
|
|
|
deployment = chart.resources(kind: "Deployment").first
|
|
|
|
containers = deployment["spec"]["template"]["spec"]["containers"]
|
|
|
|
mainContainer = containers.find{ |c| c["name"] == "common-test" }
|
|
|
|
assert_equal("UDP", mainContainer["ports"].first["protocol"])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'No annotations get set by default' do
|
2021-09-28 22:11:52 +00:00
|
|
|
values = baseValues
|
|
|
|
chart.value values
|
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_nil(service["metadata"]["annotations"])
|
|
|
|
end
|
2021-09-28 22:11:52 +00:00
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
it 'TCP port protocol does not set annotations' do
|
2021-09-28 22:11:52 +00:00
|
|
|
values = baseValues.deep_merge_override({
|
|
|
|
service: {
|
2021-09-28 13:11:36 +00:00
|
|
|
main: {
|
2021-09-28 22:11:52 +00:00
|
|
|
ports: {
|
|
|
|
main: {
|
|
|
|
protocol: 'TCP'
|
|
|
|
}
|
2021-09-28 13:11:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 22:11:52 +00:00
|
|
|
})
|
2021-09-28 13:11:36 +00:00
|
|
|
chart.value values
|
2021-09-28 22:11:52 +00:00
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_nil(service["metadata"]["annotations"])
|
|
|
|
end
|
2021-09-28 22:11:52 +00:00
|
|
|
|
2021-09-28 13:11:36 +00:00
|
|
|
it 'HTTPS port protocol sets traefik HTTPS annotation' do
|
2021-09-28 22:11:52 +00:00
|
|
|
values = baseValues.deep_merge_override({
|
|
|
|
service: {
|
2021-09-28 13:11:36 +00:00
|
|
|
main: {
|
2021-09-28 22:11:52 +00:00
|
|
|
ports: {
|
|
|
|
main: {
|
|
|
|
protocol: 'HTTPS'
|
|
|
|
}
|
2021-09-28 13:11:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 22:11:52 +00:00
|
|
|
})
|
2021-09-28 13:11:36 +00:00
|
|
|
chart.value values
|
|
|
|
service = chart.resources(kind: "Service").find{ |s| s["metadata"]["name"] == "common-test" }
|
|
|
|
refute_nil(service)
|
|
|
|
assert_equal("https", service["metadata"]["annotations"]["traefik.ingress.kubernetes.io/service.serversscheme"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|