diff --git a/ci/tasks/test-integration.yml b/ci/tasks/test-integration.yml index 7d226faa6..903d78c5f 100644 --- a/ci/tasks/test-integration.yml +++ b/ci/tasks/test-integration.yml @@ -1,5 +1,10 @@ --- platform: linux +image_resource: + type: registry-image + source: + repository: bosh/agent + tag: latest inputs: - name: agent-info diff --git a/infrastructure/complex_settings_source.go b/infrastructure/complex_settings_source.go deleted file mode 100644 index c4ff9d331..000000000 --- a/infrastructure/complex_settings_source.go +++ /dev/null @@ -1,44 +0,0 @@ -package infrastructure - -import ( - boshsettings "github.com/cloudfoundry/bosh-agent/settings" - boshlog "github.com/cloudfoundry/bosh-utils/logger" -) - -type ComplexSettingsSource struct { - metadataService MetadataService - - logTag string - logger boshlog.Logger -} - -func NewComplexSettingsSource( - metadataService MetadataService, - logger boshlog.Logger, -) ComplexSettingsSource { - return ComplexSettingsSource{ - metadataService: metadataService, - - logTag: "ComplexSettingsSource", - logger: logger, - } -} - -func (s ComplexSettingsSource) PublicSSHKeyForUsername(string) (string, error) { - return s.metadataService.GetPublicKey() -} - -func (s ComplexSettingsSource) Settings() (boshsettings.Settings, error) { - settings, err := s.GetMetadataService().GetSettings() - if err == nil && settings.AgentID != "" { - s.logger.Debug(s.logTag, "Got settings from metadata service, not contacting registry.") - return settings, nil - } - - s.logger.Debug(s.logTag, "Unable to get settings from metadata service, falling back to registry.") - return boshsettings.Settings{}, err -} - -func (s ComplexSettingsSource) GetMetadataService() MetadataService { - return s.metadataService -} diff --git a/infrastructure/complex_settings_source_test.go b/infrastructure/complex_settings_source_test.go deleted file mode 100644 index d29e64c52..000000000 --- a/infrastructure/complex_settings_source_test.go +++ /dev/null @@ -1,72 +0,0 @@ -package infrastructure_test - -import ( - "errors" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - . "github.com/cloudfoundry/bosh-agent/infrastructure" - fakeinf "github.com/cloudfoundry/bosh-agent/infrastructure/fakes" - boshsettings "github.com/cloudfoundry/bosh-agent/settings" - boshlog "github.com/cloudfoundry/bosh-utils/logger" -) - -var _ = Describe("ComplexSettingsSource", func() { - var ( - metadataService *fakeinf.FakeMetadataService - source ComplexSettingsSource - ) - - BeforeEach(func() { - metadataService = &fakeinf.FakeMetadataService{} - logger := boshlog.NewLogger(boshlog.LevelNone) - source = NewComplexSettingsSource(metadataService, logger) - }) - - Describe("PublicSSHKeyForUsername", func() { - It("returns an empty string", func() { - metadataService.PublicKey = "public-key" - - publicKey, err := source.PublicSSHKeyForUsername("fake-username") - Expect(err).ToNot(HaveOccurred()) - Expect(publicKey).To(Equal("public-key")) - }) - - It("returns an error if string", func() { - metadataService.GetPublicKeyErr = errors.New("fake-public-key-error") - - _, err := source.PublicSSHKeyForUsername("fake-username") - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("fake-public-key-error")) - }) - }) - - Describe("Settings", func() { - It("returns settings from the metadataService", func() { - metadataService = &fakeinf.FakeMetadataService{ - Settings: boshsettings.Settings{ - AgentID: "fake-agent-id", - }, - } - logger := boshlog.NewLogger(boshlog.LevelNone) - source = NewComplexSettingsSource(metadataService, logger) - - settings, err := source.Settings() - Expect(err).ToNot(HaveOccurred()) - Expect(settings.AgentID).To(Equal("fake-agent-id")) - }) - - It("returns an error if getting settings fails", func() { - metadataService = &fakeinf.FakeMetadataService{ - SettingsErr: errors.New("fake-get-settings-error"), - } - logger := boshlog.NewLogger(boshlog.LevelNone) - source = NewComplexSettingsSource(metadataService, logger) - - _, err := source.Settings() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("fake-get-settings-error")) - }) - }) -}) diff --git a/infrastructure/config_drive_metadata_service.go b/infrastructure/config_drive_metadata_service.go deleted file mode 100644 index a5012f0f4..000000000 --- a/infrastructure/config_drive_metadata_service.go +++ /dev/null @@ -1,139 +0,0 @@ -package infrastructure - -import ( - "encoding/json" - - boshplatform "github.com/cloudfoundry/bosh-agent/platform" - boshsettings "github.com/cloudfoundry/bosh-agent/settings" - bosherr "github.com/cloudfoundry/bosh-utils/errors" - boshlog "github.com/cloudfoundry/bosh-utils/logger" -) - -type configDriveMetadataService struct { - platform boshplatform.Platform - - diskPaths []string - metaDataFilePath string - userDataFilePath string - - // Loaded state - metaDataContents MetadataContentsType - userDataContents UserDataContentsType - - logger boshlog.Logger - logTag string -} - -func NewConfigDriveMetadataService( - platform boshplatform.Platform, - diskPaths []string, - metaDataFilePath string, - userDataFilePath string, - logger boshlog.Logger, -) MetadataService { - return &configDriveMetadataService{ - platform: platform, - - diskPaths: diskPaths, - metaDataFilePath: metaDataFilePath, - userDataFilePath: userDataFilePath, - - logTag: "ConfigDriveMetadataService", - logger: logger, - } -} - -func (ms *configDriveMetadataService) GetPublicKey() (string, error) { - if firstPublicKey, ok := ms.metaDataContents.PublicKeys["0"]; ok { - if openSSHKey, ok := firstPublicKey["openssh-key"]; ok { - return openSSHKey, nil - } - } - - return "", bosherr.Error("Failed to load openssh-key from config drive metadata service") -} - -func (ms *configDriveMetadataService) GetInstanceID() (string, error) { - if ms.metaDataContents.InstanceID == "" { - return "", bosherr.Error("Failed to load instance-id from config drive metadata service") - } - - ms.logger.Debug(ms.logTag, "Getting instance id: %s", ms.metaDataContents.InstanceID) - return ms.metaDataContents.InstanceID, nil -} - -func (ms *configDriveMetadataService) GetServerName() (string, error) { - if ms.userDataContents.Server.Name == "" { - return "", bosherr.Error("Failed to load server name from config drive metadata service") - } - - ms.logger.Debug(ms.logTag, "Getting server name: %s", ms.userDataContents.Server.Name) - return ms.userDataContents.Server.Name, nil -} - -func (ms *configDriveMetadataService) GetNetworks() (boshsettings.Networks, error) { - return ms.userDataContents.Networks, nil -} - -func (ms *configDriveMetadataService) GetSettings() (boshsettings.Settings, error) { - if ms.userDataContents.Settings.AgentID == "" { - return boshsettings.Settings{}, bosherr.Error("Metadata does not provide settings") - } - return ms.userDataContents.Settings, nil -} - -func (ms *configDriveMetadataService) IsAvailable() bool { - if len(ms.diskPaths) == 0 { - ms.logger.Warn(ms.logTag, "Disk paths are not given") - return false - } - - return ms.load() == nil -} - -func (ms *configDriveMetadataService) load() error { - ms.logger.Debug(ms.logTag, "Loading config drive metadata service") - - var err error - - for _, diskPath := range ms.diskPaths { - err = ms.loadFromDiskPath(diskPath) - if err == nil { - ms.logger.Debug(ms.logTag, "Successfully loaded config from %s", diskPath) - return nil - } - - ms.logger.Warn(ms.logTag, "Failed to load config from %s - %s", diskPath, err.Error()) - } - - return err -} - -func (ms *configDriveMetadataService) loadFromDiskPath(diskPath string) error { - contentPaths := []string{ms.metaDataFilePath, ms.userDataFilePath} - - contents, err := ms.platform.GetFilesContentsFromDisk(diskPath, contentPaths) - if err != nil { - return bosherr.WrapError(err, "Reading files on config drive") - } - - var metadata MetadataContentsType - - err = json.Unmarshal(contents[0], &metadata) - if err != nil { - return bosherr.WrapError(err, "Parsing config drive metadata from meta_data.json") - } - - ms.metaDataContents = metadata - - var userdata UserDataContentsType - - err = json.Unmarshal(contents[1], &userdata) - if err != nil { - return bosherr.WrapError(err, "Parsing config drive metadata from user_data") - } - - ms.userDataContents = userdata - - return nil -} diff --git a/infrastructure/config_drive_metadata_service_test.go b/infrastructure/config_drive_metadata_service_test.go deleted file mode 100644 index 8d20af8aa..000000000 --- a/infrastructure/config_drive_metadata_service_test.go +++ /dev/null @@ -1,291 +0,0 @@ -package infrastructure_test - -import ( - "encoding/json" - "errors" - "fmt" - - . "github.com/cloudfoundry/bosh-agent/infrastructure" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - "github.com/cloudfoundry/bosh-agent/platform/platformfakes" - - boshsettings "github.com/cloudfoundry/bosh-agent/settings" - boshlog "github.com/cloudfoundry/bosh-utils/logger" -) - -var _ = Describe("ConfigDriveMetadataService", describeConfigDriveMetadataService) - -func describeConfigDriveMetadataService() { //nolint:funlen - var ( - metadataService MetadataService - platform *platformfakes.FakePlatform - logger boshlog.Logger - - metadataServiceFileContents [][]byte - ) - - updateMetadata := func(metadataContents MetadataContentsType) { - metadataJSON, err := json.Marshal(metadataContents) - Expect(err).ToNot(HaveOccurred()) - metadataServiceFileContents[0] = metadataJSON - - platform.GetFilesContentsFromDiskReturns(metadataServiceFileContents, nil) - Expect(metadataService.IsAvailable()).To(BeTrue()) - } - - updateUserdata := func(userdataContents string) { - metadataServiceFileContents[1] = []byte(userdataContents) - - platform.GetFilesContentsFromDiskReturns(metadataServiceFileContents, nil) - Expect(metadataService.IsAvailable()).To(BeTrue()) - } - - BeforeEach(func() { - platform = &platformfakes.FakePlatform{} - logger = boshlog.NewLogger(boshlog.LevelNone) - diskPaths := []string{ - "/fake-disk-path-1", - "/fake-disk-path-2", - } - - metadataServiceFileContents = make([][]byte, 2) - - metadataService = NewConfigDriveMetadataService( - platform, - diskPaths, - "fake-metadata-path", - "fake-userdata-path", - logger, - ) - - userdataContents := `{"server":{"name":"fake-server-name"},"registry":{"endpoint":"fake-registry-endpoint"}}` - metadataServiceFileContents[1] = []byte(userdataContents) - - metadata := MetadataContentsType{ - PublicKeys: map[string]PublicKeyType{ - "0": PublicKeyType{ - "openssh-key": "fake-openssh-key", - }, - }, - InstanceID: "fake-instance-id", - } - metadataJSON, err := json.Marshal(metadata) - Expect(err).ToNot(HaveOccurred()) - metadataServiceFileContents[0] = metadataJSON - - updateMetadata(metadata) - }) - - Describe("GetNetworks", func() { - It("returns the network settings", func() { - userdataContents := ` - { - "networks": { - "network_1": {"type": "manual", "ip": "1.2.3.4", "netmask": "2.3.4.5", "gateway": "3.4.5.6", "default": ["dns"], "dns": ["8.8.8.8"], "mac": "fake-mac-address-1"}, - "network_2": {"type": "dynamic", "default": ["dns"], "dns": ["8.8.8.8"], "mac": "fake-mac-address-2"} - } - }` - updateUserdata(userdataContents) - - networks, err := metadataService.GetNetworks() - Expect(err).ToNot(HaveOccurred()) - Expect(networks).To(Equal(boshsettings.Networks{ - "network_1": boshsettings.Network{ - Type: "manual", - IP: "1.2.3.4", - Netmask: "2.3.4.5", - Gateway: "3.4.5.6", - Default: []string{"dns"}, - DNS: []string{"8.8.8.8"}, - Mac: "fake-mac-address-1", - }, - "network_2": boshsettings.Network{ - Type: "dynamic", - Default: []string{"dns"}, - DNS: []string{"8.8.8.8"}, - Mac: "fake-mac-address-2", - }, - })) - }) - - It("returns a nil Networks if the settings are missing (from an old CPI version)", func() { - userdataContents := `{}` - updateUserdata(userdataContents) - - networks, err := metadataService.GetNetworks() - Expect(err).ToNot(HaveOccurred()) - Expect(networks).To(BeNil()) - }) - }) - - Describe("IsAvailable", func() { - It("return true when it can load successfully", func() { - Expect(metadataService.IsAvailable()).To(BeTrue()) - }) - - It("returns an error if it fails to read meta-data.json from disk", func() { - platform.GetFilesContentsFromDiskReturns([][]byte{[]byte{}, []byte{}}, errors.New("fake-read-disk-error")) - Expect(metadataService.IsAvailable()).To(BeFalse()) - }) - - It("tries to load meta-data.json from potential disk locations", func() { - platform.GetFilesContentsFromDiskReturns([][]byte{[]byte{}, []byte{}}, errors.New("fake-read-disk-error")) - Expect(metadataService.IsAvailable()).To(BeFalse()) - - diskpath, _ := platform.GetFilesContentsFromDiskArgsForCall(1) - Expect(diskpath).To(Equal("/fake-disk-path-1")) - diskpath, _ = platform.GetFilesContentsFromDiskArgsForCall(2) - Expect(diskpath).To(Equal("/fake-disk-path-2")) - }) - - It("returns an error if it fails to parse meta-data.json contents", func() { - platform.GetFilesContentsFromDiskReturns([][]byte{[]byte("broken"), []byte{}}, nil) - Expect(metadataService.IsAvailable()).To(BeFalse()) - }) - - It("returns an error if it fails to parse user_data contents", func() { - platform.GetFilesContentsFromDiskReturns([][]byte{[]byte{}, []byte("broken")}, nil) - Expect(metadataService.IsAvailable()).To(BeFalse()) - }) - - Context("when disk paths are not given", func() { - It("returns false", func() { - metadataService = NewConfigDriveMetadataService( - platform, - []string{}, - "fake-metadata-path", - "fake-userdata-path", - logger, - ) - Expect(metadataService.IsAvailable()).To(BeFalse()) - }) - }) - }) - - Describe("GetPublicKey", func() { - It("returns public key", func() { - value, err := metadataService.GetPublicKey() - Expect(err).ToNot(HaveOccurred()) - Expect(value).To(Equal("fake-openssh-key")) - }) - - It("returns an error if it fails to get ssh key", func() { - updateMetadata(MetadataContentsType{}) - - value, err := metadataService.GetPublicKey() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("Failed to load openssh-key from config drive metadata service")) - - Expect(value).To(Equal("")) - }) - }) - - Describe("GetInstanceID", func() { - It("returns instance id", func() { - value, err := metadataService.GetInstanceID() - Expect(err).ToNot(HaveOccurred()) - Expect(value).To(Equal("fake-instance-id")) - }) - - It("returns an error if it fails to get instance id", func() { - updateMetadata(MetadataContentsType{}) - - value, err := metadataService.GetInstanceID() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("Failed to load instance-id from config drive metadata service")) - - Expect(value).To(Equal("")) - }) - }) - - Describe("GetServerName", func() { - It("returns server name", func() { - value, err := metadataService.GetServerName() - Expect(err).ToNot(HaveOccurred()) - Expect(value).To(Equal("fake-server-name")) - }) - - It("returns an error if it fails to get server name", func() { - updateUserdata("{}") - - value, err := metadataService.GetServerName() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("Failed to load server name from config drive metadata service")) - - Expect(value).To(Equal("")) - }) - }) - - Describe("GetRegistryEndpoint", func() { - // It("returns an error if it fails to get registry endpoint", func() { - // updateUserdata("{}") - - // value, err := metadataService.GetRegistryEndpoint() - // Expect(err).To(HaveOccurred()) - // Expect(err.Error()).To(ContainSubstring("Failed to load registry endpoint from config drive metadata service")) - - // Expect(value).To(Equal("")) - // }) - - // Context("when user_data does not contain a dns server", func() { - // It("returns registry endpoint", func() { - // value, err := metadataService.GetRegistryEndpoint() - // Expect(err).ToNot(HaveOccurred()) - // Expect(value).To(Equal("fake-registry-endpoint")) - // }) - // }) - - Context("when user_data contains a dns server", func() { - BeforeEach(func() { - userdataContents := fmt.Sprintf( - `{"server":{"name":"%s"},"registry":{"endpoint":"%s"},"dns":{"nameserver":["%s"]}}`, - "fake-server-name", - "http://fake-registry.com", - "fake-dns-server-ip", - ) - updateUserdata(userdataContents) - }) - - }) - }) - - Describe("GetSettings", func() { - It("returns an error if it fails to get settings", func() { - updateUserdata("{}") - - _, err := metadataService.GetSettings() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("Metadata does not provide settings")) - }) - - Context("when user_data contains settings", func() { - BeforeEach(func() { - userdataContents := fmt.Sprintf( - ` -{ - "server":{"name":"%s"}, - "registry":{"endpoint":"%s"}, - "dns":{"nameserver":["%s"]}, - "agent_id":"%s", - "mbus": "%s" -}`, - "fake-server-name", - "http://fake-registry.com", - "fake-dns-server-ip", - "Agent-Foo", - "Agent-Mbus", - ) - - updateUserdata(userdataContents) - }) - - It("returns the settings", func() { - settings, err := metadataService.GetSettings() - Expect(err).ToNot(HaveOccurred()) - Expect(settings.AgentID).To(Equal("Agent-Foo")) - }) - }) - }) -} diff --git a/infrastructure/file_metadata_service.go b/infrastructure/file_metadata_service.go deleted file mode 100644 index cb9702e8c..000000000 --- a/infrastructure/file_metadata_service.go +++ /dev/null @@ -1,161 +0,0 @@ -package infrastructure - -import ( - "encoding/json" - - boshsettings "github.com/cloudfoundry/bosh-agent/settings" - bosherr "github.com/cloudfoundry/bosh-utils/errors" - boshlog "github.com/cloudfoundry/bosh-utils/logger" - boshsys "github.com/cloudfoundry/bosh-utils/system" -) - -type PublicKeyContent struct { - PublicKey string `json:"public_key"` -} - -type fileMetadataService struct { - metaDataFilePath string - userDataFilePath string - settingsFilePath string - fs boshsys.FileSystem - - logger boshlog.Logger - logTag string -} - -func NewFileMetadataService( - metaDataFilePath string, - userDataFilePath string, - settingsFilePath string, - fs boshsys.FileSystem, - logger boshlog.Logger, -) MetadataService { - return fileMetadataService{ - metaDataFilePath: metaDataFilePath, - userDataFilePath: userDataFilePath, - settingsFilePath: settingsFilePath, - fs: fs, - logTag: "fileMetadataService", - logger: logger, - } -} - -func (ms fileMetadataService) Load() error { - return nil -} - -func (ms fileMetadataService) GetPublicKey() (string, error) { - var p PublicKeyContent - - contents, err := ms.fs.ReadFile(ms.settingsFilePath) - if err != nil { - return "", bosherr.WrapError(err, "Reading metadata file") - } - - err = json.Unmarshal(contents, &p) - if err != nil { - return "", bosherr.WrapError(err, "Unmarshalling metadata") - } - - return p.PublicKey, nil -} - -func (ms fileMetadataService) GetInstanceID() (string, error) { - var metadata MetadataContentsType - - contents, err := ms.fs.ReadFile(ms.metaDataFilePath) - if err != nil { - return "", bosherr.WrapError(err, "Reading metadata file") - } - - err = json.Unmarshal(contents, &metadata) - if err != nil { - return "", bosherr.WrapError(err, "Unmarshalling metadata") - } - - ms.logger.Debug(ms.logTag, "Read metadata '%#v'", metadata) - - return metadata.InstanceID, nil -} - -func (ms fileMetadataService) GetServerName() (string, error) { - var userData UserDataContentsType - - contents, err := ms.fs.ReadFile(ms.userDataFilePath) - if err != nil { - return "", bosherr.WrapError(err, "Reading user data") - } - - err = json.Unmarshal(contents, &userData) - if err != nil { - return "", bosherr.WrapError(err, "Unmarshalling user data") - } - - ms.logger.Debug(ms.logTag, "Read user data '%#v'", userData) - - return userData.Server.Name, nil -} - -func (ms fileMetadataService) GetRegistryEndpoint() (string, error) { - var userData UserDataContentsType - - contents, err := ms.fs.ReadFile(ms.userDataFilePath) - if err != nil { - // Older versions of bosh-warden-cpi placed - // full settings file at a specific location. - return ms.settingsFilePath, nil - } - - err = json.Unmarshal(contents, &userData) - if err != nil { - return "", bosherr.WrapError(err, "Unmarshalling user data") - } - - ms.logger.Debug(ms.logTag, "Read user data '%#v'", userData) - - return userData.Registry.Endpoint, nil -} - -func (ms fileMetadataService) GetNetworks() (boshsettings.Networks, error) { - var userData UserDataContentsType - - contents, err := ms.fs.ReadFile(ms.userDataFilePath) - if err != nil { - return nil, bosherr.WrapError(err, "Reading user data") - } - - err = json.Unmarshal(contents, &userData) - if err != nil { - return nil, bosherr.WrapError(err, "Unmarshalling user data") - } - - ms.logger.Debug(ms.logTag, "Read user data '%#v'", userData) - - return userData.Networks, nil -} - -func (ms fileMetadataService) GetSettings() (boshsettings.Settings, error) { - var userData UserDataContentsType - - contents, err := ms.fs.ReadFile(ms.userDataFilePath) - if err != nil { - return boshsettings.Settings{}, bosherr.WrapError(err, "Reading user data") - } - - err = json.Unmarshal(contents, &userData) - if err != nil { - return boshsettings.Settings{}, bosherr.WrapError(err, "Unmarshalling user data") - } - - ms.logger.Debug(ms.logTag, "Read user data '%#v'", userData) - - if userData.Settings.AgentID == "" { - return boshsettings.Settings{}, bosherr.Error("Metadata does not provide settings") - } - - return userData.Settings, nil -} - -func (ms fileMetadataService) IsAvailable() bool { - return ms.fs.FileExists(ms.settingsFilePath) -} diff --git a/infrastructure/file_metadata_service_test.go b/infrastructure/file_metadata_service_test.go deleted file mode 100644 index a21ee1906..000000000 --- a/infrastructure/file_metadata_service_test.go +++ /dev/null @@ -1,268 +0,0 @@ -package infrastructure_test - -import ( - "os" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - . "github.com/cloudfoundry/bosh-agent/infrastructure" - boshsettings "github.com/cloudfoundry/bosh-agent/settings" - bosherr "github.com/cloudfoundry/bosh-utils/errors" - boshlog "github.com/cloudfoundry/bosh-utils/logger" - fakesys "github.com/cloudfoundry/bosh-utils/system/fakes" -) - -var _ = Describe("FileMetadataService", func() { - var ( - fs *fakesys.FakeFileSystem - metadataService MetadataService - ) - - BeforeEach(func() { - fs = fakesys.NewFakeFileSystem() - logger := boshlog.NewLogger(boshlog.LevelNone) - metadataService = NewFileMetadataService( - "fake-metadata-file-path", - "fake-userdata-file-path", - "fake-settings-file-path", - fs, - logger, - ) - }) - - Describe("GetInstanceID", func() { - Context("when metadata service file exists", func() { - BeforeEach(func() { - err := fs.WriteFileString("fake-metadata-file-path", `{"instance-id":"fake-instance-id"}`) - Expect(err).NotTo(HaveOccurred()) - }) - - It("returns instance id", func() { - instanceID, err := metadataService.GetInstanceID() - Expect(err).NotTo(HaveOccurred()) - Expect(instanceID).To(Equal("fake-instance-id")) - }) - }) - - Context("when metadata service file does not exist", func() { - It("returns an error", func() { - _, err := metadataService.GetInstanceID() - Expect(err).To(HaveOccurred()) - }) - }) - - Context("when metadata service file has invalid format", func() { - BeforeEach(func() { - err := fs.WriteFileString("fake-metadata-file-path", "bad-json") - Expect(err).NotTo(HaveOccurred()) - }) - - It("returns an error", func() { - _, err := metadataService.GetInstanceID() - Expect(err).To(HaveOccurred()) - }) - }) - }) - - Describe("GetServerName", func() { - Context("when userdata file exists", func() { - BeforeEach(func() { - err := fs.WriteFileString("fake-userdata-file-path", `{"server":{"name":"fake-server-name"}}`) - Expect(err).NotTo(HaveOccurred()) - }) - - It("returns server name", func() { - serverName, err := metadataService.GetServerName() - Expect(err).NotTo(HaveOccurred()) - Expect(serverName).To(Equal("fake-server-name")) - }) - }) - - Context("when userdata file does not exist", func() { - It("returns an error", func() { - serverName, err := metadataService.GetServerName() - Expect(err).To(HaveOccurred()) - Expect(serverName).To(BeEmpty()) - }) - }) - }) - - Describe("GetNetworks", func() { - It("returns the network settings", func() { - userDataContents := ` - { - "networks": { - "network_1": {"type": "manual", "ip": "1.2.3.4", "netmask": "2.3.4.5", "gateway": "3.4.5.6", "default": ["dns"], "dns": ["8.8.8.8"], "mac": "fake-mac-address-1"}, - "network_2": {"type": "dynamic", "default": ["dns"], "dns": ["8.8.8.8"], "mac": "fake-mac-address-2"} - } - }` - err := fs.WriteFileString("fake-userdata-file-path", userDataContents) - Expect(err).NotTo(HaveOccurred()) - - networks, err := metadataService.GetNetworks() - Expect(err).ToNot(HaveOccurred()) - Expect(networks).To(Equal(boshsettings.Networks{ - "network_1": boshsettings.Network{ - Type: "manual", - IP: "1.2.3.4", - Netmask: "2.3.4.5", - Gateway: "3.4.5.6", - Default: []string{"dns"}, - DNS: []string{"8.8.8.8"}, - Mac: "fake-mac-address-1", - }, - "network_2": boshsettings.Network{ - Type: "dynamic", - Default: []string{"dns"}, - DNS: []string{"8.8.8.8"}, - Mac: "fake-mac-address-2", - }, - })) - }) - - It("returns a nil Networks if the settings are missing (from an old CPI version)", func() { - userDataContents := `{}` - err := fs.WriteFileString("fake-userdata-file-path", userDataContents) - Expect(err).NotTo(HaveOccurred()) - - networks, err := metadataService.GetNetworks() - Expect(err).ToNot(HaveOccurred()) - Expect(networks).To(BeNil()) - }) - - It("raises an error if we can't read the file", func() { - networks, err := metadataService.GetNetworks() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("Reading user data:")) - be, ok := err.(bosherr.ComplexError) - Expect(ok).To(BeTrue()) - be, ok = be.Cause.(bosherr.ComplexError) - Expect(ok).To(BeTrue()) - pe, ok := be.Cause.(*os.PathError) - Expect(ok).To(BeTrue()) - Expect(os.IsNotExist(pe)).To(BeTrue()) - Expect(networks).To(BeNil()) - }) - }) - - Describe("GetRegistryEndpoint", func() { - Context("when metadata service file exists", func() { - BeforeEach(func() { - err := fs.WriteFileString( - "fake-userdata-file-path", - `{"registry":{"endpoint":"fake-registry-endpoint"}}`, - ) - Expect(err).NotTo(HaveOccurred()) - }) - - // It("returns registry endpoint", func() { - // registryEndpoint, err := metadataService.GetRegistryEndpoint() - // Expect(err).NotTo(HaveOccurred()) - // Expect(registryEndpoint).To(Equal("fake-registry-endpoint")) - // }) - }) - - // Context("when metadata service file does not exist", func() { - // It("returns registry endpoint pointing to a settings file", func() { - // registryEndpoint, err := metadataService.GetRegistryEndpoint() - // Expect(err).NotTo(HaveOccurred()) - // Expect(registryEndpoint).To(Equal("fake-settings-file-path")) - // }) - // }) - }) - - Describe("GetSettings", func() { - Context("when metadata service file exists", func() { - BeforeEach(func() { - userDataContents := ` - { - "registry":{"endpoint":"fake-registry-endpoint"}, - "agent_id":"Agent-Foo", - "mbus": "Agent-Mbus" - }` - - err := fs.WriteFileString("fake-userdata-file-path", userDataContents) - Expect(err).NotTo(HaveOccurred()) - }) - - It("returns settings", func() { - settings, err := metadataService.GetSettings() - Expect(err).NotTo(HaveOccurred()) - Expect(settings.AgentID).To(Equal("Agent-Foo")) - }) - - Context("when metadata settings does NOT contain agentID", func() { - BeforeEach(func() { - userDataContents := ` - { - "registry":{"endpoint":"fake-registry-endpoint"}, - "mbus": "Agent-Mbus" - }` - - err := fs.WriteFileString("fake-userdata-file-path", userDataContents) - Expect(err).NotTo(HaveOccurred()) - }) - - It("returns error", func() { - _, err := metadataService.GetSettings() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal("Metadata does not provide settings")) - }) - }) - }) - - Context("when metadata service file does not exist", func() { - BeforeEach(func() { - err := fs.RemoveAll("fake-settings-file-path") - Expect(err).NotTo(HaveOccurred()) - }) - - It("returns error", func() { - _, err := metadataService.GetSettings() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(ContainSubstring("Reading user data: Not found: open fake-userdata-file-path")) - }) - }) - - Context("when we have incorrect metadata in file", func() { - BeforeEach(func() { - userDataContents := ` - { - "INCORRECT JSON": , - "registry":{"endpoint":"fake-registry-endpoint"}, - "settings":{ - "mbus": "Agent-Mbus" - }` - - err := fs.WriteFileString("fake-userdata-file-path", userDataContents) - Expect(err).NotTo(HaveOccurred()) - }) - - It("returns error", func() { - _, err := metadataService.GetSettings() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal("Unmarshalling user data: invalid character ',' looking for beginning of value")) - }) - }) - }) - - Describe("IsAvailable", func() { - Context("when file does not exist", func() { - It("returns false", func() { - Expect(metadataService.IsAvailable()).To(BeFalse()) - }) - }) - - Context("when file exists", func() { - BeforeEach(func() { - err := fs.WriteFileString("fake-settings-file-path", ``) - Expect(err).NotTo(HaveOccurred()) - }) - - It("returns true", func() { - Expect(metadataService.IsAvailable()).To(BeTrue()) - }) - }) - }) -}) diff --git a/infrastructure/http_metadata_service.go b/infrastructure/http_metadata_service.go index ab4b6f698..98643fe0f 100644 --- a/infrastructure/http_metadata_service.go +++ b/infrastructure/http_metadata_service.go @@ -38,7 +38,7 @@ func NewHTTPMetadataService( tokenPath string, platform boshplat.Platform, logger boshlog.Logger, -) DynamicMetadataService { +) HTTPMetadataService { return HTTPMetadataService{ client: createRetryClient(1*time.Second, logger), metadataHost: metadataHost, @@ -62,7 +62,7 @@ func NewHTTPMetadataServiceWithCustomRetryDelay( platform boshplat.Platform, logger boshlog.Logger, retryDelay time.Duration, -) DynamicMetadataService { +) HTTPMetadataService { return HTTPMetadataService{ client: createRetryClient(retryDelay, logger), metadataHost: metadataHost, @@ -80,7 +80,7 @@ func (ms HTTPMetadataService) Load() error { return nil } -func (ms HTTPMetadataService) GetPublicKey() (string, error) { +func (ms HTTPMetadataService) PublicSSHKeyForUsername(s string) (string, error) { if ms.sshKeysPath == "" { return "", nil } @@ -210,7 +210,7 @@ func (ms HTTPMetadataService) GetNetworks() (boshsettings.Networks, error) { func (ms HTTPMetadataService) IsAvailable() bool { return true } -func (ms HTTPMetadataService) GetSettings() (boshsettings.Settings, error) { +func (ms HTTPMetadataService) Settings() (boshsettings.Settings, error) { userData, err := ms.getUserData() if err != nil { return boshsettings.Settings{}, bosherr.WrapError(err, "Getting user data") diff --git a/infrastructure/http_metadata_service_test.go b/infrastructure/http_metadata_service_test.go index 62a35deb1..b4e90ded4 100644 --- a/infrastructure/http_metadata_service_test.go +++ b/infrastructure/http_metadata_service_test.go @@ -27,7 +27,7 @@ func describeHTTPMetadataService() { //nolint:funlen dnsResolver *fakeinf.FakeDNSResolver platform *platformfakes.FakePlatform logger boshlog.Logger - metadataService MetadataService + metadataService HTTPMetadataService ) BeforeEach(func() { @@ -78,7 +78,7 @@ func describeHTTPMetadataService() { //nolint:funlen }) }) - Describe("GetPublicKey", func() { + Describe("PublicSSHKeyForUsername", func() { var ( ts *httptest.Server sshKeysPath string @@ -110,13 +110,13 @@ func describeHTTPMetadataService() { //nolint:funlen }) It("returns fetched public key", func() { - publicKey, err := metadataService.GetPublicKey() + publicKey, err := metadataService.PublicSSHKeyForUsername("") Expect(err).NotTo(HaveOccurred()) Expect(publicKey).To(Equal("fake-public-key")) }) ItEnsuresMinimalNetworkSetup(func() (string, error) { - return metadataService.GetPublicKey() + return metadataService.PublicSSHKeyForUsername("") }) }) @@ -127,7 +127,7 @@ func describeHTTPMetadataService() { //nolint:funlen }) It("returns an empty ssh key", func() { - publicKey, err := metadataService.GetPublicKey() + publicKey, err := metadataService.PublicSSHKeyForUsername("") Expect(err).NotTo(HaveOccurred()) Expect(publicKey).To(BeEmpty()) }) @@ -168,7 +168,7 @@ func describeHTTPMetadataService() { //nolint:funlen ts.Close() }) It("returns fetched public key", func() { - publicKey, err := metadataService.GetPublicKey() + publicKey, err := metadataService.PublicSSHKeyForUsername("") Expect(err).NotTo(HaveOccurred()) Expect(tokenCalls).NotTo(BeZero()) Expect(publicKey).To(Equal("fake-public-key")) @@ -204,13 +204,13 @@ func describeHTTPMetadataService() { //nolint:funlen }) It("returns empty public key", func() { - publicKey, err := metadataService.GetPublicKey() + publicKey, err := metadataService.PublicSSHKeyForUsername("") Expect(err).NotTo(HaveOccurred()) Expect(publicKey).To(BeEmpty()) }) ItEnsuresMinimalNetworkSetup(func() (string, error) { - return metadataService.GetPublicKey() + return metadataService.PublicSSHKeyForUsername("") }) }) }) diff --git a/infrastructure/instance_metadata_settings_source.go b/infrastructure/instance_metadata_settings_source.go index 80ebe0a43..d91d9cbde 100644 --- a/infrastructure/instance_metadata_settings_source.go +++ b/infrastructure/instance_metadata_settings_source.go @@ -20,7 +20,7 @@ type InstanceMetadataSettingsSource struct { logger boshlog.Logger logTag string - metadataService DynamicMetadataService + metadataService HTTPMetadataService } func NewInstanceMetadataSettingsSource( diff --git a/infrastructure/multi_settings_source.go b/infrastructure/multi_settings_source.go index 8f0c20653..fe08f525a 100644 --- a/infrastructure/multi_settings_source.go +++ b/infrastructure/multi_settings_source.go @@ -21,6 +21,10 @@ func NewMultiSettingsSource(sources ...boshsettings.Source) (boshsettings.Source return &MultiSettingsSource{sources: sources}, err } +func (s *MultiSettingsSource) GetSources() []boshsettings.Source{ + return s.sources +} + func (s *MultiSettingsSource) PublicSSHKeyForUsername(username string) (string, error) { if s.selectedSSHKeySource != nil { return s.selectedSSHKeySource.PublicSSHKeyForUsername(username) @@ -39,16 +43,7 @@ func (s *MultiSettingsSource) PublicSSHKeyForUsername(username string) (string, return "", bosherr.WrapErrorf(err, "Getting public SSH key for '%s'", username) } -func (s *MultiSettingsSource) GetMetadataService() MetadataService { - for _, source := range s.sources { - switch source := source.(type) { - case ComplexSettingsSource: - return source.GetMetadataService() - } - } - return nil -} func (s *MultiSettingsSource) Settings() (boshsettings.Settings, error) { if s.selectedSettingsSource != nil { diff --git a/infrastructure/multi_source_metadata_service.go b/infrastructure/multi_source_metadata_service.go deleted file mode 100644 index cad7c2557..000000000 --- a/infrastructure/multi_source_metadata_service.go +++ /dev/null @@ -1,93 +0,0 @@ -package infrastructure - -import ( - "errors" - - boshsettings "github.com/cloudfoundry/bosh-agent/settings" -) - -type MultiSourceMetadataService struct { - Services []MetadataService - selectedService MetadataService -} - -func NewMultiSourceMetadataService(services ...MetadataService) MetadataService { - return &MultiSourceMetadataService{Services: services} -} - -func (ms *MultiSourceMetadataService) GetPublicKey() (string, error) { - selectedService, err := ms.getSelectedService() - - if err != nil { - return "", err - } - - return selectedService.GetPublicKey() -} - -func (ms *MultiSourceMetadataService) GetInstanceID() (string, error) { - selectedService, err := ms.getSelectedService() - - if err != nil { - return "", err - } - - return selectedService.GetInstanceID() -} - -func (ms *MultiSourceMetadataService) GetServerName() (string, error) { - selectedService, err := ms.getSelectedService() - - if err != nil { - return "", err - } - - return selectedService.GetServerName() -} - -func (ms *MultiSourceMetadataService) GetNetworks() (boshsettings.Networks, error) { - selectedService, err := ms.getSelectedService() - - if err != nil { - return boshsettings.Networks{}, err - } - - return selectedService.GetNetworks() -} - -func (ms *MultiSourceMetadataService) GetSettings() (boshsettings.Settings, error) { - selectedService, err := ms.getSelectedService() - - if err != nil { - return boshsettings.Settings{}, err - } - - return selectedService.GetSettings() -} - -func (ms *MultiSourceMetadataService) IsAvailable() bool { - for _, service := range ms.Services { - if service.IsAvailable() { - return true - } - } - - return false -} - -func (ms *MultiSourceMetadataService) getSelectedService() (MetadataService, error) { - if ms.selectedService == nil { - for _, service := range ms.Services { - if service.IsAvailable() { - ms.selectedService = service - break - } - } - } - - if ms.selectedService == nil { - return nil, errors.New("services not available") - } - - return ms.selectedService, nil -} diff --git a/infrastructure/multi_source_metadata_service_test.go b/infrastructure/multi_source_metadata_service_test.go deleted file mode 100644 index a12070784..000000000 --- a/infrastructure/multi_source_metadata_service_test.go +++ /dev/null @@ -1,211 +0,0 @@ -package infrastructure_test - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - . "github.com/cloudfoundry/bosh-agent/infrastructure" - fakeinf "github.com/cloudfoundry/bosh-agent/infrastructure/fakes" - boshsettings "github.com/cloudfoundry/bosh-agent/settings" - "github.com/cloudfoundry/bosh-utils/errors" -) - -var _ = Describe("MultiSourceMetadataService", describeMultiSourceMetadataService) - -func describeMultiSourceMetadataService() { //nolint:funlen - var ( - metadataService MetadataService - service1 fakeinf.FakeMetadataService - service2 fakeinf.FakeMetadataService - ) - - BeforeEach(func() { - service1 = fakeinf.FakeMetadataService{ - Available: false, - PublicKey: "fake-public-key-1", - InstanceID: "fake-instance-id-1", - ServerName: "fake-server-name-1", - Networks: boshsettings.Networks{"net-1": boshsettings.Network{}}, - Settings: boshsettings.Settings{ - AgentID: "Agent-Foo", - Mbus: "Agent-Mbus", - }, - } - - service2 = fakeinf.FakeMetadataService{ - Available: false, - PublicKey: "fake-public-key-2", - InstanceID: "fake-instance-id-2", - ServerName: "fake-server-name-2", - Networks: boshsettings.Networks{"net-2": boshsettings.Network{}}, - } - }) - - Context("when the first service is available", func() { - BeforeEach(func() { - service1.Available = true - metadataService = NewMultiSourceMetadataService(service1, service2) - }) - - Describe("IsAvailable", func() { - It("is true", func() { - availability := metadataService.IsAvailable() - Expect(availability).To(BeTrue()) - }) - }) - - Describe("GetPublicKey", func() { - It("returns public key from the available service", func() { - publicKey, err := metadataService.GetPublicKey() - Expect(err).NotTo(HaveOccurred()) - Expect(publicKey).To(Equal("fake-public-key-1")) - }) - }) - - Describe("GetInstanceID", func() { - It("returns instance ID from the available service", func() { - instanceID, err := metadataService.GetInstanceID() - Expect(err).NotTo(HaveOccurred()) - Expect(instanceID).To(Equal("fake-instance-id-1")) - }) - }) - - Describe("GetServerName", func() { - It("returns server name from the available service", func() { - serverName, err := metadataService.GetServerName() - Expect(err).NotTo(HaveOccurred()) - Expect(serverName).To(Equal("fake-server-name-1")) - }) - }) - - Describe("GetSettings", func() { - Context("selected metadata service did not return an error", func() { - It("returns settings", func() { - settings, err := metadataService.GetSettings() - Expect(err).NotTo(HaveOccurred()) - Expect(settings.AgentID).To(Equal("Agent-Foo")) - }) - }) - - Context("selected metadata service returned an error", func() { - BeforeEach(func() { - service1.SettingsErr = errors.Error("Foo Bar") - metadataService = NewMultiSourceMetadataService(service1, service2) - }) - - It("returns the error", func() { - _, err := metadataService.GetSettings() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal("Foo Bar")) - }) - }) - }) - - Describe("GetNetworks", func() { - It("returns network settings from the available service", func() { - networks, err := metadataService.GetNetworks() - Expect(err).NotTo(HaveOccurred()) - Expect(networks).To(Equal(boshsettings.Networks{"net-1": boshsettings.Network{}})) - }) - }) - }) - - Context("when the first service is unavailable", func() { - BeforeEach(func() { - service1.Available = false - service2.Available = true - metadataService = NewMultiSourceMetadataService(service1, service2) - }) - - Describe("IsAvailable", func() { - It("is true", func() { - Expect(metadataService.IsAvailable()).To(BeTrue()) - }) - }) - - Describe("GetPublicKey", func() { - It("returns public key from the available service", func() { - publicKey, err := metadataService.GetPublicKey() - Expect(err).NotTo(HaveOccurred()) - Expect(publicKey).To(Equal("fake-public-key-2")) - }) - }) - - Describe("GetInstanceID", func() { - It("returns instance ID from the available service", func() { - instanceID, err := metadataService.GetInstanceID() - Expect(err).NotTo(HaveOccurred()) - Expect(instanceID).To(Equal("fake-instance-id-2")) - }) - }) - - Describe("GetServerName", func() { - It("returns server name from the available service", func() { - serverName, err := metadataService.GetServerName() - Expect(err).NotTo(HaveOccurred()) - Expect(serverName).To(Equal("fake-server-name-2")) - }) - }) - - Describe("GetNetworks", func() { - It("returns network settings from the available service", func() { - networks, err := metadataService.GetNetworks() - Expect(err).NotTo(HaveOccurred()) - Expect(networks).To(Equal(boshsettings.Networks{"net-2": boshsettings.Network{}})) - }) - }) - }) - - Context("when no service is available", func() { - BeforeEach(func() { - service1.Available = false - metadataService = NewMultiSourceMetadataService(service1) - }) - - Describe("IsAvailable", func() { - It("is false", func() { - Expect(metadataService.IsAvailable()).To(BeFalse()) - }) - }) - - Describe("GetPublicKey", func() { - It("returns an error", func() { - _, err := metadataService.GetPublicKey() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal("services not available")) - }) - }) - - Describe("GetInstanceID", func() { - It("returns an error getting the instance ID", func() { - _, err := metadataService.GetInstanceID() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal("services not available")) - }) - }) - - Describe("GetServerName", func() { - It("returns an error getting the server name", func() { - _, err := metadataService.GetServerName() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal("services not available")) - }) - }) - - Describe("GetNetworks", func() { - It("returns an error getting the networks", func() { - _, err := metadataService.GetNetworks() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal("services not available")) - }) - }) - - Describe("GetSettings", func() { - It("returns an error getting the settings", func() { - _, err := metadataService.GetSettings() - Expect(err).To(HaveOccurred()) - Expect(err.Error()).To(Equal("services not available")) - }) - }) - }) -} diff --git a/infrastructure/settings_source_factory.go b/infrastructure/settings_source_factory.go index d8ff60608..ba1a63e89 100644 --- a/infrastructure/settings_source_factory.go +++ b/infrastructure/settings_source_factory.go @@ -95,15 +95,12 @@ func (f SettingsSourceFactory) New() (boshsettings.Source, error) { func (f SettingsSourceFactory) buildWithoutRegistry() (boshsettings.Source, error) { settingsSources := make([]boshsettings.Source, 0, len(f.options.Sources)) - metadataServices := make([]MetadataService, 0, 1) - var metadataSource bool for _, opts := range f.options.Sources { var settingsSource boshsettings.Source - var metadataService MetadataService switch typedOpts := opts.(type) { case HTTPSourceOptions: - metadataService = NewHTTPMetadataService( + settingsSource = NewHTTPMetadataService( typedOpts.URI, typedOpts.Headers, typedOpts.UserDataPath, @@ -113,7 +110,6 @@ func (f SettingsSourceFactory) buildWithoutRegistry() (boshsettings.Source, erro f.platform, f.logger, ) - metadataSource = true case ConfigDriveSourceOptions: settingsSource = NewConfigDriveSettingsSource( @@ -148,16 +144,9 @@ func (f SettingsSourceFactory) buildWithoutRegistry() (boshsettings.Source, erro ) } - metadataServices = append(metadataServices, metadataService) settingsSources = append(settingsSources, settingsSource) } - if metadataSource { - metadataService := NewMultiSourceMetadataService(metadataServices...) - settingsSource := NewComplexSettingsSource(metadataService, f.logger) - return NewMultiSettingsSource(settingsSource) - } - return NewMultiSettingsSource(settingsSources...) } diff --git a/infrastructure/settings_source_factory_test.go b/infrastructure/settings_source_factory_test.go index b1093e8c3..659503de2 100644 --- a/infrastructure/settings_source_factory_test.go +++ b/infrastructure/settings_source_factory_test.go @@ -1,12 +1,11 @@ package infrastructure_test import ( - "reflect" - . "github.com/cloudfoundry/bosh-agent/infrastructure" "github.com/cloudfoundry/bosh-agent/platform/platformfakes" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "reflect" boshlog "github.com/cloudfoundry/bosh-utils/logger" ) @@ -38,15 +37,12 @@ var _ = Describe("SettingsSourceFactory", func() { HTTPSourceOptions{URI: "http://fake-url"}, } }) - It("returns a settings source that uses HTTP to fetch settings", func() { settingsSource, err := factory.New() Expect(err).ToNot(HaveOccurred()) - - metadataService := settingsSource.(*MultiSettingsSource).GetMetadataService() - httpMetadataService := metadataService.(*MultiSourceMetadataService).Services[0] - - Expect(reflect.TypeOf(httpMetadataService).Name()).To(Equal(reflect.TypeOf(HTTPMetadataService{}).Name())) + sources := settingsSource.(*MultiSettingsSource).GetSources() + Expect(len(sources)).To(Equal(1)) + Expect(reflect.TypeOf(sources[0]).Name()).To(Equal(reflect.TypeOf(HTTPMetadataService{}).Name())) }) }) @@ -54,92 +50,21 @@ var _ = Describe("SettingsSourceFactory", func() { BeforeEach(func() { options.Sources = []SourceOptions{ ConfigDriveSourceOptions{ - DiskPaths: []string{"/fake-disk-path"}, - + DiskPaths: []string{"/fake-disk-path"}, MetaDataPath: "fake-meta-data-path", - UserDataPath: "fake-user-data-path", - - SettingsPath: "fake-settings-path", - }, - } - }) - // was only used when registry is set to true - // It("returns a settings source that uses config drive to fetch settings", func() { - // configDriveMetadataService := NewConfigDriveMetadataService( - // platform, - // []string{"/fake-disk-path"}, - // "fake-meta-data-path", - // "fake-user-data-path", - // logger, - // ) - // multiSourceMetadataService := NewMultiSourceMetadataService(configDriveMetadataService) - // configDriveSettingsSource := NewComplexSettingsSource(multiSourceMetadataService, logger) - - // settingsSource, err := factory.New() - // Expect(err).ToNot(HaveOccurred()) - // Expect(settingsSource).To(Equal(configDriveSettingsSource)) - // }) - }) - - Context("when using File source", func() { - BeforeEach(func() { - options.Sources = []SourceOptions{ - FileSourceOptions{ - MetaDataPath: "fake-meta-data-path", - UserDataPath: "fake-user-data-path", - SettingsPath: "fake-settings-path", }, } }) - // was only used when registry is set to true - // It("returns a settings source that uses file to fetch settings", func() { - // fileMetadataService := NewFileMetadataService( - // "fake-meta-data-path", - // "fake-user-data-path", - // "fake-settings-path", - // platform.GetFs(), - // logger, - // ) - // multiSourceMetadataService := NewMultiSourceMetadataService(fileMetadataService) - // fileSettingsSource := NewComplexSettingsSource(multiSourceMetadataService, logger) - - // settingsSource, err := factory.New() - // Expect(err).ToNot(HaveOccurred()) - // Expect(settingsSource).To(Equal(fileSettingsSource)) - // }) - }) - - Context("when using ConfigDrive source", func() { - BeforeEach(func() { - options = SettingsOptions{ - Sources: []SourceOptions{ - ConfigDriveSourceOptions{ - DiskPaths: []string{"/fake-disk-path"}, - MetaDataPath: "fake-meta-data-path", - SettingsPath: "fake-settings-path", - }, - }, - } - }) - FIt("returns a settings source that uses config drive to fetch settings", func() { - - configDriveSettingsSource := NewConfigDriveSettingsSource( - []string{"/fake-disk-path"}, - "fake-meta-data-path", - "fake-settings-path", - platform, - logger, - ) - - multiSettingsSource, err := NewMultiSettingsSource(configDriveSettingsSource) - Expect(err).ToNot(HaveOccurred()) + It("returns a settings source that uses config drive to fetch settings", func() { settingsSource, err := factory.New() - Expect(err).ToNot(HaveOccurred()) - Expect(settingsSource).To(Equal(multiSettingsSource)) + sources := settingsSource.(*MultiSettingsSource).GetSources() + Expect(len(sources)).To(Equal(1)) + Expect(reflect.TypeOf(sources[0]).Elem().Name()).To(Equal(reflect.TypeOf(ConfigDriveSettingsSource{}).Name())) + }) })