|
| 1 | +package sanity |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "time" |
| 8 | + |
| 9 | + api "github.com/libopenstorage/openstorage-sdk-clients/sdk/golang" |
| 10 | + common "github.com/libopenstorage/sdk-test/pkg/common" |
| 11 | + . "github.com/onsi/ginkgo" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | +) |
| 14 | + |
| 15 | +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 | + |
| 24 | +var _ = Describe("Security Scale", func() { |
| 25 | + var ( |
| 26 | + c api.OpenStorageVolumeClient |
| 27 | + ic api.OpenStorageIdentityClient |
| 28 | + ) |
| 29 | + |
| 30 | + BeforeEach(func() { |
| 31 | + c = api.NewOpenStorageVolumeClient(conn) |
| 32 | + ic = api.NewOpenStorageIdentityClient(conn) |
| 33 | + isSupported := isCapabilitySupported( |
| 34 | + ic, |
| 35 | + api.SdkServiceCapability_OpenStorageService_VOLUME, |
| 36 | + ) |
| 37 | + if !isSupported { |
| 38 | + Fail("Volume capability not supported , skipping related tests") |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + AfterEach(func() { |
| 43 | + }) |
| 44 | + |
| 45 | + Describe("Security", func() { |
| 46 | + |
| 47 | + BeforeEach(func() { |
| 48 | + }) |
| 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() { |
| 55 | + By("Creating volumes") |
| 56 | + err := createVolumesConcurrently(c) |
| 57 | + Expect(err).NotTo(HaveOccurred()) |
| 58 | + }) |
| 59 | + It("Should be able to inspect the created Volume", func() { |
| 60 | + By("Inspecting volumes") |
| 61 | + err := inspectVolumesConcurrently(c) |
| 62 | + Expect(err).NotTo(HaveOccurred()) |
| 63 | + }) |
| 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 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | +}) |
| 72 | + |
| 73 | +func createVolumesConcurrently(c api.OpenStorageVolumeClient) error { |
| 74 | + //userVolumeMap is mapping user'name to volumes' ID |
| 75 | + userVolumeMap = common.NewConcMap() |
| 76 | + var volErrorMap = common.NewConcStringErrChanMap() |
| 77 | + var wg sync.WaitGroup |
| 78 | + for name, userToken := range scaleUsers { |
| 79 | + userName := name |
| 80 | + token := userToken |
| 81 | + wg.Add(1) |
| 82 | + go func(userName string, token string) { |
| 83 | + defer wg.Done() |
| 84 | + t := time.Now() |
| 85 | + tstr := t.Format("20060102150405") |
| 86 | + req := &api.SdkVolumeCreateRequest{ |
| 87 | + Name: "sdk-vol-" + tstr + "-" + userName, |
| 88 | + Spec: &api.VolumeSpec{ |
| 89 | + Size: uint64(5 * GIGABYTE), |
| 90 | + HaLevel: 2, |
| 91 | + }, |
| 92 | + } |
| 93 | + createResponse, err := c.Create(setContextWithToken(context.Background(), token), req) |
| 94 | + volID := createResponse.VolumeId |
| 95 | + resp, err := c.Inspect( |
| 96 | + setContextWithToken(context.Background(), token), |
| 97 | + &api.SdkVolumeInspectRequest{ |
| 98 | + VolumeId: volID, |
| 99 | + }, |
| 100 | + ) |
| 101 | + printVolumeDetails(resp.Volume) |
| 102 | + userVolumeMap.Add(userName, resp.GetVolume().GetId()) |
| 103 | + errChan := make(chan (error), 1) |
| 104 | + errChan <- err |
| 105 | + volErrorMap.Add(volID, errChan) |
| 106 | + }(userName, token) |
| 107 | + } |
| 108 | + wg.Wait() |
| 109 | + for user, volID := range userVolumeMap.GetKeyValMap() { |
| 110 | + fmt.Printf("\nuser %s createdvolume ->: %s", user, volID) |
| 111 | + } |
| 112 | + return summarizeErrorsFromStringErrorChanMap(volErrorMap.GetKeyValMap()) |
| 113 | +} |
| 114 | + |
| 115 | +func inspectVolumesConcurrently(c api.OpenStorageVolumeClient) error { |
| 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()) |
| 140 | +} |
| 141 | + |
| 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) |
| 162 | + } |
| 163 | + wg.Wait() |
| 164 | + return summarizeErrorsFromStringErrorChanMap(volErrorMap.GetKeyValMap()) |
| 165 | +} |
0 commit comments