/*
Copyright 2019 The Volcano Authors.

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 ssh

import (
	"fmt"
	"strings"
	"testing"

	v1 "k8s.io/api/core/v1"

	batch "volcano.sh/apis/pkg/apis/batch/v1alpha1"
	pluginsinterface "volcano.sh/volcano/pkg/controllers/job/plugins/interface"
)

func TestSSHPlugin(t *testing.T) {

	tests := []struct {
		name           string
		params         []string
		noRoot         bool
		sshKeyFilePath string
		sshPrivateKey  string
		sshPublicKey   string
		sshPort        int
	}{
		{
			name:           "no params specified",
			sshKeyFilePath: SSHAbsolutePath,
			sshPrivateKey:  "",
			sshPublicKey:   "",
			sshPort:        22,
		},
		{
			name:           "--ssh-key-file-path=/a/b",
			params:         []string{"--ssh-key-file-path=/a/b"},
			sshKeyFilePath: "/a/b",
			sshPrivateKey:  "",
			sshPublicKey:   "",
			sshPort:        22,
		},
		{
			name:           "with user provided ssh keys",
			params:         []string{"--ssh-private-key=-----BEGIN RSA PRIVATE KEY-----\\nMIIEpAIBAAKCAQEAyeyZjWDx5Na9bw1f61M4s+QlLT/kyrB37AR2j5Sb/A9hvJak\\nLNQQpNC+KVfYNl4jePG+6lwHqye//pcC9+0SWsHWwgaahjMLnAthR2k8JAakNA9x\\nV/wHz0YU99OKEetaOuxXpWZPXCHX0zuQO87YbdKzRbgxACirM3Phkwr7XLtQtWZk\\nyXG34CQXZQWgBIS1Fl+PlGOpVpOPnWoZPMpbAK74i/Tz4sP8Zhqc6dya1hrbUwY3\\nYfMZNYXpaAw7wWVjq8grfs0+Fl3SxHrzTXge2m+eZAZ6iPJ8cX4uYKxi0ZmxpM/a\\ngI6Mmjq0MU75Vxpq22LaUvHIpOfX5Uxhkr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Y1fd80vSlnSOL7nRkU7B5\\nWXlJy8ECgYA4g0wc0Jq8c1Q0FulMkOQqYRDXaDo34987L+mZ70i/RtdkKjK/IKJ9\\n18UDCyEaDPD0BWBJGPejZkY8UD6FBG/5k7wNIbT7hHLRSRlw4iRmVX2hRVXrXzD8\\nvc86Qyg2iG0JqkMAvRdH40amPKp5bW4VcfcvQo4TSsI972u12rgwtg==\\n-----END RSA PRIVATE KEY-----\\n", "--ssh-public-key=ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJ7JmNYPHk1r1vDV/rUziz5CUtP+TKsHfsBHaPlJv8D2G8lqQs1BCk0L4pV9g2XiN48b7qXAerJ7/+lwL37RJawdbCBpqGMwucC2FHaTwkBqQ0D3FX/AfPRhT304oR61o67FelZk9cIdfTO5A7ztht0rNFuDEAKKszc+GTCvtcu1C1ZmTJcbfgJBdlBaAEhLUWX4+UY6lWk4+dahk8ylsArviL9PPiw/xmGpzp3JrWGttTBjdh8xk1heloDDvBZWOryCt+zT4WXdLEevNNeB7ab55kBnqI8nxxfi5grGLRmbGkz9qAjoyaOrQxTvlXGmrbYtpS8cik59flTGGSuzGX root@aiplatform"},
			sshKeyFilePath: SSHAbsolutePath,
			sshPrivateKey:  "-----BEGIN RSA PRIVATE KEY-----\\nMIIEpAIBAAKCAQEAyeyZjWDx5Na9bw1f61M4s+QlLT/kyrB37AR2j5Sb/A9hvJak\\nLNQQpNC+KVfYNl4jePG+6lwHqye//pcC9+0SWsHWwgaahjMLnAthR2k8JAakNA9x\\nV/wHz0YU99OKEetaOuxXpWZPXCHX0zuQO87YbdKzRbgxACirM3Phkwr7XLtQtWZk\\nyXG34CQXZQWgBIS1Fl+PlGOpVpOPnWoZPMpbAK74i/Tz4sP8Zhqc6dya1hrbUwY3\\nYfMZNYXpaAw7wWVjq8grfs0+Fl3SxHrzTXge2m+eZAZ6iPJ8cX4uYKxi0ZmxpM/a\\ngI6Mmjq0MU75Vxpq22LaUvHIpOfX5Uxhkrsxlw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d80vSlnSOL7nRkU7B5\\nWXlJy8ECgYA4g0wc0Jq8c1Q0FulMkOQqYRDXaDo34987L+mZ70i/RtdkKjK/IKJ9\\n18UDCyEaDPD0BWBJGPejZkY8UD6FBG/5k7wNIbT7hHLRSRlw4iRmVX2hRVXrXzD8\\nvc86Qyg2iG0JqkMAvRdH40amPKp5bW4VcfcvQo4TSsI972u12rgwtg==\\n-----END RSA PRIVATE KEY-----\\n",
			sshPublicKey:   "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDJ7JmNYPHk1r1vDV/rUziz5CUtP+TKsHfsBHaPlJv8D2G8lqQs1BCk0L4pV9g2XiN48b7qXAerJ7/+lwL37RJawdbCBpqGMwucC2FHaTwkBqQ0D3FX/AfPRhT304oR61o67FelZk9cIdfTO5A7ztht0rNFuDEAKKszc+GTCvtcu1C1ZmTJcbfgJBdlBaAEhLUWX4+UY6lWk4+dahk8ylsArviL9PPiw/xmGpzp3JrWGttTBjdh8xk1heloDDvBZWOryCt+zT4WXdLEevNNeB7ab55kBnqI8nxxfi5grGLRmbGkz9qAjoyaOrQxTvlXGmrbYtpS8cik59flTGGSuzGX root@aiplatform",
			sshPort:        22,
		},
		{
			name:           "with custom ssh port",
			params:         []string{"--ssh-port=2026"},
			sshKeyFilePath: SSHAbsolutePath,
			sshPrivateKey:  "",
			sshPublicKey:   "",
			sshPort:        2026,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			pluginInterface := New(pluginsinterface.PluginClientset{}, test.params)
			plugin := pluginInterface.(*sshPlugin)

			if plugin.sshKeyFilePath != test.sshKeyFilePath {
				t.Errorf("Expected sshKeyFilePath=%s, got %s", test.sshKeyFilePath, plugin.sshKeyFilePath)
			}
			if plugin.sshPrivateKey != test.sshPrivateKey {
				t.Errorf("Expected sshPrivateKey=%s, got %s", test.sshPrivateKey, plugin.sshPrivateKey)
			}
			if plugin.sshPublicKey != test.sshPublicKey {
				t.Errorf("Expected sshPublicKey=%s, got %s", test.sshPublicKey, plugin.sshPublicKey)
			}
			if plugin.sshPort != test.sshPort {
				t.Errorf("Expected sshPort=%d, got %d", test.sshPort, plugin.sshPort)
			}
		})
	}
}

func TestGenerateSSHConfigWithCustomPort(t *testing.T) {
	job := &batch.Job{
		Spec: batch.JobSpec{
			Tasks: []batch.TaskSpec{
				{
					Name:     "worker",
					Replicas: 1,
					Template: v1.PodTemplateSpec{
						Spec: v1.PodSpec{},
					},
				},
			},
		},
	}

	port := 2026
	config := generateSSHConfig(job, port)

	expected := fmt.Sprintf("Port %d", port)
	if !strings.Contains(config, expected) {
		t.Errorf("expected SSH config to contain %q, got:\n%s", expected, config)
	}
}

func TestInvalidSSHPortFallsBackToDefault(t *testing.T) {
	params := []string{"--ssh-port=-10"}

	pluginInterface := New(pluginsinterface.PluginClientset{}, params)
	plugin := pluginInterface.(*sshPlugin)

	plugin.validateSSHPort()

	if plugin.sshPort != 22 {
		t.Errorf("Expected sshPort to fall back to 22, got %d", plugin.sshPort)
	}
}
