func TestAddHeaders(t *testing.T) {
	testConfig := Config{
		Headers: map[string]string{
			"TestHeaderOne": "TestFieldTwo",
			"TestHeaderTwo": "TestFieldTwo",
		},
	}
	exporter := Exporter{testConfig}
	req, err := http.NewRequest("POST", "test.com", nil)
	require.NoError(t, err)
	err = exporter.addHeaders(req)
	require.NoError(t, err)
	for name, field := range testConfig.Headers {
		require.Equal(t, req.Header.Get(name), field)
	}
	require.Equal(t, req.Header.Get("Content-Encoding"), "snappy")
	require.Equal(t, req.Header.Get("Content-Type"), "application/x-protobuf")
	require.Equal(t, req.Header.Get("X-Prometheus-Remote-Write-Version"), "0.1.0")
}
TestAddHeaders tests whether the correct headers are correctly added to a http request.
package cortex
import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"strconv"
	"testing"
	"time"
	"github.com/golang/snappy"
	"github.com/google/go-cmp/cmp"
	"github.com/prometheus/prometheus/prompb"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/metric/global"
	"go.opentelemetry.io/otel/sdk/export/metric"
	export "go.opentelemetry.io/otel/sdk/export/metric"
	"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
	"go.opentelemetry.io/otel/sdk/resource"
	semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
)
var testResource = resource.NewWithAttributes(semconv.SchemaURL, attribute.String("R", "V"))
var validConfig = Config{
	Endpoint:      "/api/prom/push",
	RemoteTimeout: 30 * time.Second,
	Name:          "Valid Config Example",
	BasicAuth: map[string]string{
		"username": "user",
		"password": "password",
	},
	BearerToken:     "",
	BearerTokenFile: "",
	TLSConfig: map[string]string{
		"ca_file":              "cafile",
		"cert_file":            "certfile",
		"key_file":             "keyfile",
		"server_name":          "server",
		"insecure_skip_verify": "1",
	},
	ProxyURL:     nil,
	PushInterval: 10 * time.Second,
	Headers: map[string]string{
		"x-prometheus-remote-write-version": "0.1.0",
		"tenant-id":                         "123",
	},
	Client:    http.DefaultClient,
	Quantiles: []float64{0, 0.25, 0.5, 0.75, 1},
}
func TestExportKindFor(t *testing.T) {
	exporter := Exporter{}
	got := exporter.ExportKindFor(nil, aggregation.Kind(rune(0)))
	want := metric.CumulativeExportKind
	if got != want {
		t.Errorf("ExportKindFor() =  %q, want %q", got, want)
	}
}
func TestConvertToTimeSeries(t *testing.T) {
	exporter := Exporter{
		config: Config{
			Quantiles: []float64{0.5, 0.9, .99},
		},
	}
	startTime := time.Now()
	tests := []struct {
		name       string
		input      export.InstrumentationLibraryReader
		want       []*prompb.TimeSeries
		wantLength int
	}{
		{
			name:       "convertFromSum",
			input:      getSumReader(t, 1, 2, 3, 4, 5),
			want:       wantSumTimeSeries,
			wantLength: 1,
		},
		{
			name:       "convertFromLastValue",
			input:      getLastValueReader(t, 1, 2, 3, 4, 5),
			want:       wantLastValueTimeSeries,
			wantLength: 1,
		},
		{
			name:       "convertFromMinMaxSumCount",
			input:      getMMSCReader(t, 123.456, 876.543),
			want:       wantMMSCTimeSeries,
			wantLength: 4,
		},
		{
			name:       "convertFromHistogram",
			input:      getHistogramReader(t),
			want:       wantHistogramTimeSeries,
			wantLength: 6,
		},
	}
	endTime := time.Now()
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := exporter.ConvertToTimeSeries(testResource, tt.input)
			want := tt.want
			assert.Nil(t, err, "ConvertToTimeSeries error")
			assert.Len(t, got, tt.wantLength, "Incorrect number of timeseries")
			gotAttributes := make(map[string]bool)
			wantAttributes := make(map[string]bool)
			gotSamples := make(map[string]bool)
			wantSamples := make(map[string]bool)
			for i := 0; i < len(got); i++ {
				for _, attribute := range got[i].Labels {
					gotAttributes[attribute.String()] = true
				}
				for _, attribute := range want[i].Labels {
					wantAttributes[attribute.String()] = true
				}
				for _, sample := range got[i].Samples {
					gotSamples[fmt.Sprint(sample.Value)] = true
					assert.LessOrEqual(t, toMillis(startTime), sample.Timestamp)
					assert.GreaterOrEqual(t, toMillis(endTime), sample.Timestamp)
				}
				for _, sample := range want[i].Samples {
					wantSamples[fmt.Sprint(sample.Value)] = true
				}
			}
			assert.Equal(t, wantAttributes, gotAttributes)
			assert.Equal(t, wantSamples, gotSamples)
		})
	}
}
func TestNewRawExporter(t *testing.T) {
	exporter, err := NewRawExporter(validConfig)
	if err != nil {
		t.Fatalf("Failed to create exporter with error %v", err)
	}
	if !cmp.Equal(validConfig, exporter.config) {
		t.Fatalf("Got configuration %v, wanted %v", exporter.config, validConfig)
	}
}
func TestNewExportPipeline(t *testing.T) {
	_, err := NewExportPipeline(validConfig)
	if err != nil {
		t.Fatalf("Failed to create export pipeline with error %v", err)
	}
}
func TestInstallNewPipeline(t *testing.T) {
	cont, err := InstallNewPipeline(validConfig)
	if err != nil {
		t.Fatalf("Failed to create install pipeline with error %v", err)
	}
	if global.GetMeterProvider() != cont {
		t.Fatalf("Failed to register push Controller provider globally")
	}
}