/*
 * Copyright 2021 PingCAP, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package config_test

import (
	"testing"

	"github.com/stretchr/testify/require"
	"gopkg.in/yaml.v2"

	"github.com/pingcap/mimir/config"
)

var bs = `
---
server:
  addr: 0.0.0.0:9999
  request_timeout: 3 # second
  response_timeout: 5 # second, for pprof change it to 120
mysql:
  driver: mysql
  dsn: root:root@tcp(localhost:3306)/test?charset=utf8&parseTime=True&loc=Local
  max_open_conns: 20
  max_idle_conns: 10
  max_life_conns: 60
  trace_include_not_found: false
log:
  level: "DEBUG"
flink:
  session_endpoint: http://127.0.0.1:8081
  client_path: /path/to/client
  image: /path/to/image:tag
  rest_exposed_type: ClusterIP
  cmd:
    - executor.jar
    - /path/to/input.sql
k8s:
  in_cluster: true
  kube_config_path: /path/to/.kube/config
`

func TestAppOptionsStruct(t *testing.T) {
	assert := require.New(t)

	b := []byte(bs)
	option := config.Options
	if err := yaml.Unmarshal(b, &option); err != nil {
		t.Fatal(err)
	}
	assert.Equal("0.0.0.0:9999", option.Server.Addr)
	assert.Equal("root:root@tcp(localhost:3306)/test?charset=utf8&parseTime=True&loc=Local", option.MySql.DSN)
	assert.Equal("DEBUG", option.Log.Level)
	assert.Equal("http://127.0.0.1:8081", option.Flink.SessionEndpoint)
	assert.Equal("/path/to/client", option.Flink.ClientPath)
	assert.Equal("/path/to/image:tag", option.Flink.Image)
	assert.Equal("ClusterIP", option.Flink.RestExposedType)
	assert.Equal([]string{"executor.jar", "/path/to/input.sql"}, option.Flink.CMD)
	assert.True(option.K8S.InCluster)
	assert.Equal("/path/to/.kube/config", option.K8S.KubeConfigPath)
}
