/*
 * 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 service

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestApiService_buildRunApplicationArgs(t *testing.T) {
	assert := require.New(t)
	svc, err := New(ExecutorCMDOpt([]string{"local:///opt/flink/examples/streaming/StateMachineExample.jar"}))
	assert.Nil(err)
	assert.NotNil(svc)
	property := make(map[string]string)
	property["kubernetes.container.image"] = "foo:0.1"
	property["kubernetes.taskmanager.labels"] = "env:qa"
	property["kubernetes.jobmanager.labels"] = "env:qa"
	property["kubernetes.secrets"] = "path_to_secrets"
	property["kubernetes.namespace"] = "default"
	property["kubernetes.cluster-id"] = "foo"
	args := svc.buildFlinkArgs([]string{"run-application", "-t", "kubernetes-application"}, property)
	assert.Nil(err)
	str := strings.Join(args, " ")
	assert.Contains(str, `run-application -t kubernetes-application `)
	assert.Contains(str, `local:///opt/flink/examples/streaming/StateMachineExample.jar`)
	assert.Contains(str, `-Dkubernetes.container.image=foo:0.1`)
	assert.Contains(str, `-Dkubernetes.taskmanager.labels=env:qa`)
	assert.Contains(str, `-Dkubernetes.jobmanager.labels=env:qa`)
	assert.Contains(str, `-Dkubernetes.secrets=path_to_secrets`)
	assert.Contains(str, `-Dkubernetes.namespace=default`)
	assert.Contains(str, `-Dkubernetes.cluster-id=foo`)
}
