Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add worker index for rest multi user #10

Open
wants to merge 1 commit into
base: PR_2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 43 additions & 32 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,79 +16,90 @@ type Context interface {
GetBool(k string) (bool, bool)
MarshalJSON() ([]byte, error)
UnmarshalJSON(b []byte) error
Clone() contextMap
Clone() Context
}

type contextMap map[string]interface{}
type contextMap struct {
c map[string]interface{}
index int
}

func New() Context {
var c contextMap = make(contextMap)
var c = contextMap{
c: make(map[string]interface{}),
index: 0,
}
return &c
}

func (c contextMap) PutString(k string, v string) {
c[k] = v
func (c *contextMap) PutString(k string, v string) {
c.c[k] = v
}

func (c contextMap) GetString(k string) (string, bool) {
if c[k] == nil {
func (c *contextMap) GetString(k string) (string, bool) {
if c.c[k] == nil {
return "", false
} else {
return c[k].(string), true
return c.c[k].(string), true
}
}

func (c contextMap) PutInt(k string, v int) {
func (c *contextMap) PutInt(k string, v int) {
// saves as string, avoids json.Marshal turning int into json.numbers(float64)
c[k] = strconv.Itoa(v)
c.c[k] = strconv.Itoa(v)
}

func (c contextMap) GetInt(k string) (int, bool) {
if c[k] == nil {
func (c *contextMap) GetInt(k string) (int, bool) {
if c.c[k] == nil {
return 0, false
} else {
value, _ := strconv.ParseInt(c[k].(string), 0, 0)
value, _ := strconv.ParseInt(c.c[k].(string), 0, 0)
return int(value), true
}
}

func (c contextMap) PutFloat64(k string, v float64) {
c[k] = v
func (c *contextMap) PutFloat64(k string, v float64) {
c.c[k] = v
}

func (c contextMap) GetFloat64(k string) (float64, bool) {
if c[k] == nil {
func (c *contextMap) GetFloat64(k string) (float64, bool) {
if c.c[k] == nil {
return 0, false
} else {
return c[k].(float64), true
return c.c[k].(float64), true
}
}

func (c contextMap) PutBool(k string, v bool) {
c[k] = v
func (c *contextMap) PutBool(k string, v bool) {
c.c[k] = v
}

func (c contextMap) GetBool(k string) (bool, bool) {
if c[k] == nil {
func (c *contextMap) GetBool(k string) (bool, bool) {
if c.c[k] == nil {
return false, false
} else {
return c[k].(bool), true
return c.c[k].(bool), true
}
}

func (c contextMap) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}(c))
func (c *contextMap) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}(c.c))
}

func (c contextMap) UnmarshalJSON(b []byte) error {
var m map[string]interface{} = c
func (c *contextMap) UnmarshalJSON(b []byte) error {
var m map[string]interface{} = c.c
return json.Unmarshal(b, &m)
}

func (c contextMap) Clone() contextMap {
var clone = make(contextMap)
for k, v := range c {
clone[k] = v
func (c *contextMap) Clone() Context {
var clone = contextMap{
c: make(map[string]interface{}),
}
return clone
for k, v := range c.c {
clone.c[k] = v
}

clone.PutInt("workerIndex", c.index)
c.index++
return &clone
}
12 changes: 12 additions & 0 deletions context/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ var _ = Describe("Context map", func() {
result, _ = cloned.GetString("str1")
Ω(result).Should(Equal("abc"))
})
It("record workerIndex", func() {

cloned := localContext.Clone()
index, exists := cloned.GetInt("workerIndex")
Ω(exists).ShouldNot(BeFalse())
Ω(index).Should(Equal(0))
cloned = localContext.Clone()
index, exists = cloned.GetInt("workerIndex")
Ω(exists).ShouldNot(BeFalse())
Ω(index).Should(Equal(1))

})
})

})
10 changes: 5 additions & 5 deletions workloads/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func (r *rest) Target(ctx context.Context) error {
func (r *rest) Login(ctx context.Context) error {
body := &LoginResponse{}

iterationIndex, exist := ctx.GetInt("iterationIndex")
workerIndex, exist := ctx.GetInt("workerIndex")
if !exist {
return errors.New("Iteration Index does not exist in context map")
return errors.New("Worker Index does not exist in context map")
}

var userList, passList string
Expand All @@ -74,7 +74,7 @@ func (r *rest) Login(ctx context.Context) error {
}

return checkTargetted(ctx, func(loginEndpoint string, apiEndpoint string) error {
return r.PostToUaaSuccessfully(fmt.Sprintf("%s/oauth/token", loginEndpoint), r.oauthInputs(credentialsForWorker(iterationIndex, userList, passList)), body, func(reply Reply) error {
return r.PostToUaaSuccessfully(fmt.Sprintf("%s/oauth/token", loginEndpoint), r.oauthInputs(credentialsForWorker(workerIndex, userList, passList)), body, func(reply Reply) error {
ctx.PutString("token", body.Token)
return r.targetSpace(ctx)
})
Expand Down Expand Up @@ -246,10 +246,10 @@ func (s SpaceResponse) SpaceExists() bool {
return len(s.Resources) > 0
}

func credentialsForWorker(iterationIndex int, users string, passwords string) (string, string) {
func credentialsForWorker(workerIndex int, users string, passwords string) (string, string) {
var userList = strings.Split(users, ",")
var passList = strings.Split(passwords, ",")
return userList[iterationIndex%len(userList)], passList[iterationIndex%len(passList)]
return userList[workerIndex%len(userList)], passList[workerIndex%len(passList)]
}

func (r *rest) oauthInputs(username string, password string) url.Values {
Expand Down
16 changes: 8 additions & 8 deletions workloads/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var _ = Describe("Rest", func() {

BeforeEach(func() {
restContext = context.New()
restContext.PutInt("iterationIndex", 0)
restContext.PutInt("workerIndex", 0)
replies = make(map[string]interface{})
replyWithLocation = make(map[string]string)
client = &dummyClient{replies, replyWithLocation, make(map[call]interface{})}
Expand Down Expand Up @@ -216,22 +216,22 @@ var _ = Describe("Rest", func() {
Ω(data.(url.Values)["password"]).Should(Equal([]string{"pass1"}))
})

It("uses different username and password with different iterationIndex", func() {
restContext.PutInt("iterationIndex", 0)
It("uses different username and password with different workerIndex", func() {
restContext.PutInt("workerIndex", 0)
rest.Login(restContext)
data := client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token")
Ω(data.(url.Values)["username"]).Should(Equal([]string{"user1"}))
Ω(data.(url.Values)["password"]).Should(Equal([]string{"pass1"}))

restContext.PutInt("iterationIndex", 2)
restContext.PutInt("workerIndex", 2)
rest.Login(restContext)
data = client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token")
Ω(data.(url.Values)["username"]).Should(Equal([]string{"user3"}))
Ω(data.(url.Values)["password"]).Should(Equal([]string{"pass1"}))
})

It("recycles the list of username and password when there are more workers than username", func() {
restContext.PutInt("iterationIndex", 6)
restContext.PutInt("workerIndex", 6)
rest.Login(restContext)
data := client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token")
Ω(data.(url.Values)["username"]).Should(Equal([]string{"user1"}))
Expand All @@ -254,19 +254,19 @@ var _ = Describe("Rest", func() {
})

It("re-uses the only avaiable password", func() {
restContext.PutInt("iterationIndex", 0)
restContext.PutInt("workerIndex", 0)
rest.Login(restContext)
data := client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token")
Ω(data.(url.Values)["username"]).Should(Equal([]string{"user1"}))
Ω(data.(url.Values)["password"]).Should(Equal([]string{"pass1"}))

restContext.PutInt("iterationIndex", 1)
restContext.PutInt("workerIndex", 1)
rest.Login(restContext)
data = client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token")
Ω(data.(url.Values)["username"]).Should(Equal([]string{"user2"}))
Ω(data.(url.Values)["password"]).Should(Equal([]string{"pass1"}))

restContext.PutInt("iterationIndex", 2)
restContext.PutInt("workerIndex", 2)
rest.Login(restContext)
data = client.ShouldHaveBeenCalledWith("POST(uaa)", "THELOGINSERVER/PATH/oauth/token")
Ω(data.(url.Values)["username"]).Should(Equal([]string{"user3"}))
Expand Down