Repository: helm/helm
File: pkg/action/install_test.go (Go)

Code before:
}

func TestInstallCRDs(t *testing.T) {
	config := actionConfigFixtureWithDummyResources(t, createDummyResourceList(false))
	config.RESTClientGetter = cli.New().RESTClientGetter()

	instAction := NewInstall(config)

	mockFile := common.File{
		Name: "crds/foo.yaml",
		Data: []byte("hello"),
	}
	mockChart := buildChart(withFile(mockFile))
	crdsToInstall := mockChart.CRDObjects()

	t.Run("fresh installation", func(t *testing.T) {
		assert.Len(t, crdsToInstall, 1)
		assert.Equal(t, crdsToInstall[0].File.Data, mockFile.Data)
		require.NoError(t, instAction.installCRDs(crdsToInstall))
	})

	t.Run("already exist", func(t *testing.T) {
		assert.Len(t, crdsToInstall, 1)
		assert.Equal(t, crdsToInstall[0].File.Data, mockFile.Data)
		require.NoError(t, instAction.installCRDs(crdsToInstall))
	})
}

func TestInstallCRDs_KubeClient_BuildError(t *testing.T) {
	config := actionConfigFixture(t)
	failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
	failingKubeClient.BuildError = errors.New("build error")
	config.KubeClient = &failingKubeClient
	instAction := NewInstall(config)

	mockFile := common.File{
		Name: "crds/foo.yaml",
		Data: []byte("hello"),
	}
	mockChart := buildChart(withFile(mockFile))
	crdsToInstall := mockChart.CRDObjects()

	require.Error(t, instAction.installCRDs(crdsToInstall), "failed to install CRD")
}

func TestInstallCRDs_KubeClient_CreateError(t *testing.T) {
	config := actionConfigFixture(t)
	failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
	failingKubeClient.CreateError = errors.New("create error")
	config.KubeClient = &failingKubeClient
	instAction := NewInstall(config)

Review comment (suggestion):
I don't think this works the way you intended. The fake kubeclient doesn't persist created resources or return "already exists" on subsequent creates. The second run will just happily recreate the same resources and pass because you require NoError.

Code after:
			url1:     "https://example.com:8080",
			url2:     "https://example.com:9090",
			expected: false,
		},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			t.Parallel()

			u1, err := url.Parse(tc.url1)
			if err != nil {
				t.Fatalf("Failed to parse URL1 %s: %v", tc.url1, err)
			}
			u2, err := url.Parse(tc.url2)
			if err != nil {
				t.Fatalf("Failed to parse URL2 %s: %v", tc.url2, err)
			}

			is.Equal(tc.expected, urlEqual(u1, u2))
		})
	}
}

func TestInstallRun_UnreachableKubeClient(t *testing.T) {
	config := actionConfigFixture(t)
	failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
	failingKubeClient.ConnectionError = errors.New("connection refused")
	config.KubeClient = &failingKubeClient

	instAction := NewInstall(config)
	ctx, done := context.WithCancel(t.Context())
	chrt := buildChart()
	res, err := instAction.RunWithContext(ctx, chrt, nil)

	done()
	assert.Nil(t, res)
	assert.ErrorContains(t, err, "connection refused")
}

func TestInstallSetRegistryClient(t *testing.T) {
	config := actionConfigFixture(t)
	instAction := NewInstall(config)

	registryClient := &registry.Client{}
	instAction.SetRegistryClient(registryClient)

	assert.Equal(t, registryClient, instAction.GetRegistryClient())
}

func TestInstallCRDs(t *testing.T) {