← Back to Pull requests

test: add unit tests for pkg/tls #3163

Open
magic-peach opened this pull request with 153 lines diff across 2 files
View on GitHub
magic-peach magic-peach commented on Sep 13, 2026 13:32 UTC
Author

Purpose of this PR

pkg/tls parses the --tls-min-version and --tls-cipher-suites flags used by both the webhook and controller manager startup paths (cmd/operator/webhook/start.go and cmd/operator/controller/start.go), but had no test coverage at all.

Proposed changes:

  • Add unit tests for ParseTLSVersion covering supported and unsupported version strings

  • Add unit tests for ParseCipherSuites covering nil/empty input, a valid cipher suite name, and an unknown name

  • Add unit tests for SetupTLS covering invalid input rejection and that the returned options correctly configure a tls.Config
  • Change Category

  • Bugfix (non-breaking change which fixes an issue)

  • Feature (non-breaking change which adds functionality)

  • Breaking change (fix or feature that could affect existing functionality)

  • Documentation update
  • Rationale

    Test-only change, no behavior modified.

    Checklist

  • I have conducted a self-review of my own code.

  • I have updated documentation accordingly.

  • I have added tests that prove my changes are effective or that my feature works.

  • Existing unit tests pass locally with my changes.
  • Additional Notes

    No existing issue tracks this; found while reading the TLS setup code.

    Diff: 153 lines (+153 / -0) across 2 files Reviewer: tariq-hasan Review duration: 30504m
    All Vouch review confidence checks passed
    Residual risk 0.07 = Change Risk (0.58) × (1 − Review Confidence (0.88)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.58 · 153 lines diff across 2 files
    0.58
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.89 · 5 substantive comment(s)
    0.89
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    88%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.301
    Sensitive Paths sensitive_paths
    0.058
    Real PR Conversation Timeline
    10 items
    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Sep 13, 2026 13:33 UTC
    Reviewer

    Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

    github-actions[bot] github-actions[bot] commented on Sep 13, 2026 13:33 UTC
    Bot

    🎉 Welcome to the Kubeflow Spark Operator! 🎉

    Thanks for opening your first PR! We're happy to have you as part of our community 🚀

    Here's what happens next:

  • If you haven't already, please check out our Contributing Guide for repo-specific guidelines and the Kubeflow Contributor Guide for general community standards.

  • Our team will review your PR soon!
  • Join the community:

  • Slack: Join our #kubeflow-spark-on-kubernetes Slack channel.
  • Feel free to ask questions in the comments if you need any help or clarification!
    Thanks again for contributing to Kubeflow! 🙏

    google-oss-prow[bot] google-oss-prow[bot] commented on Sep 13, 2026 13:33 UTC
    Bot

    [APPROVALNOTIFIER] This PR is NOT APPROVED

    This pull-request has been approved by:
    Once this PR has been reviewed and has the lgtm label, please assign jacobsalway for approval. For more information see the Kubernetes Code Review Process.

    The full list of commands accepted by this bot can be found here.

    <details open>
    Needs approval from an approver in each of these files:

  • OWNERS
  • Approvers can indicate their approval by writing /approve in a comment
    Approvers can cancel approval by writing /approve cancel in a comment
    </details>
    <!-- META={"approvers":["jacobsalway"]} -->

    tariq-hasan tariq-hasan requested changes on Sep 13, 2026 15:00 UTC
    Changes Requested Reviewer

    Thanks for the PR @magic-peach! Please rewrite the tests using Ginkgo/Gomega as that would be the default going forward.

    magic-peach magic-peach commented on Sep 13, 2026 16:18 UTC
    Author

    Rewrote the tests using Ginkgo/Gomega, thanks for the pointer.

    RobuRishabh RobuRishabh commented on code on Sep 21, 2026 21:07 UTC
    Code Review
    pkg/tls/tls_test.go:1
    @@ -0,0 +1,121 @@
    +/*
    +Copyright 2026 The Kubeflow authors.
    +
    +Licensed under the Apache License, Version 2.0 (the "License");
    +you may not use this file except in compliance with the License.
    +You may obtain a copy of the License at
    +
    +    https://www.apache.org/licenses/LICENSE-2.0
    +
    +Unless required by applicable law or agreed to in writing, software
    +distributed under the License is distributed on an "AS IS" BASIS,
    +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +See the License for the specific language governing permissions and
    +limitations under the License.
    +*/
    +
    +package tls_test
    +
    +import (
    +	cryptotls "crypto/tls"
    +
    +	. "github.com/onsi/ginkgo/v2"
    +	. "github.com/onsi/gomega"
    +
    +	"github.com/kubeflow/spark-operator/v2/pkg/tls"
    +)
    +
    +var _ = Describe("ParseTLSVersion", func() {
    +	It("parses TLS 1.2", func() {
    +		got, err := tls.ParseTLSVersion("VersionTLS12")
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(got).To(Equal(uint16(cryptotls.VersionTLS12)))
    +	})
    +
    +	It("parses TLS 1.3", func() {
    +		got, err := tls.ParseTLSVersion("VersionTLS13")
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(got).To(Equal(uint16(cryptotls.VersionTLS13)))
    +	})
    +
    +	It("rejects an unsupported version", func() {
    +		_, err := tls.ParseTLSVersion("VersionTLS11")
    +		Expect(err).To(HaveOccurred())
    +	})
    +
    +	It("rejects an empty string", func() {
    +		_, err := tls.ParseTLSVersion("")
    +		Expect(err).To(HaveOccurred())
    +	})
    +})
    +
    +var _ = Describe("ParseCipherSuites", func() {
    +	It("returns nil for nil input", func() {
    +		ids, err := tls.ParseCipherSuites(nil)
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(ids).To(BeNil())
    +	})
    +
    +	It("filters out empty and whitespace entries", func() {
    +		ids, err := tls.ParseCipherSuites([]string{"", "   "})
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(ids).To(BeNil())
    +	})
    +
    +	It("resolves a valid cipher suite name to its id", func() {
    +		suites := cryptotls.CipherSuites()
    +		Expect(suites).NotTo(BeEmpty())
    +		name := suites[0].Name
    +
    +		ids, err := tls.ParseCipherSuites([]string{" " + name + " "})
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(ids).To(Equal([]uint16{suites[0].ID}))
    +	})
    +
    +	It("errors on an unknown cipher suite name", func() {
    +		_, err := tls.ParseCipherSuites([]string{"NOT_A_REAL_CIPHER_SUITE"})
    +		Expect(err).To(HaveOccurred())
    +	})
    +})
    +
    +var _ = Describe("SetupTLS", func() {
    +	It("rejects an invalid min version", func() {
    +		_, err := tls.SetupTLS("bogus", nil)
    +		Expect(err).To(HaveOccurred())
    +	})
    +
    +	It("rejects an invalid cipher suite", func() {
    +		_, err := tls.SetupTLS("VersionTLS12", []string{"NOT_A_REAL_CIPHER_SUITE"})
    +		Expect(err).To(HaveOccurred())
    +	})
    +
    +	It("configures a tls.Config from valid input", func() {
    +		suites := cryptotls.CipherSuites()
    +		Expect(suites).NotTo(BeEmpty())
    +
    +		opts, err := tls.SetupTLS("VersionTLS13", []string{suites[0].Name})
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(opts).NotTo(BeEmpty())
    +
    +		cfg := &cryptotls.Config{}
    +		for _, opt := range opts {
    +			opt(cfg)
    +		}
    +
    +		Expect(cfg.MinVersion).To(Equal(uint16(cryptotls.VersionTLS13)))
    +		Expect(cfg.CipherSuites).To(Equal([]uint16{suites[0].ID}))
    +		Expect(cfg.NextProtos).To(Equal([]string{"h2", "http/1.1"}))
    +	})
    +
    +	It("leaves CipherSuites unset when none are given", func() {
    +		opts, err := tls.SetupTLS("VersionTLS12", nil)
    +		Expect(err).NotTo(HaveOccurred())
    +
    +		cfg := &cryptotls.Config{}
    +		for _, opt := range opts {
    +			opt(cfg)
    +		}
    +
    +		Expect(cfg.CipherSuites).To(BeNil())

    Nil-cipher path under-asserts, Also pin MinVersion and NextProtos.

    RobuRishabh RobuRishabh commented on code on Sep 21, 2026 21:08 UTC
    Code Review
    pkg/tls/tls_test.go:1
    @@ -0,0 +1,121 @@
    +/*
    +Copyright 2026 The Kubeflow authors.
    +
    +Licensed under the Apache License, Version 2.0 (the "License");
    +you may not use this file except in compliance with the License.
    +You may obtain a copy of the License at
    +
    +    https://www.apache.org/licenses/LICENSE-2.0
    +
    +Unless required by applicable law or agreed to in writing, software
    +distributed under the License is distributed on an "AS IS" BASIS,
    +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    +See the License for the specific language governing permissions and
    +limitations under the License.
    +*/
    +
    +package tls_test
    +
    +import (
    +	cryptotls "crypto/tls"
    +
    +	. "github.com/onsi/ginkgo/v2"
    +	. "github.com/onsi/gomega"
    +
    +	"github.com/kubeflow/spark-operator/v2/pkg/tls"
    +)
    +
    +var _ = Describe("ParseTLSVersion", func() {
    +	It("parses TLS 1.2", func() {
    +		got, err := tls.ParseTLSVersion("VersionTLS12")
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(got).To(Equal(uint16(cryptotls.VersionTLS12)))
    +	})
    +
    +	It("parses TLS 1.3", func() {
    +		got, err := tls.ParseTLSVersion("VersionTLS13")
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(got).To(Equal(uint16(cryptotls.VersionTLS13)))
    +	})
    +
    +	It("rejects an unsupported version", func() {
    +		_, err := tls.ParseTLSVersion("VersionTLS11")
    +		Expect(err).To(HaveOccurred())
    +	})
    +
    +	It("rejects an empty string", func() {
    +		_, err := tls.ParseTLSVersion("")
    +		Expect(err).To(HaveOccurred())
    +	})
    +})
    +
    +var _ = Describe("ParseCipherSuites", func() {
    +	It("returns nil for nil input", func() {
    +		ids, err := tls.ParseCipherSuites(nil)
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(ids).To(BeNil())
    +	})
    +
    +	It("filters out empty and whitespace entries", func() {
    +		ids, err := tls.ParseCipherSuites([]string{"", "   "})
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(ids).To(BeNil())
    +	})
    +
    +	It("resolves a valid cipher suite name to its id", func() {
    +		suites := cryptotls.CipherSuites()
    +		Expect(suites).NotTo(BeEmpty())
    +		name := suites[0].Name
    +
    +		ids, err := tls.ParseCipherSuites([]string{" " + name + " "})
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(ids).To(Equal([]uint16{suites[0].ID}))
    +	})
    +
    +	It("errors on an unknown cipher suite name", func() {
    +		_, err := tls.ParseCipherSuites([]string{"NOT_A_REAL_CIPHER_SUITE"})
    +		Expect(err).To(HaveOccurred())
    +	})
    +})
    +
    +var _ = Describe("SetupTLS", func() {
    +	It("rejects an invalid min version", func() {
    +		_, err := tls.SetupTLS("bogus", nil)
    +		Expect(err).To(HaveOccurred())
    +	})
    +
    +	It("rejects an invalid cipher suite", func() {
    +		_, err := tls.SetupTLS("VersionTLS12", []string{"NOT_A_REAL_CIPHER_SUITE"})
    +		Expect(err).To(HaveOccurred())
    +	})
    +
    +	It("configures a tls.Config from valid input", func() {
    +		suites := cryptotls.CipherSuites()
    +		Expect(suites).NotTo(BeEmpty())
    +
    +		opts, err := tls.SetupTLS("VersionTLS13", []string{suites[0].Name})
    +		Expect(err).NotTo(HaveOccurred())
    +		Expect(opts).NotTo(BeEmpty())
    +
    +		cfg := &cryptotls.Config{}
    +		for _, opt := range opts {
    +			opt(cfg)
    +		}
    +
    +		Expect(cfg.MinVersion).To(Equal(uint16(cryptotls.VersionTLS13)))
    +		Expect(cfg.CipherSuites).To(Equal([]uint16{suites[0].ID}))
    +		Expect(cfg.NextProtos).To(Equal([]string{"h2", "http/1.1"}))

    Happy path should use TLS 1.2. Matches flag defaults; cipher suites actually apply there.

    RobuRishabh RobuRishabh commented on Sep 21, 2026 21:08 UTC
    Contributor

    /ok-to-test

    RobuRishabh RobuRishabh commented on Sep 23, 2026 15:34 UTC
    Contributor

    Hi @magic-peach can you address the above comments. Thanks

    Reviewers
    Assignees
    magic-peach magic-peach
    Labels
    risk: low confidence: 88.1% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57.8% (MEDIUM) · Higher = larger blast radius
    57.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 88.1% · Lower = rushed or shallow review
    88.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6.9% · Safe to merge
    6.9%