Пример #1
0
        public ActionResult Edit(PackageModel model)
        {
            bool has_error = false;

            var package_srv = CompositionRoot.Resolve <IPackageService>();

            if (ModelState.IsValid)
            {
                var package = PackageHelper.CreatePackage(model);

                try {
                    package_srv.Update(package);
                    package_srv.UpdatePackageFiles(package.Id, model.Files != null ? model.Files.Select(f => f.Id) : new List <Int64>());
                }
                catch (DomainException e) {
                    has_error = true;
                    ModelState.AddModelError("", e.Message);
                }
            }

            if (has_error)
            {
                PackageHelper.PrepareModel(model);

                return(View(model));
            }

            return(RedirectToAction("Index"));
        }
Пример #2
0
        public void BasicDelete()
        {
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            this.IndexReader.DeletePackage(testPackage.Name);

            Assert.False(File.Exists(Path.Combine(this.Settings.PackagePath, "manifest.json")));
        }
Пример #3
0
        public void GetInProgressStatus()
        {
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            // mock temp archive
            File.WriteAllText(this.IndexReader.GetPackageArchiveTempPath(testPackage.Name), string.Empty);

            Assert.Equal(1, this.IndexReader.GetPackageArchiveStatus(testPackage.Name));
        }
Пример #4
0
        public void GetArchive_Nocompress_FileMissing()
        {
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            // delete known package file via disk
            File.Delete(Path.Join(this.Settings.RepositoryPath, testPackage.Path, testPackage.Hash, "bin"));

            Exception ex = Assert.Throws <Exception>(() => { this.IndexReader.GetPackageAsArchive(testPackage.Name); });

            Assert.Contains("Failed to find expected package file", ex.Message);
        }
Пример #5
0
        public void GetBasic()
        {
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            using (Stream testContent = this.IndexReader.GetPackageAsArchive(testPackage.Name))
            {
                Dictionary <string, byte[]> items = StreamsHelper.ArchiveStreamToCollection(testContent);
                Assert.Single(items);
                Assert.Equal(testPackage.Content, items[testPackage.Path]);
            }
        }
Пример #6
0
        public void HashInvalid()
        {
            // create package
            TestPackage package = PackageHelper.CreatePackage(Settings, "mypackage");

            // manually change file on disk after package created
            File.WriteAllText(Path.Join(this.Settings.RepositoryPath, package.Path, package.Hash, "bin"), "some-different-data");

            (bool, string)result = this.IndexReader.VerifyPackage("mypackage");
            Assert.False(result.Item1);
            Assert.Contains("does not match expected manifest hash", result.Item2);
        }
Пример #7
0
        public void FilesMissing()
        {
            // create package
            TestPackage package = PackageHelper.CreatePackage(Settings, "mypackage");

            // delete known package file via disk
            File.Delete(Path.Join(this.Settings.RepositoryPath, package.Path, package.Hash, "bin"));

            (bool, string)result = this.IndexReader.VerifyPackage("mypackage");
            Assert.False(result.Item1);
            Assert.Contains("Expected package files missing", result.Item2);
        }
Пример #8
0
        public void DeleteWithArchive()
        {
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            // mock archive
            string archivePath = base.IndexReader.GetPackageArchivePath(testPackage.Name);

            File.WriteAllText(archivePath, string.Empty);

            this.IndexReader.DeletePackage(testPackage.Name);

            Assert.False(File.Exists(archivePath));
        }
Пример #9
0
        public void GetArchiveCompressionEnabled()
        {
            base.Settings.IsStorageCompressionEnabled = true;

            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            using (Stream testContent = this.IndexReader.GetPackageAsArchive(testPackage.Name))
            {
                Dictionary <string, byte[]> items = StreamsHelper.ArchiveStreamToCollection(testContent);
                Assert.Single(items);
                Assert.Equal(testPackage.Content, items[testPackage.Path]);
            }
        }
Пример #10
0
        public void GetExistingArchive()
        {
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            using (Stream testContent1 = this.IndexReader.GetPackageAsArchive(testPackage.Name))
            {
                // get again
                using (Stream testContent2 = this.IndexReader.GetPackageAsArchive(testPackage.Name))
                {
                    Dictionary <string, byte[]> items = StreamsHelper.ArchiveStreamToCollection(testContent2);
                    Assert.Single(items);
                }
            }
        }
Пример #11
0
        public void Basic()
        {
            TestPackage package = PackageHelper.CreatePackage(this.Settings);

            string[] tags = new[] { "mytag" };

            foreach (string tag in tags)
            {
                base.TagsService.AddTag(package.Name, tag);
            }

            base.IndexReader.DeletePackage(package.Name);
            IEnumerable <Package> packages = base.PackageList.GetWithTags(tags, 0, 10);

            Assert.Empty(packages);
        }
Пример #12
0
        public void Basic()
        {
            TestPackage package = PackageHelper.CreatePackage(this.Settings);

            string[] tags = new[] { "mytag", "mytag3" };

            foreach (string tag in tags)
            {
                base.TagsService.AddTag(package.Name, tag);
            }

            IEnumerable <Package> packages = base.PackageList.GetWithTags(tags, 0, 10);

            Assert.Single(packages);
            Assert.True(tags.IsSubsetOf(packages.ElementAt(0).Tags));
        }
Пример #13
0
        public void LockedArchive()
        {
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            // mock its archive
            string archivePath = base.IndexReader.GetPackageArchivePath(testPackage.Name);

            File.WriteAllText(archivePath, string.Empty);

            // lock the archive by opening a read stream on it
            using (new FileStream(archivePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                // no need to assert, this test is for coverage, and all we want is no exception to be thrown here
                this.IndexReader.DeletePackage(testPackage.Name);
            }
        }
Пример #14
0
        public void Basic()
        {
            TestPackage package1 = PackageHelper.CreatePackage(this.Settings, "package1");
            string      tag1     = "mytag1";

            base.TagsService.AddTag(package1.Name, tag1);

            TestPackage package2 = PackageHelper.CreatePackage(this.Settings, "package2");
            string      tag2     = "mytag2";

            base.TagsService.AddTag(package2.Name, tag2);

            IEnumerable <string> tags = base.TagsService.GetAllTags();

            Assert.Equal(2, tags.Count());
            Assert.Contains(tag1, tags);
            Assert.Contains(tag2, tags);
        }
Пример #15
0
        public void Basic()
        {
            string[] tags = new [] { "mytag" };

            TestPackage package1 = PackageHelper.CreatePackage(this.Settings, "package1");
            TestPackage package2 = PackageHelper.CreatePackage(this.Settings, "package2");

            foreach (string tag in tags)
            {
                base.TagsService.AddTag(package1.Name, tag);
                base.TagsService.AddTag(package2.Name, tag);
            }

            IEnumerable <string> packageIds = base.TagsService.GetPackageIdsWithTags(tags);

            Assert.Equal(2, packageIds.Count());
            Assert.Contains("package1", packageIds);
            Assert.Contains("package2", packageIds);
        }
Пример #16
0
        public void LockedTag()
        {
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            IMemoryCache     _memoryCache     = MemoryCacheHelper.GetInstance();
            PackageListCache PackageListCache = new PackageListCache(_memoryCache);
            ITagsService     tagsService      = new Core.TagsService(this.Settings, new TestLogger <ITagsService>(), PackageListCache);

            tagsService.AddTag(testPackage.Name, "mytag");

            string[] tagDirectories = Directory.GetDirectories(Path.Join(this.Settings.TagsPath));
            Assert.Single(tagDirectories); // should be 1 only

            string[] tagSubscribers = Directory.GetFiles(tagDirectories.First());
            Assert.Single(tagSubscribers); // should be 1 only

            // lock the tag by opening a read stream on it
            using (new FileStream(tagSubscribers.First(), FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                // no need to assert, this test is for coverage, and all we want is no exception to be thrown here
                this.IndexReader.DeletePackage(testPackage.Name);
            }
        }
Пример #17
0
        public void GetAfterWaiting()
        {
            // we need a valid package first
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            // create a fake archive temp file so GetPackageAsArchive() goes into wait state
            string tempArchivePath = this.IndexReader.GetPackageArchiveTempPath(testPackage.Name);

            File.WriteAllText(tempArchivePath, string.Empty);

            // seeing as our fake temp archive bypasses archive creation, we should make a fake archive too, it should just be a file
            string archivePath = this.IndexReader.GetPackageArchivePath(testPackage.Name);

            File.WriteAllText(archivePath, "some-fake-archive-content");

            // lock the temp archive so it doesn't get auto-clobbered
            using (Stream lockStream = new FileStream(tempArchivePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                // mock IThread.Sleep, in this method, we will release and delete our temp archive lock, so the archive fetch can proceed
                IThread mockThread = Mock.Of <IThread>();
                Mock.Get(mockThread)
                .Setup(r => r.Sleep(It.IsAny <int>()))
                .Callback <int>(time => {
                    lockStream.Close();
                    File.Delete(tempArchivePath);
                });

                // make a custom reader with our mocked Thread
                IIndexReader indexReader = new Core.IndexReader(this.Settings, mockThread, this.TagService, this.Logger, new FileSystem(), HashServiceHelper.Instance());

                using (Stream zipStream = indexReader.GetPackageAsArchive(testPackage.Name))
                {
                    // confirm we got our fake archive content back
                    Assert.Equal("some-fake-archive-content", StreamsHelper.StreamToString(zipStream));
                }
            }
        }
Пример #18
0
        public void GetTimesOut()
        {
            // zero wait times to speed up test, this should trigger an instant timeout
            base.Settings.ArchiveWaitTimeout           = 0;
            base.Settings.ArchiveAvailablePollInterval = 0;

            // we need a valid package first
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            // mock a temp archive file and lock it to simulate an ongoing zip
            string tempArchivePath = this.IndexReader.GetPackageArchiveTempPath(testPackage.Name);

            File.WriteAllText(tempArchivePath, string.Empty);

            using (Stream lockStream = new FileStream(tempArchivePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                Assert.Throws <TimeoutException>(() => {
                    using (Stream zipStream = this.IndexReader.GetPackageAsArchive(testPackage.Name))
                    {
                        // do nothing, exception expected
                    }
                });
            }
        }
Пример #19
0
        public void DeleteWithLockedArchive()
        {
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            // mock archive
            string archivePath = base.IndexReader.GetPackageArchivePath(testPackage.Name);

            File.WriteAllText(archivePath, string.Empty);

            // force create dummy zip file in archive folder
            File.WriteAllText(archivePath, "dummy content");

            // open stream in write mode to lock it, then attempt to purge archives
            using (FileStream fs = File.OpenWrite(archivePath))
            {
                // force write something to stream to ensure it locks
                fs.Write(Encoding.ASCII.GetBytes("random"));

                this.IndexReader.DeletePackage(testPackage.Name);

                Assert.Single(base.Logger.LogEntries);
                Assert.Contains("Failed to purge archive", base.Logger.LogEntries[0]);
            }
        }
Пример #20
0
        public void GetDefault()
        {
            TestPackage testPackage = PackageHelper.CreatePackage(this.Settings);

            Assert.Equal(0, this.IndexReader.GetPackageArchiveStatus(testPackage.Name));
        }
Пример #21
0
 public void Basic()
 {
     PackageHelper.CreatePackage(Settings, "mypackage");
     (bool, string)result = this.IndexReader.VerifyPackage("mypackage");
     Assert.True(result.Item1);
 }