Skip to content

Commit 401692c

Browse files
committed
Created concurrent maps for scale testing; created inspect and delete volumes tests; Create summarizeErrorsFromStringErrorChanMap
1 parent 4ccbdf2 commit 401692c

3 files changed

Lines changed: 187 additions & 50 deletions

File tree

pkg/common/common.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package common
2+
3+
import (
4+
"sync"
5+
)
6+
7+
type ConcMap struct {
8+
l sync.RWMutex
9+
KVMap map[interface{}]interface{}
10+
}
11+
12+
func NewConcMap() *ConcMap {
13+
return &ConcMap{
14+
KVMap: make(map[interface{}]interface{}),
15+
}
16+
}
17+
18+
func (c *ConcMap) Add(key interface{}, value interface{}) {
19+
c.l.Lock()
20+
defer c.l.Unlock()
21+
c.KVMap[key] = value
22+
}
23+
24+
func (c *ConcMap) GetKeyValMap() map[interface{}]interface{} {
25+
return c.KVMap
26+
}
27+
28+
type ConcStrToStrMap struct {
29+
l sync.RWMutex
30+
KVMap map[string]string
31+
}
32+
33+
func NewConcStrToStrMap() *ConcStrToStrMap {
34+
return &ConcStrToStrMap{
35+
KVMap: make(map[string]string),
36+
}
37+
}
38+
39+
func (c *ConcStrToStrMap) Add(key string, value string) {
40+
c.l.Lock()
41+
defer c.l.Unlock()
42+
c.KVMap[key] = value
43+
}
44+
45+
func (c *ConcStrToStrMap) GetKeyValMap() map[string]string {
46+
return c.KVMap
47+
}
48+
49+
type ConcStringErrChanMap struct {
50+
l sync.RWMutex
51+
StrErrChanMap map[string]chan (error)
52+
}
53+
54+
func NewConcStringErrChanMap() *ConcStringErrChanMap {
55+
return &ConcStringErrChanMap{
56+
StrErrChanMap: make(map[string]chan (error)),
57+
}
58+
}
59+
60+
func (c *ConcStringErrChanMap) Add(key string, value chan (error)) {
61+
c.l.Lock()
62+
defer c.l.Unlock()
63+
c.StrErrChanMap[key] = value
64+
}
65+
66+
func (c *ConcStringErrChanMap) GetKeyValMap() map[string]chan (error) {
67+
c.l.Lock()
68+
defer c.l.Unlock()
69+
return c.StrErrChanMap
70+
}

pkg/sanity/securityScale.go

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,55 @@ package sanity
33
import (
44
"context"
55
"fmt"
6-
//"strconv"
76
"sync"
87
"time"
98

109
api "github.com/libopenstorage/openstorage-sdk-clients/sdk/golang"
11-
10+
common "github.com/libopenstorage/sdk-test/pkg/common"
1211
. "github.com/onsi/ginkgo"
1312
. "github.com/onsi/gomega"
1413
)
1514

1615
var scaleUsers map[string]string
16+
var userVolumeMap *common.ConcMap
17+
18+
type VolumeRequest struct {
19+
VolID string
20+
CreateRequest *api.SdkVolumeCreateRequest
21+
Token string
22+
}
23+
1724
var _ = Describe("Security Scale", func() {
1825
var (
1926
c api.OpenStorageVolumeClient
2027
ic api.OpenStorageIdentityClient
21-
// ma api.OpenStorageMountAttachClient
2228
)
2329

24-
fmt.Println("\nIt is going to run security scale tests...")
25-
2630
BeforeEach(func() {
2731
c = api.NewOpenStorageVolumeClient(conn)
2832
ic = api.NewOpenStorageIdentityClient(conn)
29-
By("creating users")
30-
scaleUsers = createXUsersTokens("scaleUsers", 10)
31-
fmt.Printf("scaleUsers: %v\n", scaleUsers)
32-
// ma = api.NewOpenStorageMountAttachClient(conn)
33-
3433
isSupported := isCapabilitySupported(
3534
ic,
3635
api.SdkServiceCapability_OpenStorageService_VOLUME,
3736
)
38-
3937
if !isSupported {
40-
Skip("Volume capability not supported , skipping related tests")
38+
Fail("Volume capability not supported , skipping related tests")
4139
}
4240
})
4341

4442
AfterEach(func() {
45-
fmt.Println("\nSecurity tests are done")
4643
})
4744

4845
Describe("Security", func() {
4946

5047
BeforeEach(func() {
5148
})
52-
It("Should be able to create the created Volume", func() {
49+
50+
It("Should be able to create the users", func() {
51+
By("Creating users")
52+
scaleUsers = createXUsersTokens("scaleUsers", 30)
53+
})
54+
It("Should be able to create the volumes", func() {
5355
By("Creating volumes")
5456
err := createVolumesConcurrently(c)
5557
Expect(err).NotTo(HaveOccurred())
@@ -59,34 +61,36 @@ var _ = Describe("Security Scale", func() {
5961
err := inspectVolumesConcurrently(c)
6062
Expect(err).NotTo(HaveOccurred())
6163
})
64+
It("Owner Should be able to delete its own Volume", func() {
65+
By("Deleting volumes")
66+
err := deleteVolumesConcurrently(c)
67+
Expect(err).NotTo(HaveOccurred())
68+
})
6269
})
6370

6471
})
6572

6673
func createVolumesConcurrently(c api.OpenStorageVolumeClient) error {
74+
//userVolumeMap is mapping user'name to volumes' ID
75+
userVolumeMap = common.NewConcMap()
76+
var volErrorMap = common.NewConcStringErrChanMap()
6777
var wg sync.WaitGroup
6878
for name, userToken := range scaleUsers {
6979
userName := name
7080
token := userToken
7181
wg.Add(1)
7282
go func(userName string, token string) {
73-
fmt.Printf("It is going to create volume by %s\n", userName)
7483
defer wg.Done()
7584
t := time.Now()
7685
tstr := t.Format("20060102150405")
77-
time.Sleep(2 * time.Second)
7886
req := &api.SdkVolumeCreateRequest{
79-
Name: "inspect-vol-" + tstr + "-" + userName,
87+
Name: "sdk-vol-" + tstr + "-" + userName,
8088
Spec: &api.VolumeSpec{
8189
Size: uint64(5 * GIGABYTE),
8290
HaLevel: 2,
8391
},
8492
}
8593
createResponse, err := c.Create(setContextWithToken(context.Background(), token), req)
86-
Expect(err).NotTo(HaveOccurred())
87-
Expect(createResponse).NotTo(BeNil())
88-
Expect(createResponse.VolumeId).NotTo(BeEmpty())
89-
9094
volID := createResponse.VolumeId
9195
resp, err := c.Inspect(
9296
setContextWithToken(context.Background(), token),
@@ -95,28 +99,67 @@ func createVolumesConcurrently(c api.OpenStorageVolumeClient) error {
9599
},
96100
)
97101
printVolumeDetails(resp.Volume)
98-
o := resp.GetVolume().GetId()
99-
name := resp.GetName()
100-
fmt.Printf("volume name: %s id is %v\n", name, o)
102+
userVolumeMap.Add(userName, resp.GetVolume().GetId())
103+
errChan := make(chan (error), 1)
104+
errChan <- err
105+
volErrorMap.Add(volID, errChan)
101106
}(userName, token)
102107
}
103108
wg.Wait()
104-
fmt.Printf("All volumes created\n")
105-
return nil
109+
for user, volID := range userVolumeMap.GetKeyValMap() {
110+
fmt.Printf("\nuser %s createdvolume ->: %s", user, volID)
111+
}
112+
return summarizeErrorsFromStringErrorChanMap(volErrorMap.GetKeyValMap())
106113
}
107114

108115
func inspectVolumesConcurrently(c api.OpenStorageVolumeClient) error {
109-
fmt.Printf("All volumes inspected. To be implemented\n")
110-
return nil
116+
var wg sync.WaitGroup
117+
var volErrorMap = common.NewConcStringErrChanMap()
118+
for user, id := range userVolumeMap.GetKeyValMap() {
119+
userName := user.(string)
120+
volID := id.(string)
121+
fmt.Printf("\nNow user %s is going to inspect volume %s", userName, volID)
122+
token := scaleUsers[userName]
123+
wg.Add(1)
124+
go func(userName string, volID string, token string) {
125+
defer wg.Done()
126+
_, err := c.Inspect(
127+
setContextWithToken(context.Background(), token),
128+
&api.SdkVolumeInspectRequest{
129+
VolumeId: volID,
130+
},
131+
)
132+
errChan := make(chan (error), 1)
133+
errChan <- err
134+
volErrorMap.Add(volID, errChan)
135+
}(userName, volID, token)
136+
}
137+
wg.Wait()
138+
//Receiving all errors from channels
139+
return summarizeErrorsFromStringErrorChanMap(volErrorMap.GetKeyValMap())
111140
}
112141

113-
func getUsernames(amount int) []string {
114-
var userNames []string
115-
if amount < 1 {
116-
return userNames
117-
}
118-
for i := 1; i <= amount; i++ {
119-
userNames = append(userNames, fmt.Sprintf("user%d", i))
142+
func deleteVolumesConcurrently(c api.OpenStorageVolumeClient) error {
143+
var wg sync.WaitGroup
144+
var volErrorMap = common.NewConcStringErrChanMap()
145+
for user, id := range userVolumeMap.GetKeyValMap() {
146+
userName := user.(string)
147+
volID := id.(string)
148+
token := scaleUsers[userName]
149+
fmt.Printf("\nNow user %s is going to delete volume %s", userName, volID)
150+
wg.Add(1)
151+
go func(userName string, volID string, token string) {
152+
defer wg.Done()
153+
err := deleteVol(
154+
setContextWithToken(context.Background(), token),
155+
c,
156+
volID,
157+
)
158+
errChan := make(chan (error), 1)
159+
errChan <- err
160+
volErrorMap.Add(volID, errChan)
161+
}(userName, volID, token)
120162
}
121-
return userNames
163+
wg.Wait()
164+
return summarizeErrorsFromStringErrorChanMap(volErrorMap.GetKeyValMap())
122165
}

pkg/sanity/utils.go

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ package sanity
1919
import (
2020
"context"
2121
"fmt"
22+
"strings"
23+
"sync"
2224
"time"
2325

2426
api "github.com/libopenstorage/openstorage-sdk-clients/sdk/golang"
25-
"github.com/libopenstorage/sdk-test/pkg/auth"
27+
auth "github.com/libopenstorage/sdk-test/pkg/auth"
28+
common "github.com/libopenstorage/sdk-test/pkg/common"
2629
"google.golang.org/grpc/metadata"
2730

2831
. "github.com/onsi/gomega"
@@ -103,24 +106,30 @@ func createUsersTokens() map[string]string {
103106
}
104107

105108
func createXUsersTokens(prefix string, amount int) map[string]string {
106-
users := make(map[string]string)
109+
users := common.NewConcStrToStrMap()
107110
if amount <= 0 {
108-
return users
111+
return users.GetKeyValMap()
109112
}
113+
var wg sync.WaitGroup
110114
for i := 0; i < amount; i++ {
111115
name := fmt.Sprintf("%s%d", prefix, i)
112-
user := createToken(&auth.Claims{
113-
Subject: name,
114-
Name: name,
115-
Email: fmt.Sprintf("%s@portworx.com", name),
116-
Roles: []string{"system.user"},
117-
Groups: []string{"users"},
118-
}, &auth.Options{
119-
Expiration: time.Now().Add(1 * time.Hour).Unix(),
120-
}, config.SharedSecret)
121-
users[name] = user
116+
wg.Add(1)
117+
go func(name string) {
118+
defer wg.Done()
119+
user := createToken(&auth.Claims{
120+
Subject: name,
121+
Name: name,
122+
Email: fmt.Sprintf("%s@portworx.com", name),
123+
Roles: []string{"system.user"},
124+
Groups: []string{"users"},
125+
}, &auth.Options{
126+
Expiration: time.Now().Add(1 * time.Hour).Unix(),
127+
}, config.SharedSecret)
128+
users.Add(name, user)
129+
}(name)
122130
}
123-
return users
131+
wg.Wait()
132+
return users.GetKeyValMap()
124133
}
125134

126135
func setContextWithToken(ctx context.Context, token string) context.Context {
@@ -422,3 +431,18 @@ func printVolumeDetails(
422431
) {
423432
fmt.Printf("volume ID: %s\n", volume.Id)
424433
}
434+
435+
func summarizeErrorsFromStringErrorChanMap(stringErrMap map[string]chan (error)) error {
436+
var summarizedErrMsg string
437+
for key, errChan := range stringErrMap {
438+
err := <-errChan
439+
if err != nil {
440+
summarizedErrMsg = fmt.Sprintf("%s\nerror of %s: %v", summarizedErrMsg, key, err)
441+
}
442+
}
443+
fmt.Printf("\nsummarizedErrMsg: %s", summarizedErrMsg)
444+
if strings.TrimSpace(summarizedErrMsg) == "" {
445+
return nil
446+
}
447+
return fmt.Errorf("%s", summarizedErrMsg)
448+
}

0 commit comments

Comments
 (0)