2016-09-21 12:44:57 -04:00
|
|
|
/*
|
2017-01-18 15:24:34 -05:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-09-21 12:44:57 -04:00
|
|
|
*
|
|
|
|
* 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 cmd
|
|
|
|
|
|
|
|
import (
|
2017-05-06 13:16:59 -04:00
|
|
|
"fmt"
|
2016-09-21 12:44:57 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-10-03 20:29:55 -04:00
|
|
|
// Test if config v1 is purged
|
2016-09-21 12:44:57 -04:00
|
|
|
func TestServerConfigMigrateV1(t *testing.T) {
|
2017-01-18 15:24:34 -05:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-09-21 12:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 02:37:12 -05:00
|
|
|
// remove the root directory after the test ends.
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(rootPath)
|
2016-09-21 12:44:57 -04:00
|
|
|
|
2017-03-02 17:21:30 -05:00
|
|
|
setConfigDir(rootPath)
|
2016-09-21 12:44:57 -04:00
|
|
|
|
|
|
|
// Create a V1 config json file and store it
|
|
|
|
configJSON := "{ \"version\":\"1\", \"accessKeyId\":\"abcde\", \"secretAccessKey\":\"abcdefgh\"}"
|
|
|
|
configPath := rootPath + "/fsUsers.json"
|
|
|
|
if err := ioutil.WriteFile(configPath, []byte(configJSON), 0644); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fire a migrateConfig()
|
|
|
|
if err := migrateConfig(); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Check if config v1 is removed from filesystem
|
2017-10-13 06:01:15 -04:00
|
|
|
if _, err := os.Stat(configPath); err == nil || !os.IsNotExist(err) {
|
2016-09-21 12:44:57 -04:00
|
|
|
t.Fatal("Config V1 file is not purged")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize server config and check again if everything is fine
|
2017-03-30 14:21:19 -04:00
|
|
|
if err := loadConfig(); err != nil {
|
2016-09-21 12:44:57 -04:00
|
|
|
t.Fatalf("Unable to initialize from updated config file %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 20:29:55 -04:00
|
|
|
// Test if all migrate code returns nil when config file does not
|
|
|
|
// exist
|
2016-09-21 12:44:57 -04:00
|
|
|
func TestServerConfigMigrateInexistentConfig(t *testing.T) {
|
2017-01-18 15:24:34 -05:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-09-21 12:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 02:37:12 -05:00
|
|
|
// remove the root directory after the test ends.
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(rootPath)
|
2016-09-21 12:44:57 -04:00
|
|
|
|
2017-03-02 17:21:30 -05:00
|
|
|
setConfigDir(rootPath)
|
2017-03-08 22:20:01 -05:00
|
|
|
configPath := rootPath + "/" + minioConfigFile
|
2016-09-21 12:44:57 -04:00
|
|
|
|
|
|
|
// Remove config file
|
|
|
|
if err := os.Remove(configPath); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := migrateV2ToV3(); err != nil {
|
|
|
|
t.Fatal("migrate v2 to v3 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV3ToV4(); err != nil {
|
|
|
|
t.Fatal("migrate v3 to v4 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV4ToV5(); err != nil {
|
|
|
|
t.Fatal("migrate v4 to v5 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV5ToV6(); err != nil {
|
|
|
|
t.Fatal("migrate v5 to v6 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV6ToV7(); err != nil {
|
|
|
|
t.Fatal("migrate v6 to v7 should succeed when no config file is found")
|
|
|
|
}
|
2016-09-30 02:42:10 -04:00
|
|
|
if err := migrateV7ToV8(); err != nil {
|
|
|
|
t.Fatal("migrate v7 to v8 should succeed when no config file is found")
|
|
|
|
}
|
2016-10-03 20:29:55 -04:00
|
|
|
if err := migrateV8ToV9(); err != nil {
|
|
|
|
t.Fatal("migrate v8 to v9 should succeed when no config file is found")
|
|
|
|
}
|
2016-11-23 18:00:53 -05:00
|
|
|
if err := migrateV9ToV10(); err != nil {
|
|
|
|
t.Fatal("migrate v9 to v10 should succeed when no config file is found")
|
|
|
|
}
|
2016-12-15 11:23:48 -05:00
|
|
|
if err := migrateV10ToV11(); err != nil {
|
|
|
|
t.Fatal("migrate v10 to v11 should succeed when no config file is found")
|
|
|
|
}
|
2017-01-11 19:41:05 -05:00
|
|
|
if err := migrateV11ToV12(); err != nil {
|
2017-01-09 17:22:10 -05:00
|
|
|
t.Fatal("migrate v11 to v12 should succeed when no config file is found")
|
|
|
|
}
|
|
|
|
if err := migrateV12ToV13(); err != nil {
|
|
|
|
t.Fatal("migrate v12 to v13 should succeed when no config file is found")
|
2017-01-11 19:41:05 -05:00
|
|
|
}
|
2017-02-27 17:59:53 -05:00
|
|
|
if err := migrateV13ToV14(); err != nil {
|
|
|
|
t.Fatal("migrate v13 to v14 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-17 12:29:17 -04:00
|
|
|
if err := migrateV14ToV15(); err != nil {
|
|
|
|
t.Fatal("migrate v14 to v15 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-23 11:27:22 -04:00
|
|
|
if err := migrateV15ToV16(); err != nil {
|
|
|
|
t.Fatal("migrate v15 to v16 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-27 14:27:25 -04:00
|
|
|
if err := migrateV16ToV17(); err != nil {
|
|
|
|
t.Fatal("migrate v16 to v17 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-31 06:34:26 -04:00
|
|
|
if err := migrateV17ToV18(); err != nil {
|
|
|
|
t.Fatal("migrate v17 to v18 should succeed when no config file is found")
|
|
|
|
}
|
2017-06-14 20:27:03 -04:00
|
|
|
if err := migrateV18ToV19(); err != nil {
|
|
|
|
t.Fatal("migrate v18 to v19 should succeed when no config file is found")
|
|
|
|
}
|
2017-03-27 14:27:25 -04:00
|
|
|
|
2016-09-21 12:44:57 -04:00
|
|
|
}
|
|
|
|
|
2017-06-14 20:27:03 -04:00
|
|
|
// Test if a config migration from v2 to v19 is successfully done
|
|
|
|
func TestServerConfigMigrateV2toV19(t *testing.T) {
|
2017-01-18 15:24:34 -05:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-09-21 12:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 02:37:12 -05:00
|
|
|
// remove the root directory after the test ends.
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(rootPath)
|
2016-09-21 12:44:57 -04:00
|
|
|
|
2017-03-02 17:21:30 -05:00
|
|
|
setConfigDir(rootPath)
|
2017-03-08 22:20:01 -05:00
|
|
|
configPath := rootPath + "/" + minioConfigFile
|
2016-09-21 12:44:57 -04:00
|
|
|
|
|
|
|
// Create a corrupted config file
|
|
|
|
if err := ioutil.WriteFile(configPath, []byte("{ \"version\":\"2\","), 0644); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
// Fire a migrateConfig()
|
|
|
|
if err := migrateConfig(); err == nil {
|
|
|
|
t.Fatal("migration should fail with corrupted config file")
|
|
|
|
}
|
|
|
|
|
|
|
|
accessKey := "accessfoo"
|
|
|
|
secretKey := "secretfoo"
|
|
|
|
|
|
|
|
// Create a V2 config json file and store it
|
|
|
|
configJSON := "{ \"version\":\"2\", \"credentials\": {\"accessKeyId\":\"" + accessKey + "\", \"secretAccessKey\":\"" + secretKey + "\", \"region\":\"us-east-1\"}, \"mongoLogger\":{\"addr\":\"127.0.0.1:3543\", \"db\":\"foodb\", \"collection\":\"foo\"}, \"syslogLogger\":{\"network\":\"127.0.0.1:543\", \"addr\":\"addr\"}, \"fileLogger\":{\"filename\":\"log.out\"}}"
|
|
|
|
if err := ioutil.WriteFile(configPath, []byte(configJSON), 0644); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
|
2016-09-21 12:44:57 -04:00
|
|
|
// Fire a migrateConfig()
|
|
|
|
if err := migrateConfig(); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize server config and check again if everything is fine
|
2017-03-30 14:21:19 -04:00
|
|
|
if err := loadConfig(); err != nil {
|
2016-09-21 12:44:57 -04:00
|
|
|
t.Fatalf("Unable to initialize from updated config file %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the version number in the upgraded config file
|
2017-06-14 20:27:03 -04:00
|
|
|
expectedVersion := v19
|
2016-09-21 12:44:57 -04:00
|
|
|
if serverConfig.Version != expectedVersion {
|
|
|
|
t.Fatalf("Expect version "+expectedVersion+", found: %v", serverConfig.Version)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if accessKey and secretKey are not altered during migration
|
2016-12-26 13:21:23 -05:00
|
|
|
if serverConfig.Credential.AccessKey != accessKey {
|
|
|
|
t.Fatalf("Access key lost during migration, expected: %v, found:%v", accessKey, serverConfig.Credential.AccessKey)
|
2016-09-21 12:44:57 -04:00
|
|
|
}
|
2016-12-26 13:21:23 -05:00
|
|
|
if serverConfig.Credential.SecretKey != secretKey {
|
|
|
|
t.Fatalf("Secret key lost during migration, expected: %v, found: %v", secretKey, serverConfig.Credential.SecretKey)
|
2016-09-21 12:44:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 20:29:55 -04:00
|
|
|
// Test if all migrate code returns error with corrupted config files
|
2016-09-21 12:44:57 -04:00
|
|
|
func TestServerConfigMigrateFaultyConfig(t *testing.T) {
|
2017-01-18 15:24:34 -05:00
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
2016-09-21 12:44:57 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
2016-11-10 02:37:12 -05:00
|
|
|
// remove the root directory after the test ends.
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(rootPath)
|
2016-09-21 12:44:57 -04:00
|
|
|
|
2017-03-02 17:21:30 -05:00
|
|
|
setConfigDir(rootPath)
|
2017-03-08 22:20:01 -05:00
|
|
|
configPath := rootPath + "/" + minioConfigFile
|
2016-09-21 12:44:57 -04:00
|
|
|
|
|
|
|
// Create a corrupted config file
|
2017-05-06 13:16:59 -04:00
|
|
|
if err := ioutil.WriteFile(configPath, []byte("{ \"version\":\"2\", \"test\":"), 0644); err != nil {
|
2016-09-21 12:44:57 -04:00
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test different migrate versions and be sure they are returning an error
|
|
|
|
if err := migrateV2ToV3(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV2ToV3() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV3ToV4(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV3ToV4() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV4ToV5(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV4ToV5() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV5ToV6(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV5ToV6() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
if err := migrateV6ToV7(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV6ToV7() should fail with a corrupted json")
|
|
|
|
}
|
2016-09-30 02:42:10 -04:00
|
|
|
if err := migrateV7ToV8(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV7ToV8() should fail with a corrupted json")
|
|
|
|
}
|
2016-10-03 20:29:55 -04:00
|
|
|
if err := migrateV8ToV9(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV8ToV9() should fail with a corrupted json")
|
|
|
|
}
|
2016-11-23 18:00:53 -05:00
|
|
|
if err := migrateV9ToV10(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV9ToV10() should fail with a corrupted json")
|
|
|
|
}
|
2016-12-15 11:23:48 -05:00
|
|
|
if err := migrateV10ToV11(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV10ToV11() should fail with a corrupted json")
|
|
|
|
}
|
2017-01-11 19:41:05 -05:00
|
|
|
if err := migrateV11ToV12(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV11ToV12() should fail with a corrupted json")
|
|
|
|
}
|
2017-01-09 17:22:10 -05:00
|
|
|
if err := migrateV12ToV13(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV12ToV13() should fail with a corrupted json")
|
|
|
|
}
|
2017-02-27 17:59:53 -05:00
|
|
|
if err := migrateV13ToV14(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV13ToV14() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-17 12:29:17 -04:00
|
|
|
if err := migrateV14ToV15(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV14ToV15() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-23 11:27:22 -04:00
|
|
|
if err := migrateV15ToV16(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV15ToV16() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-27 14:27:25 -04:00
|
|
|
if err := migrateV16ToV17(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV16ToV17() should fail with a corrupted json")
|
|
|
|
}
|
2017-03-31 06:34:26 -04:00
|
|
|
if err := migrateV17ToV18(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV17ToV18() should fail with a corrupted json")
|
|
|
|
}
|
2017-06-14 20:27:03 -04:00
|
|
|
if err := migrateV18ToV19(); err == nil {
|
|
|
|
t.Fatal("migrateConfigV18ToV19() should fail with a corrupted json")
|
|
|
|
}
|
2016-09-21 12:44:57 -04:00
|
|
|
}
|
2017-05-06 13:16:59 -04:00
|
|
|
|
|
|
|
// Test if all migrate code returns error with corrupted config files
|
|
|
|
func TestServerConfigMigrateCorruptedConfig(t *testing.T) {
|
|
|
|
rootPath, err := newTestConfig(globalMinioDefaultRegion)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Init Test config failed")
|
|
|
|
}
|
|
|
|
// remove the root directory after the test ends.
|
2017-08-12 22:25:43 -04:00
|
|
|
defer os.RemoveAll(rootPath)
|
2017-05-06 13:16:59 -04:00
|
|
|
|
|
|
|
setConfigDir(rootPath)
|
|
|
|
configPath := rootPath + "/" + minioConfigFile
|
|
|
|
|
|
|
|
for i := 3; i <= 17; i++ {
|
|
|
|
// Create a corrupted config file
|
|
|
|
if err = ioutil.WriteFile(configPath, []byte(fmt.Sprintf("{ \"version\":\"%d\", \"credential\": { \"accessKey\": 1 } }", i)),
|
|
|
|
0644); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test different migrate versions and be sure they are returning an error
|
|
|
|
if err = migrateConfig(); err == nil {
|
|
|
|
t.Fatal("migrateConfig() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a corrupted config file for version '2'.
|
|
|
|
if err = ioutil.WriteFile(configPath, []byte("{ \"version\":\"2\", \"credentials\": { \"accessKeyId\": 1 } }"), 0644); err != nil {
|
|
|
|
t.Fatal("Unexpected error: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test different migrate versions and be sure they are returning an error
|
|
|
|
if err = migrateConfig(); err == nil {
|
|
|
|
t.Fatal("migrateConfig() should fail with a corrupted json")
|
|
|
|
}
|
|
|
|
}
|