|
| 1 | +/* |
| 2 | +SPDX-FileCopyrightText: Copyright (c) NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package dsl |
| 19 | + |
| 20 | +import ( |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +func TestHelmfileEnvironmentPathSupportsKnownStacks(t *testing.T) { |
| 27 | + repoRoot := t.TempDir() |
| 28 | + for _, stack := range []string{"self-managed", "observability", "nvcf-compute-plane"} { |
| 29 | + t.Run(stack, func(t *testing.T) { |
| 30 | + got, err := HelmfileEnvironmentPath(repoRoot, stack, "local-bdd") |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("environment path: %v", err) |
| 33 | + } |
| 34 | + want := filepath.Join(repoRoot, "deploy", "stacks", stack, "environments", "local-bdd.yaml") |
| 35 | + if got != want { |
| 36 | + t.Fatalf("path = %q, want %q", got, want) |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestHelmfileEnvironmentPathRejectsInvalidInput(t *testing.T) { |
| 43 | + repoRoot := t.TempDir() |
| 44 | + tests := []struct { |
| 45 | + name string |
| 46 | + root string |
| 47 | + stack string |
| 48 | + environment string |
| 49 | + want string |
| 50 | + }{ |
| 51 | + {name: "relative root", root: "repo", stack: "self-managed", environment: "local", want: "repository root must be absolute"}, |
| 52 | + {name: "unknown stack", root: repoRoot, stack: "other", environment: "local", want: `unsupported Helmfile stack "other"`}, |
| 53 | + {name: "empty environment", root: repoRoot, stack: "self-managed", environment: "", want: `invalid Helmfile environment name ""`}, |
| 54 | + {name: "path traversal", root: repoRoot, stack: "self-managed", environment: "../local", want: `invalid Helmfile environment name "../local"`}, |
| 55 | + {name: "path separator", root: repoRoot, stack: "self-managed", environment: "team/local", want: `invalid Helmfile environment name "team/local"`}, |
| 56 | + } |
| 57 | + for _, tc := range tests { |
| 58 | + t.Run(tc.name, func(t *testing.T) { |
| 59 | + _, err := HelmfileEnvironmentPath(tc.root, tc.stack, tc.environment) |
| 60 | + if err == nil || !strings.Contains(err.Error(), tc.want) { |
| 61 | + t.Fatalf("err = %v, want containing %q", err, tc.want) |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
0 commit comments