Merge pull request #111 from namsral/patch-1
Trim newlines from the cloud-config-url option
This commit is contained in:
commit
59d1eba423
@ -12,19 +12,22 @@ const (
|
|||||||
ProcCmdlineCloudConfigFlag = "cloud-config-url"
|
ProcCmdlineCloudConfigFlag = "cloud-config-url"
|
||||||
)
|
)
|
||||||
|
|
||||||
type procCmdline struct{}
|
type procCmdline struct{
|
||||||
|
Location string
|
||||||
|
}
|
||||||
|
|
||||||
func NewProcCmdline() *procCmdline {
|
func NewProcCmdline() *procCmdline {
|
||||||
return &procCmdline{}
|
return &procCmdline{Location: ProcCmdlineLocation}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *procCmdline) Fetch() ([]byte, error) {
|
func (self *procCmdline) Fetch() ([]byte, error) {
|
||||||
cmdline, err := ioutil.ReadFile(ProcCmdlineLocation)
|
contents, err := ioutil.ReadFile(self.Location)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
url, err := findCloudConfigURL(string(cmdline))
|
cmdline := strings.TrimSpace(string(contents))
|
||||||
|
url, err := findCloudConfigURL(cmdline)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package datasource
|
package datasource
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -45,3 +50,39 @@ func TestParseCmdlineCloudConfigFound(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProcCmdlineAndFetchConfig(t *testing.T) {
|
||||||
|
|
||||||
|
var (
|
||||||
|
ProcCmdlineTmpl = "foo=bar cloud-config-url=%s/config\n"
|
||||||
|
CloudConfigContent = "#cloud-config\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == "GET" && r.RequestURI == "/config" {
|
||||||
|
fmt.Fprint(w, CloudConfigContent)
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
file, err := ioutil.TempFile(os.TempDir(), "test_proc_cmdline")
|
||||||
|
defer os.Remove(file.Name())
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Test produced error: %v", err)
|
||||||
|
}
|
||||||
|
_, err = file.Write([]byte(fmt.Sprintf(ProcCmdlineTmpl, ts.URL)))
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Test produced error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
p := NewProcCmdline()
|
||||||
|
p.Location = file.Name()
|
||||||
|
cfg, err := p.Fetch()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Test produced error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if string(cfg) != CloudConfigContent {
|
||||||
|
t.Errorf("Test failed, response body: %s != %s", cfg, CloudConfigContent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user