github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/config/config_test.go (about)

     1  package config_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "github.com/onsi/ginkgo"
     7  	. "github.com/onsi/gomega"
     8  
     9  	"github.com/cloudfoundry-incubator/ltc/config"
    10  )
    11  
    12  var _ = Describe("Config", func() {
    13  	var (
    14  		testPersister *fakePersister
    15  		testConfig    *config.Config
    16  	)
    17  
    18  	BeforeEach(func() {
    19  		testPersister = &fakePersister{}
    20  		testConfig = config.New(testPersister)
    21  	})
    22  
    23  	Describe("Target", func() {
    24  		It("sets the target", func() {
    25  			testConfig.SetTarget("mynewapi.com")
    26  
    27  			Expect(testConfig.Target()).To(Equal("mynewapi.com"))
    28  		})
    29  	})
    30  
    31  	Describe("Username", func() {
    32  		It("sets the target", func() {
    33  			testConfig.SetLogin("ausername", "apassword")
    34  
    35  			Expect(testConfig.Username()).To(Equal("ausername"))
    36  		})
    37  	})
    38  
    39  	Describe("Receptor", func() {
    40  		It("returns the Receptor with a username and password", func() {
    41  			testConfig.SetTarget("mynewapi.com")
    42  			testConfig.SetLogin("testusername", "testpassword")
    43  
    44  			Expect(testConfig.Receptor()).To(Equal("http://testusername:testpassword@receptor.mynewapi.com"))
    45  		})
    46  
    47  		It("returns a Receptor without a username and password", func() {
    48  			testConfig.SetTarget("mynewapi.com")
    49  			testConfig.SetLogin("", "")
    50  
    51  			Expect(testConfig.Receptor()).To(Equal("http://receptor.mynewapi.com"))
    52  		})
    53  	})
    54  
    55  	Describe("Loggregator", func() {
    56  		It("provides the loggregator doppler path", func() {
    57  			testConfig.SetTarget("mytestapi.com")
    58  
    59  			Expect(testConfig.Loggregator()).To(Equal("doppler.mytestapi.com"))
    60  		})
    61  	})
    62  
    63  	Describe("Save", func() {
    64  		It("Saves the target with the persistor", func() {
    65  			testConfig.SetTarget("mynewapi.com")
    66  			testConfig.SetLogin("testusername", "testpassword")
    67  			Expect(testConfig.Save()).To(Succeed())
    68  
    69  			Expect(testPersister.target).To(Equal("mynewapi.com"))
    70  			Expect(testPersister.username).To(Equal("testusername"))
    71  			Expect(testPersister.password).To(Equal("testpassword"))
    72  		})
    73  
    74  		It("returns errors from the persistor", func() {
    75  			testPersister.err = errors.New("Error")
    76  
    77  			err := testConfig.Save()
    78  			Expect(err).To(MatchError("Error"))
    79  		})
    80  	})
    81  
    82  	Describe("Load", func() {
    83  		It("loads the target, username, and password from the persister", func() {
    84  			testPersister.target = "mysavedapi.com"
    85  			testPersister.username = "saveduser"
    86  			testPersister.password = "password"
    87  
    88  			Expect(testConfig.Load()).To(Succeed())
    89  
    90  			Expect(testPersister.target).To(Equal("mysavedapi.com"))
    91  			Expect(testConfig.Receptor()).To(Equal("http://saveduser:password@receptor.mysavedapi.com"))
    92  		})
    93  
    94  		It("returns errors from loading the config", func() {
    95  			testPersister.err = errors.New("Error")
    96  
    97  			err := testConfig.Load()
    98  			Expect(err).To(MatchError("Error"))
    99  		})
   100  	})
   101  
   102  	Describe("ActiveBlobStore", func() {
   103  		It("defaults to 'dav'", func() {
   104  			Expect(testConfig.ActiveBlobStore().String()).To(Equal("dav"))
   105  		})
   106  
   107  		It("reports the active blobstore", func() {
   108  			testConfig.SetS3BlobStore("some-access-key", "some-secret-key", "some-bucket-name", "some-s3-region")
   109  			Expect(testConfig.ActiveBlobStore().String()).To(Equal("s3"))
   110  		})
   111  	})
   112  
   113  	Describe("TargetBlob", func() {
   114  		It("sets the blob target", func() {
   115  			testConfig.SetBlobStore("some-host", "7474", "some-username", "some-password")
   116  
   117  			Expect(testConfig.BlobStore()).To(Equal(config.BlobStoreConfig{
   118  				Host:     "some-host",
   119  				Port:     "7474",
   120  				Username: "some-username",
   121  				Password: "some-password",
   122  			}))
   123  		})
   124  
   125  		It("sets the activeBlobStore to 'dav'", func() {
   126  			testConfig.SetS3BlobStore("some-access-key", "some-secret-key", "some-bucket-name", "some-region")
   127  			testConfig.SetBlobStore("some-host", "7474", "some-username", "some-password")
   128  			Expect(testConfig.ActiveBlobStore().String()).To(Equal("dav"))
   129  		})
   130  	})
   131  
   132  	Describe("TargetS3Blob", func() {
   133  		It("sets the s3 blob target", func() {
   134  			testConfig.SetS3BlobStore("some-access-key", "some-secret-key", "some-bucket-name", "some-region")
   135  
   136  			Expect(testConfig.S3BlobStore()).To(Equal(config.S3BlobStoreConfig{
   137  				Region:     "some-region",
   138  				AccessKey:  "some-access-key",
   139  				SecretKey:  "some-secret-key",
   140  				BucketName: "some-bucket-name",
   141  			}))
   142  		})
   143  
   144  		It("sets the activeBlobStore to 's3'", func() {
   145  			testConfig.SetS3BlobStore("some-access-key", "some-secret-key", "some-bucket-name", "some-region")
   146  			Expect(testConfig.ActiveBlobStore().String()).To(Equal("s3"))
   147  		})
   148  	})
   149  })
   150  
   151  type fakePersister struct {
   152  	target   string
   153  	username string
   154  	password string
   155  	err      error
   156  }
   157  
   158  func (f *fakePersister) Load(dataInterface interface{}) error {
   159  	data, ok := dataInterface.(*config.Data)
   160  	Expect(ok).To(BeTrue())
   161  
   162  	data.Target = f.target
   163  	data.Username = f.username
   164  	data.Password = f.password
   165  	return f.err
   166  }
   167  
   168  func (f *fakePersister) Save(dataInterface interface{}) error {
   169  	data, ok := dataInterface.(*config.Data)
   170  	Expect(ok).To(BeTrue())
   171  
   172  	f.target = data.Target
   173  	f.username = data.Username
   174  	f.password = data.Password
   175  	return f.err
   176  }