Пример #1
0
        public void GetValue_SingleDocument_ArrayPath(string path, string expected)
        {
            var x = @"
instances:
- field: abc
  image:
    tag: develop-2892
- field: def
  image:
    tag: develop-123
- field: xyz
  image:
    tag: develop-10";

            var byteArray = Encoding.UTF8.GetBytes(x);
            var inStream  = new MemoryStream(byteArray);

            var yamlUtilities = new YamlUtilities();
            var yaml          = new YamlStream();

            yamlUtilities.ReadYamlStream(yaml, inStream);

            var doc       = yaml.Documents.First();
            var readValue = yamlUtilities.ExtractValueFromDoc(path, doc);

            readValue.Should().Be(expected);
        }
Пример #2
0
        private Task <FileInfo?> InternalUpdateApplicationImageTag(
            RawDeploymentManifest manifest,
            DeploymentManifestInfo deploymentManifestInfo,
            ApplicationImage applicationImage,
            string instance,
            string targetTag
            )
        {
            var yamlUtilities = new YamlUtilities();

            var    key             = new DeploymentManifestInstanceKey(applicationImage, instance);
            string currentImageTag = deploymentManifestInfo.ManifestTags.GetValueOrDefault(key) ?? "n/a";

            if (!deploymentManifestInfo.ApplicationImageToFileMapping.TryGetValue(key, out FileInfo? file))
            {
                // TODO: warn that we have an image tag update but no corresponding file
                // _log.LogWarning("Update to {Repository} cannot be applied since there isn't matching file.");
                throw new InvalidOperationException("Update cannot be applied since there isn't matching file.");
            }

            _log.LogTrace("Upgrading {Repository} to {NewTag} from {Tag}",
                          applicationImage.Repository,
                          targetTag,
                          currentImageTag
                          );

            return(yamlUtilities.UpdateValueInYamlFile(file, applicationImage.TagProperty.Path, targetTag)
                ? Task.FromResult((FileInfo?)file)
                : Task.FromResult((FileInfo?)null));
        }
        public async Task <IEnumerable <StubModel> > GetStubsAsync()
        {
            using (var conn = _queryStore.GetConnection())
            {
                var queryResults = await conn.QueryAsync <DbStubModel>(_queryStore.GetStubsQuery);

                var result = new List <StubModel>();
                foreach (var queryResult in queryResults)
                {
                    switch (queryResult.StubType)
                    {
                    case StubJsonType:
                        result.Add(JsonConvert.DeserializeObject <StubModel>(queryResult.Stub));
                        break;

                    case StubYamlType:
                        result.Add(YamlUtilities.Parse <StubModel>(queryResult.Stub));
                        break;

                    default:
                        throw new NotImplementedException($"StubType '{queryResult.StubType}' not supported: stub '{queryResult.StubId}'.");
                    }
                }

                return(result);
            }
        }
Пример #4
0
 /// <summary>
 /// Adds YAML formatting.
 /// </summary>
 /// <param name="options">The <see cref="MvcOptions"/>.</param>
 public static MvcOptions AddYamlFormatting(this MvcOptions options)
 {
     options.InputFormatters.Add(new YamlInputFormatter(YamlUtilities.BuildDeserializer()));
     options.OutputFormatters.Add(new YamlOutputFormatter(YamlUtilities.BuildSerializer()));
     options.FormatterMappings.SetMediaTypeMappingForFormat("yaml", MediaTypeHeaderValues.ApplicationYaml);
     return(options);
 }
Пример #5
0
    public void Parse_HappyFlow()
    {
        // Arrange
        const string yaml = "value: val1";

        // Act
        var result = YamlUtilities.Parse <TestModel>(yaml);

        // Assert
        Assert.AreEqual("val1", result.Value);
    }
Пример #6
0
    public void Parse_HappyFlow_ShouldHandleUnmappedFields()
    {
        // Arrange
        const string yaml = "value: val1\nunmappedField: 123";

        // Act
        var result = YamlUtilities.Parse <TestModel>(yaml);

        // Assert
        Assert.AreEqual("val1", result.Value);
    }
Пример #7
0
        private IEnumerable <DeploymentManifestInstanceInfo> LoadDeploymentManifestInstanceInfos(
            IEnumerable <ApplicationImage> applicationImages,
            string instanceName,
            IEnumerable <FileInfo> files
            )
        {
            var yamlUtilities      = new YamlUtilities();
            var imageToFilenameMap = new Dictionary <ApplicationImage, FileInfo>();

            var result = new List <DeploymentManifestInstanceInfo>();

            var fileInfos = files as FileInfo[] ?? files.ToArray();

            foreach (var file in fileInfos)
            {
                var yaml = new YamlStream();
                yamlUtilities.ReadYamlStream(yaml, file.FullName);

                foreach (var doc in yaml.Documents)
                {
                    foreach (var image in applicationImages)
                    {
                        var tagInManifest = yamlUtilities.ExtractValueFromDoc(image.TagProperty.Path, doc);

                        if (tagInManifest == null)
                        {
                            continue;
                        }

                        if (imageToFilenameMap.ContainsKey(image))
                        {
                            // TODO: handle situation where multiple files define the same image tag (ERROR and warn user)
                        }

                        imageToFilenameMap[image] = file;
                        result.Add(
                            new DeploymentManifestInstanceInfo(
                                instanceName,
                                image,
                                tagInManifest,
                                file,
                                fileInfos,
                                ApplicationImageInstanceType.Primary,
                                new ApplicationImageInstanceSource(ApplicationImageInstanceSourceType.Manual, 0)
                                )
                            );
                    }
                }
            }

            return(result);
        }
Пример #8
0
    public void Serialize_HappyFlow()
    {
        // Arrange
        var input = new TestModel {
            Value = "val1"
        };

        // Act
        var result = YamlUtilities.Serialize(input);

        // Assert
        Assert.AreEqual("value: val1\n", result);
    }
Пример #9
0
        public void SetValueInDoc_MultiDocument()
        {
            var x = @"cat:
  image:
    tag: develop-2892
dog:
  image:
    tag: develop-2892
---
sheep:
  image:
    tag: develop-10";

            var expected = @"cat:
  image:
    tag: develop-2892
dog:
  image:
    tag: develop-999
---
sheep:
  image:
    tag: develop-10
";

            var byteArray = Encoding.UTF8.GetBytes(x);
            var inStream  = new MemoryStream(byteArray);

            var yamlUtilities = new YamlUtilities();
            var yaml          = new YamlStream();

            yamlUtilities.ReadYamlStream(yaml, inStream);

            var doc = yaml.Documents.First();

            yamlUtilities.SetValueInDoc("dog.image.tag", doc, "develop-999");

            var outStream = new MemoryStream();

            yamlUtilities.WriteYamlStream(yaml, outStream);

            outStream.Position = 0;
            var reader = new StreamReader(outStream);
            var text   = reader.ReadToEnd();

            text.Should().Be(expected);
        }
Пример #10
0
        public void GetValue_SingleDocument_MapPath(string path, string expected)
        {
            var x = @"
cat:
  image1:
    tag: develop-2892
  image2:
    tag: develop-123";

            var byteArray = Encoding.UTF8.GetBytes(x);
            var inStream  = new MemoryStream(byteArray);

            var yamlUtilities = new YamlUtilities();
            var yaml          = new YamlStream();

            yamlUtilities.ReadYamlStream(yaml, inStream);

            var doc       = yaml.Documents.First();
            var readValue = yamlUtilities.ExtractValueFromDoc(path, doc);

            readValue.Should().Be(expected);
        }
Пример #11
0
        private async Task <List <FileInfo> > InternalCreateRelease(RawDeploymentManifest manifest, string instance)
        {
            // the Raw Deployment Manifest type builds instances by cloning the original files and executes modifications on them.
            var copyOperations = new List <(FileInfo source, FileInfo dest)>();
            var deploymentManifestSourcePath = manifest.GetFullPath().FullName;

            foreach (var file in manifest.Files)
            {
                var sourceFilePath = Path.Combine(deploymentManifestSourcePath, file);
                var sourceFileInfo = new FileInfo(sourceFilePath);

                var targetFilename =
                    $"{Path.GetFileNameWithoutExtension(sourceFileInfo.Name)}-{instance}{sourceFileInfo.Extension}";
                var targetFilePath = Path.Combine(deploymentManifestSourcePath, targetFilename);
                var targetFileInfo = new FileInfo(targetFilePath);

                copyOperations.Add((sourceFileInfo, targetFileInfo));
            }

            copyOperations.ForEach(x => x.source.CopyTo(x.dest.FullName));

            // prepare to apply required modifications

            var yamlUtilities = new YamlUtilities();

            // TODO: load replacement entires from configuration
            var replacementEntries = new Dictionary <string, string>()
            {
                { "ingress.hosts[0].host", "{instanceid}.{original}" }
            };

            var modifiedFiles = new List <FileInfo>();

            foreach (var file in copyOperations.Select(x => x.dest))
            {
                var modified = false;

                var yamlStream = new YamlStream();

                {
                    await using var stream = file.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
                    yamlUtilities.ReadYamlStream(yamlStream, stream);

                    foreach (var item in replacementEntries)
                    {
                        var currentValue = yamlUtilities.ExtractValueFromDoc(item.Key, yamlStream.Documents.First());
                        if (currentValue == null)
                        {
                            continue;
                        }

                        var replacementValue = item.Value;
                        replacementValue = replacementValue.Replace("{original}", currentValue);
                        replacementValue = replacementValue.Replace("{instanceid}", instance);
                        yamlUtilities.SetValueInDoc(item.Key, yamlStream.Documents.First(), replacementValue);
                        modified = true;
                    }
                }

                if (modified)
                {
                    await using var stream = file.Open(FileMode.Create, FileAccess.Write, FileShare.None);
                    yamlUtilities.WriteYamlStream(yamlStream, stream);

                    modifiedFiles.Add(file);
                }
            }

            return(modifiedFiles);
        }
        public Task <IEnumerable <StubModel> > GetStubsAsync()
        {
            var inputFileLocation = _settings.Storage?.InputFile;
            var fileLocations     = new List <string>();

            if (string.IsNullOrEmpty(inputFileLocation))
            {
                // If the input file location is not set, try looking in the current directory for .yml files.
                var currentDirectory = _fileService.GetCurrentDirectory();
                var yamlFiles        = _fileService.GetFiles(currentDirectory, "*.yml");
                fileLocations.AddRange(yamlFiles);
            }
            else
            {
                // Split on ";": it is possible to supply multiple locations.
                var parts = inputFileLocation.Split(new[] { "%%" }, StringSplitOptions.RemoveEmptyEntries);
                parts = parts.Select(StripIllegalCharacters).ToArray();
                foreach (var part in parts)
                {
                    var location = part.Trim();
                    _logger.LogInformation($"Reading location '{location}'.");
                    if (_fileService.IsDirectory(location))
                    {
                        var yamlFiles = _fileService.GetFiles(location, "*.yml");
                        fileLocations.AddRange(yamlFiles);
                    }
                    else
                    {
                        fileLocations.Add(location);
                    }
                }
            }

            if (fileLocations.Count == 0)
            {
                _logger.LogInformation("No .yml input files found.");
                return(Task.FromResult(new StubModel[0].AsEnumerable()));
            }

            if (_stubs == null || GetLastStubFileModificationDateTime(fileLocations) > _stubLoadDateTime)
            {
                var result = new List <StubModel>();
                foreach (var file in fileLocations)
                {
                    // Load the stubs.
                    var input = _fileService.ReadAllText(file);
                    _logger.LogInformation($"File contents of '{file}': '{input}'");

                    IEnumerable <StubModel> stubs;

                    if (YamlIsArray(input))
                    {
                        stubs = YamlUtilities.Parse <List <StubModel> >(input);
                    }
                    else
                    {
                        stubs = new[] { YamlUtilities.Parse <StubModel>(input) };
                    }

                    EnsureStubsHaveId(stubs);
                    result.AddRange(stubs);
                    _stubLoadDateTime = DateTime.UtcNow;
                }

                _stubs = result;
            }
            else
            {
                _logger.LogInformation("No stub file contents changed in the meanwhile.");
            }

            return(Task.FromResult(_stubs));
        }
Пример #13
0
    /// <inheritdoc />
    public Task <IEnumerable <StubModel> > GetStubsAsync()
    {
        var inputFileLocation = _settings.Storage?.InputFile;
        var fileLocations     = new List <string>();

        if (string.IsNullOrEmpty(inputFileLocation))
        {
            // If the input file location is not set, try looking in the current directory for .yml files.
            var currentDirectory = _fileService.GetCurrentDirectory();
            var yamlFiles        = _fileService.GetFiles(currentDirectory, _extensions);
            fileLocations.AddRange(yamlFiles);
        }
        else
        {
            // Split file path: it is possible to supply multiple locations.
            var parts = inputFileLocation.Split(Constants.InputFileSeparators,
                                                StringSplitOptions.RemoveEmptyEntries);
            parts = parts.Select(StripIllegalCharacters).ToArray();
            foreach (var part in parts)
            {
                var location = part.Trim();
                _logger.LogInformation($"Reading location '{location}'.");
                if (_fileService.IsDirectory(location))
                {
                    var yamlFiles = _fileService.GetFiles(location, _extensions);
                    fileLocations.AddRange(yamlFiles);
                }
                else
                {
                    fileLocations.Add(location);
                }
            }
        }

        if (fileLocations.Count == 0)
        {
            _logger.LogInformation("No .yml input files found.");
            return(Task.FromResult(Array.Empty <StubModel>().AsEnumerable()));
        }

        if (_stubs == null || GetLastStubFileModificationDateTime(fileLocations) > _stubLoadDateTime)
        {
            var result = new List <StubModel>();
            foreach (var file in fileLocations)
            {
                // Load the stubs.
                var input = _fileService.ReadAllText(file);
                _logger.LogInformation($"Parsing .yml file '{file}'.");
                _logger.LogDebug($"File contents of '{file}': '{input}'");

                try
                {
                    IEnumerable <StubModel> stubs;

                    if (YamlIsArray(input))
                    {
                        stubs = YamlUtilities.Parse <List <StubModel> >(input);
                    }
                    else
                    {
                        stubs = new[] { YamlUtilities.Parse <StubModel>(input) };
                    }

                    ParseAndValidateStubs(stubs);
                    result.AddRange(stubs);
                    _stubLoadDateTime = DateTime.Now;
                }
                catch (YamlException ex)
                {
                    _logger.LogWarning(ex, $"Error occurred while parsing YAML file '{file}'");
                }
            }

            _stubs = result;
        }
        else
        {
            _logger.LogInformation("No stub file contents changed in the meanwhile.");
        }

        return(Task.FromResult(_stubs));
    }