-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add validation to not fail on same secret (#239)
* fix: Add validation to not fail on same secret Signed-off-by: Diego Alfonso <[email protected]> * test: Add unit test to GetSecret func Signed-off-by: Diego Alfonso <[email protected]> * fix: Changed the validation to be on load Signed-off-by: Diego Alfonso <[email protected]> * test: Remove e2e and assert error message Signed-off-by: Diego Alfonso <[email protected]> --------- Signed-off-by: Diego Alfonso <[email protected]>
- Loading branch information
1 parent
99f8a36
commit 43a7724
Showing
2 changed files
with
247 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/vmware-tanzu/carvel-vendir/pkg/vendir/config" | ||
) | ||
|
@@ -27,3 +28,234 @@ kind: Config`) | |
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestSecretsForNewConfigFromFiles(t *testing.T) { | ||
t.Run("Config with single secret", func(t *testing.T) { | ||
tempConfigPath := filepath.Join(t.TempDir(), "config.yml") | ||
configWithWhitespace := []byte(` | ||
apiVersion: vendir.k14s.io/v1alpha1 | ||
kind: Config | ||
directories: | ||
- path: "repo" | ||
contents: | ||
- path: "folder-1" | ||
git: | ||
url: [email protected]:my-user/my-repo.git | ||
secretRef: | ||
name: ssh-key-secret | ||
ref: origin/main | ||
includePaths: | ||
- folder-1/**/* | ||
- path: "folder-2" | ||
git: | ||
url: [email protected]:my-user/my-repo.git | ||
secretRef: | ||
name: ssh-key-secret | ||
ref: origin/main | ||
includePaths: | ||
- folder-2/**/* | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: FOO= | ||
kind: Secret | ||
metadata: | ||
name: ssh-key-secret | ||
`) | ||
|
||
require.NoError(t, os.WriteFile(tempConfigPath, configWithWhitespace, 0666)) | ||
|
||
_, _, _, err := config.NewConfigFromFiles([]string{tempConfigPath}) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("Config with same secret", func(t *testing.T) { | ||
tempConfigPath := filepath.Join(t.TempDir(), "config.yml") | ||
configWithWhitespace := []byte(` | ||
apiVersion: vendir.k14s.io/v1alpha1 | ||
kind: Config | ||
directories: | ||
- path: "repo" | ||
contents: | ||
- path: "folder-1" | ||
git: | ||
url: [email protected]:my-user/my-repo.git | ||
secretRef: | ||
name: ssh-key-secret | ||
ref: origin/main | ||
includePaths: | ||
- folder-1/**/* | ||
- path: "folder-2" | ||
git: | ||
url: [email protected]:my-user/my-repo.git | ||
secretRef: | ||
name: ssh-key-secret | ||
ref: origin/main | ||
includePaths: | ||
- folder-2/**/* | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: FOO= | ||
kind: Secret | ||
metadata: | ||
name: ssh-key-secret | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: FOO= | ||
kind: Secret | ||
metadata: | ||
name: ssh-key-secret | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: FOO= | ||
kind: Secret | ||
metadata: | ||
name: ssh-key-secret | ||
`) | ||
|
||
require.NoError(t, os.WriteFile(tempConfigPath, configWithWhitespace, 0666)) | ||
|
||
_, _, _, err := config.NewConfigFromFiles([]string{tempConfigPath}) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("Config with multiple secret", func(t *testing.T) { | ||
tempConfigPath := filepath.Join(t.TempDir(), "config.yml") | ||
configWithWhitespace := []byte(` | ||
apiVersion: vendir.k14s.io/v1alpha1 | ||
kind: Config | ||
directories: | ||
- path: "repo" | ||
contents: | ||
- path: "folder-1" | ||
git: | ||
url: [email protected]:my-user/my-repo.git | ||
secretRef: | ||
name: ssh-key-secret | ||
ref: origin/main | ||
includePaths: | ||
- folder-1/**/* | ||
- path: "folder-2" | ||
git: | ||
url: [email protected]:my-user/my-repo.git | ||
secretRef: | ||
name: ssh-key-secret | ||
ref: origin/main | ||
includePaths: | ||
- folder-2/**/* | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: FOO= | ||
kind: Secret | ||
metadata: | ||
name: ssh-key-secret | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: FOO= | ||
kind: Secret | ||
metadata: | ||
name: ssh-key-secret | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: FOO= | ||
kind: Secret | ||
metadata: | ||
name: ssh-key-secret | ||
--- | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: BAR= | ||
kind: Secret | ||
metadata: | ||
name: another-secret | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: BAR= | ||
kind: Secret | ||
metadata: | ||
name: another-secret | ||
`) | ||
|
||
require.NoError(t, os.WriteFile(tempConfigPath, configWithWhitespace, 0666)) | ||
|
||
_, s, _, err := config.NewConfigFromFiles([]string{tempConfigPath}) | ||
assert.Equal(t, len(s), 2) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("Config with same secrets name but different data", func(t *testing.T) { | ||
tempConfigPath := filepath.Join(t.TempDir(), "config.yml") | ||
configWithWhitespace := []byte(` | ||
apiVersion: vendir.k14s.io/v1alpha1 | ||
kind: Config | ||
directories: | ||
- path: "repo" | ||
contents: | ||
- path: "folder-1" | ||
git: | ||
url: [email protected]:my-user/my-repo.git | ||
secretRef: | ||
name: ssh-key-secret | ||
ref: origin/main | ||
includePaths: | ||
- folder-1/**/* | ||
- path: "folder-2" | ||
git: | ||
url: [email protected]:my-user/my-repo.git | ||
secretRef: | ||
name: ssh-key-secret | ||
ref: origin/main | ||
includePaths: | ||
- folder-2/**/* | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: FOO= | ||
kind: Secret | ||
metadata: | ||
name: ssh-key-secret | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: FOO= | ||
kind: Secret | ||
metadata: | ||
name: ssh-key-secret | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: BAR= | ||
kind: Secret | ||
metadata: | ||
name: ssh-key-secret | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: BAR= | ||
kind: Secret | ||
metadata: | ||
name: another-secret | ||
--- | ||
apiVersion: v1 | ||
data: | ||
ssh-privatekey: BAZ= | ||
kind: Secret | ||
metadata: | ||
name: another-secret | ||
`) | ||
|
||
require.NoError(t, os.WriteFile(tempConfigPath, configWithWhitespace, 0666)) | ||
|
||
_, _, _, err := config.NewConfigFromFiles([]string{tempConfigPath}) | ||
require.Error(t, err) | ||
assert.Contains(t, err.Error(), "Expected to find one secret 'ssh-key-secret', but found multiple") | ||
}) | ||
} |