This repository has been archived by the owner on Jun 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from suzuki-shunsuke/test/test-graylog_role
Test graylog_role
- Loading branch information
Showing
9 changed files
with
242 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,161 @@ | ||
package graylog | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/suzuki-shunsuke/flute/flute" | ||
"github.com/suzuki-shunsuke/go-set/v6" | ||
) | ||
|
||
// func testDeleteRole( | ||
// ctx context.Context, cl *client.Client, name string, | ||
// ) resource.TestCheckFunc { | ||
// return func(tfState *terraform.State) error { | ||
// if _, _, err := cl.GetRole(ctx, name); err == nil { | ||
// return fmt.Errorf(`role "%s" must be deleted`, name) | ||
// } | ||
// return nil | ||
// } | ||
// } | ||
// | ||
// func testCreateRole( | ||
// ctx context.Context, cl *client.Client, name string, | ||
// ) resource.TestCheckFunc { | ||
// return func(tfState *terraform.State) error { | ||
// _, _, err := cl.GetRole(ctx, name) | ||
// return err | ||
// } | ||
// } | ||
// | ||
// func testUpdateRole( | ||
// ctx context.Context, cl *client.Client, name, description string, | ||
// ) resource.TestCheckFunc { | ||
// return func(tfState *terraform.State) error { | ||
// role, _, err := cl.GetRole(ctx, name) | ||
// if err != nil { | ||
// return err | ||
// } | ||
// if role.Description != description { | ||
// return errors.New("role.Description is not updated") | ||
// } | ||
// return nil | ||
// } | ||
// } | ||
func genTestRoleBody(role map[string]interface{}, bodyString string, store *bodyStore) func(t *testing.T, req *http.Request, svc *flute.Service, route *flute.Route) { | ||
return func(t *testing.T, req *http.Request, svc *flute.Service, route *flute.Route) { | ||
body := map[string]interface{}{} | ||
require.Nil(t, json.NewDecoder(req.Body).Decode(&body)) | ||
|
||
perms := set.NewStrSet() | ||
for _, p := range body["permissions"].([]interface{}) { | ||
perms.Add(p.(string)) | ||
} | ||
body["permissions"] = perms | ||
assert.Equal(t, role, body) | ||
store.Set(bodyString) | ||
} | ||
} | ||
|
||
func TestAccRole(t *testing.T) { | ||
// ctx := context.Background() | ||
// cl, err := setEnv() | ||
// if err != nil { | ||
// t.Fatal(err) | ||
// } | ||
// | ||
// testAccProvider := Provider() | ||
// testAccProviders := map[string]terraform.ResourceProvider{ | ||
// "graylog": testAccProvider, | ||
// } | ||
// | ||
// roleTf := ` | ||
//resource "graylog_role" "test-terraform" { | ||
// name = "test terraform name" | ||
// description = "test terraform description" | ||
// permissions = ["*"] | ||
//}` | ||
// description := "test terraform description updated" | ||
// updateTf := fmt.Sprintf(` | ||
//resource "graylog_role" "test-terraform" { | ||
// name = "test terraform name" | ||
// description = "%s" | ||
// permissions = ["*"] | ||
//}`, description) | ||
// name := "test terraform name" | ||
// resource.Test(t, resource.TestCase{ | ||
// Providers: testAccProviders, | ||
// CheckDestroy: testDeleteRole(ctx, cl, name), | ||
// Steps: []resource.TestStep{ | ||
// { | ||
// Config: roleTf, | ||
// Check: resource.ComposeTestCheckFunc( | ||
// testCreateRole(ctx, cl, name), | ||
// ), | ||
// }, | ||
// { | ||
// Config: updateTf, | ||
// Check: resource.ComposeTestCheckFunc( | ||
// testUpdateRole(ctx, cl, name, description), | ||
// ), | ||
// }, | ||
// }, | ||
// }) | ||
setEnv() | ||
|
||
roleBody, err := ioutil.ReadFile("../../testdata/role/role.json") | ||
require.Nil(t, err) | ||
|
||
updateRoleBody, err := ioutil.ReadFile("../../testdata/role/update_role.json") | ||
require.Nil(t, err) | ||
|
||
roleTF, err := ioutil.ReadFile("../../testdata/role/role.tf") | ||
require.Nil(t, err) | ||
|
||
updateRoleTF, err := ioutil.ReadFile("../../testdata/role/update_role.tf") | ||
require.Nil(t, err) | ||
|
||
store := newBodyStore("") | ||
|
||
defaultTransport := http.DefaultClient.Transport | ||
defer func() { | ||
http.DefaultClient.Transport = defaultTransport | ||
}() | ||
http.DefaultClient.Transport = &flute.Transport{ | ||
T: t, | ||
Services: []flute.Service{ | ||
{ | ||
Endpoint: "http://example.com", | ||
Routes: []flute.Route{ | ||
{ | ||
Matcher: &flute.Matcher{ | ||
Method: "GET", | ||
}, | ||
Tester: &flute.Tester{ | ||
Path: "/api/roles/foo", | ||
PartOfHeader: getTestHeader(), | ||
}, | ||
Response: &flute.Response{ | ||
Response: func(req *http.Request) (*http.Response, error) { | ||
return &http.Response{ | ||
StatusCode: 200, | ||
Body: ioutil.NopCloser(strings.NewReader(store.Get())), | ||
}, nil | ||
}, | ||
}, | ||
}, | ||
{ | ||
Matcher: &flute.Matcher{ | ||
Method: "POST", | ||
}, | ||
Tester: &flute.Tester{ | ||
Path: "/api/roles", | ||
PartOfHeader: getTestHeader(), | ||
Test: genTestRoleBody(map[string]interface{}{ | ||
"name": "foo", | ||
"description": "Allows reading and writing all views and extended searches (built-in)", | ||
"permissions": set.NewStrSet( | ||
"extendedsearch:create", | ||
"extendedsearch:use", | ||
"view:create", | ||
"view:edit", | ||
"view:read", | ||
"view:use", | ||
), | ||
"read_only": true, | ||
}, string(roleBody), store), | ||
}, | ||
Response: &flute.Response{ | ||
Base: http.Response{ | ||
StatusCode: 201, | ||
}, | ||
BodyString: string(roleBody), | ||
}, | ||
}, | ||
{ | ||
Matcher: &flute.Matcher{ | ||
Method: "PUT", | ||
}, | ||
Tester: &flute.Tester{ | ||
Path: "/api/roles/foo", | ||
PartOfHeader: getTestHeader(), | ||
Test: genTestRoleBody(map[string]interface{}{ | ||
"name": "foo", | ||
"description": "updated description", | ||
"permissions": set.NewStrSet( | ||
"extendedsearch:create", | ||
"extendedsearch:use", | ||
"view:edit", | ||
"view:read", | ||
"view:use", | ||
), | ||
"read_only": false, | ||
}, string(updateRoleBody), store), | ||
}, | ||
Response: &flute.Response{ | ||
Base: http.Response{ | ||
StatusCode: 201, | ||
}, | ||
BodyString: string(roleBody), | ||
}, | ||
}, | ||
{ | ||
Matcher: &flute.Matcher{ | ||
Method: "DELETE", | ||
}, | ||
Tester: &flute.Tester{ | ||
Path: "/api/roles/foo", | ||
PartOfHeader: getTestHeader(), | ||
}, | ||
Response: &flute.Response{ | ||
Base: http.Response{ | ||
StatusCode: 204, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
resource.Test(t, resource.TestCase{ | ||
Providers: getTestProviders(), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: string(roleTF), | ||
}, | ||
{ | ||
Config: string(updateRoleTF), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package graylog | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func getTestProviders() map[string]terraform.ResourceProvider { | ||
return map[string]terraform.ResourceProvider{ | ||
"graylog": Provider(), | ||
} | ||
} | ||
|
||
func getTestHeader() http.Header { | ||
return http.Header{ | ||
"Content-Type": []string{"application/json"}, | ||
"X-Requested-By": []string{"terraform-provider-graylog"}, | ||
"Authorization": nil, | ||
} | ||
} | ||
|
||
type bodyStore struct { | ||
body string | ||
mutex *sync.RWMutex | ||
} | ||
|
||
func newBodyStore(body string) *bodyStore { | ||
return &bodyStore{ | ||
body: body, | ||
mutex: &sync.RWMutex{}, | ||
} | ||
} | ||
|
||
func (store *bodyStore) Get() string { | ||
store.mutex.RLock() | ||
a := store.body | ||
store.mutex.RUnlock() | ||
return a | ||
} | ||
|
||
func (store *bodyStore) Set(body string) { | ||
store.mutex.Lock() | ||
store.body = body | ||
store.mutex.Unlock() | ||
} |
Oops, something went wrong.