From abc5462c77888a3a1ef54d3c89a79f93f5e2bcab Mon Sep 17 00:00:00 2001 From: Nathan LeClaire Date: Wed, 25 Mar 2015 10:57:39 -0700 Subject: [PATCH] Rename migration functions to be more descriptive and add tests Signed-off-by: Nathan LeClaire --- libmachine/filestore.go | 2 +- libmachine/host.go | 2 +- libmachine/{validate.go => migrate.go} | 13 ++- libmachine/migrate_test.go | 119 +++++++++++++++++++++++++ libmachine/validate_test.go | 34 ------- 5 files changed, 131 insertions(+), 39 deletions(-) rename libmachine/{validate.go => migrate.go} (84%) create mode 100644 libmachine/migrate_test.go delete mode 100644 libmachine/validate_test.go diff --git a/libmachine/filestore.go b/libmachine/filestore.go index 5e3bdf8abe..b5965f7f21 100644 --- a/libmachine/filestore.go +++ b/libmachine/filestore.go @@ -32,7 +32,7 @@ func (s Filestore) loadHost(name string) (*Host, error) { return nil, err } - h := ValidateHost(host) + h := FillNestedHost(host) return h, nil } diff --git a/libmachine/host.go b/libmachine/host.go index e7eed24b50..2d4969f2e0 100644 --- a/libmachine/host.go +++ b/libmachine/host.go @@ -266,7 +266,7 @@ func (h *Host) LoadConfig() error { return err } - meta := ValidateHostMetadata(&hostMetadata) + meta := FillNestedHostMetadata(&hostMetadata) authOptions := meta.HostOptions.AuthOptions diff --git a/libmachine/validate.go b/libmachine/migrate.go similarity index 84% rename from libmachine/validate.go rename to libmachine/migrate.go index a0b30c709a..69f062e016 100644 --- a/libmachine/validate.go +++ b/libmachine/migrate.go @@ -9,9 +9,16 @@ import ( "github.com/docker/machine/utils" ) +// In the 0.0.1 => 0.0.2 transition, the JSON representation of +// machines changed from a "flat" to a more "nested" structure +// for various options and configuration settings. To preserve +// compatibility with existing machines, these migration functions +// have been introduced. They preserve backwards compat at the expense +// of some duplicated information. + // validates host config and modifies if needed // this is used for configuration updates -func ValidateHost(host *Host) *Host { +func FillNestedHost(host *Host) *Host { certInfo := getCertInfoFromHost(host) if host.HostOptions == nil { @@ -46,9 +53,9 @@ func ValidateHost(host *Host) *Host { return host } -// validates host metadata and modifies if needed +// fills nested host metadata and modifies if needed // this is used for configuration updates -func ValidateHostMetadata(m *HostMetadata) *HostMetadata { +func FillNestedHostMetadata(m *HostMetadata) *HostMetadata { if m.HostOptions.EngineOptions == nil { m.HostOptions.EngineOptions = &engine.EngineOptions{} } diff --git a/libmachine/migrate_test.go b/libmachine/migrate_test.go new file mode 100644 index 0000000000..57c726cf1f --- /dev/null +++ b/libmachine/migrate_test.go @@ -0,0 +1,119 @@ +package libmachine + +import ( + "os" + "reflect" + "testing" + + "github.com/docker/machine/libmachine/auth" + "github.com/docker/machine/libmachine/engine" + "github.com/docker/machine/libmachine/swarm" +) + +func TestFillNestedHost(t *testing.T) { + os.Setenv("MACHINE_STORAGE_PATH", "/tmp/migration") + originalHost := &Host{ + HostOptions: nil, + SwarmDiscovery: "token://foobar", + SwarmHost: "1.2.3.4:2376", + SwarmMaster: true, + CaCertPath: "", + PrivateKeyPath: "", + ClientCertPath: "", + ClientKeyPath: "", + ServerCertPath: "", + ServerKeyPath: "", + } + hostOptions := &HostOptions{ + SwarmOptions: &swarm.SwarmOptions{ + Master: true, + Discovery: "token://foobar", + Host: "1.2.3.4:2376", + }, + AuthOptions: &auth.AuthOptions{ + CaCertPath: "/tmp/migration/certs/ca.pem", + PrivateKeyPath: "/tmp/migration/certs/ca-key.pem", + ClientCertPath: "/tmp/migration/certs/cert.pem", + ClientKeyPath: "/tmp/migration/certs/key.pem", + ServerCertPath: "/tmp/migration/certs/server.pem", + ServerKeyPath: "/tmp/migration/certs/server-key.pem", + }, + EngineOptions: &engine.EngineOptions{}, + } + + expectedHost := &Host{ + SwarmHost: "1.2.3.4:2376", + SwarmDiscovery: "token://foobar", + SwarmMaster: true, + HostOptions: hostOptions, + } + + host := FillNestedHost(originalHost) + + if !reflect.DeepEqual(host, expectedHost) { + t.Logf("\n%+v\n%+v", host, expectedHost) + t.Logf("\n%+v\n%+v", host.HostOptions, expectedHost.HostOptions) + t.Fatal("Expected these structs to be equal, they were different") + } +} + +func TestFillNestedHostMetadata(t *testing.T) { + metadata := &HostMetadata{ + HostOptions: HostOptions{ + EngineOptions: nil, + AuthOptions: nil, + }, + StorePath: "/tmp/store", + CaCertPath: "/tmp/store/certs/ca.pem", + ServerCertPath: "/tmp/store/certs/server.pem", + } + expectedAuthOptions := &auth.AuthOptions{ + StorePath: "/tmp/store", + CaCertPath: "/tmp/store/certs/ca.pem", + ServerCertPath: "/tmp/store/certs/server.pem", + } + + expectedMetadata := &HostMetadata{ + HostOptions: HostOptions{ + EngineOptions: &engine.EngineOptions{}, + AuthOptions: expectedAuthOptions, + }, + StorePath: "/tmp/store", + CaCertPath: "/tmp/store/certs/ca.pem", + ServerCertPath: "/tmp/store/certs/server.pem", + } + + m := FillNestedHostMetadata(metadata) + + if !reflect.DeepEqual(m, expectedMetadata) { + t.Logf("\n%+v\n%+v", m, expectedMetadata) + t.Fatal("Expected these structs to be equal, they were different") + } +} + +// Tests a function which "prefills" certificate information for a host +// due to a schema migration from "flat" to a "nested" structure. +func TestGetCertInfoFromHost(t *testing.T) { + os.Setenv("MACHINE_STORAGE_PATH", "/tmp/migration") + host := &Host{ + CaCertPath: "", + PrivateKeyPath: "", + ClientCertPath: "", + ClientKeyPath: "", + ServerCertPath: "", + ServerKeyPath: "", + } + expectedCertInfo := CertPathInfo{ + CaCertPath: "/tmp/migration/certs/ca.pem", + CaKeyPath: "/tmp/migration/certs/ca-key.pem", + ClientCertPath: "/tmp/migration/certs/cert.pem", + ClientKeyPath: "/tmp/migration/certs/key.pem", + ServerCertPath: "/tmp/migration/certs/server.pem", + ServerKeyPath: "/tmp/migration/certs/server-key.pem", + } + certInfo := getCertInfoFromHost(host) + if !reflect.DeepEqual(expectedCertInfo, certInfo) { + t.Log("\n\n\n", expectedCertInfo, "\n\n\n", certInfo) + t.Fatal("Expected these structs to be equal, they were different") + } +} diff --git a/libmachine/validate_test.go b/libmachine/validate_test.go deleted file mode 100644 index faee10030a..0000000000 --- a/libmachine/validate_test.go +++ /dev/null @@ -1,34 +0,0 @@ -package libmachine - -import ( - "os" - "reflect" - "testing" -) - -// Tests a function which "prefills" certificate information for a host -// due to a schema migration from "flat" to a "nested" structure. -func TestGetCertInfoFromHost(t *testing.T) { - os.Setenv("MACHINE_STORAGE_PATH", "/tmp/migration") - host := &Host{ - CaCertPath: "", - PrivateKeyPath: "", - ClientCertPath: "", - ClientKeyPath: "", - ServerCertPath: "", - ServerKeyPath: "", - } - expectedCertInfo := CertPathInfo{ - CaCertPath: "/tmp/migration/certs/ca.pem", - CaKeyPath: "/tmp/migration/certs/ca-key.pem", - ClientCertPath: "/tmp/migration/certs/cert.pem", - ClientKeyPath: "/tmp/migration/certs/key.pem", - ServerCertPath: "/tmp/migration/certs/server.pem", - ServerKeyPath: "/tmp/migration/certs/server-key.pem", - } - certInfo := getCertInfoFromHost(host) - if !reflect.DeepEqual(expectedCertInfo, certInfo) { - t.Log("\n\n\n", expectedCertInfo, "\n\n\n", certInfo) - t.Fatal("Expected these structs to be equal, they were different") - } -}