From 849e06a3169626e83798c7832e33f58b1e431c16 Mon Sep 17 00:00:00 2001 From: Andreas Auernhammer Date: Wed, 10 Apr 2019 20:05:53 +0200 Subject: [PATCH] crypto: add unit test for vault config verification (#7413) This commit adds a unit test for the vault config verification (which covers also `IsEmpty()`). Vault-related code is hard to test with unit tests since a Vault service would be necessary. Therefore this commit only adds tests for a fraction of the code. Fixes #7409 --- cmd/crypto/vault_test.go | 97 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 cmd/crypto/vault_test.go diff --git a/cmd/crypto/vault_test.go b/cmd/crypto/vault_test.go new file mode 100644 index 000000000..8acd6e723 --- /dev/null +++ b/cmd/crypto/vault_test.go @@ -0,0 +1,97 @@ +// Minio Cloud Storage, (C) 2019 Minio, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package crypto + +import ( + "fmt" + "testing" +) + +var verifyVaultConfigTests = []struct { + Config VaultConfig + ShouldFail bool +}{ + { + ShouldFail: false, // 0 + Config: VaultConfig{}, + }, + { + ShouldFail: true, + Config: VaultConfig{Endpoint: "https://127.0.0.1:8080"}, + }, + { + ShouldFail: true, // 1 + Config: VaultConfig{ + Endpoint: "https://127.0.0.1:8080", + Auth: VaultAuth{Type: "unsupported"}, + }, + }, + { + ShouldFail: true, // 2 + Config: VaultConfig{ + Endpoint: "https://127.0.0.1:8080", + Auth: VaultAuth{ + Type: "approle", + AppRole: VaultAppRole{}, + }, + }, + }, + { + ShouldFail: true, // 3 + Config: VaultConfig{ + Endpoint: "https://127.0.0.1:8080", + Auth: VaultAuth{ + Type: "approle", + AppRole: VaultAppRole{ID: "123456"}, + }, + }, + }, + { + ShouldFail: true, // 4 + Config: VaultConfig{ + Endpoint: "https://127.0.0.1:8080", + Auth: VaultAuth{ + Type: "approle", + AppRole: VaultAppRole{ID: "123456", Secret: "abcdef"}, + }, + }, + }, + { + ShouldFail: true, // 5 + Config: VaultConfig{ + Endpoint: "https://127.0.0.1:8080", + Auth: VaultAuth{ + Type: "approle", + AppRole: VaultAppRole{ID: "123456", Secret: "abcdef"}, + }, + Key: VaultKey{Name: "default-key", Version: -1}, + }, + }, +} + +func TestVerifyVaultConfig(t *testing.T) { + for i, test := range verifyVaultConfigTests { + test := test + t.Run(fmt.Sprintf("Test-%d", i), func(t *testing.T) { + err := test.Config.Verify() + if test.ShouldFail && err == nil { + t.Errorf("Verify should fail but returned 'err == nil'") + } + if !test.ShouldFail && err != nil { + t.Errorf("Verify should succeed but returned err: %s", err) + } + }) + } +}