// Copyright 2022 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 testcases

import (
	"time"

	"github.com/pingcap/test-infra/caselib/pkg/model/workload"
	"github.com/pingcap/test-infra/caselib/pkg/spec"
	"github.com/pingcap/test-infra/caselib/pkg/steps"
)

func init() {
	var (
		taskName    = "test"
		dbName      = "test"
		env         = newCommonEnv()
		sourceName0 = "source-0"
		sourceName1 = "source-1"
		name        = "dm_error_handling_update_config"
		masterName  = "dm-dm-master-0"
		workerName  = "dm-dm-worker-0"
	)
	AddTestCase(spec.CaseName(name), func() steps.Step {
		env.init().b.
			DropDatabasesIfExists(env.DownStream, &dbName).
			CreateDatabase(env.UpStreams[0], dbName).
			CreateDatabase(env.UpStreams[1], dbName).
			DmRunWorkload(env.UpStreams[0], env.Dm, env.Workload, &workload.QihooOption{
				Command:        workload.CommandTypeLoad,
				PropertyFile:   "/workloads/workloadd",
				PropertyValues: []string{"recordcount=100", "autocommit=1", "databasename=" + dbName, "tablecount=1"},
				Threads:        20,
				Duration:       3 * time.Minute,
			}).
			DmRunWorkload(env.UpStreams[1], env.Dm, env.Workload, &workload.QihooOption{
				Command:        workload.CommandTypeLoad,
				PropertyFile:   "/workloads/workloadp",
				PropertyValues: []string{"recordcount=100", "autocommit=1", "databasename=" + dbName, "tablecount=1"},
				Threads:        20,
				Duration:       3 * time.Minute,
			}).
			DmCreateSources(env.Dm, env.DmHost, Path.SourcePaths).
			DmStartTasks(env.Dm, env.DmHost, Path.TaskPaths).
			DmDmCtlCmd(env.DmHost, "config task "+taskName).
			DmDmCtlCmd(env.DmHost, "config source "+sourceName0).
			DmDmCtlCmd(env.DmHost, "config source "+sourceName1).
			DmDmCtlCmd(env.DmHost, "config master "+masterName).
			DmDmCtlCmd(env.DmHost, "config worker "+workerName)
		return env.toBuilder().Build()
	})
}
