/usr/share/gocode/src/github.com/go-chef/chef/role_test.go is in golang-github-go-chef-chef-dev 0.0.1+git20161023.60.deb8c38-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | package chef
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"reflect"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var (
testRoleJSON = "test/role.json"
// FML
testRole = &Role{
Name: "test",
ChefType: "role",
Description: "Test Role",
RunList: []string{"recipe[foo]", "recipe[baz]", "role[banana]"},
JsonClass: "Chef::Role",
DefaultAttributes: struct{}{},
OverrideAttributes: struct{}{},
}
)
func TestRoleName(t *testing.T) {
// BUG(spheromak): Pull these constructors out into a Convey Decorator
n1 := testRole
name := n1.Name
Convey("Role name is 'test'", t, func() {
So(name, ShouldEqual, "test")
})
}
// BUG(fujin): re-do with goconvey
func TestRoleFromJSONDecoder(t *testing.T) {
if file, err := os.Open(testRoleJSON); err != nil {
t.Error("unexpected error", err, "during os.Open on", testRoleJSON)
} else {
dec := json.NewDecoder(file)
var n Role
if err := dec.Decode(&n); err == io.EOF {
log.Println(n)
} else if err != nil {
log.Fatal(err)
}
}
}
func TestRolesService_List(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/roles", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{"foo":"http://localhost:4000/roles/foo", "webserver":"http://localhost:4000/roles/webserver"}`)
})
roles, err := client.Roles.List()
if err != nil {
t.Errorf("Roles.List returned error: %v", err)
}
want := &RoleListResult{"foo": "http://localhost:4000/roles/foo", "webserver": "http://localhost:4000/roles/webserver"}
if !reflect.DeepEqual(roles, want) {
t.Errorf("Roles.List returned %+v, want %+v", roles, want)
}
}
func TestRolesService_Get(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/roles/webserver", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{
"name": "webserver",
"chef_type": "role",
"json_class": "Chef::Role",
"default_attributes": "",
"description": "A webserver",
"run_list": [
"recipe[unicorn]",
"recipe[apache2]"
],
"override_attributes": ""
}
`)
})
role, err := client.Roles.Get("webserver")
if err != nil {
t.Errorf("Roles.Get returned error: %v", err)
}
want := &Role{
Name: "webserver",
ChefType: "role",
JsonClass: "Chef::Role",
DefaultAttributes: "",
Description: "A webserver",
RunList: []string{"recipe[unicorn]", "recipe[apache2]"},
OverrideAttributes: "",
}
if !reflect.DeepEqual(role, want) {
t.Errorf("Roles.Get returned %+v, want %+v", role, want)
}
}
func TestRolesService_Create(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/roles", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{ "uri": "http://localhost:4000/roles/webserver" }`)
})
role := &Role{
Name: "webserver",
ChefType: "role",
JsonClass: "Chef::Role",
DefaultAttributes: "",
Description: "A webserver",
RunList: []string{"recipe[unicorn]", "recipe[apache2]"},
OverrideAttributes: "",
}
uri, err := client.Roles.Create(role)
if err != nil {
t.Errorf("Roles.Create returned error: %v", err)
}
want := &RoleCreateResult{"uri": "http://localhost:4000/roles/webserver"}
if !reflect.DeepEqual(uri, want) {
t.Errorf("Roles.Create returned %+v, want %+v", uri, want)
}
}
func TestRolesService_Put(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/roles/webserver", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{
"name": "webserver",
"chef_type": "role",
"json_class": "Chef::Role",
"description": "A webserver",
"run_list": [
"recipe[apache2]"
]
}`)
})
role := &Role{
Name: "webserver",
ChefType: "role",
JsonClass: "Chef::Role",
Description: "A webserver",
RunList: []string{"recipe[apache2]"},
}
updatedRole, err := client.Roles.Put(role)
if err != nil {
t.Errorf("Roles.Put returned error: %v", err)
}
if !reflect.DeepEqual(updatedRole, role) {
t.Errorf("Roles.Put returned %+v, want %+v", updatedRole, role)
}
}
func TestRolesService_RoleListResultString(t *testing.T) {
r := &RoleListResult{"foo": "http://localhost:4000/roles/foo"}
want := "foo => http://localhost:4000/roles/foo\n"
if r.String() != want {
t.Errorf("RoleListResult.String returned %+v, want %+v", r.String(), want)
}
}
func TestRolesService_RoleCreateResultString(t *testing.T) {
r := &RoleCreateResult{"uri": "http://localhost:4000/roles/webserver"}
want := "uri => http://localhost:4000/roles/webserver\n"
if r.String() != want {
t.Errorf("RoleCreateResult.String returned %+v, want %+v", r.String(), want)
}
}
func TestRolesService_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/roles/webserver", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `{
"name": "webserver",
"chef_type": "role",
"json_class": "Chef::Role",
"description": "A webserver",
"run_list": [
"recipe[apache2]"
]
}`)
})
err := client.Roles.Delete("webserver")
if err != nil {
t.Errorf("Roles.Delete returned error: %v", err)
}
}
|