github.com/go-chef/chef@v0.30.1/databag_test.go (about) 1 package chef 2 3 import ( 4 "fmt" 5 "net/http" 6 "reflect" 7 "testing" 8 ) 9 10 func TestDataBagsService_List(t *testing.T) { 11 setup() 12 defer teardown() 13 14 mux.HandleFunc("/data", func(w http.ResponseWriter, r *http.Request) { 15 fmt.Fprintf(w, `{"bag1":"http://localhost/data/bag1", "bag2":"http://localhost/data/bag2"}`) 16 }) 17 18 databags, err := client.DataBags.List() 19 if err != nil { 20 t.Errorf("DataBags.List returned error: %v", err) 21 } 22 23 want := &DataBagListResult{"bag1": "http://localhost/data/bag1", "bag2": "http://localhost/data/bag2"} 24 if !reflect.DeepEqual(databags, want) { 25 t.Errorf("DataBags.List returned %+v, want %+v", databags, want) 26 } 27 } 28 29 func TestDataBagsService_Create(t *testing.T) { 30 setup() 31 defer teardown() 32 33 mux.HandleFunc("/data", func(w http.ResponseWriter, r *http.Request) { 34 fmt.Fprintf(w, `{"uri": "http://localhost/data/newdatabag"}`) 35 }) 36 37 databag := &DataBag{Name: "newdatabag"} 38 response, err := client.DataBags.Create(databag) 39 if err != nil { 40 t.Errorf("DataBags.Create returned error: %v", err) 41 } 42 43 want := &DataBagCreateResult{URI: "http://localhost/data/newdatabag"} 44 if !reflect.DeepEqual(response, want) { 45 t.Errorf("DataBags.Create returned %+v, want %+v", response, want) 46 } 47 } 48 49 func TestDataBagsService_Delete(t *testing.T) { 50 setup() 51 defer teardown() 52 53 mux.HandleFunc("/data/databag", func(w http.ResponseWriter, r *http.Request) { 54 fmt.Fprintf(w, `{"name": "databag", "json_class": "Chef::DataBag", "chef_type": "data_bag"}`) 55 }) 56 57 response, err := client.DataBags.Delete("databag") 58 if err != nil { 59 t.Errorf("DataBags.Delete returned error: %v", err) 60 } 61 62 want := &DataBag{ 63 Name: "databag", 64 JsonClass: "Chef::DataBag", 65 ChefType: "data_bag", 66 } 67 68 if !reflect.DeepEqual(response, want) { 69 t.Errorf("DataBags.Delete returned %+v, want %+v", response, want) 70 } 71 } 72 73 func TestDataBagsService_ListItems(t *testing.T) { 74 setup() 75 defer teardown() 76 77 mux.HandleFunc("/data/bag1", func(w http.ResponseWriter, r *http.Request) { 78 fmt.Fprintf(w, `{"item1":"http://localhost/data/bag1/item1", "item2":"http://localhost/data/bag1/item2"}`) 79 }) 80 81 databags, err := client.DataBags.ListItems("bag1") 82 if err != nil { 83 t.Errorf("DataBags.ListItems returned error: %v", err) 84 } 85 86 want := &DataBagListResult{"item1": "http://localhost/data/bag1/item1", "item2": "http://localhost/data/bag1/item2"} 87 if !reflect.DeepEqual(databags, want) { 88 t.Errorf("DataBags.ListItems returned %+v, want %+v", databags, want) 89 } 90 } 91 92 func TestDataBagsService_GetItem(t *testing.T) { 93 setup() 94 defer teardown() 95 96 mux.HandleFunc("/data/bag1/item1", func(w http.ResponseWriter, r *http.Request) { 97 fmt.Fprintf(w, `{"id":"item1", "stuff":"things"}`) 98 }) 99 100 _, err := client.DataBags.GetItem("bag1", "item1") 101 if err != nil { 102 t.Errorf("DataBags.GetItem returned error: %v", err) 103 } 104 } 105 106 func TestDataBagsService_CreateItem(t *testing.T) { 107 setup() 108 defer teardown() 109 110 mux.HandleFunc("/data/bag1", func(w http.ResponseWriter, r *http.Request) { 111 fmt.Fprintf(w, ``) 112 }) 113 114 dbi := map[string]string{ 115 "id": "item1", 116 "foo": "test123", 117 } 118 119 err := client.DataBags.CreateItem("bag1", dbi) 120 if err != nil { 121 t.Errorf("DataBags.CreateItem returned error: %v", err) 122 } 123 } 124 125 func TestDataBagsService_DeleteItem(t *testing.T) { 126 setup() 127 defer teardown() 128 129 mux.HandleFunc("/data/bag1/item1", func(w http.ResponseWriter, r *http.Request) { 130 fmt.Fprintf(w, ``) 131 }) 132 133 err := client.DataBags.DeleteItem("bag1", "item1") 134 if err != nil { 135 t.Errorf("DataBags.DeleteItem returned error: %v", err) 136 } 137 } 138 139 func TestDataBagsService_UpdateItem(t *testing.T) { 140 setup() 141 defer teardown() 142 143 mux.HandleFunc("/data/bag1/item1", func(w http.ResponseWriter, r *http.Request) { 144 fmt.Fprintf(w, ``) 145 }) 146 147 dbi := map[string]string{ 148 "id": "item1", 149 "foo": "test123", 150 } 151 152 err := client.DataBags.UpdateItem("bag1", "item1", dbi) 153 if err != nil { 154 t.Errorf("DataBags.UpdateItem returned error: %v", err) 155 } 156 } 157 158 func TestDataBagsService_DataBagListResultString(t *testing.T) { 159 e := &DataBagListResult{"bag1": "http://localhost/data/bag1", "bag2": "http://localhost/data/bag2"} 160 // The output order is not guarenteed by the String function, check for either order 161 want := "bag1 => http://localhost/data/bag1\nbag2 => http://localhost/data/bag2\n" 162 want2 := "bag2 => http://localhost/data/bag2\nbag1 => http://localhost/data/bag1\n" 163 ebag := e.String() 164 if ebag != want && ebag != want2 { 165 t.Errorf("DataBagListResult.String returned:\n%+v\nwant:\n%+v\n", ebag, want) 166 } 167 }