func TestMigrateServedTypesTask(t *testing.T) {
	fake := fakevtctlclient.NewFakeVtctlClient()
	vtctlclient.RegisterFactory("fake", fake.FakeVtctlClientFactory)
	defer vtctlclient.UnregisterFactoryForTest("fake")
	flag.Set("vtctl_client_protocol", "fake")
	task := &MigrateServedTypesTask{}

	fake.RegisterResult([]string{"MigrateServedTypes", "test_keyspace/0", "rdonly"},
		"",  // No output.
		nil) // No error.
	parameters := map[string]string{
		"keyspace":        "test_keyspace",
		"source_shard":    "0",
		"type":            "rdonly",
		"vtctld_endpoint": "localhost:15000",
	}
	testTask(t, "MigrateServedTypes", task, parameters, fake)

	fake.RegisterResult([]string{"MigrateServedTypes", "--cells=cell1", "--reverse=true", "test_keyspace/0", "rdonly"},
		"",  // No output.
		nil) // No error.
	parameters["cells"] = "cell1"
	parameters["reverse"] = "true"
	testTask(t, "MigrateServedTypes", task, parameters, fake)
}
func TestCopySchemaShardTask(t *testing.T) {
	fake := fakevtctlclient.NewFakeVtctlClient()
	vtctlclient.RegisterFactory("fake", fake.FakeVtctlClientFactory)
	defer vtctlclient.UnregisterFactoryForTest("fake")
	flag.Set("vtctl_client_protocol", "fake")
	fake.RegisterResult([]string{"CopySchemaShard", "test_keyspace/0", "test_keyspace/2"},
		"",  // No output.
		nil) // No error.

	task := &CopySchemaShardTask{}
	parameters := map[string]string{
		"keyspace":        "test_keyspace",
		"source_shard":    "0",
		"dest_shard":      "2",
		"vtctld_endpoint": "localhost:15000",
		"exclude_tables":  "",
	}
	testTask(t, "CopySchemaShard", task, parameters, fake)

	fake.RegisterResult([]string{"CopySchemaShard", "--exclude_tables=excluded_table1", "test_keyspace/0", "test_keyspace/2"},
		"",  // No output.
		nil) // No error.
	parameters["exclude_tables"] = "excluded_table1"
	testTask(t, "CopySchemaShard", task, parameters, fake)
}
func TestCopySchemaShardTask(t *testing.T) {
	fake := fakevtctlclient.NewFakeVtctlClient()
	vtctlclient.RegisterFactory("fake", fake.FakeVtctlClientFactory)
	defer vtctlclient.UnregisterFactoryForTest("fake")
	flag.Set("vtctl_client_protocol", "fake")
	fake.RegisterResult([]string{"CopySchemaShard", "test_keyspace/0", "test_keyspace/2"},
		"",  // No output.
		nil) // No error.

	task := &CopySchemaShardTask{}
	parameters := map[string]string{
		"keyspace":        "test_keyspace",
		"source_shard":    "0",
		"dest_shard":      "2",
		"vtctld_endpoint": "localhost:15000",
	}

	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)
	}
}
// 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 */)
}
func TestRebuildKeyspaceGraphTask(t *testing.T) {
	fake := fakevtctlclient.NewFakeVtctlClient()
	vtctlclient.RegisterFactory("fake", fake.FakeVtctlClientFactory)
	defer vtctlclient.UnregisterFactoryForTest("fake")
	flag.Set("vtctl_client_protocol", "fake")
	task := &RebuildKeyspaceGraphTask{}

	fake.RegisterResult([]string{"RebuildKeyspaceGraph", "test_keyspace"},
		"",  // No output.
		nil) // No error.
	parameters := map[string]string{
		"keyspace":        "test_keyspace",
		"vtctld_endpoint": "localhost:15000",
	}
	testTask(t, "RebuildKeyspaceGraph", task, parameters, fake)

	fake.RegisterResult([]string{"RebuildKeyspaceGraph", "--cells=cell1", "test_keyspace"},
		"",  // No output.
		nil) // No error.
	parameters["cells"] = "cell1"
	testTask(t, "RebuildKeyspaceGraph", task, parameters, fake)
}