func (s *managedStorageSuite) checkPutResponse(c *gc.C, index int, wg *sync.WaitGroup, requestId int64, sha384Hash string, blob []byte) { // After a random time, respond to a previously queued put request and check the result. go func() { delay := rand.Intn(3) time.Sleep(time.Duration(delay) * time.Millisecond) expectError := index == 2 if expectError { sha384Hash = "bad" } response := blobstore.NewPutResponse(requestId, sha384Hash) err := s.managedStorage.ProofOfAccessResponse(response) if expectError { c.Check(err, gc.NotNil) } else { c.Check(err, gc.IsNil) if err == nil { r, length, err := s.managedStorage.GetForEnvironment("env", fmt.Sprintf("path/to/blob%d", index)) c.Check(err, gc.IsNil) if err == nil { data, err := ioutil.ReadAll(r) c.Check(err, gc.IsNil) c.Check(data, gc.DeepEquals, blob) c.Check(int(length), gc.DeepEquals, len(blob)) } } } wg.Done() }() }
func (s *managedStorageSuite) TestPutRequestResponseHashMismatch(c *gc.C) { _, sha384Hash := s.putTestRandomBlob(c, "path/to/blob") reqResp, err := s.managedStorage.PutForEnvironmentRequest("env", "path/to/blob", sha384Hash) c.Assert(err, gc.IsNil) response := blobstore.NewPutResponse(reqResp.RequestId, "notsha384") err = s.managedStorage.ProofOfAccessResponse(response) c.Assert(err, gc.Equals, blobstore.ErrResponseMismatch) c.Assert(blobstore.RequestQueueLength(s.managedStorage), gc.Equals, 0) }
func (s *managedStorageSuite) TestPutRequestResponseSHA256Mismatch(c *gc.C) { blob, md5hash, sha256hash := s.putTestBlob(c, "path/to/blob") hash := blobstore.ResourceHash{md5hash, sha256hash} reqResp, err := s.managedStorage.PutForEnvironmentRequest("env", "path/to/blob", hash) c.Assert(err, gc.IsNil) md5Response, _ := calculateCheckSums(c, reqResp.RangeStart, reqResp.RangeLength, blob) response := blobstore.NewPutResponse(reqResp.RequestId, md5Response, "notsha256") err = s.managedStorage.ProofOfAccessResponse(response) c.Assert(err, gc.Equals, blobstore.ErrResponseMismatch) c.Assert(blobstore.RequestQueueLength(s.managedStorage), gc.Equals, 0) }
func (s *managedStorageSuite) TestPutRequestDeleted(c *gc.C) { blob, sha384Hash := s.putTestRandomBlob(c, "path/to/blob") reqResp, err := s.managedStorage.PutForEnvironmentRequest("env", "path/to/blob", sha384Hash) c.Assert(err, gc.IsNil) err = s.managedStorage.RemoveForEnvironment("env", "path/to/blob") c.Assert(err, gc.IsNil) sha384Response := calculateCheckSum(c, reqResp.RangeStart, reqResp.RangeLength, blob) response := blobstore.NewPutResponse(reqResp.RequestId, sha384Response) err = s.managedStorage.ProofOfAccessResponse(response) c.Assert(err, gc.Equals, blobstore.ErrResourceDeleted) }
func (s *managedStorageSuite) TestPutRequestExpiredMulti(c *gc.C) { ch := make(chan struct{}) s.PatchValue(blobstore.AfterFunc, patchedAfterFunc(ch)) blob, sha384Hash := s.putTestRandomBlob(c, "path/to/blob") reqResp, err := s.managedStorage.PutForEnvironmentRequest("env", "path/to/blob", sha384Hash) c.Assert(err, gc.IsNil) sha384Response := calculateCheckSum(c, reqResp.RangeStart, reqResp.RangeLength, blob) reqResp2, err := s.managedStorage.PutForEnvironmentRequest("env", "path/to/blob2", sha384Hash) c.Assert(err, gc.IsNil) sha384Response2 := calculateCheckSum(c, reqResp.RangeStart, reqResp.RangeLength, blob) // Trigger the request timeouts. ch <- trigger <-ch ch <- trigger <-ch c.Assert(blobstore.RequestQueueLength(s.managedStorage), gc.Equals, 0) response := blobstore.NewPutResponse(reqResp.RequestId, sha384Response) response2 := blobstore.NewPutResponse(reqResp2.RequestId, sha384Response2) err = s.managedStorage.ProofOfAccessResponse(response) c.Assert(err, gc.Equals, blobstore.ErrRequestExpired) err = s.managedStorage.ProofOfAccessResponse(response2) c.Assert(err, gc.Equals, blobstore.ErrRequestExpired) }
// Run one simple test with the real time.AfterFunc to ensure it works. func (s *managedStorageSuite) TestPutRequestExpiredWithRealTimeAfter(c *gc.C) { s.PatchValue(blobstore.RequestExpiry, 5*time.Millisecond) blob, sha384Hash := s.putTestRandomBlob(c, "path/to/blob") reqResp, err := s.managedStorage.PutForEnvironmentRequest("env", "path/to/blob", sha384Hash) c.Assert(err, gc.IsNil) sha384Response := calculateCheckSum(c, reqResp.RangeStart, reqResp.RangeLength, blob) // Wait for request timer to trigger. time.Sleep(7 * time.Millisecond) response := blobstore.NewPutResponse(reqResp.RequestId, sha384Response) err = s.managedStorage.ProofOfAccessResponse(response) c.Assert(err, gc.Equals, blobstore.ErrRequestExpired) c.Assert(blobstore.RequestQueueLength(s.managedStorage), gc.Equals, 0) c.Assert(blobstore.RequestQueueLength(s.managedStorage), gc.Equals, 0) }
func (s *managedStorageSuite) TestPutRequestExpired(c *gc.C) { ch := make(chan struct{}) s.PatchValue(blobstore.AfterFunc, patchedAfterFunc(ch)) blob, md5hash, sha256hash := s.putTestBlob(c, "path/to/blob") hash := blobstore.ResourceHash{md5hash, sha256hash} reqResp, err := s.managedStorage.PutForEnvironmentRequest("env", "path/to/blob", hash) c.Assert(err, gc.IsNil) md5Response, sha256Response := calculateCheckSums(c, reqResp.RangeStart, reqResp.RangeLength, blob) // Trigger the request timeout. ch <- trigger <-ch response := blobstore.NewPutResponse(reqResp.RequestId, md5Response, sha256Response) err = s.managedStorage.ProofOfAccessResponse(response) c.Assert(err, gc.Equals, blobstore.ErrRequestExpired) c.Assert(blobstore.RequestQueueLength(s.managedStorage), gc.Equals, 0) }
func (s *managedStorageSuite) assertPutRequestSingle(c *gc.C, blob []byte, resourceCount int) { if blob == nil { id := bson.NewObjectId().Hex() blob = []byte(id) } rdr := bytes.NewReader(blob) err := s.managedStorage.PutForEnvironment("env", "path/to/blob", rdr, int64(len(blob))) c.Assert(err, gc.IsNil) sha384Hash := calculateCheckSum(c, 0, int64(len(blob)), blob) reqResp, err := s.managedStorage.PutForEnvironmentRequest("env", "path/to/blob", sha384Hash) c.Assert(err, gc.IsNil) sha384Response := calculateCheckSum(c, reqResp.RangeStart, reqResp.RangeLength, blob) response := blobstore.NewPutResponse(reqResp.RequestId, sha384Response) err = s.managedStorage.ProofOfAccessResponse(response) c.Assert(err, gc.IsNil) s.assertGet(c, "path/to/blob", blob) s.assertResourceCatalogCount(c, resourceCount) }