Repository: kubernetes/kubernetes
File: test/integration/etcd/etcd_storage_path_test.go (Go)

Code before:
	gvk("custom.fancy.com", "v2", "Pant"):      true,
}

// TestEtcdStoragePath tests to make sure that all objects are stored in an expected location in etcd.
// It will start failing when a new type is added to ensure that all future types are added to this test.
// It will also fail when a type gets moved to a different location. Be very careful in this situation because
// it essentially means that you will be break old clusters unless you create some migration path for the old data.
func TestEtcdStoragePath(t *testing.T) {
	supportedVersions := GetSupportedEmulatedVersions()
	for _, v := range supportedVersions {
		t.Run(v, func(t *testing.T) {
			testEtcdStoragePathWithVersion(t, v)
		})
	}
}

func testEtcdStoragePathWithVersion(t *testing.T, v string) {
	if v == componentbaseversion.DefaultKubeBinaryVersion {
		featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, "AllAlpha", true)
		featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, "AllBeta", true)
	} else {
		// Only test for beta and GA APIs with emulated version.
		featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, feature.DefaultFeatureGate, version.MustParse(v))
		featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, "AllBeta", true)
		registerEffectiveEmulationVersion(t)
	}

	apiServer := StartRealAPIServerOrDie(t, func(opts *options.ServerRunOptions) {
		// Disable alphas when emulating previous versions.
		if v != componentbaseversion.DefaultKubeBinaryVersion {
			opts.Options.APIEnablement.RuntimeConfig["api/alpha"] = "false"
		}
	})

	defer apiServer.Cleanup()
	defer dumpEtcdKVOnFailure(t, apiServer.KV)

	client := &allClient{dynamicClient: apiServer.Dynamic}

	if _, err := apiServer.Client.CoreV1().Namespaces().Create(context.TODO(), &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}}, metav1.CreateOptions{}); err != nil {
		t.Fatal(err)
	}

	var etcdStorageData map[schema.GroupVersionResource]StorageData
	if v == componentbaseversion.DefaultKubeBinaryVersion {
		etcdStorageData = GetEtcdStorageDataForNamespaceServedAt("etcdstoragepathtestnamespace", v, false)
	} else {
		// Drop alphas from etcd data fixtures when emulating previous versions
		// as alphas are not supported with emulation.
		etcdStorageData = GetEtcdStorageDataForNamespaceServedAt("etcdstoragepathtestnamespace", v, true)
	}

Review comment (suggestion):
Issue discovered in https://github.com/kubernetes/kubernetes/pull/128279#discussion_r1908013803. https://github.com/kubernetes/kubernetes/pull/129416 will fix it and remove the need for this line

Code after:
	gvk("custom.fancy.com", "v2", "Pant"):      true,
}

// TestEtcdStoragePath tests to make sure that all objects are stored in an expected location in etcd.
// It will start failing when a new type is added to ensure that all future types are added to this test.
// It will also fail when a type gets moved to a different location. Be very careful in this situation because
// it essentially means that you will be break old clusters unless you create some migration path for the old data.
func TestEtcdStoragePath(t *testing.T) {
	supportedVersions := GetSupportedEmulatedVersions()
	for _, v := range supportedVersions {
		t.Run(v, func(t *testing.T) {
			testEtcdStoragePathWithVersion(t, v)
		})
	}
}

func testEtcdStoragePathWithVersion(t *testing.T, v string) {
	if v == componentbaseversion.DefaultKubeBinaryVersion {
		featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, "AllAlpha", true)
		featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, "AllBeta", true)
	} else {
		// Only test for beta and GA APIs with emulated version.
		featuregatetesting.SetFeatureGateEmulationVersionDuringTest(t, feature.DefaultFeatureGate, version.MustParse(v))
		featuregatetesting.SetFeatureGateDuringTest(t, feature.DefaultFeatureGate, "AllBeta", true)
	}
	registerEffectiveEmulationVersion(t)

	apiServer := StartRealAPIServerOrDie(t, func(opts *options.ServerRunOptions) {
		// Disable alphas when emulating previous versions.
		if v != componentbaseversion.DefaultKubeBinaryVersion {
			opts.Options.APIEnablement.RuntimeConfig["api/alpha"] = "false"
		}
	})

	defer apiServer.Cleanup()
	defer dumpEtcdKVOnFailure(t, apiServer.KV)

	client := &allClient{dynamicClient: apiServer.Dynamic}

	if _, err := apiServer.Client.CoreV1().Namespaces().Create(context.TODO(), &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: testNamespace}}, metav1.CreateOptions{}); err != nil {
		t.Fatal(err)
	}

	var etcdStorageData map[schema.GroupVersionResource]StorageData
	if v == componentbaseversion.DefaultKubeBinaryVersion {
		etcdStorageData = GetEtcdStorageDataForNamespaceServedAt("etcdstoragepathtestnamespace", v, false)
	} else {
		// Drop alphas from etcd data fixtures when emulating previous versions
		// as alphas are not supported with emulation.
		etcdStorageData = GetEtcdStorageDataForNamespaceServedAt("etcdstoragepathtestnamespace", v, true)
	}