package types_test

import (
	"testing"

	"github.com/canonical/k8s/pkg/k8sd/types"
	"github.com/canonical/k8s/pkg/snap/mock"
	"github.com/canonical/k8s/pkg/utils"
	. "github.com/onsi/gomega"
)

func TestDatastoreToKubeAPIServerArguments(t *testing.T) {
	snap := &mock.Snap{
		Mock: mock.Mock{
			K8sDqliteStateDir: "/k8s-dqlite",
			EtcdPKIDir:        "/pki/etcd",
		},
	}

	for _, tc := range []struct {
		name             string
		config           types.Datastore
		expectUpdateArgs map[string]string
		expectDeleteArgs []string
	}{
		{
			name:             "Nil",
			expectUpdateArgs: map[string]string{},
		},
		{
			name: "K8sDqlite",
			config: types.Datastore{
				Type: utils.Pointer("k8s-dqlite"),
			},
			expectUpdateArgs: map[string]string{
				"--etcd-servers":             "unix:///k8s-dqlite/k8s-dqlite.sock",
				"--etcd-healthcheck-timeout": "4s",
				"--feature-gates":            "DetectCacheInconsistency=false,ListFromCacheSnapshot=false,SizeBasedListCostEstimate=false",
			},
			expectDeleteArgs: []string{"--etcd-cafile", "--etcd-certfile", "--etcd-keyfile"},
		},
		{
			name: "ExternalFull",
			config: types.Datastore{
				Type:               utils.Pointer("external"),
				ExternalServers:    utils.Pointer([]string{"https://10.0.0.10:2379", "https://10.0.0.11:2379"}),
				ExternalCACert:     utils.Pointer("data"),
				ExternalClientCert: utils.Pointer("data"),
				ExternalClientKey:  utils.Pointer("data"),
			},
			expectUpdateArgs: map[string]string{
				"--etcd-servers":  "https://10.0.0.10:2379,https://10.0.0.11:2379",
				"--etcd-cafile":   "/pki/etcd/ca.crt",
				"--etcd-certfile": "/pki/etcd/client.crt",
				"--etcd-keyfile":  "/pki/etcd/client.key",
			},
		},
		{
			name: "ExternalOnlyCA",
			config: types.Datastore{
				Type:            utils.Pointer("external"),
				ExternalServers: utils.Pointer([]string{"https://10.0.0.10:2379", "https://10.0.0.11:2379"}),
				ExternalCACert:  utils.Pointer("data"),
			},
			expectUpdateArgs: map[string]string{
				"--etcd-servers": "https://10.0.0.10:2379,https://10.0.0.11:2379",
				"--etcd-cafile":  "/pki/etcd/ca.crt",
			},
			expectDeleteArgs: []string{"--etcd-certfile", "--etcd-keyfile"},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			g := NewWithT(t)

			update, delete, err := tc.config.ToKubeAPIServerArguments(snap)
			g.Expect(update).To(Equal(tc.expectUpdateArgs))
			g.Expect(delete).To(Equal(tc.expectDeleteArgs))
			g.Expect(err).ToNot(HaveOccurred())
		})
	}
}
