package context

import (
	"os"
	"testing"

	"github.com/docker/cli/cli/config"
	"gotest.tools/v3/assert"
	"gotest.tools/v3/golden"
	"gotest.tools/v3/icmd"
)

func TestContextList(t *testing.T) {
	cmd := icmd.Command("docker", "context", "ls")
	cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"=testdata/test-dockerconfig")
	result := icmd.RunCmd(cmd).Assert(t, icmd.Expected{
		Err:      icmd.None,
		ExitCode: 0,
	})
	golden.Assert(t, result.Stdout(), "context-ls.golden")
}

func TestContextImportNoTLS(t *testing.T) {
	d := t.TempDir()
	cmd := icmd.Command("docker", "context", "import", "remote", "./testdata/test-dockerconfig.tar")
	cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
	icmd.RunCmd(cmd).Assert(t, icmd.Success)

	cmd = icmd.Command("docker", "context", "ls")
	cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
	result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
	golden.Assert(t, result.Stdout(), "context-ls.golden")
}

func TestContextImportTLS(t *testing.T) {
	d := t.TempDir()
	cmd := icmd.Command("docker", "context", "import", "test", "./testdata/test-dockerconfig-tls.tar")
	cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
	icmd.RunCmd(cmd).Assert(t, icmd.Success)

	cmd = icmd.Command("docker", "context", "ls")
	cmd.Env = append(cmd.Env, config.EnvOverrideConfigDir+"="+d)
	result := icmd.RunCmd(cmd).Assert(t, icmd.Success)
	golden.Assert(t, result.Stdout(), "context-ls-tls.golden")

	b, err := os.ReadFile(d + "/contexts/tls/9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08/kubernetes/key.pem")
	assert.NilError(t, err)
	assert.Equal(t, string(b), `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEArQk77K5sgrQYY6HiQ1y7AC+67HrRB36oEvR+Fq60RsFcc3cZ
xAvMkRSBPjQyskjdYY7kfykGHhfJxGKopb3c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