From 8ca3c2ed1ff3a1aef49c27ea9b4ad9e21cc6cf3a Mon Sep 17 00:00:00 2001 From: Camilo Aguilar Date: Thu, 22 May 2014 14:37:19 -0400 Subject: [PATCH] style(httpbackoff -> pkg): Adjusts package name to follow convention --- .gitignore | 2 +- build | 7 ++++++- datasource/metadata_service.go | 4 ++-- datasource/proc_cmdline.go | 4 ++-- initialize/ssh_keys.go | 4 ++-- {httpbackoff => pkg}/http_client.go | 12 +++++------- {httpbackoff => pkg}/http_client_test.go | 10 +++++----- src/github.com/coreos/coreos-cloudinit | 1 - test | 2 +- 9 files changed, 24 insertions(+), 22 deletions(-) rename {httpbackoff => pkg}/http_client.go (93%) rename {httpbackoff => pkg}/http_client_test.go (94%) delete mode 120000 src/github.com/coreos/coreos-cloudinit diff --git a/.gitignore b/.gitignore index a95eb17..874ddb5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ *.swp bin/ coverage/ -pkg/ +gopath/ diff --git a/build b/build index a47f5e3..e6ed9bc 100755 --- a/build +++ b/build @@ -3,7 +3,12 @@ ORG_PATH="github.com/coreos" REPO_PATH="${ORG_PATH}/coreos-cloudinit" +if [ ! -h gopath/src/${REPO_PATH} ]; then + mkdir -p gopath/src/${ORG_PATH} + ln -s ../../../.. gopath/src/${REPO_PATH} || exit 255 +fi + export GOBIN=${PWD}/bin -export GOPATH=${PWD} +export GOPATH=${PWD}/gopath go build -o bin/coreos-cloudinit ${REPO_PATH} diff --git a/datasource/metadata_service.go b/datasource/metadata_service.go index 032150e..4ed036a 100644 --- a/datasource/metadata_service.go +++ b/datasource/metadata_service.go @@ -1,6 +1,6 @@ package datasource -import "github.com/coreos/coreos-cloudinit/httpbackoff" +import "github.com/coreos/coreos-cloudinit/pkg" type metadataService struct { url string @@ -11,7 +11,7 @@ func NewMetadataService(url string) *metadataService { } func (ms *metadataService) Fetch() ([]byte, error) { - client := httpbackoff.NewHttpClient() + client := pkg.NewHttpClient() return client.Get(ms.url) } diff --git a/datasource/proc_cmdline.go b/datasource/proc_cmdline.go index 716eb9b..c1e2057 100644 --- a/datasource/proc_cmdline.go +++ b/datasource/proc_cmdline.go @@ -6,7 +6,7 @@ import ( "log" "strings" - "github.com/coreos/coreos-cloudinit/httpbackoff" + "github.com/coreos/coreos-cloudinit/pkg" ) const ( @@ -31,7 +31,7 @@ func (self *procCmdline) Fetch() ([]byte, error) { return nil, err } - client := httpbackoff.NewHttpClient() + client := pkg.NewHttpClient() cfg, err := client.Get(url) if err != nil { return nil, err diff --git a/initialize/ssh_keys.go b/initialize/ssh_keys.go index 5c92e2a..67870d8 100644 --- a/initialize/ssh_keys.go +++ b/initialize/ssh_keys.go @@ -4,7 +4,7 @@ import ( "encoding/json" "fmt" - "github.com/coreos/coreos-cloudinit/httpbackoff" + "github.com/coreos/coreos-cloudinit/pkg" "github.com/coreos/coreos-cloudinit/system" ) @@ -24,7 +24,7 @@ func SSHImportKeysFromURL(system_user string, url string) error { } func fetchUserKeys(url string) ([]string, error) { - client := httpbackoff.NewHttpClient() + client := pkg.NewHttpClient() data, err := client.Get(url) if err != nil { return nil, err diff --git a/httpbackoff/http_client.go b/pkg/http_client.go similarity index 93% rename from httpbackoff/http_client.go rename to pkg/http_client.go index 6d2465a..60de072 100644 --- a/httpbackoff/http_client.go +++ b/pkg/http_client.go @@ -1,4 +1,4 @@ -package httpbackoff +package pkg import ( "crypto/tls" @@ -23,14 +23,14 @@ type HttpClient struct { // Maximum exp backoff duration. Defaults to 5 seconds MaxBackoff time.Duration - // Maximum amount of connection retries. Defaults to 15 + // Maximum number of connection retries. Defaults to 15 MaxRetries int // HTTP client timeout, this is suggested to be low since exponential // backoff will kick off too. Defaults to 2 seconds Timeout time.Duration - //Whether or not to skip TLS verification. Defaults to false + // Whether or not to skip TLS verification. Defaults to false SkipTLS bool } @@ -45,10 +45,6 @@ func NewHttpClient() *HttpClient { // Fetches a given URL with support for exponential backoff and maximum retries func (h *HttpClient) Get(rawurl string) ([]byte, error) { - if h == nil { - return nil, nil - } - if rawurl == "" { return nil, errors.New("URL is empty. Skipping.") } @@ -115,6 +111,8 @@ func (h *HttpClient) Get(rawurl string) ([]byte, error) { duration = h.MaxBackoff } + log.Printf("Sleeping for %d seconds", duration) + time.Sleep(duration) } diff --git a/httpbackoff/http_client_test.go b/pkg/http_client_test.go similarity index 94% rename from httpbackoff/http_client_test.go rename to pkg/http_client_test.go index a0ca82e..b0f0e46 100644 --- a/httpbackoff/http_client_test.go +++ b/pkg/http_client_test.go @@ -1,4 +1,4 @@ -package httpbackoff +package pkg import ( "fmt" @@ -19,7 +19,7 @@ var expBackoffTests = []struct { // Test exponential backoff and that it continues retrying if a 5xx response is // received -func TestFetchURLExpBackOff(t *testing.T) { +func TestGetURLExpBackOff(t *testing.T) { client := NewHttpClient() for i, tt := range expBackoffTests { @@ -52,7 +52,7 @@ func TestFetchURLExpBackOff(t *testing.T) { } // Test that it stops retrying if a 4xx response comes back -func TestFetchURL4xx(t *testing.T) { +func TestGetURL4xx(t *testing.T) { client := NewHttpClient() retries := 0 ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -72,7 +72,7 @@ func TestFetchURL4xx(t *testing.T) { } // Test that it fetches and returns user-data just fine -func TestFetchURL2xx(t *testing.T) { +func TestGetURL2xx(t *testing.T) { var cloudcfg = ` #cloud-config coreos: @@ -103,7 +103,7 @@ coreos: } // Test attempt to fetching using malformed URL -func TestFetchURLMalformed(t *testing.T) { +func TestGetMalformedURL(t *testing.T) { client := NewHttpClient() var tests = []struct { diff --git a/src/github.com/coreos/coreos-cloudinit b/src/github.com/coreos/coreos-cloudinit deleted file mode 120000 index 1b20c9f..0000000 --- a/src/github.com/coreos/coreos-cloudinit +++ /dev/null @@ -1 +0,0 @@ -../../../ \ No newline at end of file diff --git a/test b/test index 73875c7..801c9f8 100755 --- a/test +++ b/test @@ -13,7 +13,7 @@ COVER=${COVER:-"-cover"} source ./build -declare -a TESTPKGS=(initialize system datasource httpbackoff) +declare -a TESTPKGS=(initialize system datasource pkg) if [ -z "$PKG" ]; then GOFMTPATH="$TESTPKGS coreos-cloudinit.go"