|
| 1 | +// Copyright Cozystack Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Contract: `talm init --name <X>` validates X as a DNS-1123 |
| 16 | +// subdomain in PreRunE before any file is written. The check uses |
| 17 | +// k8s.io/apimachinery/pkg/util/validation.IsDNS1123Subdomain so the |
| 18 | +// error wording, character class, and length limits match the |
| 19 | +// upstream Kubernetes contract that the rendered Talos config |
| 20 | +// downstream relies on. |
| 21 | + |
| 22 | +package commands |
| 23 | + |
| 24 | +import ( |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | +) |
| 28 | + |
| 29 | +// withInitFlagsSnapshot captures the package-level initCmdFlags so a |
| 30 | +// test can mutate them without leaking into subsequent tests. |
| 31 | +func withInitFlagsSnapshot(t *testing.T) { |
| 32 | + t.Helper() |
| 33 | + saved := initCmdFlags |
| 34 | + t.Cleanup(func() { initCmdFlags = saved }) |
| 35 | +} |
| 36 | + |
| 37 | +// Contract: a valid DNS-1123 subdomain passes PreRunE without |
| 38 | +// touching the validation guard. Includes single-label, multi-label |
| 39 | +// (`my.cluster.example`), leading-digit, dashes-in-the-middle. The |
| 40 | +// shipped chart names (`cozystack`, `generic`, `talm`) ALL must |
| 41 | +// pass — they are valid by construction, so a regression in the |
| 42 | +// validator that rejected them would brick every default install. |
| 43 | +func TestContract_InitPreRun_AcceptsValidDNS1123Subdomain(t *testing.T) { |
| 44 | + withInitFlagsSnapshot(t) |
| 45 | + |
| 46 | + cases := []string{ |
| 47 | + "cozystack", |
| 48 | + "generic", |
| 49 | + "talm", |
| 50 | + "my-cluster", |
| 51 | + "my.cluster.example", |
| 52 | + "1leading-digit", |
| 53 | + "a", // single character |
| 54 | + "prod-2", // trailing digit |
| 55 | + } |
| 56 | + for _, name := range cases { |
| 57 | + t.Run(name, func(t *testing.T) { |
| 58 | + initCmdFlags.preset = "cozystack" |
| 59 | + initCmdFlags.name = name |
| 60 | + initCmdFlags.encrypt = false |
| 61 | + initCmdFlags.decrypt = false |
| 62 | + initCmdFlags.update = false |
| 63 | + initCmdFlags.image = "" |
| 64 | + if err := initCmd.PreRunE(initCmd, nil); err != nil { |
| 65 | + t.Errorf("expected %q to pass PreRunE, got: %v", name, err) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// Contract: an invalid DNS-1123 subdomain is rejected in PreRunE |
| 72 | +// with an error that names the offending value AND includes the |
| 73 | +// upstream k8s validator message (so the operator sees the precise |
| 74 | +// constraint that was violated). Each row covers a distinct |
| 75 | +// failure class so a regression that loosens the validator only on |
| 76 | +// some axis surfaces here. |
| 77 | +func TestContract_InitPreRun_RejectsInvalidDNS1123Subdomain(t *testing.T) { |
| 78 | + withInitFlagsSnapshot(t) |
| 79 | + |
| 80 | + cases := []struct { |
| 81 | + name string |
| 82 | + clusterName string |
| 83 | + expectInMsg string // substring of the k8s validator message |
| 84 | + }{ |
| 85 | + {"uppercase", "MyCluster", "lower case"}, |
| 86 | + {"underscore", "my_cluster", "alphanumeric"}, |
| 87 | + {"leading dash", "-bad", "alphanumeric"}, |
| 88 | + {"trailing dash", "bad-", "alphanumeric"}, |
| 89 | + {"space", "my cluster", "alphanumeric"}, |
| 90 | + {"empty label between dots", "foo..bar", "alphanumeric"}, |
| 91 | + {"subdomain too long", strings.Repeat("a", 254), "253"}, |
| 92 | + } |
| 93 | + for _, tc := range cases { |
| 94 | + t.Run(tc.name, func(t *testing.T) { |
| 95 | + initCmdFlags.preset = "cozystack" |
| 96 | + initCmdFlags.name = tc.clusterName |
| 97 | + initCmdFlags.encrypt = false |
| 98 | + initCmdFlags.decrypt = false |
| 99 | + initCmdFlags.update = false |
| 100 | + initCmdFlags.image = "" |
| 101 | + |
| 102 | + err := initCmd.PreRunE(initCmd, nil) |
| 103 | + if err == nil { |
| 104 | + t.Fatalf("expected %q to fail PreRunE", tc.clusterName) |
| 105 | + } |
| 106 | + if !strings.Contains(err.Error(), `"`+tc.clusterName+`"`) { |
| 107 | + t.Errorf("error must quote the offending value, got: %v", err) |
| 108 | + } |
| 109 | + if !strings.Contains(err.Error(), "DNS-1123 subdomain") { |
| 110 | + t.Errorf("error must mention 'DNS-1123 subdomain' for grep-ability, got: %v", err) |
| 111 | + } |
| 112 | + if !strings.Contains(err.Error(), tc.expectInMsg) { |
| 113 | + t.Errorf("error must include upstream substring %q, got: %v", tc.expectInMsg, err) |
| 114 | + } |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// Contract: validation runs ONLY when --name applies — under |
| 120 | +// --encrypt / --decrypt / --update the name flag is not required, so |
| 121 | +// the validator must not fire on an empty initCmdFlags.name. Pin |
| 122 | +// so a regression that always validates would break the |
| 123 | +// regenerate-talosconfig flow operators rely on (they pass --decrypt |
| 124 | +// without --name). |
| 125 | +// |
| 126 | +// These modes also require an existing project root, so the test |
| 127 | +// stages a fixture with Chart.yaml + secrets.yaml inside a tempdir |
| 128 | +// and points Config.RootDir at it. Without the project-root setup |
| 129 | +// PreRunE fails earlier than the validator can be reached. |
| 130 | +func TestContract_InitPreRun_SkipsValidationOnExclusiveModes(t *testing.T) { |
| 131 | + withInitFlagsSnapshot(t) |
| 132 | + |
| 133 | + cases := []struct { |
| 134 | + name string |
| 135 | + set func() |
| 136 | + }{ |
| 137 | + {"encrypt", func() { initCmdFlags.encrypt = true }}, |
| 138 | + {"decrypt", func() { initCmdFlags.decrypt = true }}, |
| 139 | + {"update", func() { initCmdFlags.update = true }}, |
| 140 | + } |
| 141 | + for _, tc := range cases { |
| 142 | + t.Run(tc.name, func(t *testing.T) { |
| 143 | + dir := t.TempDir() |
| 144 | + makeProjectRoot(t, dir) |
| 145 | + setRoot(t, dir) |
| 146 | + |
| 147 | + initCmdFlags.preset = "" |
| 148 | + initCmdFlags.name = "" |
| 149 | + initCmdFlags.encrypt = false |
| 150 | + initCmdFlags.decrypt = false |
| 151 | + initCmdFlags.update = false |
| 152 | + initCmdFlags.image = "" |
| 153 | + tc.set() |
| 154 | + |
| 155 | + if err := initCmd.PreRunE(initCmd, nil); err != nil { |
| 156 | + t.Errorf("expected --%s with empty name to pass PreRunE, got: %v", tc.name, err) |
| 157 | + } |
| 158 | + }) |
| 159 | + } |
| 160 | +} |
0 commit comments