|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package constants |
| 22 | + |
| 23 | +import ( |
| 24 | + "fmt" |
| 25 | + "os" |
| 26 | + "path" |
| 27 | + "strings" |
| 28 | + "testing" |
| 29 | + |
| 30 | + "github.com/dchest/uniuri" |
| 31 | + "github.com/stretchr/testify/require" |
| 32 | +) |
| 33 | + |
| 34 | +func withFile(t *testing.T, ns string) func(in func(file string)) { |
| 35 | + return func(in func(key string)) { |
| 36 | + p := t.TempDir() |
| 37 | + |
| 38 | + ret := path.Join(p, strings.ToLower(uniuri.NewLen(32))) |
| 39 | + |
| 40 | + require.NoError(t, os.WriteFile(ret, []byte(ns), 0644)) |
| 41 | + |
| 42 | + in(ret) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func withEnv(t *testing.T, ns string) func(in func(env string)) { |
| 47 | + return func(in func(key string)) { |
| 48 | + key := fmt.Sprintf("MY_NS_ENV_%s", strings.ToUpper(uniuri.NewLen(8))) |
| 49 | + |
| 50 | + require.NoError(t, os.Setenv(key, ns)) |
| 51 | + defer func() { |
| 52 | + require.NoError(t, os.Unsetenv(key)) |
| 53 | + }() |
| 54 | + |
| 55 | + in(key) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func Test_Namespace(t *testing.T) { |
| 60 | + t.Run("Default", func(t *testing.T) { |
| 61 | + n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, EnvOperatorPodNamespace) |
| 62 | + require.False(t, ok) |
| 63 | + require.EqualValues(t, "", n) |
| 64 | + }) |
| 65 | + t.Run("With Env", func(t *testing.T) { |
| 66 | + withEnv(t, "myNs1")(func(env string) { |
| 67 | + n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, env) |
| 68 | + require.True(t, ok) |
| 69 | + require.EqualValues(t, "myNs1", n) |
| 70 | + }) |
| 71 | + }) |
| 72 | + t.Run("With Empty Env", func(t *testing.T) { |
| 73 | + withEnv(t, "")(func(env string) { |
| 74 | + n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, env) |
| 75 | + require.False(t, ok) |
| 76 | + require.EqualValues(t, "", n) |
| 77 | + }) |
| 78 | + }) |
| 79 | + t.Run("With Whitespace Env", func(t *testing.T) { |
| 80 | + withEnv(t, " \n ")(func(env string) { |
| 81 | + n, ok := namespaceWithSAAndEnv(PathMountServiceAccountNamespace, env) |
| 82 | + require.False(t, ok) |
| 83 | + require.EqualValues(t, "", n) |
| 84 | + }) |
| 85 | + }) |
| 86 | + t.Run("With File", func(t *testing.T) { |
| 87 | + withFile(t, "myNs2")(func(file string) { |
| 88 | + n, ok := namespaceWithSAAndEnv(file, EnvOperatorPodNamespace) |
| 89 | + require.True(t, ok) |
| 90 | + require.EqualValues(t, "myNs2", n) |
| 91 | + }) |
| 92 | + }) |
| 93 | + t.Run("With Missing File", func(t *testing.T) { |
| 94 | + withFile(t, "myNs2")(func(file string) { |
| 95 | + n, ok := namespaceWithSAAndEnv(fmt.Sprintf("%s.missing", file), EnvOperatorPodNamespace) |
| 96 | + require.False(t, ok) |
| 97 | + require.EqualValues(t, "", n) |
| 98 | + }) |
| 99 | + }) |
| 100 | + t.Run("With Empty File", func(t *testing.T) { |
| 101 | + withFile(t, "")(func(file string) { |
| 102 | + n, ok := namespaceWithSAAndEnv(file, EnvOperatorPodNamespace) |
| 103 | + require.False(t, ok) |
| 104 | + require.EqualValues(t, "", n) |
| 105 | + }) |
| 106 | + }) |
| 107 | + t.Run("With Whitespace File", func(t *testing.T) { |
| 108 | + withFile(t, " \n ")(func(file string) { |
| 109 | + n, ok := namespaceWithSAAndEnv(file, EnvOperatorPodNamespace) |
| 110 | + require.False(t, ok) |
| 111 | + require.EqualValues(t, "", n) |
| 112 | + }) |
| 113 | + }) |
| 114 | + t.Run("With File & Env", func(t *testing.T) { |
| 115 | + withFile(t, "myNs2")(func(file string) { |
| 116 | + withEnv(t, "myNs1")(func(env string) { |
| 117 | + n, ok := namespaceWithSAAndEnv(file, env) |
| 118 | + require.True(t, ok) |
| 119 | + require.EqualValues(t, "myNs1", n) |
| 120 | + }) |
| 121 | + }) |
| 122 | + }) |
| 123 | +} |
0 commit comments