From 7eef44f0c85807530c1dfc0cd78611ad8f3ea3ac Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Wed, 21 May 2014 15:44:38 -0400 Subject: [PATCH] Adding examples that can optionally be used as acceptance tests. Change-Id: Ie13680476865ece878a1c3486bbc3fd61c0d41b9 --- README.md | 11 ++++++++++- examples/00-authentication.go | 22 ++++++++++++++++++++++ examples/config.json.dist | 7 +++++++ examples/run-all.sh | 20 ++++++++++++++++++++ examples/setup.go | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 examples/00-authentication.go create mode 100644 examples/config.json.dist create mode 100755 examples/run-all.sh create mode 100644 examples/setup.go diff --git a/README.md b/README.md index 7971817..fdfadd8 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,19 @@ convention: `go doc`. Here is a short example code snippet: httpHdr, err := objectstorage.GetAccountMeta(objectstorageHost, 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 ------- 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 you can properly test. If you do not have an account, then running `go test` on the `*_integration_test.go` files will fail. diff --git a/examples/00-authentication.go b/examples/00-authentication.go new file mode 100644 index 0000000..c03b1e6 --- /dev/null +++ b/examples/00-authentication.go @@ -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.") + } +} diff --git a/examples/config.json.dist b/examples/config.json.dist new file mode 100644 index 0000000..1e253b1 --- /dev/null +++ b/examples/config.json.dist @@ -0,0 +1,7 @@ +{ + "Host": "https://.../tokens", + "Username": "", + "Password": "", + "ProjectID": "", + "ProjectName": "" +} \ No newline at end of file diff --git a/examples/run-all.sh b/examples/run-all.sh new file mode 100755 index 0000000..41b5022 --- /dev/null +++ b/examples/run-all.sh @@ -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 \ No newline at end of file diff --git a/examples/setup.go b/examples/setup.go new file mode 100644 index 0000000..7a3e6d9 --- /dev/null +++ b/examples/setup.go @@ -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 +}