1// Copyright 2016 The Prometheus Authors
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14package config
15
16import (
17	"crypto/tls"
18	"io/ioutil"
19	"reflect"
20	"testing"
21
22	"gopkg.in/yaml.v2"
23)
24
25// LoadTLSConfig parses the given YAML file into a tls.Config.
26func LoadTLSConfig(filename string) (*tls.Config, error) {
27	content, err := ioutil.ReadFile(filename)
28	if err != nil {
29		return nil, err
30	}
31	cfg := TLSConfig{}
32	if err = yaml.UnmarshalStrict(content, &cfg); err != nil {
33		return nil, err
34	}
35	return NewTLSConfig(&cfg)
36}
37
38var expectedTLSConfigs = []struct {
39	filename string
40	config   *tls.Config
41}{
42	{
43		filename: "tls_config.empty.good.yml",
44		config:   &tls.Config{},
45	}, {
46		filename: "tls_config.insecure.good.yml",
47		config:   &tls.Config{InsecureSkipVerify: true},
48	},
49}
50
51func TestValidTLSConfig(t *testing.T) {
52	for _, cfg := range expectedTLSConfigs {
53		cfg.config.BuildNameToCertificate()
54		got, err := LoadTLSConfig("testdata/" + cfg.filename)
55		if err != nil {
56			t.Errorf("Error parsing %s: %s", cfg.filename, err)
57		}
58		if !reflect.DeepEqual(*got, *cfg.config) {
59			t.Fatalf("%v: unexpected config result: \n\n%v\n expected\n\n%v", cfg.filename, got, cfg.config)
60		}
61	}
62}
63