func TestSplitCloneTask(t *testing.T) {
	fake := fakevtworkerclient.NewFakeVtworkerClient()
	vtworkerclient.RegisterFactory("fake", fake.FakeVtworkerClientFactory)
	defer vtworkerclient.UnregisterFactoryForTest("fake")
	flag.Set("vtworker_client_protocol", "fake")
	fake.RegisterResult([]string{"SplitClone", "--online=false", "--offline=true", "--exclude_tables=unrelated1", "--write_query_max_rows=1", "--write_query_max_size=1024", "--min_healthy_rdonly_tablets=1", "test_keyspace/0"},
		"",  // No output.
		nil) // No error.

	task := &SplitCloneTask{}
	parameters := map[string]string{
		"keyspace":                   "test_keyspace",
		"source_shard":               "0",
		"vtworker_endpoint":          "localhost:15001",
		"online":                     "false",
		"offline":                    "true",
		"exclude_tables":             "unrelated1",
		"write_query_max_rows":       "1",
		"write_query_max_size":       "1024",
		"min_healthy_rdonly_tablets": "1",
	}

	err := validateParameters(task, parameters)
	if err != nil {
		t.Fatalf("Not all required parameters were specified: %v", err)
	}

	newTasks, _ /* output */, err := task.Run(parameters)
	if newTasks != nil {
		t.Errorf("Task should not emit new tasks: %v", newTasks)
	}
	if err != nil {
		t.Errorf("Task should not fail: %v", err)
	}
}
func TestSplitCloneTask(t *testing.T) {
	fake := fakevtworkerclient.NewFakeVtworkerClient()
	vtworkerclient.RegisterFactory("fake", fake.FakeVtworkerClientFactory)
	flag.Set("vtworker_client_protocol", "fake")
	fake.RegisterResult([]string{"SplitClone", "--strategy=-populate_blp_checkpoint", "test_keyspace/0"},
		"",  // No output.
		nil) // No error.

	task := &SplitCloneTask{}
	parameters := map[string]string{
		"keyspace":          "test_keyspace",
		"source_shard":      "0",
		"vtworker_endpoint": "localhost:15001",
	}

	err := checkRequiredParameters(task, parameters)
	if err != nil {
		t.Fatalf("Not all required parameters were specified: %v", err)
	}

	newTasks, _ /* output */, err := task.run(parameters)
	if newTasks != nil {
		t.Errorf("Task should not emit new tasks: %v", newTasks)
	}
	if err != nil {
		t.Errorf("Task should not fail: %v", err)
	}
}
// TestVerticalSplitTask tests the vertical split cluster operation
// using mocked out vtctld and vtworker responses.
func TestVerticalSplitTask(t *testing.T) {
	vtctld := fakevtctlclient.NewFakeVtctlClient()
	vtctlclient.RegisterFactory("fake", vtctld.FakeVtctlClientFactory)
	defer vtctlclient.UnregisterFactoryForTest("fake")
	flag.Set("vtctl_client_protocol", "fake")

	vtworker := fakevtworkerclient.NewFakeVtworkerClient()
	vtworkerclient.RegisterFactory("fake", vtworker.FakeVtworkerClientFactory)
	defer vtworkerclient.UnregisterFactoryForTest("fake")
	flag.Set("vtworker_client_protocol", "fake")

	vtctld.RegisterResult([]string{"CopySchemaShard", "--tables=table1,table2", "source_keyspace/0", "destination_keyspace/0"},
		"",  // No output.
		nil) // No error.
	vtworker.RegisterResult([]string{"VerticalSplitClone", "--tables=table1,table2", "--min_healthy_rdonly_tablets=1", "destination_keyspace/0"}, "", nil)
	vtctld.RegisterResult([]string{"WaitForFilteredReplication", "-max_delay", "30s", "destination_keyspace/0"}, "", nil)
	vtworker.RegisterResult([]string{"VerticalSplitDiff", "--min_healthy_rdonly_tablets=1", "destination_keyspace/0"}, "", nil)
	vtctld.RegisterResult([]string{"MigrateServedFrom", "destination_keyspace/0", "rdonly"}, "", nil)
	vtctld.RegisterResult([]string{"MigrateServedFrom", "destination_keyspace/0", "replica"}, "", nil)
	vtctld.RegisterResult([]string{"MigrateServedFrom", "destination_keyspace/0", "master"},
		"ALL_DONE",
		nil)

	scheduler, err := NewScheduler()
	if err != nil {
		t.Fatalf("Failed to create scheduler: %v", err)
	}
	defer scheduler.ShutdownAndWait()

	scheduler.Run()

	enqueueRequest := &automationpb.EnqueueClusterOperationRequest{
		Name: "VerticalSplitTask",
		Parameters: map[string]string{
			"source_keyspace":            "source_keyspace",
			"dest_keyspace":              "destination_keyspace",
			"shard_list":                 "0",
			"tables":                     "table1,table2",
			"vtctld_endpoint":            "localhost:15000",
			"vtworker_endpoint":          "localhost:15001",
			"min_healthy_rdonly_tablets": "1",
		},
	}
	enqueueResponse, err := scheduler.EnqueueClusterOperation(context.Background(), enqueueRequest)
	if err != nil {
		t.Fatalf("Failed to start cluster operation. Request: %v Error: %v", enqueueRequest, err)
	}

	waitForClusterOperation(t, scheduler, enqueueResponse.Id,
		"ALL_DONE\n",
		"" /* expected error */)
}