func (s *migrateCharmStorageSuite) testMigrateCharmStorage(c *gc.C, curl *charm.URL, agentConfig agent.Config) { curlPlaceholder := charm.MustParseURL("cs:quantal/dummy-1") err := s.State.AddStoreCharmPlaceholder(curlPlaceholder) c.Assert(err, jc.ErrorIsNil) curlPending := charm.MustParseURL("cs:quantal/missing-123") _, err = s.State.PrepareStoreCharmUpload(curlPending) c.Assert(err, jc.ErrorIsNil) var storagePath string var called bool s.PatchValue(upgrades.StateAddCharmStoragePaths, func(st *state.State, storagePaths map[*charm.URL]string) error { c.Assert(storagePaths, gc.HasLen, 1) for k, v := range storagePaths { c.Assert(k.String(), gc.Equals, curl.String()) storagePath = v } called = true return nil }) err = upgrades.MigrateCharmStorage(s.State, agentConfig) c.Assert(err, jc.ErrorIsNil) c.Assert(called, jc.IsTrue) storage := storage.NewStorage(s.State.EnvironUUID(), s.State.MongoSession()) r, length, err := storage.Get(storagePath) c.Assert(err, jc.ErrorIsNil) c.Assert(r, gc.NotNil) defer r.Close() c.Assert(length, gc.Equals, int64(3)) data, err := ioutil.ReadAll(r) c.Assert(err, jc.ErrorIsNil) c.Assert(string(data), gc.Equals, "abc") }
func (s *migrateCharmStorageSuite) TestMigrateCharmStorageIdempotency(c *gc.C) { // If MigrateCharmStorage is called a second time, it will // leave alone the charms that have already been migrated. // The final step of migration is a transactional update // of the charm document in state, which is what we base // the decision on. s.PatchValue(upgrades.CharmStoragePath, func(ch *state.Charm) string { return "alreadyset" }) s.AddTestingCharm(c, "dummy") var called bool s.PatchValue(upgrades.StateAddCharmStoragePaths, func(st *state.State, storagePaths map[*charm.URL]string) error { c.Assert(storagePaths, gc.HasLen, 0) called = true return nil }) err := upgrades.MigrateCharmStorage(s.State, &mockAgentConfig{}) c.Assert(err, jc.ErrorIsNil) c.Assert(called, jc.IsTrue) }