github.com/go-chef/chef@v0.30.1/group_test.go (about) 1 package chef 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io" 7 "log" 8 "net/http" 9 "os" 10 "reflect" 11 "testing" 12 ) 13 14 var ( 15 testGroupJSON = "test/group.json" 16 ) 17 18 func TestGroupFromJSONDecoder(t *testing.T) { 19 if file, err := os.Open(testGroupJSON); err != nil { 20 t.Error("Unexpected error '", err, "' during os.Open on", testGroupJSON) 21 } else { 22 dec := json.NewDecoder(file) 23 var g Group 24 if err := dec.Decode(&g); err == io.EOF { 25 log.Println(g) 26 } else if err != nil { 27 log.Fatal(err) 28 } 29 } 30 } 31 32 // TODO: Break out these method tests into separate test functions. 33 func TestGroupsService_Methods(t *testing.T) { 34 setup() 35 defer teardown() 36 37 // Set up our HTTP routes. 38 // FIXME: We should return HTTP response codes as defined by the Chef API so we can test for them. 39 mux.HandleFunc("/groups", func(w http.ResponseWriter, r *http.Request) { 40 switch { 41 case r.Method == "GET": 42 fmt.Fprintf(w, `{"group1": "https://url/for/group1", "group2": "https://url/for/group2"}`) 43 case r.Method == "POST": 44 fmt.Fprintf(w, `{ "uri": "http://localhost:4545/groups/group3" }`) 45 } 46 }) 47 48 mux.HandleFunc("/groups/group3", func(w http.ResponseWriter, r *http.Request) { 49 switch { 50 // TODO: Add true test for PUT, updating an existing value. 51 case r.Method == "GET": 52 fmt.Fprintf(w, `{ 53 "name": "group3", 54 "groupname": "group3", 55 "orgname": "Test Org, LLC", 56 "actors": ["tester"], 57 "clients": ["tester"], 58 "groups": ["nested-group"] 59 }`) 60 case r.Method == "PUT": 61 fmt.Fprintf(w, `{ 62 "name": "group3", 63 "groupname": "group3", 64 "actors": { 65 "clients": [], 66 "groups": [], 67 "users": ["tester2"] 68 } 69 }`) 70 case r.Method == "DELETE": 71 } 72 }) 73 74 // Test list 75 groups, err := client.Groups.List() 76 if err != nil { 77 t.Errorf("Groups.List returned error: %v", err) 78 } 79 80 listWant := map[string]string{"group1": "https://url/for/group1", "group2": "https://url/for/group2"} 81 82 if !reflect.DeepEqual(groups, listWant) { 83 t.Errorf("Groups.List returned %+v, want %+v", groups, listWant) 84 } 85 86 // test Get 87 group, err := client.Groups.Get("group3") 88 if err != nil { 89 t.Errorf("Groups.Get returned error: %v", err) 90 } 91 92 var wantGroup Group 93 wantGroup.Name = "group3" 94 wantGroup.GroupName = "group3" 95 wantGroup.OrgName = "Test Org, LLC" 96 wantGroup.Actors = []string{"tester"} 97 wantGroup.Clients = []string{"tester"} 98 wantGroup.Groups = []string{"nested-group"} 99 if !reflect.DeepEqual(group, wantGroup) { 100 t.Errorf("Groups.Get returned %+v, want %+v", group, wantGroup) 101 } 102 103 // test Create 104 res, err := client.Groups.Create(wantGroup) 105 if err != nil { 106 t.Errorf("Groups.Create returned error: %s", err.Error()) 107 } 108 109 createResult := &GroupResult{"http://localhost:4545/groups/group3"} 110 if !reflect.DeepEqual(createResult, res) { 111 t.Errorf("Groups.Post returned %+v, want %+v", res, createResult) 112 } 113 114 // test Update 115 groupupdate := GroupUpdate{} 116 groupupdate.Name = "group3" 117 groupupdate.GroupName = "group3" 118 groupupdate.Actors.Clients = []string{} 119 groupupdate.Actors.Groups = []string{} 120 groupupdate.Actors.Users = []string{"tester2"} 121 updateRes, err := client.Groups.Update(groupupdate) 122 if err != nil { 123 t.Errorf("Groups Update returned error %v", err) 124 } 125 126 if !reflect.DeepEqual(updateRes, groupupdate) { 127 t.Errorf("Groups Update returned %+v, want %+v", updateRes, groupupdate) 128 } 129 130 // test Delete 131 err = client.Groups.Delete(group.Name) 132 if err != nil { 133 t.Errorf("Groups.Delete returned error: %v", err) 134 } 135 }