Skip to content

Commit

Permalink
TestSuggestCloudGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Jul 13, 2015
1 parent fae975e commit b1e4e9d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 4 deletions.
5 changes: 1 addition & 4 deletions spec/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ func isEC2() bool {

func isGCE() bool {
_, err := requestGCEMeta()
if err != nil {
return false
}
return true
return err == nil
}

func requestGCEMeta() ([]byte, error) {
Expand Down
70 changes: 70 additions & 0 deletions spec/cloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,73 @@ func TestGCEGenerate(t *testing.T) {
}

}

func TestSuggestCloudGenerator(t *testing.T) {
// both of ec2BaseURL and gceMetaURL are unreachable
unreachableURL, _ := url.Parse("http://unreachable.localhost")
ec2BaseURL = unreachableURL
gceMetaURL = unreachableURL
cGen := SuggestCloudGenerator()
if cGen != nil {
t.Errorf("cGen should be nil but, %s", cGen)
}

func() { // ec2BaseURL is reachable but returns 404
ts := httptest.NewServer(http.NotFoundHandler())
defer ts.Close()
u, _ := url.Parse(ts.URL)
ec2BaseURL = u

cGen = SuggestCloudGenerator()
if cGen != nil {
t.Errorf("cGen should be nil but, %s", cGen)
}
}()

func() { // suggest EC2Generator
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
fmt.Fprint(res, "OK:ami-id")
}))
defer ts.Close()
u, _ := url.Parse(ts.URL)
ec2BaseURL = u

cGen = SuggestCloudGenerator()
if cGen == nil {
t.Errorf("cGen should not be nil.")
}

ec2gen, ok := cGen.CloudMetaGenerator.(*EC2Generator)

if !ok {
t.Errorf("cGen should be *EC2Generator")
}

if ec2gen.baseURL != ec2BaseURL {
t.Errorf("something went wrong")
}
}()

func() { // suggest EC2Generator
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
fmt.Fprint(res, "GCE:OK")
}))
defer ts.Close()
ec2BaseURL = unreachableURL
u, _ := url.Parse(ts.URL)
gceMetaURL = u

cGen = SuggestCloudGenerator()
if cGen == nil {
t.Errorf("cGen should not be nil.")
}

gceGen, ok := cGen.CloudMetaGenerator.(*GCEGenerator)
if !ok {
t.Errorf("cGen should be *GCEGenerator")
}
if gceGen.metaURL != gceMetaURL {
t.Errorf("something went wrong")
}
}()
}

0 comments on commit b1e4e9d

Please sign in to comment.