/*
Copyright 2018 the Heptio Ark contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package restore

import (
	"strings"
	"testing"
	"unicode"

	"github.com/stretchr/testify/assert"

	corev1api "k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

	arktest "github.com/heptio/ark/pkg/util/test"
)

var mergedServiceAccountsBenchmarkResult *unstructured.Unstructured

func BenchmarkMergeServiceAccountBasic(b *testing.B) {
	tests := []struct {
		name        string
		fromCluster *unstructured.Unstructured
		fromBackup  *unstructured.Unstructured
	}{
		{
			name: "only default tokens present",
			fromCluster: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
			fromBackup: arktest.UnstructuredOrDie(
				`{
					"kind": "ServiceAccount",
					"apiVersion": "v1",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-xzy12" }
					]
				}`,
			),
		},
		{
			name: "service accounts with multiple secrets",
			fromCluster: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" },
						{ "name": "my-secret" },
						{ "name": "sekrit" }
					]
				}`,
			),

			fromBackup: arktest.UnstructuredOrDie(
				`{
					"kind": "ServiceAccount",
					"apiVersion": "v1",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-xzy12" },
						{ "name": "my-old-secret" },
						{ "name": "secrete"}
					]
				}`,
			),
		},
		{
			name: "service accounts with labels and annotations",
			fromCluster: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default",
						"labels": {
							"l1": "v1",
							"l2": "v2",
							"l3": "v3"
						},
						"annotations": {
							"a1": "v1",
							"a2": "v2",
							"a3": "v3",
							"a4": "v4"
						}
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),

			fromBackup: arktest.UnstructuredOrDie(
				`{
					"kind": "ServiceAccount",
					"apiVersion": "v1",
					"metadata": {
						"namespace": "ns1",
						"name": "default",
						"labels": {
							"l1": "v1",
							"l2": "v2",
							"l3": "v3",
							"l4": "v4",
							"l5": "v5"
						},
						"annotations": {
							"a1": "v1",
							"a2": "v2",
							"a3": "v3",
							"a4": "v4",
							"a5": "v5",
							"a6": "v6"
						}
					},
					"secrets": [
						{ "name": "default-token-xzy12" }
					]
				}`,
			),
		},
	}

	var desired *unstructured.Unstructured

	for _, test := range tests {
		b.Run(test.name, func(b *testing.B) {
			for n := 0; n < b.N; n++ {
				desired, _ = mergeServiceAccounts(test.fromCluster, test.fromBackup)
			}

			mergedServiceAccountsBenchmarkResult = desired
		})
	}
}

func TestMergeLocalObjectReferenceSlices(t *testing.T) {
	tests := []struct {
		name     string
		first    []corev1api.LocalObjectReference
		second   []corev1api.LocalObjectReference
		expected []corev1api.LocalObjectReference
	}{
		{
			name: "two slices without overlapping elements",
			first: []corev1api.LocalObjectReference{
				{Name: "lor1"},
				{Name: "lor2"},
			},
			second: []corev1api.LocalObjectReference{
				{Name: "lor3"},
				{Name: "lor4"},
			},
			expected: []corev1api.LocalObjectReference{
				{Name: "lor1"},
				{Name: "lor2"},
				{Name: "lor3"},
				{Name: "lor4"},
			},
		},
		{
			name: "two slices with an overlapping element",
			first: []corev1api.LocalObjectReference{
				{Name: "lor1"},
				{Name: "lor2"},
			},
			second: []corev1api.LocalObjectReference{
				{Name: "lor3"},
				{Name: "lor2"},
			},
			expected: []corev1api.LocalObjectReference{
				{Name: "lor1"},
				{Name: "lor2"},
				{Name: "lor3"},
			},
		},
		{
			name: "merging always adds elements to the end",
			first: []corev1api.LocalObjectReference{
				{Name: "lor3"},
				{Name: "lor4"},
			},
			second: []corev1api.LocalObjectReference{
				{Name: "lor1"},
				{Name: "lor2"},
			},
			expected: []corev1api.LocalObjectReference{
				{Name: "lor3"},
				{Name: "lor4"},
				{Name: "lor1"},
				{Name: "lor2"},
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			result := mergeLocalObjectReferenceSlices(test.first, test.second)
			assert.Equal(t, test.expected, result)
		})
	}
}

func TestMergeObjectReferenceSlices(t *testing.T) {
	tests := []struct {
		name     string
		first    []corev1api.ObjectReference
		second   []corev1api.ObjectReference
		expected []corev1api.ObjectReference
	}{
		{
			name: "two slices without overlapping elements",
			first: []corev1api.ObjectReference{
				{Name: "or1"},
				{Name: "or2"},
			},
			second: []corev1api.ObjectReference{
				{Name: "or3"},
				{Name: "or4"},
			},
			expected: []corev1api.ObjectReference{
				{Name: "or1"},
				{Name: "or2"},
				{Name: "or3"},
				{Name: "or4"},
			},
		},
		{
			name: "two slices with an overlapping element",
			first: []corev1api.ObjectReference{
				{Name: "or1"},
				{Name: "or2"},
			},
			second: []corev1api.ObjectReference{
				{Name: "or3"},
				{Name: "or2"},
			},
			expected: []corev1api.ObjectReference{
				{Name: "or1"},
				{Name: "or2"},
				{Name: "or3"},
			},
		},
		{
			name: "merging always adds elements to the end",
			first: []corev1api.ObjectReference{
				{Name: "or3"},
				{Name: "or4"},
			},
			second: []corev1api.ObjectReference{
				{Name: "or1"},
				{Name: "or2"},
			},
			expected: []corev1api.ObjectReference{
				{Name: "or3"},
				{Name: "or4"},
				{Name: "or1"},
				{Name: "or2"},
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			result := mergeObjectReferenceSlices(test.first, test.second)
			assert.Equal(t, test.expected, result)
		})
	}
}

// stripWhitespace removes any Unicode whitespace from a string.
// Useful for cleaning up formatting on expected JSON strings before comparison
func stripWhitespace(s string) string {
	return strings.Map(func(r rune) rune {
		if unicode.IsSpace(r) {
			return -1
		}
		return r
	}, s)
}

func TestGeneratePatch(t *testing.T) {
	tests := []struct {
		name           string
		fromCluster    *unstructured.Unstructured
		desired        *unstructured.Unstructured
		expectedString string
		expectedErr    bool
	}{
		{
			name: "objects are equal, no patch needed",
			fromCluster: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
			desired: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
			expectedString: "",
			expectedErr:    false,
		},
		{
			name: "patch is required when labels are present",
			fromCluster: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
			desired: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default",
						"labels": {
							"label1": "value1",
							"label2": "value2"
						}
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
			expectedString: stripWhitespace(
				`{
					"metadata": {
						"labels": {
							"label1":"value1",
							"label2":"value2"
						}
					}
				}`,
			),
			expectedErr: false,
		},
		{
			name: "patch is required when annotations are present",
			fromCluster: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
			desired: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default",
						"annotations" :{
							"a1": "v1",
							"a2": "v2"
						}
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
			expectedString: stripWhitespace(
				`{
					"metadata": {
						"annotations": {
							"a1":"v1",
							"a2":"v2"
						}
					}
				}`,
			),
			expectedErr: false,
		},
		{
			name: "patch is required many secrets are present",
			fromCluster: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
			desired: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" },
						{ "name": "sekrit" },
						{ "name": "secrete" }
					]
				}`,
			),
			expectedString: stripWhitespace(
				`{
					"secrets": [
						{"name": "default-token-abcde"},
						{"name": "sekrit"},
						{"name": "secrete"}
					]
				}`,
			),
			expectedErr: false,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			result, err := generatePatch(test.fromCluster, test.desired)
			if assert.Equal(t, test.expectedErr, err != nil) {
				assert.Equal(t, test.expectedString, string(result))
			}
		})
	}
}

func TestMergeServiceAccountBasic(t *testing.T) {
	tests := []struct {
		name        string
		fromCluster *unstructured.Unstructured
		fromBackup  *unstructured.Unstructured
		expectedRes *unstructured.Unstructured
		expectedErr bool
	}{
		{
			name: "only default tokens present",
			fromCluster: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
			fromBackup: arktest.UnstructuredOrDie(
				`{
					"kind": "ServiceAccount",
					"apiVersion": "v1",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-xzy12" }
					]
				}`,
			),
			expectedRes: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
		},
		{
			name: "service accounts with multiple secrets",
			fromCluster: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" },
						{ "name": "my-secret" },
						{ "name": "sekrit" }
					]
				}`,
			),

			fromBackup: arktest.UnstructuredOrDie(
				`{
					"kind": "ServiceAccount",
					"apiVersion": "v1",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-xzy12" },
						{ "name": "my-old-secret" },
						{ "name": "secrete"}
					]
				}`,
			),
			expectedRes: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default"
					},
					"secrets": [
						{ "name": "default-token-abcde" },
						{ "name": "my-secret" },
						{ "name": "sekrit" },
						{ "name": "my-old-secret" },
						{ "name": "secrete"}
					]
				}`,
			),
		},
		{
			name: "service accounts with labels and annotations",
			fromCluster: arktest.UnstructuredOrDie(
				`{
					"apiVersion": "v1",
					"kind": "ServiceAccount",
					"metadata": {
						"namespace": "ns1",
						"name": "default",
						"labels": {
							"l1": "v1",
							"l2": "v2",
							"l3": "v3"
						},
						"annotations": {
							"a1": "v1",
							"a2": "v2",
							"a3": "v3",
							"a4": "v4"
						}
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),

			fromBackup: arktest.UnstructuredOrDie(
				`{
					"kind": "ServiceAccount",
					"apiVersion": "v1",
					"metadata": {
						"namespace": "ns1",
						"name": "default",
						"labels": {
							"l1": "v1",
							"l2": "v2",
							"l3": "v3",
							"l4": "v4",
							"l5": "v5"
						},
						"annotations": {
							"a1": "v1",
							"a2": "v2",
							"a3": "v3",
							"a4": "v4",
							"a5": "v5",
							"a6": "v6"
						}
					},
					"secrets": [
						{ "name": "default-token-xzy12" }
					]
				}`,
			),
			expectedRes: arktest.UnstructuredOrDie(
				`{
					"kind": "ServiceAccount",
					"apiVersion": "v1",
					"metadata": {
						"namespace": "ns1",
						"name": "default",
						"labels": {
							"l1": "v1",
							"l2": "v2",
							"l3": "v3",
							"l4": "v4",
							"l5": "v5"
						},
						"annotations": {
							"a1": "v1",
							"a2": "v2",
							"a3": "v3",
							"a4": "v4",
							"a5": "v5",
							"a6": "v6"
						}
					},
					"secrets": [
						{ "name": "default-token-abcde" }
					]
				}`,
			),
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(b *testing.T) {
			result, err := mergeServiceAccounts(test.fromCluster, test.fromBackup)
			if err != nil {
				assert.Equal(t, test.expectedRes, result)
			}
		})
	}
}
