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
14// +build go1.8
15
16package config
17
18import (
19	"crypto/tls"
20	"io/ioutil"
21	"reflect"
22	"testing"
23
24	"gopkg.in/yaml.v2"
25)
26
27// LoadTLSConfig parses the given YAML file into a tls.Config.
28func LoadTLSConfig(filename string) (*tls.Config, error) {
29	content, err := ioutil.ReadFile(filename)
30	if err != nil {
31		return nil, err
32	}
33	cfg := TLSConfig{}
34	if err = yaml.UnmarshalStrict(content, &cfg); err != nil {
35		return nil, err
36	}
37	return NewTLSConfig(&cfg)
38}
39
40var expectedTLSConfigs = []struct {
41	filename string
42	config   *tls.Config
43}{
44	{
45		filename: "tls_config.empty.good.yml",
46		config:   &tls.Config{},
47	}, {
48		filename: "tls_config.insecure.good.yml",
49		config:   &tls.Config{InsecureSkipVerify: true},
50	},
51}
52
53func TestValidTLSConfig(t *testing.T) {
54	for _, cfg := range expectedTLSConfigs {
55		got, err := LoadTLSConfig("testdata/" + cfg.filename)
56		if err != nil {
57			t.Errorf("Error parsing %s: %s", cfg.filename, err)
58		}
59		// non-nil functions are never equal.
60		got.GetClientCertificate = nil
61		if !reflect.DeepEqual(got, cfg.config) {
62			t.Fatalf("%v: unexpected config result: \n\n%v\n expected\n\n%v", cfg.filename, got, cfg.config)
63		}
64	}
65}
66