11package remote
22
3- import "testing"
3+ import (
4+ "bytes"
5+ "crypto/md5"
6+ "os"
7+ "testing"
8+
9+ "github.com/armon/consul-api"
10+ "github.com/hashicorp/terraform/terraform"
11+ )
412
513func TestConsulRemote_Interface (t * testing.T ) {
614 var client interface {} = & ConsulRemoteClient {}
@@ -9,6 +17,157 @@ func TestConsulRemote_Interface(t *testing.T) {
917 }
1018}
1119
12- func TestConsulRemote (t * testing.T ) {
13- // TODO
20+ func checkConsul (t * testing.T ) {
21+ if os .Getenv ("CONSUL_ADDR" ) == "" {
22+ t .SkipNow ()
23+ }
24+ }
25+
26+ func TestConsulRemote_Validate (t * testing.T ) {
27+ conf := map [string ]string {}
28+ if _ , err := NewConsulRemoteClient (conf ); err == nil {
29+ t .Fatalf ("expect error" )
30+ }
31+
32+ conf ["path" ] = "test"
33+ if _ , err := NewConsulRemoteClient (conf ); err != nil {
34+ t .Fatalf ("err: %v" , err )
35+ }
36+ }
37+
38+ func TestConsulRemote_GetState (t * testing.T ) {
39+ checkConsul (t )
40+ type tcase struct {
41+ Path string
42+ Body []byte
43+ ExpectMD5 []byte
44+ ExpectErr string
45+ }
46+ inp := []byte ("testing" )
47+ inpMD5 := md5 .Sum (inp )
48+ hash := inpMD5 [:16 ]
49+ cases := []* tcase {
50+ & tcase {
51+ Path : "foo" ,
52+ Body : inp ,
53+ ExpectMD5 : hash ,
54+ },
55+ & tcase {
56+ Path : "none" ,
57+ },
58+ }
59+
60+ for _ , tc := range cases {
61+ if tc .Body != nil {
62+ conf := consulapi .DefaultConfig ()
63+ conf .Address = os .Getenv ("CONSUL_ADDR" )
64+ client , _ := consulapi .NewClient (conf )
65+ pair := & consulapi.KVPair {Key : tc .Path , Value : tc .Body }
66+ client .KV ().Put (pair , nil )
67+ }
68+
69+ remote := & terraform.RemoteState {
70+ Type : "consul" ,
71+ Config : map [string ]string {
72+ "address" : os .Getenv ("CONSUL_ADDR" ),
73+ "path" : tc .Path ,
74+ },
75+ }
76+ r , err := NewClientByState (remote )
77+ if err != nil {
78+ t .Fatalf ("Err: %v" , err )
79+ }
80+
81+ payload , err := r .GetState ()
82+ errStr := ""
83+ if err != nil {
84+ errStr = err .Error ()
85+ }
86+ if errStr != tc .ExpectErr {
87+ t .Fatalf ("bad err: %v %v" , errStr , tc .ExpectErr )
88+ }
89+
90+ if tc .ExpectMD5 != nil {
91+ if payload == nil || ! bytes .Equal (payload .MD5 , tc .ExpectMD5 ) {
92+ t .Fatalf ("bad: %#v" , payload )
93+ }
94+ }
95+
96+ if tc .Body != nil {
97+ if ! bytes .Equal (payload .State , tc .Body ) {
98+ t .Fatalf ("bad: %#v" , payload )
99+ }
100+ }
101+ }
102+ }
103+
104+ func TestConsulRemote_PutState (t * testing.T ) {
105+ checkConsul (t )
106+ path := "foobar"
107+ inp := []byte ("testing" )
108+
109+ remote := & terraform.RemoteState {
110+ Type : "consul" ,
111+ Config : map [string ]string {
112+ "address" : os .Getenv ("CONSUL_ADDR" ),
113+ "path" : path ,
114+ },
115+ }
116+ r , err := NewClientByState (remote )
117+ if err != nil {
118+ t .Fatalf ("Err: %v" , err )
119+ }
120+
121+ err = r .PutState (inp , false )
122+ if err != nil {
123+ t .Fatalf ("err: %v" , err )
124+ }
125+
126+ conf := consulapi .DefaultConfig ()
127+ conf .Address = os .Getenv ("CONSUL_ADDR" )
128+ client , _ := consulapi .NewClient (conf )
129+ pair , _ , err := client .KV ().Get (path , nil )
130+ if err != nil {
131+ t .Fatalf ("err: %v" , err )
132+ }
133+ if ! bytes .Equal (pair .Value , inp ) {
134+ t .Fatalf ("bad value" )
135+ }
136+ }
137+
138+ func TestConsulRemote_DeleteState (t * testing.T ) {
139+ checkConsul (t )
140+ path := "testdelete"
141+
142+ // Create the state
143+ conf := consulapi .DefaultConfig ()
144+ conf .Address = os .Getenv ("CONSUL_ADDR" )
145+ client , _ := consulapi .NewClient (conf )
146+ pair := & consulapi.KVPair {Key : path , Value : []byte ("test" )}
147+ client .KV ().Put (pair , nil )
148+
149+ remote := & terraform.RemoteState {
150+ Type : "consul" ,
151+ Config : map [string ]string {
152+ "address" : os .Getenv ("CONSUL_ADDR" ),
153+ "path" : path ,
154+ },
155+ }
156+ r , err := NewClientByState (remote )
157+ if err != nil {
158+ t .Fatalf ("Err: %v" , err )
159+ }
160+
161+ err = r .DeleteState ()
162+ if err != nil {
163+ t .Fatalf ("Err: %v" , err )
164+ }
165+
166+ pair , _ , err = client .KV ().Get (path , nil )
167+ if err != nil {
168+ t .Fatalf ("Err: %v" , err )
169+ }
170+ if pair != nil {
171+ t .Fatalf ("state not deleted" )
172+ }
14173}
0 commit comments