2014-10-08 21:08:35 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-02-22 20:02:19 +01:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-10-08 21:08:35 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPull_noRemote(t *testing.T) {
|
|
|
|
tmp, cwd := testCwd(t)
|
2014-10-09 23:45:08 +02:00
|
|
|
defer testFixCwd(t, tmp, cwd)
|
2014-10-08 21:08:35 +02:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PullCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 1 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-08 23:57:22 +02:00
|
|
|
func TestPull_local(t *testing.T) {
|
2014-10-08 21:08:35 +02:00
|
|
|
tmp, cwd := testCwd(t)
|
2014-10-09 23:45:08 +02:00
|
|
|
defer testFixCwd(t, tmp, cwd)
|
2014-10-08 21:08:35 +02:00
|
|
|
|
|
|
|
s := terraform.NewState()
|
|
|
|
s.Serial = 10
|
2014-10-09 00:15:14 +02:00
|
|
|
conf, srv := testRemoteState(t, s, 200)
|
2014-10-08 21:08:35 +02:00
|
|
|
|
|
|
|
s = terraform.NewState()
|
|
|
|
s.Serial = 5
|
|
|
|
s.Remote = conf
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
// Store the local state
|
2015-02-22 20:02:19 +01:00
|
|
|
statePath := filepath.Join(tmp, DefaultDataDir, DefaultStateFilename)
|
|
|
|
if err := os.MkdirAll(filepath.Dir(statePath), 0755); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
f, err := os.Create(statePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
err = terraform.WriteState(s, f)
|
|
|
|
f.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2014-10-08 21:08:35 +02:00
|
|
|
|
|
|
|
ui := new(cli.MockUi)
|
|
|
|
c := &PullCommand{
|
|
|
|
Meta: Meta{
|
|
|
|
ContextOpts: testCtxConfig(testProvider()),
|
|
|
|
Ui: ui,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
args := []string{}
|
|
|
|
if code := c.Run(args); code != 0 {
|
|
|
|
t.Fatalf("bad: \n%s", ui.ErrorWriter.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// testRemoteState is used to make a test HTTP server to
|
|
|
|
// return a given state file
|
2014-10-09 00:15:14 +02:00
|
|
|
func testRemoteState(t *testing.T, s *terraform.State, c int) (*terraform.RemoteState, *httptest.Server) {
|
2014-10-08 21:08:35 +02:00
|
|
|
var b64md5 string
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
|
|
|
|
if s != nil {
|
|
|
|
enc := json.NewEncoder(buf)
|
|
|
|
if err := enc.Encode(s); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
md5 := md5.Sum(buf.Bytes())
|
|
|
|
b64md5 = base64.StdEncoding.EncodeToString(md5[:16])
|
|
|
|
}
|
|
|
|
|
|
|
|
cb := func(resp http.ResponseWriter, req *http.Request) {
|
2014-10-09 00:15:14 +02:00
|
|
|
if req.Method == "PUT" {
|
|
|
|
resp.WriteHeader(c)
|
|
|
|
return
|
|
|
|
}
|
2014-10-08 21:08:35 +02:00
|
|
|
if s == nil {
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp.Header().Set("Content-MD5", b64md5)
|
|
|
|
resp.Write(buf.Bytes())
|
|
|
|
}
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(cb))
|
|
|
|
remote := &terraform.RemoteState{
|
2014-12-05 04:06:47 +01:00
|
|
|
Type: "http",
|
|
|
|
Config: map[string]string{"address": srv.URL},
|
2014-10-08 21:08:35 +02:00
|
|
|
}
|
|
|
|
return remote, srv
|
|
|
|
}
|