Exemplo n.º 1
0
        public async Task GivenAgentAssignment_WhenDuplicatedAdded_ShouldSuccessAsUpdate()
        {
            // Arrange
            IWatcherRepository watcherRepository = new CosmosWatcherRepository(_watcherOption, _loggerFactory);
            await watcherRepository.Database.Delete(_databaseName, CancellationToken.None);

            IRecordContainer <TargetRecord> container = await watcherRepository.Container.Create <TargetRecord>(_databaseName);

            container.Should().NotBeNull();

            // Act - Write
            var record = CreateTargetRecord(1);

            ETag etag = await container.Set(record);

            etag.Should().NotBeNull();
            etag.Value.Should().NotBeEmpty();

            IReadOnlyList <TargetRecord> activeList = await container.Search("select * from ROOT");

            activeList.Should().NotBeNull();
            activeList.Count.Should().Be(1);

            ETag dup = await container.Set(record);

            dup.Should().NotBeNull();

            activeList = await container.Search("select * from ROOT");

            activeList.Should().NotBeNull();
            activeList.Count.Should().Be(1);

            // Clean up
            (await watcherRepository.Database.Delete(_databaseName, CancellationToken.None)).Should().BeTrue();
        }
Exemplo n.º 2
0
        public async Task GivenDirectoryEntry_WhenRoundTripWithETag_Fail()
        {
            DirectoryClient client = TestApplication.GetDirectoryClient();

            var documentId = new DocumentId("test/unit-tests/entry1");

            var query = new QueryParameter()
            {
                Filter    = "test",
                Recursive = false,
            };

            await client.Delete(documentId);

            var entry = new DirectoryEntryBuilder()
                        .SetDirectoryId(documentId)
                        .SetClassType("test")
                        .AddProperty(new EntryProperty {
                Name = "property1", Value = "value1"
            })
                        .Build();

            await client.Set(entry);

            DirectoryEntry?readEntry = await client.Get(documentId);

            readEntry.Should().NotBeNull();
            readEntry !.ETag.Should().NotBeNull();
            readEntry.Properties.Count.Should().Be(1);

            var updateEntry = new DirectoryEntryBuilder(readEntry)
                              .SetClassType("test-next")
                              .SetETag(new ETag("0xFF9CA90CB9F5120"))
                              .AddProperty(new EntryProperty {
                Name = "property2", Value = "value2"
            })
                              .Build();

            bool failed;

            try
            {
                await client.Set(updateEntry);

                failed = false;
            }
            catch (Azure.RequestFailedException)
            {
                failed = true;
            }

            failed.Should().BeTrue();

            await client.Delete(documentId);

            IReadOnlyList <DatalakePathItem> search = (await client.Search(query).ReadNext()).Records;

            search.Any(x => x.Name == (string)documentId).Should().BeFalse();
        }
Exemplo n.º 3
0
        public async Task GivenAgentAssignment_WhenRoundTrip_ShouldSucceed()
        {
            // Arrange
            IWatcherRepository watcherRepository = new CosmosWatcherRepository(_watcherOption, _loggerFactory);
            await watcherRepository.Database.Delete(_databaseName, CancellationToken.None);

            IRecordContainer <TraceRecord> container = await watcherRepository.Container.Create <TraceRecord>(_databaseName);

            container.Should().NotBeNull();

            // Act - Write
            var record = new TraceRecord
            {
                Id             = Guid.NewGuid().ToString(),
                AgentId        = "agent",
                TargetId       = "target",
                Url            = "http://localhost",
                HttpStatusCode = HttpStatusCode.BadRequest,
                Body           = "this is the body",
                TargetState    = TargetState.Ok.ToString(),
            };

            ETag etag = await container.Set(record);

            etag.Should().NotBeNull();
            etag.Value.Should().NotBeEmpty();

            IReadOnlyList <TraceRecord> activeList = await container.Search("select * from ROOT");

            activeList.Should().NotBeNull();
            activeList.Count.Should().Be(1);

            // Act - Read
            Record <TraceRecord>?read = await container.Get(record.Id);

            read.Should().NotBeNull();
            (read !.Value == record).Should().BeTrue();

            // Act - Delete
            (await container.Delete(record.Id, etag)).Should().BeTrue();

            IReadOnlyList <TraceRecord> deleteList = await container.Search("select * from ROOT");

            deleteList.Should().NotBeNull();
            deleteList.Count.Should().Be(0);

            // Clean up
            (await watcherRepository.Database.Delete(_databaseName, CancellationToken.None)).Should().BeTrue();
        }
Exemplo n.º 4
0
        public async Task GivenAgent_WhenRoundTrip_ShouldSucceed()
        {
            // Arrange
            IWatcherRepository watcherRepository = new CosmosWatcherRepository(_watcherOption, _loggerFactory);
            await watcherRepository.Database.Delete(_databaseName, CancellationToken.None);

            IRecordContainer <AgentRecord> container = await watcherRepository.Container.Create <AgentRecord>(_databaseName);

            container.Should().NotBeNull();

            // Act - Write
            var record = new AgentRecord
            {
                Id           = "Agent1",
                State        = AgentState.Running,
                UtcHeartbeat = DateTime.UtcNow,
            };

            ETag etag = await container.Set(record);

            etag.Should().NotBeNull();
            etag.Value.Should().NotBeEmpty();

            IReadOnlyList <AgentRecord> activeList = await container.Search("select * from ROOT");

            activeList.Should().NotBeNull();
            activeList.Count.Should().Be(1);

            // Act - Read
            Record <AgentRecord>?read = await container.Get(record.Id);

            read.Should().NotBeNull();
            (read !.Value == record).Should().BeTrue();

            // Act - Delete
            (await container.Delete(record.Id, etag)).Should().BeTrue();

            IReadOnlyList <AgentRecord> deleteList = await container.Search("select * from ROOT");

            deleteList.Should().NotBeNull();
            deleteList.Count.Should().Be(0);

            // Clean up
            (await watcherRepository.Database.Delete(_databaseName, CancellationToken.None)).Should().BeTrue();
        }
Exemplo n.º 5
0
        public async Task GivenMultipleAgents_WhenRoundTrip_ShouldSuccess()
        {
            const int max = 10;

            // Arrange
            IWatcherRepository watcherRepository = new CosmosWatcherRepository(_watcherOption, _loggerFactory);
            await watcherRepository.Database.Delete(_databaseName, CancellationToken.None);

            IRecordContainer <AgentRecord> container = await watcherRepository.Container.Create <AgentRecord>(_databaseName);

            container.Should().NotBeNull();

            // Act - Write
            IReadOnlyList <AgentRecord> records = Enumerable.Range(0, max)
                                                  .Select((x, i) => new AgentRecord
            {
                Id           = $"Agent1_{i}",
                State        = AgentState.Running,
                UtcHeartbeat = DateTime.UtcNow,
            })
                                                  .ToArray();

            var eTags = new List <ETag>();

            foreach (AgentRecord item in records)
            {
                ETag eTag = await container.Set(item);

                eTag.Should().NotBeNull();
                eTag.Value.Should().NotBeEmpty();

                eTags.Add(eTag);
            }

            // Act - list records
            IReadOnlyList <AgentRecord> activeList = await container.Search("select * from ROOT");

            activeList.Should().NotBeNull();
            activeList.Count.Should().Be(max);

            records
            .Zip(activeList, (o, i) => (o, i))
            .All(x => x.o == x.i)
            .Should().BeTrue();

            var deleteList = records
                             .Zip(eTags, (o, i) => (record: o, eTag: i))
                             .ToArray();

            // Act - lookup each record
            foreach (var record in activeList)
            {
                IReadOnlyList <AgentRecord> queryList = await container.Search($"select * from ROOT r where r.id = \"{record.Id}\"");

                queryList.Should().NotBeNull();
                queryList.Count.Should().Be(1);
                (record == queryList.Single()).Should().BeTrue();
            }

            // Act - delete all records
            foreach (var record in deleteList)
            {
                (await container.Delete(record.record.Id, record.eTag)).Should().BeTrue();
            }

            // Clean up
            (await watcherRepository.Database.Delete(_databaseName, CancellationToken.None)).Should().BeTrue();
        }
Exemplo n.º 6
0
        public async Task GivenMultipleTraces_WhenRoundTrip_ShouldSuccess()
        {
            const int max = 10;

            // Arrange
            IWatcherRepository watcherRepository = new CosmosWatcherRepository(_watcherOption, _loggerFactory);
            await watcherRepository.Database.Delete(_databaseName, CancellationToken.None);

            IRecordContainer <TraceRecord> container = await watcherRepository.Container.Create <TraceRecord>(_databaseName);

            container.Should().NotBeNull();

            // Act - Write
            IReadOnlyList <TraceRecord> records = Enumerable.Range(0, max)
                                                  .Select(x => new TraceRecord
            {
                Id             = Guid.NewGuid().ToString(),
                Timestamp      = DateTime.UtcNow,
                AgentId        = "agent",
                TargetId       = "target",
                Url            = "http://localhost",
                HttpStatusCode = HttpStatusCode.BadRequest,
                Body           = "this is the body",
                TargetState    = TargetState.Ok.ToString(),
            })
                                                  .ToArray();

            var eTags = new List <ETag>();

            foreach (TraceRecord item in records)
            {
                ETag eTag = await container.Set(item);

                eTag.Should().NotBeNull();
                eTag.Value.Should().NotBeEmpty();

                eTags.Add(eTag);
            }

            // Act - list records
            IReadOnlyList <TraceRecord> activeList = await container.Search("select * from ROOT");

            activeList.Should().NotBeNull();
            activeList.Count.Should().Be(max);

            records
            .Zip(activeList, (o, i) => (o, i))
            .All(x => x.o == x.i)
            .Should().BeTrue();

            var deleteList = records
                             .Zip(eTags, (o, i) => (record: o, eTag: i))
                             .ToArray();

            // Act - lookup each record
            foreach (var record in activeList)
            {
                IReadOnlyList <TraceRecord> queryList = await container.Search($"select * from ROOT r where r.id = \"{record.Id}\"");

                queryList.Should().NotBeNull();
                queryList.Count.Should().Be(1);
                (record == queryList.Single()).Should().BeTrue();
            }

            // Act - delete all records
            foreach (var record in deleteList)
            {
                (await container.Delete(record.record.Id, record.eTag)).Should().BeTrue();
            }

            // Clean up
            (await watcherRepository.Database.Delete(_databaseName, CancellationToken.None)).Should().BeTrue();
        }