|
| 1 | +/* |
| 2 | +Copyright 2021 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "os" |
| 21 | + "testing" |
| 22 | + |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 27 | +) |
| 28 | + |
| 29 | +func TestTempDirForObj(t *testing.T) { |
| 30 | + g := NewWithT(t) |
| 31 | + |
| 32 | + got, err := TempDirForObj("", mockObj()) |
| 33 | + g.Expect(err).ToNot(HaveOccurred()) |
| 34 | + g.Expect(got).To(BeADirectory()) |
| 35 | + defer os.RemoveAll(got) |
| 36 | + |
| 37 | + got2, err := TempDirForObj(got, mockObj()) |
| 38 | + g.Expect(err).ToNot(HaveOccurred()) |
| 39 | + g.Expect(got2).To(BeADirectory()) |
| 40 | + defer os.RemoveAll(got2) |
| 41 | + g.Expect(got2).To(ContainSubstring(got)) |
| 42 | +} |
| 43 | + |
| 44 | +func TestTempPathForObj(t *testing.T) { |
| 45 | + tests := []struct { |
| 46 | + name string |
| 47 | + dir string |
| 48 | + suffix string |
| 49 | + want string |
| 50 | + }{ |
| 51 | + { |
| 52 | + name: "default", |
| 53 | + want: os.TempDir() + "/secret-default-foo-", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "with directory", |
| 57 | + dir: "/foo", |
| 58 | + want: "/foo/secret-default-foo-", |
| 59 | + }, |
| 60 | + } |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + g := NewWithT(t) |
| 64 | + got := TempPathForObj(tt.dir, tt.suffix, mockObj()) |
| 65 | + g.Expect(got[:len(got)-32]).To(Equal(tt.want)) |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func Test_pattern(t *testing.T) { |
| 71 | + g := NewWithT(t) |
| 72 | + g.Expect(pattern(mockObj())).To(Equal("secret-default-foo-")) |
| 73 | +} |
| 74 | + |
| 75 | +func mockObj() client.Object { |
| 76 | + return &corev1.Secret{ |
| 77 | + TypeMeta: metav1.TypeMeta{ |
| 78 | + Kind: "Secret", |
| 79 | + }, |
| 80 | + ObjectMeta: metav1.ObjectMeta{ |
| 81 | + Name: "foo", |
| 82 | + Namespace: "default", |
| 83 | + }, |
| 84 | + } |
| 85 | +} |
0 commit comments