Adding examples that can optionally be used as acceptance tests.

Change-Id: Ie13680476865ece878a1c3486bbc3fd61c0d41b9
This commit is contained in:
Matt Farina 2014-05-21 15:44:38 -04:00
parent fa86cfe9b6
commit 7eef44f0c8
5 changed files with 91 additions and 1 deletions

View File

@ -32,10 +32,19 @@ convention: `go doc`. Here is a short example code snippet:
httpHdr, err := objectstorage.GetAccountMeta(objectstorageHost, httpHdr, err := objectstorage.GetAccountMeta(objectstorageHost,
auth.Access.Token.Id) auth.Access.Token.Id)
Examples
--------
The examples directory contains examples for using the SDK using
real world working code. Each example starts with a two digit number followed
by a name (e.g., `00-authentication.go`). If you have a `config.json` file in the
examples directory following the format of `config.json.dist` the example can be
executed using `go run [example name] setup.go`. Or, all the examples can be
executed running the script `run-all.sh` from the examples directory.
Testing Testing
------- -------
There are two types of test files. The `*_test.go` are standard There are two types of test files. The `*_test.go` are standard
golang unit test files. The `*_integration_test.go` are golang unit test files. The `*_integration_test.go` are
test files that require an active OpenStack service account before test files that require an active OpenStack service account before
you can properly test. If you do not have an account, you can properly test. If you do not have an account,
then running `go test` on the `*_integration_test.go` files will fail. then running `go test` on the `*_integration_test.go` files will fail.

View File

@ -0,0 +1,22 @@
package main
import (
"fmt"
"git.openstack.org/stackforge/golang-client.git/identity"
"time"
)
// Authentication examples.
func main() {
config := getConfig()
auth, err := identity.AuthUserName(config.Host,
config.Username,
config.Password)
if err != nil {
fmt.Println("There was an error authenticating:", err)
}
if !auth.Access.Token.Expires.After(time.Now()) {
fmt.Println("There was an error. The auth token has an invalid expiration.")
}
}

View File

@ -0,0 +1,7 @@
{
"Host": "https://.../tokens",
"Username": "",
"Password": "",
"ProjectID": "",
"ProjectName": ""
}

20
examples/run-all.sh Executable file
View File

@ -0,0 +1,20 @@
#!/bin/bash
#
# Enables all the examples to execute as a form of acceptance testing.
# Get the directory the examples are in and change into it.
DIR="$(cd $(dirname "$0") && pwd)"
echo "Executing the examples in: $DIR"
cd $DIR
# Run all the tests.
for T in $(ls -1 [0-9][0-9]*.go); do
if ! [ -x $T ]; then
CMD="go run $T setup.go"
echo "$CMD ..."
if ! $CMD ; then
echo "Error executing example $T."
exit 1
fi
fi
done

32
examples/setup.go Normal file
View File

@ -0,0 +1,32 @@
// The acceptance package is a set of acceptance tests showcasing how the
// contents of the package are meant to be used. This is setup in a similar
// manner to a consuming application.
package main
import (
"encoding/json"
"io/ioutil"
)
// testconfig contains the user information needed by the acceptance and
// integration tests.
type testconfig struct {
Host string
Username string
Password string
ProjectID string
ProjectName string
}
// getConfig provides access to credentials in other tests and examples.
func getConfig() *testconfig {
config := &testconfig{}
userJSON, err := ioutil.ReadFile("config.json")
if err != nil {
panic("ReadFile json failed")
}
if err = json.Unmarshal(userJSON, &config); err != nil {
panic("Unmarshal json failed")
}
return config
}